Files
oleibman c5527ba534 Unexpected Namespacing in rels File (#3722)
* Unexpected Namespacing in rels File

Fix #3720. Third-party product created a spreadsheet which PhpSpreadsheet could not read because of unexpected namespacing in workbook.xml.rels.

The file which demonstrated the problem was attached to #3423, however I do not believe it was related to the original problem. Nevertheless, the original issue specifically called out Protection, so I put some Protection tests in the validation test for the fix. In doing so, I found that Style/Protection is particularly confusing. Its properties will often have the value `inherit`, which isn't all that helpful; and, even when the `locked` value is `protected`, the cell won't actually be locked unless the sheet is protected as well. The `hidden` property is even more obscure - it applies only to formulas, and refers to hiding the property on the formula bar, not in the cell. I have added methods `isLocked` and `isHiddenOnFormulaBar` to `Cell`. I corrected the docs to explain this. And, as long as I was looking at the docs, I corrected some examples to use `getHighestDataRow/Column` rather than `getHighestRow/Column`, a frequent problem for users (e.g. #3721).

As a side note, the change to Cell.php is my first use of the nullsafe operator. This is one of many new options available now that we require Php8.0+.

* Minor Simplifications

* Scrutinizer

It's being silly again. In many tests, we test a variable for non-null, then use that variable later and Scrutinizer knows it's not null. Not here. Oh well.

* Add Methods

Test if protected without allocating cell if it doesn't exist.
2023-09-12 19:13:03 -07:00

167 lines
8.0 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Protection;
use PHPUnit\Framework\TestCase;
class Protection2Test extends TestCase
{
public function testisHiddenOnFormulaBar(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue('X')
->getStyle()->getProtection()
->setHidden(Protection::PROTECTION_UNPROTECTED);
$sheet->getCell('A2')->setValue('=SUM(1,2)')
->getStyle()->getProtection()
->setHidden(Protection::PROTECTION_UNPROTECTED);
$sheet->getCell('B1')->setValue('X')
->getStyle()->getProtection()
->setHidden(Protection::PROTECTION_PROTECTED);
$sheet->getCell('B2')->setValue('=SUM(1,2)')
->getStyle()->getProtection()
->setHidden(Protection::PROTECTION_PROTECTED);
$sheet->getCell('C1')->setValue('X');
$sheet->getCell('C2')->setValue('=SUM(1,2)');
self::assertFalse($sheet->getCell('A1')->isHiddenOnFormulaBar());
self::assertFalse($sheet->getCell('A2')->isHiddenOnFormulaBar());
self::assertFalse($sheet->getCell('B1')->isHiddenOnFormulaBar());
self::assertFalse($sheet->getCell('B2')->isHiddenOnFormulaBar());
self::assertFalse($sheet->getCell('C1')->isHiddenOnFormulaBar());
self::assertFalse($sheet->getCell('C2')->isHiddenOnFormulaBar());
$sheetProtection = $sheet->getProtection();
$sheetProtection->setSheet(true);
self::assertFalse($sheet->getCell('A1')->isHiddenOnFormulaBar());
self::assertFalse($sheet->getCell('A2')->isHiddenOnFormulaBar());
self::assertFalse($sheet->getCell('B1')->isHiddenOnFormulaBar(), 'not a formula1');
self::assertTrue($sheet->getCell('B2')->isHiddenOnFormulaBar());
self::assertFalse($sheet->getCell('C1')->isHiddenOnFormulaBar(), 'not a formula2');
self::assertTrue($sheet->getCell('C2')->isHiddenOnFormulaBar());
self::assertFalse($sheet->getCell('D1')->isHiddenOnFormulaBar(), 'uninitialized cell is not formula');
$spreadsheet->disconnectWorksheets();
}
/**
* Same as above, but uses $sheet->isCellHiddenOnFormulaBar
* rather than $sheet->getCell()->isHiddenOnFormulaBar().
*/
public function testisHiddenOnFormulaBar2(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue('X')
->getStyle()->getProtection()
->setHidden(Protection::PROTECTION_UNPROTECTED);
$sheet->getCell('A2')->setValue('=SUM(1,2)')
->getStyle()->getProtection()
->setHidden(Protection::PROTECTION_UNPROTECTED);
$sheet->getCell('B1')->setValue('X')
->getStyle()->getProtection()
->setHidden(Protection::PROTECTION_PROTECTED);
$sheet->getCell('B2')->setValue('=SUM(1,2)')
->getStyle()->getProtection()
->setHidden(Protection::PROTECTION_PROTECTED);
$sheet->getCell('C1')->setValue('X');
$sheet->getCell('C2')->setValue('=SUM(1,2)');
self::assertFalse($sheet->isCellHiddenOnFormulaBar('A1'));
self::assertFalse($sheet->isCellHiddenOnFormulaBar('A2'));
self::assertFalse($sheet->isCellHiddenOnFormulaBar('B1'));
self::assertFalse($sheet->isCellHiddenOnFormulaBar('B2'));
self::assertFalse($sheet->isCellHiddenOnFormulaBar('C1'));
self::assertFalse($sheet->isCellHiddenOnFormulaBar('C2'));
$sheetProtection = $sheet->getProtection();
$sheetProtection->setSheet(true);
self::assertFalse($sheet->isCellHiddenOnFormulaBar('A1'));
self::assertFalse($sheet->isCellHiddenOnFormulaBar('A2'));
self::assertFalse($sheet->isCellHiddenOnFormulaBar('B1'), 'not a formula1');
self::assertTrue($sheet->isCellHiddenOnFormulaBar('B2'));
self::assertFalse($sheet->isCellHiddenOnFormulaBar('C1'), 'not a formula2');
self::assertTrue($sheet->isCellHiddenOnFormulaBar('C2'));
self::assertFalse($sheet->isCellHiddenOnFormulaBar('D1'), 'uninitialized cell is not formula');
self::assertFalse($sheet->cellExists('D1'));
$spreadsheet->disconnectWorksheets();
}
public function testisLocked(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue('X')
->getStyle()->getProtection()
->setLocked(Protection::PROTECTION_UNPROTECTED);
$sheet->getCell('A2')->setValue('=SUM(1,2)')
->getStyle()->getProtection()
->setLocked(Protection::PROTECTION_UNPROTECTED);
$sheet->getCell('B1')->setValue('X')
->getStyle()->getProtection()
->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getCell('B2')->setValue('=SUM(1,2)')
->getStyle()->getProtection()
->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getCell('C1')->setValue('X');
$sheet->getCell('C2')->setValue('=SUM(1,2)');
self::assertFalse($sheet->getCell('A1')->isLocked());
self::assertFalse($sheet->getCell('A2')->isLocked());
self::assertFalse($sheet->getCell('B1')->isLocked());
self::assertFalse($sheet->getCell('B2')->isLocked());
self::assertFalse($sheet->getCell('C1')->isLocked());
self::assertFalse($sheet->getCell('C2')->isLocked());
$sheetProtection = $sheet->getProtection();
$sheetProtection->setSheet(true);
self::assertFalse($sheet->getCell('A1')->isLocked());
self::assertFalse($sheet->getCell('A2')->isLocked());
self::assertTrue($sheet->getCell('B1')->isLocked());
self::assertTrue($sheet->getCell('B2')->isLocked());
self::assertTrue($sheet->getCell('C1')->isLocked());
self::assertTrue($sheet->getCell('C2')->isLocked());
self::assertTrue($sheet->getCell('D1')->isLocked(), 'uninitialized cell');
$spreadsheet->disconnectWorksheets();
}
/**
* Same as above, but uses $sheet->isCellLocked
* rather than $sheet->getCell()->isLocked().
*/
public function testisLocked2(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->getCell('A1')->setValue('X')
->getStyle()->getProtection()
->setLocked(Protection::PROTECTION_UNPROTECTED);
$sheet->getCell('A2')->setValue('=SUM(1,2)')
->getStyle()->getProtection()
->setLocked(Protection::PROTECTION_UNPROTECTED);
$sheet->getCell('B1')->setValue('X')
->getStyle()->getProtection()
->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getCell('B2')->setValue('=SUM(1,2)')
->getStyle()->getProtection()
->setLocked(Protection::PROTECTION_PROTECTED);
$sheet->getCell('C1')->setValue('X');
$sheet->getCell('C2')->setValue('=SUM(1,2)');
self::assertFalse($sheet->isCellLocked('A1'));
self::assertFalse($sheet->isCellLocked('A2'));
self::assertFalse($sheet->isCellLocked('B1'));
self::assertFalse($sheet->isCellLocked('B2'));
self::assertFalse($sheet->isCellLocked('C1'));
self::assertFalse($sheet->isCellLocked('C2'));
$sheetProtection = $sheet->getProtection();
$sheetProtection->setSheet(true);
self::assertFalse($sheet->isCellLocked('A1'));
self::assertFalse($sheet->isCellLocked('A2'));
self::assertTrue($sheet->isCellLocked('B1'));
self::assertTrue($sheet->isCellLocked('B2'));
self::assertTrue($sheet->isCellLocked('C1'));
self::assertTrue($sheet->isCellLocked('C2'));
self::assertTrue($sheet->isCellLocked('D1'), 'uninitialized cell');
self::assertFalse($sheet->cellExists('D1'));
$spreadsheet->disconnectWorksheets();
}
}