Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Xlsx/Issue3863Test.php
T
oleibman 9e89c36c97 Excel Omits Between Operator for Data Validation
Fix #3863. Data Validation default operator is `between`. When Excel writes out a data validation item, it may omit the operator. Xlsx reader will therefore initialize operator to null string. Issue indicates that user wants `between` returned for `getOperator`. A more serious problem is that `isValid` method does not handle this situation correctly. Data Validation is changed to set Operator to the default value if an attempt is made to set it to null string.
2024-01-11 23:03:55 -08:00

43 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Cell\DataValidator;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
class Issue3863Test extends \PHPUnit\Framework\TestCase
{
private static string $testbook = 'tests/data/Reader/XLSX/issue.3863.xlsx';
public function testPreliminaries(): void
{
$file = 'zip://';
$file .= self::$testbook;
$file .= '#xl/worksheets/sheet1.xml';
$data = file_get_contents($file);
if ($data === false) {
self::fail('Unable to read file');
} else {
// Only 1 Data Validation and it does not specify operator
self::assertStringContainsString('<dataValidations count="1"><dataValidation type="whole" allowBlank="1" showInputMessage="1" showErrorMessage="1" sqref="A1" xr:uid="{D0F98CC5-7234-4ADF-BD42-F33321DCD3CA}"><formula1>5</formula1><formula2>10</formula2></dataValidation></dataValidations>', $data);
}
}
public function testValidData(): void
{
$reader = new Xlsx();
$spreadsheet = $reader->load(self::$testbook);
$sheet = $spreadsheet->getActiveSheet();
self::assertSame('between', $sheet->getCell('A1')->getDataValidation()->getOperator());
$validator = new DataValidator();
self::assertTrue($validator->isValid($sheet->getCell('A1')));
$sheet->getCell('A1')->setValue(3);
self::assertFalse($validator->isValid($sheet->getCell('A1')));
$sheet->getCell('A1')->setValue(7);
self::assertTrue($validator->isValid($sheet->getCell('A1')));
$spreadsheet->disconnectWorksheets();
}
}