DefaultPasswordHasherTest.php 1.7 KB

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