@@ -718,6 +718,100 @@ public function testValidateArrayKeys()
718718 $ this ->assertFalse ($ v ->passes ());
719719 }
720720
721+ public function testValidateCurrentPassword ()
722+ {
723+ // Fails when user is not logged in.
724+ $ auth = m::mock (Guard::class);
725+ $ auth ->shouldReceive ('guard ' )->andReturn ($ auth );
726+ $ auth ->shouldReceive ('guest ' )->andReturn (true );
727+
728+ $ hasher = m::mock (Hasher::class);
729+
730+ $ container = m::mock (Container::class);
731+ $ container ->shouldReceive ('make ' )->with ('auth ' )->andReturn ($ auth );
732+ $ container ->shouldReceive ('make ' )->with ('hash ' )->andReturn ($ hasher );
733+
734+ $ trans = $ this ->getTranslator ();
735+ $ trans ->shouldReceive ('get ' )->andReturnArg (0 );
736+
737+ $ v = new Validator ($ trans , ['password ' => 'foo ' ], ['password ' => 'current_password ' ]);
738+ $ v ->setContainer ($ container );
739+
740+ $ this ->assertFalse ($ v ->passes ());
741+
742+ // Fails when password is incorrect.
743+ $ user = m::mock (Authenticatable::class);
744+ $ user ->shouldReceive ('getAuthPassword ' );
745+
746+ $ auth = m::mock (Guard::class);
747+ $ auth ->shouldReceive ('guard ' )->andReturn ($ auth );
748+ $ auth ->shouldReceive ('guest ' )->andReturn (false );
749+ $ auth ->shouldReceive ('user ' )->andReturn ($ user );
750+
751+ $ hasher = m::mock (Hasher::class);
752+ $ hasher ->shouldReceive ('check ' )->andReturn (false );
753+
754+ $ container = m::mock (Container::class);
755+ $ container ->shouldReceive ('make ' )->with ('auth ' )->andReturn ($ auth );
756+ $ container ->shouldReceive ('make ' )->with ('hash ' )->andReturn ($ hasher );
757+
758+ $ trans = $ this ->getTranslator ();
759+ $ trans ->shouldReceive ('get ' )->andReturnArg (0 );
760+
761+ $ v = new Validator ($ trans , ['password ' => 'foo ' ], ['password ' => 'current_password ' ]);
762+ $ v ->setContainer ($ container );
763+
764+ $ this ->assertFalse ($ v ->passes ());
765+
766+ // Succeeds when password is correct.
767+ $ user = m::mock (Authenticatable::class);
768+ $ user ->shouldReceive ('getAuthPassword ' );
769+
770+ $ auth = m::mock (Guard::class);
771+ $ auth ->shouldReceive ('guard ' )->andReturn ($ auth );
772+ $ auth ->shouldReceive ('guest ' )->andReturn (false );
773+ $ auth ->shouldReceive ('user ' )->andReturn ($ user );
774+
775+ $ hasher = m::mock (Hasher::class);
776+ $ hasher ->shouldReceive ('check ' )->andReturn (true );
777+
778+ $ container = m::mock (Container::class);
779+ $ container ->shouldReceive ('make ' )->with ('auth ' )->andReturn ($ auth );
780+ $ container ->shouldReceive ('make ' )->with ('hash ' )->andReturn ($ hasher );
781+
782+ $ trans = $ this ->getTranslator ();
783+ $ trans ->shouldReceive ('get ' )->andReturnArg (0 );
784+
785+ $ v = new Validator ($ trans , ['password ' => 'foo ' ], ['password ' => 'current_password ' ]);
786+ $ v ->setContainer ($ container );
787+
788+ $ this ->assertTrue ($ v ->passes ());
789+
790+ // We can use a specific guard.
791+ $ user = m::mock (Authenticatable::class);
792+ $ user ->shouldReceive ('getAuthPassword ' );
793+
794+ $ auth = m::mock (Guard::class);
795+ $ auth ->shouldReceive ('guard ' )->with ('custom ' )->andReturn ($ auth );
796+ $ auth ->shouldReceive ('guest ' )->andReturn (false );
797+ $ auth ->shouldReceive ('user ' )->andReturn ($ user );
798+
799+ $ hasher = m::mock (Hasher::class);
800+ $ hasher ->shouldReceive ('check ' )->andReturn (true );
801+
802+ $ container = m::mock (Container::class);
803+ $ container ->shouldReceive ('make ' )->with ('auth ' )->andReturn ($ auth );
804+ $ container ->shouldReceive ('make ' )->with ('hash ' )->andReturn ($ hasher );
805+
806+ $ trans = $ this ->getTranslator ();
807+ $ trans ->shouldReceive ('get ' )->andReturnArg (0 );
808+
809+ $ v = new Validator ($ trans , ['password ' => 'foo ' ], ['password ' => 'current_password:custom ' ]);
810+ $ v ->setContainer ($ container );
811+
812+ $ this ->assertTrue ($ v ->passes ());
813+ }
814+
721815 public function testValidateFilled ()
722816 {
723817 $ trans = $ this ->getIlluminateArrayTranslator ();
0 commit comments