|
5 | 5 | use DateTime;
|
6 | 6 | use DateTimeImmutable;
|
7 | 7 | use Illuminate\Container\Container;
|
| 8 | +use Illuminate\Contracts\Auth\Authenticatable; |
| 9 | +use Illuminate\Contracts\Auth\Guard; |
| 10 | +use Illuminate\Contracts\Hashing\Hasher; |
8 | 11 | use Illuminate\Contracts\Translation\Translator as TranslatorContract;
|
9 | 12 | use Illuminate\Contracts\Validation\ImplicitRule;
|
10 | 13 | use Illuminate\Contracts\Validation\Rule;
|
@@ -686,6 +689,100 @@ public function testValidationStopsAtFailedPresenceCheck()
|
686 | 689 | $this->assertEquals(['validation.present'], $v->errors()->get('name'));
|
687 | 690 | }
|
688 | 691 |
|
| 692 | + public function testValidatePassword() |
| 693 | + { |
| 694 | + // Fails when user is not logged in. |
| 695 | + $auth = m::mock(Guard::class); |
| 696 | + $auth->shouldReceive('guard')->andReturn($auth); |
| 697 | + $auth->shouldReceive('guest')->andReturn(true); |
| 698 | + |
| 699 | + $hasher = m::mock(Hasher::class); |
| 700 | + |
| 701 | + $container = m::mock(Container::class); |
| 702 | + $container->shouldReceive('make')->with('auth')->andReturn($auth); |
| 703 | + $container->shouldReceive('make')->with('hash')->andReturn($hasher); |
| 704 | + |
| 705 | + $trans = $this->getTranslator(); |
| 706 | + $trans->shouldReceive('get'); |
| 707 | + |
| 708 | + $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']); |
| 709 | + $v->setContainer($container); |
| 710 | + |
| 711 | + $this->assertFalse($v->passes()); |
| 712 | + |
| 713 | + // Fails when password is incorrect. |
| 714 | + $user = m::mock(Authenticatable::class); |
| 715 | + $user->shouldReceive('getAuthPassword'); |
| 716 | + |
| 717 | + $auth = m::mock(Guard::class); |
| 718 | + $auth->shouldReceive('guard')->andReturn($auth); |
| 719 | + $auth->shouldReceive('guest')->andReturn(false); |
| 720 | + $auth->shouldReceive('user')->andReturn($user); |
| 721 | + |
| 722 | + $hasher = m::mock(Hasher::class); |
| 723 | + $hasher->shouldReceive('check')->andReturn(false); |
| 724 | + |
| 725 | + $container = m::mock(Container::class); |
| 726 | + $container->shouldReceive('make')->with('auth')->andReturn($auth); |
| 727 | + $container->shouldReceive('make')->with('hash')->andReturn($hasher); |
| 728 | + |
| 729 | + $trans = $this->getTranslator(); |
| 730 | + $trans->shouldReceive('get'); |
| 731 | + |
| 732 | + $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']); |
| 733 | + $v->setContainer($container); |
| 734 | + |
| 735 | + $this->assertFalse($v->passes()); |
| 736 | + |
| 737 | + // Succeeds when password is correct. |
| 738 | + $user = m::mock(Authenticatable::class); |
| 739 | + $user->shouldReceive('getAuthPassword'); |
| 740 | + |
| 741 | + $auth = m::mock(Guard::class); |
| 742 | + $auth->shouldReceive('guard')->andReturn($auth); |
| 743 | + $auth->shouldReceive('guest')->andReturn(false); |
| 744 | + $auth->shouldReceive('user')->andReturn($user); |
| 745 | + |
| 746 | + $hasher = m::mock(Hasher::class); |
| 747 | + $hasher->shouldReceive('check')->andReturn(true); |
| 748 | + |
| 749 | + $container = m::mock(Container::class); |
| 750 | + $container->shouldReceive('make')->with('auth')->andReturn($auth); |
| 751 | + $container->shouldReceive('make')->with('hash')->andReturn($hasher); |
| 752 | + |
| 753 | + $trans = $this->getTranslator(); |
| 754 | + $trans->shouldReceive('get'); |
| 755 | + |
| 756 | + $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password']); |
| 757 | + $v->setContainer($container); |
| 758 | + |
| 759 | + $this->assertTrue($v->passes()); |
| 760 | + |
| 761 | + // We can use a specific guard. |
| 762 | + $user = m::mock(Authenticatable::class); |
| 763 | + $user->shouldReceive('getAuthPassword'); |
| 764 | + |
| 765 | + $auth = m::mock(Guard::class); |
| 766 | + $auth->shouldReceive('guard')->with('custom')->andReturn($auth); |
| 767 | + $auth->shouldReceive('guest')->andReturn(false); |
| 768 | + $auth->shouldReceive('user')->andReturn($user); |
| 769 | + |
| 770 | + $hasher = m::mock(Hasher::class); |
| 771 | + $hasher->shouldReceive('check')->andReturn(true); |
| 772 | + |
| 773 | + $container = m::mock(Container::class); |
| 774 | + $container->shouldReceive('make')->with('auth')->andReturn($auth); |
| 775 | + $container->shouldReceive('make')->with('hash')->andReturn($hasher); |
| 776 | + |
| 777 | + $trans = $this->getTranslator(); |
| 778 | + $trans->shouldReceive('get'); |
| 779 | + |
| 780 | + $v = new Validator($trans, ['password' => 'foo'], ['password' => 'password:custom']); |
| 781 | + $v->setContainer($container); |
| 782 | + |
| 783 | + $this->assertTrue($v->passes()); |
| 784 | + } |
| 785 | + |
689 | 786 | public function testValidatePresent()
|
690 | 787 | {
|
691 | 788 | $trans = $this->getIlluminateArrayTranslator();
|
|
0 commit comments