mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-13 11:36:24 +00:00
e9cf27354d
Successor to PR #3523. There are 494 single-line changes (`public function provider` to `public static function provider`) in this PR. None of these were made manually; they were all created with the following script (adapted from https://stackoverflow.com/questions/25909820/how-to-recursively-iterate-through-files-in-php): ```php $dir = 'C:/git/unit10prep2/tests/PhpSpreadsheetTests'; $it = new RecursiveDirectoryIterator($dir); // Loop through files foreach(new RecursiveIteratorIterator($it) as $file) { if ($file->getExtension() === 'php') { $contents = file_get_contents($file); $new = preg_replace('/public function (\\w*)([Pp])rovider/', 'public static function $1$2rovider', $contents); if ($new !== $contents) { echo "changing $file\n"; file_put_contents($file, $new); } } } ``` After this PR, there will be one more, with a small number of test changes, and enabling PhpUnit 10 for Php 8.1+.
76 lines
3.2 KiB
PHP
76 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Document;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
|
|
|
|
class SecurityTest extends AbstractFunctional
|
|
{
|
|
public function testSecurity(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$spreadsheet->getActiveSheet()->getCell('A1')->setValue('Hello');
|
|
$security = $spreadsheet->getSecurity();
|
|
$security->setLockRevision(true);
|
|
$revisionsPassword = 'revpasswd';
|
|
$security->setRevisionsPassword($revisionsPassword);
|
|
$hashedRevisionsPassword = $security->getRevisionsPassword();
|
|
self::assertNotEquals($revisionsPassword, $hashedRevisionsPassword);
|
|
$security->setLockWindows(true);
|
|
$security->setLockStructure(true);
|
|
$workbookPassword = 'wbpasswd';
|
|
$security->setWorkbookPassword($workbookPassword);
|
|
$hashedWorkbookPassword = $security->getWorkbookPassword();
|
|
self::assertNotEquals($workbookPassword, $hashedWorkbookPassword);
|
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
|
|
$reloadedSecurity = $reloadedSpreadsheet->getSecurity();
|
|
self::assertTrue($reloadedSecurity->getLockRevision());
|
|
self::assertTrue($reloadedSecurity->getLockWindows());
|
|
self::assertTrue($reloadedSecurity->getLockStructure());
|
|
self::assertSame($hashedWorkbookPassword, $reloadedSecurity->getWorkbookPassword());
|
|
self::assertSame($hashedRevisionsPassword, $reloadedSecurity->getRevisionsPassword());
|
|
|
|
$reloadedSecurity->setRevisionsPassword($hashedWorkbookPassword, true);
|
|
self::assertSame($hashedWorkbookPassword, $reloadedSecurity->getRevisionsPassword());
|
|
$reloadedSecurity->setWorkbookPassword($hashedRevisionsPassword, true);
|
|
self::assertSame($hashedRevisionsPassword, $reloadedSecurity->getWorkbookPassword());
|
|
}
|
|
|
|
public static function providerLocks(): array
|
|
{
|
|
return [
|
|
[false, false, false],
|
|
[false, false, true],
|
|
[false, true, false],
|
|
[false, true, true],
|
|
[true, false, false],
|
|
[true, false, true],
|
|
[true, true, false],
|
|
[true, true, true],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerLocks
|
|
*/
|
|
public function testLocks(bool $revision, bool $windows, bool $structure): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$spreadsheet->getActiveSheet()->getCell('A1')->setValue('Hello');
|
|
$security = $spreadsheet->getSecurity();
|
|
$security->setLockRevision($revision);
|
|
$security->setLockWindows($windows);
|
|
$security->setLockStructure($structure);
|
|
$enabled = $security->isSecurityEnabled();
|
|
self::assertSame($enabled, $revision || $windows || $structure);
|
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xlsx');
|
|
$reloadedSecurity = $reloadedSpreadsheet->getSecurity();
|
|
self::assertSame($revision, $reloadedSecurity->getLockRevision());
|
|
self::assertSame($windows, $reloadedSecurity->getLockWindows());
|
|
self::assertSame($structure, $reloadedSecurity->getLockStructure());
|
|
}
|
|
}
|