From 8dd9371e6aa484f5db227f510a8ad79312236f4d Mon Sep 17 00:00:00 2001 From: Cedric Pons Date: Wed, 21 Oct 2020 13:39:35 +0200 Subject: [PATCH] Limit of setImageValue --- src/PhpWord/TemplateProcessor.php | 3 + tests/PhpWord/TemplateProcessorTest.php | 78 +++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/src/PhpWord/TemplateProcessor.php b/src/PhpWord/TemplateProcessor.php index 7efc0f1ac8..092cf1babd 100644 --- a/src/PhpWord/TemplateProcessor.php +++ b/src/PhpWord/TemplateProcessor.php @@ -585,6 +585,9 @@ public function setImageValue($search, $replace, $limit = self::MAXIMUM_REPLACEM return ($partVar == $searchString) || preg_match('/^' . preg_quote($searchString) . ':/', $partVar); }); + // Exclude multiple same variable name + $varsToReplace = array_unique($varsToReplace); + foreach ($varsToReplace as $varNameWithArgs) { $varInlineArgs = $this->getImageArgs($varNameWithArgs); $preparedImageAttrs = $this->prepareImageAttrs($replaceImage, $varInlineArgs); diff --git a/tests/PhpWord/TemplateProcessorTest.php b/tests/PhpWord/TemplateProcessorTest.php index 4caca77aeb..584084a7cd 100644 --- a/tests/PhpWord/TemplateProcessorTest.php +++ b/tests/PhpWord/TemplateProcessorTest.php @@ -844,4 +844,82 @@ public function testShouldMakeFieldsUpdateOnOpen() $templateProcessor->setUpdateFields(false); $this->assertContains('', $templateProcessor->getSettingsPart()); } + + /** + * @covers ::setImageValue + * @test + */ + public function testSetImageValueLimit() + { + $templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/setImageValue-limit.docx'); + $imagePath = __DIR__ . '/_files/images/earth.jpg'; + + $variablesReplace = array( + 'picture' => array('path' => $imagePath, 'width' => 500, 'height' => 500), + ); + $templateProcessor->setImageValue(array_keys($variablesReplace), $variablesReplace, 2); + + $docName = 'setImageValue-limit-test-result.docx'; + $templateProcessor->saveAs($docName); + + $this->assertFileExists($docName, "Generated file '{$docName}' not found!"); + + $expectedDocumentZip = new \ZipArchive(); + $expectedDocumentZip->open($docName); + $expectedContentTypesXml = $expectedDocumentZip->getFromName('[Content_Types].xml'); + $expectedDocumentRelationsXml = $expectedDocumentZip->getFromName('word/_rels/document.xml.rels'); + $expectedMainPartXml = $expectedDocumentZip->getFromName('word/document.xml'); + $expectedImage = $expectedDocumentZip->getFromName('word/media/image_rId14_document.jpeg'); + if (false === $expectedDocumentZip->close()) { + throw new \Exception("Could not close zip file \"{$docName}\"."); + } + + $this->assertNotEmpty($expectedImage, 'Embed image doesn\'t found.'); + $this->assertContains('/word/media/image_rId14_document.jpeg', $expectedContentTypesXml, '[Content_Types].xml missed "/word/media/image5_document.jpeg"'); + + $this->assertContains('${picture}', $expectedMainPartXml, 'word/document.xml has replace second item.'); + + $this->assertSame(1, substr_count($expectedMainPartXml, '${picture}'), 'word/document.xml only first item has been replaced.'); + $this->assertContains('media/image_rId14_document.jpeg', $expectedDocumentRelationsXml, 'word/_rels/document.xml.rels missed "media/image5_document.jpeg"'); + + unlink($docName); + } + + /** + * @covers ::setImageValue + * @test + */ + public function testSetImageValueNoLimit() + { + $templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/setImageValue-limit.docx'); + $imagePath = __DIR__ . '/_files/images/earth.jpg'; + + $variablesReplace = array( + 'picture' => array('path' => $imagePath, 'width' => 500, 'height' => 500), + ); + $templateProcessor->setImageValue(array_keys($variablesReplace), $variablesReplace); + + $docName = 'setImageValue-limit-test-result.docx'; + $templateProcessor->saveAs($docName); + + $this->assertFileExists($docName, "Generated file '{$docName}' not found!"); + + $expectedDocumentZip = new \ZipArchive(); + $expectedDocumentZip->open($docName); + $expectedContentTypesXml = $expectedDocumentZip->getFromName('[Content_Types].xml'); + $expectedDocumentRelationsXml = $expectedDocumentZip->getFromName('word/_rels/document.xml.rels'); + $expectedMainPartXml = $expectedDocumentZip->getFromName('word/document.xml'); + $expectedImage = $expectedDocumentZip->getFromName('word/media/image_rId14_document.jpeg'); + if (false === $expectedDocumentZip->close()) { + throw new \Exception("Could not close zip file \"{$docName}\"."); + } + + $this->assertNotEmpty($expectedImage, 'Embed image doesn\'t found.'); + $this->assertContains('/word/media/image_rId14_document.jpeg', $expectedContentTypesXml, '[Content_Types].xml missed "/word/media/image5_document.jpeg"'); + + $this->assertNotContains('${picture}', $expectedMainPartXml, 'word/document.xml has replace second item.'); + $this->assertContains('media/image_rId14_document.jpeg', $expectedDocumentRelationsXml, 'word/_rels/document.xml.rels missed "media/image5_document.jpeg"'); + + unlink($docName); + } }