Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Reader/Xml/XmlLoadTest.php
T
oleibman 04e7c30758 Fix Two 32-bit Timestamp Problems, and Minor getFormattedValue Bug (#1891)
I ran the test suite using 32-bit PHP. There were 2 places where changes
were needed due to 32-bit timestamps.

Reader\\Xml.php was using strtotime as an intermediate step in converting
a string timestamp to an Excel timestamp. The XML file type stores pure timestamps
(i.e. no date portion) as, e.g., 1899-12-31T02:30:00.000, and that value
causes an error using strtotime on a 32-bit system. However, it is sufficient
to use that value in a DateTime constructor, and that will work for 32- and 64-bit.

There was no test for that particular cell, so I added one to the XML read test.
And that's when I discovered the getFormattedValue bug. The cell's format
is `hh":"mm":"ss`. The quotes around the colons are disrupting the formatting.
PhpSpreadsheet formats the cell by converting the Excel format
to a Php Date format, in this case `H\:m\:s`.
That's a problem,
since Excel thinks 'm' means *minutes*, but PHP thinks it means *months*.
This is not a problem when the colon is not quoted; there are ample tests for that.
I added my best guess as to how to recognize this situation,
changing `\:m` to `:i`. The XML read test
now succeeds, and no other tests were broken by this change.

Test Shared\\DateTest had one test where the expected result of converting to a
Unix timestamp exceeds 2**32. Since a Unix timestamp is strictly an int,
that test fails on a 32-bit system. In the discussion regarding recently merged
PR #1870, it was felt that the user base might still be using the functions
that convert to and from a timestamp. So, we should not drop this test, but,
since it cannot succeed on a 32-bit system, I changed it to be skipped
whenever the expected result exceeded PHP_INT_MAX. There are 3 "toTimestamp"
functions within that test. Only one of these had been affected, but I thought
it was a good idea to add additional tests to the others to demonstrate this
condition.

In the course of testing, I also discovered some 32-bit problems with
bitwise and base-conversion functions. I am preparing separate PRs to
deal with those.
2021-03-03 10:52:11 +01:00

100 lines
4.4 KiB
PHP

<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xml;
use PhpOffice\PhpSpreadsheet\Reader\Xml;
use PHPUnit\Framework\TestCase;
class XmlLoadTest extends TestCase
{
public function testLoad(): void
{
$filename = __DIR__
. '/../../../..'
. '/samples/templates/excel2003.xml';
$reader = new Xml();
$spreadsheet = $reader->load($filename);
self::assertEquals(2, $spreadsheet->getSheetCount());
$sheet = $spreadsheet->getSheet(1);
self::assertEquals('Report Data', $sheet->getTitle());
self::assertEquals('BCD', $sheet->getCell('A4')->getValue());
$props = $spreadsheet->getProperties();
self::assertEquals('Mark Baker', $props->getCreator());
self::assertEquals('AbCd1234', $props->getCustomPropertyValue('my_API_Token'));
self::assertEquals('2', $props->getCustomPropertyValue('myאInt'));
$sheet = $spreadsheet->getSheet(0);
self::assertEquals('Sample Data', $sheet->getTitle());
self::assertEquals('Test String 1', $sheet->getCell('A1')->getValue());
self::assertEquals('Test with (") in string', $sheet->getCell('A4')->getValue());
self::assertEquals(22269, $sheet->getCell('A10')->getValue());
self::assertEquals('dd/mm/yyyy', $sheet->getCell('A10')->getStyle()->getNumberFormat()->getFormatCode());
self::assertEquals('19/12/1960', $sheet->getCell('A10')->getFormattedValue());
self::assertEquals(1.5, $sheet->getCell('A11')->getValue());
self::assertEquals('# ?0/??0', $sheet->getCell('A11')->getStyle()->getNumberFormat()->getFormatCode());
// Same pattern, same value, different display in Gnumeric vs Excel
//self::assertEquals('1 1/2', $sheet->getCell('A11')->getFormattedValue());
self::assertEquals('hh":"mm":"ss', $sheet->getCell('A13')->getStyle()->getNumberFormat()->getFormatCode());
self::assertEquals('02:30:00', $sheet->getCell('A13')->getFormattedValue());
self::assertEquals('d/m/yy hh":"mm', $sheet->getCell('A15')->getStyle()->getNumberFormat()->getFormatCode());
self::assertEquals('19/12/60 01:30', $sheet->getCell('A15')->getFormattedValue());
self::assertEquals('=B1+C1', $sheet->getCell('H1')->getValue());
self::assertEquals('=E2&F2', $sheet->getCell('J2')->getValue());
self::assertEquals('=SUM(C1:C4)', $sheet->getCell('I5')->getValue());
// property not yet supported
//self::assertFalse($sheet->getRowDimension(30)->getVisible());
$hyperlink = $sheet->getCell('A21');
self::assertEquals('PhpSpreadsheet', $hyperlink->getValue());
self::assertEquals('https://github.com/PHPOffice/PhpSpreadsheet', $hyperlink->getHyperlink()->getUrl());
}
public function testLoadFilter(): void
{
$filename = __DIR__
. '/../../../..'
. '/samples/templates/excel2003.xml';
$reader = new Xml();
$filter = new XmlFilter();
$reader->setReadFilter($filter);
$spreadsheet = $reader->load($filename);
self::assertEquals(2, $spreadsheet->getSheetCount());
$sheet = $spreadsheet->getSheet(1);
self::assertEquals('Report Data', $sheet->getTitle());
self::assertEquals('', $sheet->getCell('A4')->getValue());
$props = $spreadsheet->getProperties();
self::assertEquals('Mark Baker', $props->getCreator());
}
public function testLoadSelectedSheets(): void
{
$filename = __DIR__
. '/../../../..'
. '/samples/templates/excel2003.xml';
$reader = new Xml();
$reader->setLoadSheetsOnly(['Unknown Sheet', 'Report Data']);
$spreadsheet = $reader->load($filename);
self::assertEquals(1, $spreadsheet->getSheetCount());
$sheet = $spreadsheet->getSheet(0);
self::assertEquals('Report Data', $sheet->getTitle());
self::assertEquals('Third Heading', $sheet->getCell('C2')->getValue());
}
public function testLoadUnusableSample(): void
{
// Sample spreadsheet is not readable by Excel.
// But PhpSpreadsheet can load it.
$filename = __DIR__
. '/../../../..'
. '/samples/templates/excel2003.short.bad.xml';
$reader = new Xml();
$spreadsheet = $reader->load($filename);
self::assertEquals(1, $spreadsheet->getSheetCount());
$sheet = $spreadsheet->getSheet(0);
self::assertEquals('Sample Data', $sheet->getTitle());
}
}