Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix For #1772 Null Exception on ODS Read #1776

Merged
merged 1 commit into from
Jan 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Reader/Ods/PageSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ private function readPageSettingStyles(DOMDocument $styleDom): void
$marginBottom = $pageLayoutProperties->getAttributeNS($this->stylesFo, 'margin-bottom');
$header = $styleSet->getElementsByTagNameNS($this->stylesNs, 'header-style')[0];
$headerProperties = $header->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0];
$marginHeader = $headerProperties->getAttributeNS($this->stylesFo, 'min-height');
$marginHeader = isset($headerProperties) ? $headerProperties->getAttributeNS($this->stylesFo, 'min-height') : null;
MarkBaker marked this conversation as resolved.
Show resolved Hide resolved
$footer = $styleSet->getElementsByTagNameNS($this->stylesNs, 'footer-style')[0];
$footerProperties = $footer->getElementsByTagNameNS($this->stylesNs, 'header-footer-properties')[0];
$marginFooter = $footerProperties->getAttributeNS($this->stylesFo, 'min-height');
$marginFooter = isset($footerProperties) ? $footerProperties->getAttributeNS($this->stylesFo, 'min-height') : null;

$this->pageLayoutStyles[$styleName] = (object) [
'orientation' => $styleOrientation ?: PageSetup::ORIENTATION_DEFAULT,
Expand Down
98 changes: 98 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Ods/PageSetupBug1772Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;

use PhpOffice\PhpSpreadsheet\Reader\Ods;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PHPUnit\Framework\TestCase;

class PageSetupBug1772Test extends TestCase
{
private const MARGIN_PRECISION = 0.00000001;

/**
* @var Spreadsheet
*/
private $spreadsheet;

protected function setup(): void
{
$filename = 'tests/data/Reader/Ods/bug1772.ods';
$reader = new Ods();
$this->spreadsheet = $reader->load($filename);
}

public function testPageSetup(): void
{
$assertions = $this->pageSetupAssertions();

foreach ($this->spreadsheet->getAllSheets() as $worksheet) {
if (!array_key_exists($worksheet->getTitle(), $assertions)) {
continue;
}

$sheetAssertions = $assertions[$worksheet->getTitle()];
foreach ($sheetAssertions as $test => $expectedResult) {
$testMethodName = 'get' . ucfirst($test);
$actualResult = $worksheet->getPageSetup()->$testMethodName();
self::assertSame(
$expectedResult,
$actualResult,
"Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test}"
);
}
}
}

public function testPageMargins(): void
{
$assertions = $this->pageMarginAssertions();

foreach ($this->spreadsheet->getAllSheets() as $worksheet) {
if (!array_key_exists($worksheet->getTitle(), $assertions)) {
continue;
}

$sheetAssertions = $assertions[$worksheet->getTitle()];
foreach ($sheetAssertions as $test => $expectedResult) {
$testMethodName = 'get' . ucfirst($test);
$actualResult = $worksheet->getPageMargins()->$testMethodName();
self::assertEqualsWithDelta(
$expectedResult,
$actualResult,
self::MARGIN_PRECISION,
"Failed assertion for Worksheet '{$worksheet->getTitle()}' {$test} margin"
);
}
}
}

private function pageSetupAssertions(): array
{
return [
'Employee update template' => [
'orientation' => PageSetup::ORIENTATION_DEFAULT,
'scale' => 100,
'horizontalCentered' => false,
'verticalCentered' => false,
'pageOrder' => PageSetup::PAGEORDER_DOWN_THEN_OVER,
],
];
}

private function pageMarginAssertions(): array
{
return [
'Employee update template' => [
// Here the values are in cm
'top' => 0.0,
'header' => 0.2953,
'left' => 0.0,
'right' => 0.0,
'bottom' => 0.0,
'footer' => 0.2953,
],
];
}
}
8 changes: 4 additions & 4 deletions tests/data/Calculation/DateTime/DATEVALUE.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,12 @@
],
// 01/01 of the current year
[
43831,
44197,
'1 Jan',
],
// 31/12 of the current year
[
44196,
44561,
'31/12',
],
// Excel reads as 1st December 1931, not 31st December in current year
Expand All @@ -176,12 +176,12 @@
],
// 05/07 of the current year
[
44017,
44382,
'5-JUL',
],
// 05/07 of the current year
[
44017,
44382,
'5 Jul',
],
[
Expand Down
Binary file added tests/data/Reader/Ods/bug1772.ods
Binary file not shown.