ProtectionTest.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
  3. use PhpOffice\PhpSpreadsheet\Worksheet\Protection;
  4. use PHPUnit\Framework\TestCase;
  5. class ProtectionTest extends TestCase
  6. {
  7. public function testVerifyPassword(): void
  8. {
  9. $protection = new Protection();
  10. self::assertTrue($protection->verify('foo'), 'non-protected always pass');
  11. $protection->setSheet(true);
  12. self::assertFalse($protection->verify('foo'), 'protected will fail');
  13. $protection->setPassword('foo', true);
  14. self::assertSame('foo', $protection->getPassword(), 'was not stored as-is, without hashing');
  15. self::assertFalse($protection->verify('foo'), 'setting already hashed password will not match');
  16. $protection->setPassword('foo');
  17. self::assertSame('CC40', $protection->getPassword(), 'was hashed');
  18. self::assertTrue($protection->verify('foo'), 'setting non-hashed password will hash it and not match');
  19. $protection->setAlgorithm(Protection::ALGORITHM_MD5);
  20. self::assertFalse($protection->verify('foo'), 'changing algorithm will not match anymore');
  21. $protection->setPassword('foo');
  22. $hash1 = $protection->getPassword();
  23. $protection->setPassword('foo');
  24. $hash2 = $protection->getPassword();
  25. self::assertSame(24, mb_strlen($hash1));
  26. self::assertSame(24, mb_strlen($hash2));
  27. self::assertNotSame($hash1, $hash2, 'was hashed with automatic salt');
  28. self::assertTrue($protection->verify('foo'), 'setting password again, will hash with proper algorithm and will match');
  29. }
  30. }