b3d30f4cbc
* Xls Writer - Correct Timestamp Bug, Improve Coverage I believe that Xls Writer is 100% covered now. The Xls Writer sets its timestamp incorrectly. The problem is actually in Shared/Ole::localDateToOLE, which converts its timestamp using gmmktime; mktime is correct. If I save a file at 3:00 p.m. in San Francisco, this bug means the time is actually recorded as 3:00 p.m. UTC. A consequence of this is that if you use Phpspreadsheet to read the file and save it as a new Xls, the creation timestamp goes further and further back in time with each generation (or further forward if east of Greenwich). One of the tests added confirms that the creation timestamp is consistent with the start and end times of the test. The major change in coverage is adding tests to save GIF and BMP images, which aren't supported in Xls, but are converted to PNG in the PhpSpreadsheet code.
85 lines
3.1 KiB
PHP
85 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
|
|
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional;
|
|
|
|
class XlsGifBmpTest extends AbstractFunctional
|
|
{
|
|
private $filename = '';
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
if ($this->filename) {
|
|
unlink($this->filename);
|
|
}
|
|
$this->filename = '';
|
|
}
|
|
|
|
public function testBmp(): void
|
|
{
|
|
$pgmstart = time();
|
|
$spreadsheet = new Spreadsheet();
|
|
$filstart = $spreadsheet->getProperties()->getModified();
|
|
self::assertLessThanOrEqual($filstart, $pgmstart);
|
|
|
|
// Add a drawing to the worksheet
|
|
$drawing = new Drawing();
|
|
$drawing->setName('Letters B, M, and P');
|
|
$drawing->setDescription('Handwritten B, M, and P');
|
|
$drawing->setPath(__DIR__ . '/../../../../samples/images/bmp.bmp');
|
|
$drawing->setHeight(36);
|
|
$drawing->setWorksheet($spreadsheet->getActiveSheet());
|
|
$drawing->setCoordinates('A1');
|
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xls');
|
|
$creationDatestamp = $reloadedSpreadsheet->getProperties()->getCreated();
|
|
$filstart = $creationDatestamp;
|
|
$pSheet = $reloadedSpreadsheet->getActiveSheet();
|
|
$drawings = $pSheet->getDrawingCollection();
|
|
self::assertCount(1, $drawings);
|
|
foreach ($pSheet->getDrawingCollection() as $drawing) {
|
|
// See if Scrutinizer approves this
|
|
$mimeType = ($drawing instanceof MemoryDrawing) ? $drawing->getMimeType() : 'notmemorydrawing';
|
|
self::assertEquals('image/png', $mimeType);
|
|
}
|
|
$pgmend = time();
|
|
|
|
self::assertLessThanOrEqual($pgmend, $pgmstart);
|
|
self::assertLessThanOrEqual($pgmend, $filstart);
|
|
self::assertLessThanOrEqual($filstart, $pgmstart);
|
|
}
|
|
|
|
public function testGif(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
// Add a drawing to the worksheet
|
|
$drawing = new Drawing();
|
|
$drawing->setName('Letters G, I, and G');
|
|
$drawing->setDescription('Handwritten G, I, and F');
|
|
$drawing->setPath(__DIR__ . '/../../../../samples/images/gif.gif');
|
|
$drawing->setHeight(36);
|
|
$drawing->setWorksheet($spreadsheet->getActiveSheet());
|
|
$drawing->setCoordinates('A1');
|
|
|
|
$reloadedSpreadsheet = $this->writeAndReload($spreadsheet, 'Xls');
|
|
$pSheet = $reloadedSpreadsheet->getActiveSheet();
|
|
$drawings = $pSheet->getDrawingCollection();
|
|
self::assertCount(1, $drawings);
|
|
foreach ($pSheet->getDrawingCollection() as $drawing) {
|
|
$mimeType = ($drawing instanceof MemoryDrawing) ? $drawing->getMimeType() : 'notmemorydrawing';
|
|
self::assertEquals('image/png', $mimeType);
|
|
}
|
|
}
|
|
|
|
public function testInvalidTimestamp(): void
|
|
{
|
|
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
|
|
\PhpOffice\PhpSpreadsheet\Shared\OLE::OLE2LocalDate(' ');
|
|
}
|
|
}
|