diff --git a/core/tests/Drupal/Tests/Core/Password/PasswordHashingTest.php b/core/tests/Drupal/Tests/Core/Password/PasswordHashingTest.php index 9306c20..bea698a 100644 --- a/core/tests/Drupal/Tests/Core/Password/PasswordHashingTest.php +++ b/core/tests/Drupal/Tests/Core/Password/PasswordHashingTest.php @@ -46,6 +46,12 @@ class PasswordHashingTest extends UnitTestCase { protected $md5Password; /** + * @var string + * The hashed password. + */ + protected $hashedPassword; + + /** * @var \Drupal\Core\Password\PhpassHashedPassword * The password hasher under test. */ @@ -64,7 +70,7 @@ public static function getInfo() { */ protected function setUp() { parent::setUp(); - $this->user = $this->getMock('Drupal\user\UserInterface'); + $this->user = $this->getMockBuilder('Drupal\user\Entity\User')->disableOriginalConstructor()->getMock(); $this->passwordHasher = new PhpassHashedPassword(1); } @@ -72,7 +78,7 @@ protected function setUp() { * Tests the hash count boundaries are enforced. */ public function testWithinBounds() { - $hasher = new MockPhpassHashedPassword(); + $hasher = new FakePhpassHashedPassword(); $this->assertEquals(PhpassHashedPassword::MIN_HASH_COUNT, $hasher->enforceLog2Boundaries(1), "Min hash count enforced"); $this->assertEquals(PhpassHashedPassword::MAX_HASH_COUNT, $hasher->enforceLog2Boundaries(100), "Max hash count enforced"); } @@ -93,28 +99,35 @@ public function testPasswordNeedsUpdate() { * Test password hashing. */ public function testPasswordHashing() { - $rehashed_password = $this->passwordHasher->hash($this->password); + $this->hashedPassword = $this->passwordHasher->hash($this->password); $this->user->expects($this->any()) ->method('getPassword') - ->will($this->returnValue($rehashed_password)); - $this->assertSame($this->passwordHasher->getCountLog2($rehashed_password), PhpassHashedPassword::MIN_HASH_COUNT, 'Re-hashed password has the minimum number of log2 iterations.'); - $this->assertTrue($rehashed_password != $this->md5Password, 'Password hash changed.'); + ->will($this->returnValue($this->hashedPassword)); + $this->assertSame($this->passwordHasher->getCountLog2($this->hashedPassword), PhpassHashedPassword::MIN_HASH_COUNT, 'Hashed password has the minimum number of log2 iterations.'); + $this->assertTrue($this->hashedPassword != $this->md5Password, 'Password hash changed.'); $this->assertTrue($this->passwordHasher->check($this->password, $this->user), 'Password check succeeds.'); // Since the log2 setting hasn't changed and the user has a valid password, // userNeedsNewHash() should return FALSE. $this->assertFalse($this->passwordHasher->userNeedsNewHash($this->user), 'User does not need a new hash.'); + } + + /** + * Tests password rehashing. + */ + public function testPasswordRehashing() { // Increment the log2 iteration to MIN + 1. $this->passwordHasher = new PhpassHashedPassword(PhpassHashedPassword::MIN_HASH_COUNT + 1); $this->assertTrue($this->passwordHasher->userNeedsNewHash($this->user), 'User needs a new hash after incrementing the log2 count.'); // Re-hash the password. - $rehashed_password2 = $this->passwordHasher->hash($this->password); + $rehashed_password = $this->passwordHasher->hash($this->password); $this->user->expects($this->any()) ->method('getPassword') - ->will($this->returnValue($rehashed_password2)); - $this->assertSame($this->passwordHasher->getCountLog2($rehashed_password2), PhpassHashedPassword::MIN_HASH_COUNT + 1, 'Re-hashed password has the correct number of log2 iterations.'); - $this->assertTrue($rehashed_password2 != $rehashed_password, 'Password hash changed again.'); + ->will($this->returnValue($rehashed_password)); + $this->assertSame($this->passwordHasher->getCountLog2($rehashed_password), PhpassHashedPassword::MIN_HASH_COUNT + 1, 'Re-hashed password has the correct number of log2 iterations.'); + $this->assertTrue($rehashed_password != $this->hashedPassword, 'Password hash changed again.'); + // Now the hash should be OK. $this->assertFalse($this->passwordHasher->userNeedsNewHash($this->user), 'Re-hashed password does not need a new hash.'); $this->assertTrue($this->passwordHasher->check($this->password, $this->user), 'Password check succeeds with re-hashed password.'); @@ -123,9 +136,9 @@ public function testPasswordHashing() { } /** - * A mock class for tests. + * A fake class for tests. */ -class MockPhpassHashedPassword extends PhpassHashedPassword { +class FakePhpassHashedPassword extends PhpassHashedPassword { function __construct() { // noop