mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-17 13:36:49 +00:00
10ec62707e
Fix #3999. Fix #4000. Fix #4002. Several bug reports and feature requests for Xml Reader arrived practically simultaneously. They are all small and hit the same code modules, so I have bundled them together in one PR. - `loadSpreadsheetFromString` might try to open a file with a falsy name (like '0'), which results in an exception with a misleading message (or a completely unexpected result if a file with that name exists). Code will still throw an exception, but the message will no longer be misleading, and no file I/O will be attempted. - function `trySimpleXmlLoadString` is deprecated. It should never have been implemented with public visibility, and the fact that it was made the fix above a little more difficult than it would otherwise have been. It is replaced with a private equivalent. - Style reader function `parseStyles` will now use a better namespace-aware method of reading its Xml data. Peculiarly, the Xml for the Style elements can either include or not a namespace prefix. This is probably because the global namespace and the styles namespace are the same. The existing prefix-based code does not recognize their equivalence, but the new namespace-based code does. Xml Reader continues to use prefix-based code in several other places. - Border line styles with Weight omitted or equal to 0 have been treated as no border, but they should be treated as 'hair' thickness. - Support for Zoom is added to Xml Reader. - In support of the above, new properties (and getters and setters) zoomScalePageLayoutView and zoomScaleSheetLayoutView are added to Worksheet/SheetView. (As far as I can tell, Excel does not support Sheet Layout View for Xml spreadsheets). - Support is added for those new properties in Xlsx Reader and Writer. - Xls Reader and Writer seem to work okay without changes. There is one test where Xls shows a different value for one of the properties than Xml or Xlsx, but the spreadsheet looks okay and I don't see any practical consequences of the difference. - PageBreak support is added to Xml Reader. - Code for writing out Column Page Breaks in Xlsx Writer was wrong (and, unsurprisingly, untested). A one-line change fixes it, and tests are added.
109 lines
3.5 KiB
PHP
109 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xml;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
|
|
use PhpOffice\PhpSpreadsheet\Reader\Xml;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class XmlTest extends TestCase
|
|
{
|
|
/**
|
|
* @dataProvider providerInvalidSimpleXML
|
|
*/
|
|
public function testInvalidSimpleXML(string $filename): void
|
|
{
|
|
$xmlReader = new Xml();
|
|
$this->expectException(ReaderException::class);
|
|
$this->expectExceptionMessage('Invalid Spreadsheet file');
|
|
$xmlReader->load($filename);
|
|
}
|
|
|
|
public static function providerInvalidSimpleXML(): array
|
|
{
|
|
$tests = [];
|
|
$glob = glob('tests/data/Reader/Xml/XEETestInvalidSimpleXML*.xml');
|
|
self::assertNotFalse($glob);
|
|
foreach ($glob as $file) {
|
|
$tests[basename($file)] = [realpath($file)];
|
|
}
|
|
|
|
return $tests;
|
|
}
|
|
|
|
/**
|
|
* Check if it can read XML Hyperlink correctly.
|
|
*/
|
|
public function testHyperlinksAltCharset(): void
|
|
{
|
|
$reader = new Xml();
|
|
$spreadsheet = $reader->load('tests/data/Reader/Xml/excel2003.iso8859-1.xml');
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
self::assertSame('Voilà', $spreadsheet->getActiveSheet()->getCell('A1')->getValue());
|
|
|
|
$hyperlink = $firstSheet->getCell('A2');
|
|
|
|
self::assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType());
|
|
self::assertEquals('PhpSpreadsheet', $hyperlink->getValue());
|
|
self::assertEquals('https://phpspreadsheet.readthedocs.io', $hyperlink->getHyperlink()->getUrl());
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public function testLoadCorruptedFile(): void
|
|
{
|
|
$this->expectException(ReaderException::class);
|
|
$this->expectExceptionMessage('Cannot load invalid XML file');
|
|
|
|
$xmlReader = new Xml();
|
|
$spreadsheet = @$xmlReader->load('tests/data/Reader/Xml/CorruptedXmlFile.xml');
|
|
self::assertNotSame('', $spreadsheet->getID());
|
|
}
|
|
|
|
public function testListWorksheetNamesCorruptedFile(): void
|
|
{
|
|
$this->expectException(ReaderException::class);
|
|
$this->expectExceptionMessage('Problem reading');
|
|
|
|
$xmlReader = new Xml();
|
|
$names = @$xmlReader->listWorksheetNames('tests/data/Reader/Xml/CorruptedXmlFile.xml');
|
|
self::assertNotEmpty($names);
|
|
}
|
|
|
|
public function testListWorksheetInfoCorruptedFile(): void
|
|
{
|
|
$this->expectException(ReaderException::class);
|
|
$this->expectExceptionMessage('Problem reading');
|
|
|
|
$xmlReader = new Xml();
|
|
$info = @$xmlReader->listWorksheetInfo('tests/data/Reader/Xml/CorruptedXmlFile.xml');
|
|
self::assertNotEmpty($info);
|
|
}
|
|
|
|
public function testInvalidXMLFromString(): void
|
|
{
|
|
$xmlReader = new Xml();
|
|
$this->expectException(ReaderException::class);
|
|
$this->expectExceptionMessage('Cannot load invalid XML string: 0');
|
|
$xmlReader->loadSpreadsheetFromString('0');
|
|
}
|
|
|
|
public function testInvalidXMLFromEmptyString(): void
|
|
{
|
|
$xmlReader = new Xml();
|
|
$this->expectException(ReaderException::class);
|
|
$this->expectExceptionMessage('Cannot load invalid XML string: ');
|
|
$xmlReader->loadSpreadsheetFromString('');
|
|
}
|
|
|
|
public function testEmptyFilename(): void
|
|
{
|
|
$xmlReader = new Xml();
|
|
$this->expectException(ReaderException::class);
|
|
$this->expectExceptionMessage('File "" does not exist');
|
|
$xmlReader->load('');
|
|
}
|
|
}
|