mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-24 15:48:34 +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.
54 lines
1.8 KiB
PHP
54 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
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'],
|
|
];
|
|
}
|
|
}
|