fixed php8 deprecation warning for libxml_disable_entity_loader() (#1625)

* fixed php8 deprecation warning for libxml_disable_entity_loader()
This commit is contained in:
Roland Eigelsreiter 2020-10-08 15:02:14 +02:00 committed by GitHub
parent 12bdcaa783
commit ab4d7413b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 13 deletions

View File

@ -560,6 +560,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Ignore inlineStr type if formula element exists - @ncrypthic [#570](https://github.com/PHPOffice/PHPExcel/issues/570) - Ignore inlineStr type if formula element exists - @ncrypthic [#570](https://github.com/PHPOffice/PHPExcel/issues/570)
- Excel 2007 Reader freezes because of conditional formatting - @rentalhost [#575](https://github.com/PHPOffice/PHPExcel/issues/575) - Excel 2007 Reader freezes because of conditional formatting - @rentalhost [#575](https://github.com/PHPOffice/PHPExcel/issues/575)
- Readers will now parse files containing worksheet titles over 31 characters [#176](https://github.com/PHPOffice/PhpSpreadsheet/pull/176) - Readers will now parse files containing worksheet titles over 31 characters [#176](https://github.com/PHPOffice/PhpSpreadsheet/pull/176)
- Fixed PHP8 deprecation warning for libxml_disable_entity_loader() [#1625](https://github.com/phpoffice/phpspreadsheet/pull/1625)
### General ### General

View File

@ -63,7 +63,7 @@ class XmlScanner
private function disableEntityLoaderCheck(): void private function disableEntityLoaderCheck(): void
{ {
if (Settings::getLibXmlDisableEntityLoader()) { if (Settings::getLibXmlDisableEntityLoader() && \PHP_VERSION_ID < 80000) {
$libxmlDisableEntityLoaderValue = libxml_disable_entity_loader(true); $libxmlDisableEntityLoaderValue = libxml_disable_entity_loader(true);
if (self::$libxmlDisableEntityLoaderValue === null) { if (self::$libxmlDisableEntityLoaderValue === null) {
@ -74,7 +74,7 @@ class XmlScanner
public static function shutdown(): void public static function shutdown(): void
{ {
if (self::$libxmlDisableEntityLoaderValue !== null) { if (self::$libxmlDisableEntityLoaderValue !== null && \PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader(self::$libxmlDisableEntityLoaderValue); libxml_disable_entity_loader(self::$libxmlDisableEntityLoaderValue);
self::$libxmlDisableEntityLoaderValue = null; self::$libxmlDisableEntityLoaderValue = null;
} }

View File

@ -12,8 +12,11 @@ class XmlScannerTest extends TestCase
{ {
protected function setUp(): void protected function setUp(): void
{ {
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader(false); libxml_disable_entity_loader(false);
} }
}
/** /**
* @dataProvider providerValidXML * @dataProvider providerValidXML
@ -24,14 +27,20 @@ class XmlScannerTest extends TestCase
*/ */
public function testValidXML($filename, $expectedResult, $libxmlDisableEntityLoader): void public function testValidXML($filename, $expectedResult, $libxmlDisableEntityLoader): void
{ {
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
$oldDisableEntityLoaderState = libxml_disable_entity_loader($libxmlDisableEntityLoader); $oldDisableEntityLoaderState = libxml_disable_entity_loader($libxmlDisableEntityLoader);
}
$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml()); $reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml());
$result = $reader->scanFile($filename); $result = $reader->scanFile($filename);
self::assertEquals($expectedResult, $result); self::assertEquals($expectedResult, $result);
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader($oldDisableEntityLoaderState); libxml_disable_entity_loader($oldDisableEntityLoaderState);
} }
}
public function providerValidXML() public function providerValidXML()
{ {
@ -56,14 +65,20 @@ class XmlScannerTest extends TestCase
{ {
$this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class); $this->expectException(\PhpOffice\PhpSpreadsheet\Reader\Exception::class);
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader($libxmlDisableEntityLoader); libxml_disable_entity_loader($libxmlDisableEntityLoader);
}
$reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml()); $reader = XmlScanner::getInstance(new \PhpOffice\PhpSpreadsheet\Reader\Xml());
$expectedResult = 'FAILURE: Should throw an Exception rather than return a value'; $expectedResult = 'FAILURE: Should throw an Exception rather than return a value';
$result = $reader->scanFile($filename); $result = $reader->scanFile($filename);
self::assertEquals($expectedResult, $result); self::assertEquals($expectedResult, $result);
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
self::assertEquals($libxmlDisableEntityLoader, libxml_disable_entity_loader()); self::assertEquals($libxmlDisableEntityLoader, libxml_disable_entity_loader());
} }
}
public function providerInvalidXML() public function providerInvalidXML()
{ {

View File

@ -14,27 +14,39 @@ class SettingsTest extends TestCase
protected function setUp(): void protected function setUp(): void
{ {
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
$this->prevValue = libxml_disable_entity_loader(); $this->prevValue = libxml_disable_entity_loader();
libxml_disable_entity_loader(false); // Enable entity loader libxml_disable_entity_loader(false); // Enable entity loader
} }
}
protected function tearDown(): void protected function tearDown(): void
{ {
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
libxml_disable_entity_loader($this->prevValue); libxml_disable_entity_loader($this->prevValue);
} }
}
public function testGetXMLSettings(): void public function testGetXMLSettings(): void
{ {
$result = Settings::getLibXmlLoaderOptions(); $result = Settings::getLibXmlLoaderOptions();
self::assertTrue((bool) ((LIBXML_DTDLOAD | LIBXML_DTDATTR) & $result)); self::assertTrue((bool) ((LIBXML_DTDLOAD | LIBXML_DTDATTR) & $result));
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
self::assertFalse(libxml_disable_entity_loader()); self::assertFalse(libxml_disable_entity_loader());
} }
}
public function testSetXMLSettings(): void public function testSetXMLSettings(): void
{ {
Settings::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_DTDVALID); Settings::setLibXmlLoaderOptions(LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_DTDVALID);
$result = Settings::getLibXmlLoaderOptions(); $result = Settings::getLibXmlLoaderOptions();
self::assertTrue((bool) ((LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_DTDVALID) & $result)); self::assertTrue((bool) ((LIBXML_DTDLOAD | LIBXML_DTDATTR | LIBXML_DTDVALID) & $result));
// php 8.+ deprecated libxml_disable_entity_loader() - It's on by default
if (\PHP_VERSION_ID < 80000) {
self::assertFalse(libxml_disable_entity_loader()); self::assertFalse(libxml_disable_entity_loader());
} }
} }
}