SecurityTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace PhpOffice\PhpSpreadsheetTests\Document;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
  5. class SecurityTest extends AbstractFunctional
  6. {
  7. public function testSecurity(): void
  8. {
  9. $spreadsheet = new Spreadsheet();
  10. $spreadsheet->getActiveSheet()->getCell('A1')->setValue('Hello');
  11. $security = $spreadsheet->getSecurity();
  12. $security->setLockRevision(true);
  13. $revisionsPassword = 'revpasswd';
  14. $security->setRevisionsPassword($revisionsPassword);
  15. $hashedRevisionsPassword = $security->getRevisionsPassword();
  16. self::assertNotEquals($revisionsPassword, $hashedRevisionsPassword);
  17. $security->setLockWindows(true);
  18. $security->setLockStructure(true);
  19. $workbookPassword = 'wbpasswd';
  20. $security->setWorkbookPassword($workbookPassword);
  21. $hashedWorkbookPassword = $security->getWorkbookPassword();
  22. self::assertNotEquals($workbookPassword, $hashedWorkbookPassword);
  23. $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
  24. $reloadedSecurity = $reloadedSpreadsheet->getSecurity();
  25. self::assertTrue($reloadedSecurity->getLockRevision());
  26. self::assertTrue($reloadedSecurity->getLockWindows());
  27. self::assertTrue($reloadedSecurity->getLockStructure());
  28. self::assertSame($hashedWorkbookPassword, $reloadedSecurity->getWorkbookPassword());
  29. self::assertSame($hashedRevisionsPassword, $reloadedSecurity->getRevisionsPassword());
  30. $reloadedSecurity->setRevisionsPassword($hashedWorkbookPassword, true);
  31. self::assertSame($hashedWorkbookPassword, $reloadedSecurity->getRevisionsPassword());
  32. $reloadedSecurity->setWorkbookPassword($hashedRevisionsPassword, true);
  33. self::assertSame($hashedRevisionsPassword, $reloadedSecurity->getWorkbookPassword());
  34. }
  35. public function providerLocks(): array
  36. {
  37. return [
  38. [false, false, false],
  39. [false, false, true],
  40. [false, true, false],
  41. [false, true, true],
  42. [true, false, false],
  43. [true, false, true],
  44. [true, true, false],
  45. [true, true, true],
  46. ];
  47. }
  48. /**
  49. * @dataProvider providerLocks
  50. */
  51. public function testLocks(bool $revision, bool $windows, bool $structure): void
  52. {
  53. $spreadsheet = new Spreadsheet();
  54. $spreadsheet->getActiveSheet()->getCell('A1')->setValue('Hello');
  55. $security = $spreadsheet->getSecurity();
  56. $security->setLockRevision($revision);
  57. $security->setLockWindows($windows);
  58. $security->setLockStructure($structure);
  59. $enabled = $security->isSecurityEnabled();
  60. self::assertSame($enabled, $revision || $windows || $structure);
  61. $reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
  62. $reloadedSecurity = $reloadedSpreadsheet->getSecurity();
  63. self::assertSame($revision, $reloadedSecurity->getLockRevision());
  64. self::assertSame($windows, $reloadedSecurity->getLockWindows());
  65. self::assertSame($structure, $reloadedSecurity->getLockStructure());
  66. }
  67. }