From c5d82647918f9d44476daff51453c307c200b467 Mon Sep 17 00:00:00 2001 From: Stefan Thoolen Date: Tue, 6 Nov 2018 10:02:21 +0100 Subject: [PATCH] Remove leading slash when required --- src/Common/XMLReader.php | 6 ++++++ tests/Common/Tests/XMLReaderTest.php | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Common/XMLReader.php b/src/Common/XMLReader.php index 601074e..abca427 100644 --- a/src/Common/XMLReader.php +++ b/src/Common/XMLReader.php @@ -61,6 +61,12 @@ public function getDomFromZip(string $zipFile, string $xmlFile) $zip = new \ZipArchive(); $zip->open($zipFile); $content = $zip->getFromName($xmlFile); + + // Files downloaded from Sharepoint are somehow different and fail on the leading slash. + if ($content === false && substr($xmlFile, 0, 1) === '/') { + $content = $zip->getFromName(substr($xmlFile, 1)); + } + $zip->close(); if ($content === false) { diff --git a/tests/Common/Tests/XMLReaderTest.php b/tests/Common/Tests/XMLReaderTest.php index 9923d56..70ea251 100644 --- a/tests/Common/Tests/XMLReaderTest.php +++ b/tests/Common/Tests/XMLReaderTest.php @@ -51,13 +51,24 @@ public function testDomFromZip(): void $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; $reader = new XMLReader(); - $reader->getDomFromZip($pathResources . 'reader.zip', 'test.xml'); + $this->assertInstanceOf(\DOMDocument::class, $reader->getDomFromZip($pathResources . 'reader.zip', 'test.xml')); $this->assertTrue($reader->elementExists('/element/child')); $this->assertFalse($reader->getDomFromZip($pathResources . 'reader.zip', 'non_existing_xml_file.xml')); } + /** + * Test reading XML from zip + */ + public function testDomFromZipWithSharepointPath(): void + { + $pathResources = PHPOFFICE_COMMON_TESTS_BASE_DIR . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR; + + $reader = new XMLReader(); + $this->assertInstanceOf(\DOMDocument::class, $reader->getDomFromZip($pathResources . 'reader.zip', '/test.xml')); + } + /** * Test that read from non existing archive throws exception */