Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template Processor Persist File After Destruct #2545

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/changes/1.x/1.2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@


### BC Breaks
- Removed dependency `laminas/laminas-escaper`
- Removed dependency `laminas/laminas-escaper`
- *Unintended Break* TemplateProcessor Does Not Persist File After Destruct. [#2539](https://github.com/PHPOffice/PHPWord/issues/2539) To be fixed by [#2545](https://github.com/PHPOffice/PHPWord/pull/2545
2 changes: 1 addition & 1 deletion docs/changes/2.x/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
### Bug fixes

- MsDoc Reader : Correct Font Size Calculation by [@oleibman](https://github.com/oleibman) fixing [#2526](https://github.com/PHPOffice/PHPWord/issues/2526) in [#2531](https://github.com/PHPOffice/PHPWord/pull/2531)

- TemplateProcessor Persist File After Destruct [@oleibman](https://github.com/oleibman) fixing [#2539](https://github.com/PHPOffice/PHPWord/issues/2539) in [#2545](https://github.com/PHPOffice/PHPWord/pull/2545)
- bug: TemplateProcessor fix multiline values [@gimler](https://github.com/gimler) fixing [#268](https://github.com/PHPOffice/PHPWord/issues/268), [#2323](https://github.com/PHPOffice/PHPWord/issues/2323) and [#2486](https://github.com/PHPOffice/PHPWord/issues/2486) in [#2522](https://github.com/PHPOffice/PHPWord/pull/2522)

### Miscellaneous
Expand Down
17 changes: 5 additions & 12 deletions src/PhpWord/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,6 @@ public function __destruct()
// Nothing to do here.
}
}
// Temporary file
if ($this->tempDocumentFilename && file_exists($this->tempDocumentFilename)) {
unlink($this->tempDocumentFilename);
}
}

public function __wakeup(): void
{
$this->tempDocumentFilename = '';
$this->zipClass = null;

throw new Exception('unserialize not permitted for this class');
}

/**
Expand Down Expand Up @@ -1506,4 +1494,9 @@ public function setMacroChars(string $macroOpeningChars, string $macroClosingCha
self::$macroOpeningChars = $macroOpeningChars;
self::$macroClosingChars = $macroClosingChars;
}

public function getTempDocumentFilename(): string
{
return $this->tempDocumentFilename;
}
}
73 changes: 41 additions & 32 deletions tests/PhpWordTests/TemplateProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
use Exception;
use PhpOffice\PhpWord\Element\Text;
use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Exception\Exception as WordException;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\Settings;
Expand All @@ -38,14 +37,36 @@
*/
final class TemplateProcessorTest extends \PHPUnit\Framework\TestCase
{
/** @var ?TemplateProcessor */
private $templateProcessor;

private function getTemplateProcessor(string $filename): TemplateProcessor
{
$this->templateProcessor = new TemplateProcessor($filename);

return $this->templateProcessor;
}

protected function tearDown(): void
{
if ($this->templateProcessor !== null) {
$filename = $this->templateProcessor->getTempDocumentFilename();
$this->templateProcessor = null;
if (file_exists($filename)) {
@unlink($filename);
}
}
}

/**
* Construct test.
*
* @covers ::__construct
* @covers ::__destruct
*/
public function testTheConstruct(): void
{
$object = new TemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
$object = $this->getTemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
self::assertInstanceOf('PhpOffice\\PhpWord\\TemplateProcessor', $object);
self::assertEquals([], $object->getVariables());
}
Expand Down Expand Up @@ -106,7 +127,7 @@ public function xtestTemplateCanBeSavedInTemporaryLocation(string $templateFqfn,
public function testXslStyleSheetCanBeApplied(): void
{
$templateFqfn = __DIR__ . '/_files/templates/with_table_macros.docx';
$templateProcessor = new TemplateProcessor($templateFqfn);
$templateProcessor = $this->getTemplateProcessor($templateFqfn);

$actualDocumentFqfn = $this->xtestTemplateCanBeSavedInTemporaryLocation($templateFqfn, $templateProcessor);
$expectedDocumentFqfn = __DIR__ . '/_files/documents/without_table_macros.docx';
Expand Down Expand Up @@ -150,7 +171,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParameterValue
$this->expectExceptionMessage('Could not set values for the given XSL style sheet parameters.');
}

$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/blank.docx');

$xslDomDocument = new DOMDocument();
$xslDomDocument->load(__DIR__ . '/_files/xsl/passthrough.xsl');
Expand All @@ -171,7 +192,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplat
{
$this->expectException(\PhpOffice\PhpWord\Exception\Exception::class);
$this->expectExceptionMessage('Could not load the given XML document.');
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/corrupted_main_document_part.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/corrupted_main_document_part.docx');

$xslDomDocument = new DOMDocument();
$xslDomDocument->load(__DIR__ . '/_files/xsl/passthrough.xsl');
Expand All @@ -190,7 +211,7 @@ public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplat
*/
public function testDeleteRow(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/delete-row.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/delete-row.docx');

self::assertEquals(
['deleteMe', 'deleteMeToo'],
Expand All @@ -216,7 +237,7 @@ public function testDeleteRow(): void
*/
public function testCloneRow(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx');

self::assertEquals(
['tableHeader', 'userId', 'userName', 'userLocation'],
Expand All @@ -240,7 +261,7 @@ public function testCloneRow(): void
*/
public function testCloneRowWithCustomMacro(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx');

$templateProcessor->setMacroOpeningChars('{#');
$templateProcessor->setMacroClosingChars('#}');
Expand Down Expand Up @@ -397,7 +418,7 @@ public function testCloneRowAndSetValuesWithCustomMacro(): void
*/
public function testMacrosCanBeReplacedInHeaderAndFooter(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx');

self::assertEquals(['documentContent', 'headerValue:100:100', 'footerValue'], $templateProcessor->getVariables());

Expand All @@ -418,7 +439,7 @@ public function testMacrosCanBeReplacedInHeaderAndFooter(): void
*/
public function testCustomMacrosCanBeReplacedInHeaderAndFooter(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/header-footer-with-custom-macro.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/header-footer-with-custom-macro.docx');
$templateProcessor->setMacroOpeningChars('{{');
$templateProcessor->setMacroClosingChars('}}');

Expand All @@ -440,7 +461,7 @@ public function testCustomMacrosCanBeReplacedInHeaderAndFooter(): void
*/
public function testSetValue(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx');
Settings::setOutputEscapingEnabled(true);
$helloworld = "hello\nworld";
$templateProcessor->setValue('userName', $helloworld);
Expand All @@ -455,7 +476,7 @@ public function testSetValue(): void
*/
public function testSetValueWithCustomMacro(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/clone-merge-with-custom-macro.docx');
$templateProcessor->setMacroChars('{#', '#}');
Settings::setOutputEscapingEnabled(true);
$helloworld = "hello\nworld";
Expand Down Expand Up @@ -786,7 +807,7 @@ public function testSetCheckboxWithCustomMacro(): void
*/
public function testSetImageValue(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx');
$imagePath = __DIR__ . '/_files/images/earth.jpg';

$variablesReplace = [
Expand Down Expand Up @@ -866,7 +887,7 @@ public function testSetImageValue(): void
*/
public function testCloneDeleteBlock(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-delete-block.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/clone-delete-block.docx');

self::assertEquals(
['DELETEME', '/DELETEME', 'CLONEME', 'blockVariable', '/CLONEME'],
Expand Down Expand Up @@ -906,7 +927,7 @@ public function testGetVariableCountCountsHowManyTimesEachPlaceholderIsPresent()
$templatePath = 'test.docx';
$objWriter->save($templatePath);

$templateProcessor = new TemplateProcessor($templatePath);
$templateProcessor = $this->getTemplateProcessor($templatePath);
$variableCount = $templateProcessor->getVariableCount();
unlink($templatePath);

Expand Down Expand Up @@ -943,7 +964,7 @@ public function testGetVariableCountCountsHowManyTimesEachPlaceholderIsPresentWi
$templatePath = 'test.docx';
$objWriter->save($templatePath);

$templateProcessor = new TemplateProcessor($templatePath);
$templateProcessor = $this->getTemplateProcessor($templatePath);
$templateProcessor->setMacroChars('{{', '}}');
$variableCount = $templateProcessor->getVariableCount();
unlink($templatePath);
Expand Down Expand Up @@ -981,7 +1002,7 @@ public function testCloneBlockCanCloneABlockTwice(): void
$objWriter->save($templatePath);

// replace placeholders and save the file
$templateProcessor = new TemplateProcessor($templatePath);
$templateProcessor = $this->getTemplateProcessor($templatePath);
$templateProcessor->setValue('title', 'Some title');
$templateProcessor->cloneBlock('subreport', 2);
$templateProcessor->setValue('subreport.id', '123', 1);
Expand Down Expand Up @@ -1034,7 +1055,7 @@ public function testCloneBlockCanCloneABlockTwiceWithCustomMacro(): void
$objWriter->save($templatePath);

// replace placeholders and save the file
$templateProcessor = new TemplateProcessor($templatePath);
$templateProcessor = $this->getTemplateProcessor($templatePath);
$templateProcessor->setMacroChars('{{', '}}');
$templateProcessor->setValue('title', 'Some title');
$templateProcessor->cloneBlock('subreport', 2);
Expand Down Expand Up @@ -1323,7 +1344,7 @@ public function testFixBrokenMacrosWithCustomMacro(): void
*/
public function testMainPartNameDetection(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/document22-xml.docx');

$variables = ['test'];

Expand All @@ -1335,7 +1356,7 @@ public function testMainPartNameDetection(): void
*/
public function testMainPartNameDetectionWithCustomMacro(): void
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/document22-with-custom-macro-xml.docx');
$templateProcessor = $this->getTemplateProcessor(__DIR__ . '/_files/templates/document22-with-custom-macro-xml.docx');
$templateProcessor->setMacroOpeningChars('{#');
$templateProcessor->setMacroClosingChars('#}');
$variables = ['test'];
Expand Down Expand Up @@ -1595,18 +1616,6 @@ public function testShouldMakeFieldsUpdateOnOpen(): void
self::assertStringContainsString('<w:updateFields w:val="false"/>', $templateProcessor->getSettingsPart());
}

/**
* Should not allow unserialize to avoid malware.
*/
public function testUnserialize(): void
{
$this->expectException(WordException::class);
$this->expectExceptionMessage('unserialize not permitted');
$object = new TemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
$serialized = serialize($object);
$object2 = unserialize($serialized);
}

public function testShouldMakeFieldsUpdateOnOpenWithCustomMacro(): void
{
$settingsPart = '<w:settings xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
Expand Down
Loading