Files
oleibman 5c94de34a9 More Phpstan Level 10 Prep - Readers
Also upgrade Phpstan.
2025-05-03 21:18:55 -07:00

133 lines
5.1 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();
}
/** @return array<array{sheet: ?bool, autoFilter: ?bool, formatCells: ?bool, formatColumns: ?bool, formatRows: ?bool, insertColumns: ?bool, insertHyperlinks: ?bool, insertRows: ?bool, deleteColumns: ?bool, deleteRows: ?bool, objects: ?bool, pivotTables: ?bool, scenarios: ?bool, selectLockedCells: ?bool, selectUnlockedCells: ?bool, sort: ?bool, algorithm: string, spinCount: int}> */
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,
],
];
}
}