Skip to content

Commit 8154081

Browse files
committed
Fix: enhance media file handling to avoid duplicates and update test assertions
1 parent cbfc565 commit 8154081

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

src/PhpWord/Writer/WPS/Part/Manifest.php

+17-10
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,23 @@ private function writeManifestItem(XMLWriter $xmlWriter, string $href, string $m
7070
*/
7171
private function writeMediaFiles(XMLWriter $xmlWriter): void
7272
{
73-
// Document media
74-
$media = Media::getElements('section');
75-
if (!empty($media)) {
76-
foreach ($media as $medium) {
77-
if ($medium['type'] == 'image') {
78-
$this->writeManifestItem(
79-
$xmlWriter,
80-
'Pictures/' . $medium['target'],
81-
$this->getMediaType($medium['target'])
82-
);
73+
$mediaParts = ['section', 'header', 'footer'];
74+
$writtenTargets = []; // Keep track of written targets to avoid duplicates
75+
76+
foreach ($mediaParts as $partName) {
77+
$media = Media::getElements($partName);
78+
if (!empty($media)) {
79+
foreach ($media as $medium) {
80+
$targetPath = 'Pictures/' . $medium['target'];
81+
// Only write entry if it hasn't been written yet
82+
if (!isset($writtenTargets[$targetPath]) && $medium['type'] == 'image') {
83+
$this->writeManifestItem(
84+
$xmlWriter,
85+
$targetPath,
86+
$this->getMediaType($medium['target'])
87+
);
88+
$writtenTargets[$targetPath] = true; // Mark as written
89+
}
8390
}
8491
}
8592
}

tests/PhpWordTests/Writer/WPS/Part/ContentTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ public function testWrite(): void
3232
self::assertStringContainsString('xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0"', $result);
3333
self::assertStringContainsString('xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0"', $result);
3434
self::assertStringContainsString('xmlns:xlink="http://www.w3.org/1999/xlink"', $result);
35-
self::assertStringContainsString('<office:scripts>', $result);
35+
self::assertStringContainsString('<office:scripts/>', $result);
3636
self::assertStringContainsString('<office:font-face-decls>', $result);
3737
self::assertStringContainsString('<style:font-face style:name="Arial" svg:font-family="Arial"', $result);
38-
self::assertStringContainsString('<office:automatic-styles>', $result);
38+
self::assertStringContainsString('<office:automatic-styles/>', $result);
3939
self::assertStringContainsString('<office:body>', $result);
4040
self::assertStringContainsString('<office:text>', $result);
4141
}

tests/PhpWordTests/Writer/WPSTest.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ public function testWithMedia(): void
6464
$section = $phpWord->addSection();
6565

6666
// Add an image to the document
67-
// Correct the relative path and check if realpath returns a valid path
68-
$imagePath = realpath(__DIR__ . 'tests/PhpWordTests/_files/images/earth.jpg');
67+
$imagePath = realpath('tests/PhpWordTests/_files/images/earth.jpg');
6968
self::assertIsString($imagePath, 'Test image file not found or accessible at the expected location.'); // Ensure path is valid string
7069
self::assertFileExists($imagePath, "Test image file not found at: {$imagePath}"); // Use validated path
7170
$section->addImage($imagePath); // Use validated path
@@ -91,7 +90,7 @@ public function testWithMedia(): void
9190

9291
// Verify the Pictures directory exists and contains images
9392
self::assertTrue($zip->locateName('Pictures/') !== false, "'Pictures/' directory not found in ZIP.");
94-
self::assertTrue($zip->locateName('Pictures/earth.jpg') !== false, "'earth.jpg' not found in 'Pictures/' directory.");
93+
self::assertTrue($zip->locateName('Pictures/image1.jpg') !== false, "'Pictures/image1.jpg' not found in ZIP.");
9594

9695
$zip->close();
9796
unlink($tempFile);

0 commit comments

Comments
 (0)