mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-22 16:19:19 +00:00
ec4098c8fd
While we might never be able to have 100% of our code strict, we can at the very least do it for all of our tests. This ensures that our tests are using our API with the types as intended by the test author, and not silently be cast to what our API requires.
132 lines
4.7 KiB
PHP
132 lines
4.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
|
|
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
|
|
|
|
class SheetProtectionTest extends AbstractFunctional
|
|
{
|
|
private const FILENAME = 'tests/data/Reader/XLSX/sheetprotect.xlsx';
|
|
|
|
public function testSheetProtection(): void
|
|
{
|
|
$reader = new Xlsx();
|
|
$originalSpreadsheet = $reader->load(self::FILENAME);
|
|
$spreadsheet = $this->writeAndReload($originalSpreadsheet, 'Xlsx');
|
|
$originalSpreadsheet->disconnectWorksheets();
|
|
$assertions = $this->pageSetupAssertions();
|
|
|
|
foreach ($spreadsheet->getAllSheets() as $worksheet) {
|
|
if (!array_key_exists($worksheet->getTitle(), $assertions)) {
|
|
continue;
|
|
}
|
|
|
|
$sheetAssertions = $assertions[$worksheet->getTitle()];
|
|
foreach ($sheetAssertions as $test => $expectedResult) {
|
|
$testMethodName = 'get' . ucfirst($test);
|
|
$actualResult = $worksheet->getProtection()->$testMethodName();
|
|
self::assertSame(
|
|
$expectedResult,
|
|
$actualResult,
|
|
"Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test}"
|
|
);
|
|
}
|
|
}
|
|
$protection0 = $spreadsheet->getSheet(0)->getProtection();
|
|
self::assertTrue($protection0->verify('password'));
|
|
self::assertFalse($protection0->verify('passwordx'));
|
|
$protection1 = $spreadsheet->getSheet(1)->getProtection();
|
|
self::assertTrue($protection1->verify('anything'), 'no password so anything works');
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
private function pageSetupAssertions(): array
|
|
{
|
|
return [
|
|
'Sheet1' => [
|
|
'sheet' => true,
|
|
'autoFilter' => null,
|
|
'formatCells' => null,
|
|
'formatColumns' => null,
|
|
'formatRows' => false,
|
|
'insertColumns' => null,
|
|
'insertHyperlinks' => null,
|
|
'insertRows' => null,
|
|
'deleteColumns' => false,
|
|
'deleteRows' => null,
|
|
'objects' => true,
|
|
'pivotTables' => null,
|
|
'scenarios' => null,
|
|
'selectLockedCells' => null,
|
|
'selectUnlockedCells' => null,
|
|
'sort' => false,
|
|
'algorithm' => 'SHA-512',
|
|
'spinCount' => 100000,
|
|
],
|
|
'Sheet2' => [
|
|
'sheet' => true,
|
|
'autoFilter' => null,
|
|
'formatCells' => false,
|
|
'formatColumns' => false,
|
|
'formatRows' => false,
|
|
'insertColumns' => false,
|
|
'insertHyperlinks' => null,
|
|
'insertRows' => null,
|
|
'deleteColumns' => null,
|
|
'deleteRows' => null,
|
|
'objects' => true,
|
|
'pivotTables' => null,
|
|
'scenarios' => true,
|
|
'selectLockedCells' => null,
|
|
'selectUnlockedCells' => null,
|
|
'sort' => null,
|
|
'algorithm' => '',
|
|
'spinCount' => 10000,
|
|
],
|
|
'Sheet3' => [
|
|
'sheet' => true,
|
|
'autoFilter' => null,
|
|
'formatCells' => null,
|
|
'formatColumns' => null,
|
|
'formatRows' => null,
|
|
'insertColumns' => null,
|
|
'insertHyperlinks' => false,
|
|
'insertRows' => null,
|
|
'deleteColumns' => null,
|
|
'deleteRows' => null,
|
|
'objects' => null,
|
|
'pivotTables' => null,
|
|
'scenarios' => true,
|
|
'selectLockedCells' => true,
|
|
'selectUnlockedCells' => true,
|
|
'sort' => null,
|
|
'algorithm' => '',
|
|
'spinCount' => 10000,
|
|
],
|
|
'Sheet4' => [
|
|
'sheet' => null,
|
|
'autoFilter' => null,
|
|
'formatCells' => null,
|
|
'formatColumns' => null,
|
|
'formatRows' => null,
|
|
'insertColumns' => null,
|
|
'insertHyperlinks' => null,
|
|
'insertRows' => null,
|
|
'deleteColumns' => null,
|
|
'deleteRows' => null,
|
|
'objects' => null,
|
|
'pivotTables' => null,
|
|
'scenarios' => null,
|
|
'selectLockedCells' => null,
|
|
'selectUnlockedCells' => null,
|
|
'sort' => null,
|
|
'algorithm' => '',
|
|
'spinCount' => 10000,
|
|
],
|
|
];
|
|
}
|
|
}
|