mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-18 14:06:34 +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+.
52 lines
1.8 KiB
PHP
52 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\PasswordHasher;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
|
|
|
|
class PasswordReloadTest extends AbstractFunctional
|
|
{
|
|
/**
|
|
* @dataProvider providerPasswords
|
|
*/
|
|
public function testPasswordReload(string $format, string $algorithm, bool $supported = true): void
|
|
{
|
|
$password = 'hello';
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->getCell('A1')->setValue(1);
|
|
$protection = $sheet->getProtection();
|
|
$protection->setAlgorithm($algorithm);
|
|
$protection->setPassword($password);
|
|
$protection->setSheet(true);
|
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, $format);
|
|
$resheet = $reloadedSpreadsheet->getActiveSheet();
|
|
$reprot = $resheet->getProtection();
|
|
$repassword = $reprot->getPassword();
|
|
$hash = '';
|
|
if ($supported) {
|
|
$readAlgorithm = $reprot->getAlgorithm();
|
|
self::assertSame($algorithm, $readAlgorithm);
|
|
$salt = $reprot->getSalt();
|
|
$spin = $reprot->getSpinCount();
|
|
$hash = PasswordHasher::hashPassword($password, $readAlgorithm, $salt, $spin);
|
|
}
|
|
self::assertSame($repassword, $hash);
|
|
$spreadsheet->disconnectWorksheets();
|
|
$reloadedSpreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public static function providerPasswords(): array
|
|
{
|
|
return [
|
|
'Xls basic algorithm' => ['Xls', ''],
|
|
'Xls cannot use SHA512' => ['Xls', 'SHA-512', false],
|
|
'Xlsx basic algorithm' => ['Xlsx', ''],
|
|
'Xlsx can use SHA512' => ['Xlsx', 'SHA-512'],
|
|
];
|
|
}
|
|
}
|