DefaultPasswordHasherTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Auth;
  17. use Cake\Auth\DefaultPasswordHasher;
  18. use Cake\TestSuite\TestCase;
  19. /**
  20. * Test case for DefaultPasswordHasher
  21. */
  22. class DefaultPasswordHasherTest extends TestCase
  23. {
  24. /**
  25. * Tests that a password not produced by DefaultPasswordHasher needs
  26. * to be rehashed
  27. *
  28. * @return void
  29. */
  30. public function testNeedsRehash(): void
  31. {
  32. $hasher = new DefaultPasswordHasher();
  33. $this->assertTrue($hasher->needsRehash(md5('foo')));
  34. $password = $hasher->hash('foo');
  35. $this->assertFalse($hasher->needsRehash($password));
  36. }
  37. /**
  38. * Tests that when the hash options change, the password needs
  39. * to be rehashed
  40. *
  41. * @return void
  42. */
  43. public function testNeedsRehashWithDifferentOptions(): void
  44. {
  45. $defaultHasher = new DefaultPasswordHasher(['hashType' => PASSWORD_BCRYPT, 'hashOptions' => ['cost' => 10]]);
  46. $updatedHasher = new DefaultPasswordHasher(['hashType' => PASSWORD_BCRYPT, 'hashOptions' => ['cost' => 12]]);
  47. $password = $defaultHasher->hash('foo');
  48. $this->assertTrue($updatedHasher->needsRehash($password));
  49. }
  50. }