Skip to content

Commit

Permalink
Remove leading slash when required
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Thoolen authored and Progi1984 committed Dec 11, 2023
1 parent 8303744 commit c5d8264
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Common/XMLReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion tests/Common/Tests/XMLReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit c5d8264

Please sign in to comment.