|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * This file is part of PHPWord - A pure PHP library for reading and writing |
| 4 | + * word processing documents. |
| 5 | + * |
| 6 | + * PHPWord is free software distributed under the terms of the GNU Lesser |
| 7 | + * General Public License version 3 as published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * For the full copyright and license information, please read the LICENSE |
| 10 | + * file that was distributed with this source code. For the full list of |
| 11 | + * contributors, visit https://github.com/PHPOffice/PHPWord/contributors. |
| 12 | + * |
| 13 | + * @see https://github.com/PHPOffice/PHPWord |
| 14 | + * |
| 15 | + * @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3 |
| 16 | + */ |
| 17 | + |
| 18 | +namespace PhpOffice\PhpWordTests; |
| 19 | + |
| 20 | +use PhpOffice\PhpWord\Element\Section; |
| 21 | +use PhpOffice\PhpWord\Element\TextRun; |
| 22 | +use PhpOffice\PhpWord\Shared\Html; |
| 23 | +use PhpOffice\PhpWord\TemplateProcessor; |
| 24 | + |
| 25 | +/** |
| 26 | + * @covers \PhpOffice\PhpWord\TemplateProcessor |
| 27 | + * |
| 28 | + * @coversDefaultClass \PhpOffice\PhpWord\TemplateProcessor |
| 29 | + * |
| 30 | + * @runTestsInSeparateProcesses |
| 31 | + */ |
| 32 | +final class TemplateProcessorSectionTest extends \PHPUnit\Framework\TestCase |
| 33 | +{ |
| 34 | + /** @var ?TemplateProcessor */ |
| 35 | + private $templateProcessor; |
| 36 | + |
| 37 | + private function getTemplateProcessor(string $filename): TemplateProcessor |
| 38 | + { |
| 39 | + $this->templateProcessor = new TemplateProcessor($filename); |
| 40 | + |
| 41 | + return $this->templateProcessor; |
| 42 | + } |
| 43 | + |
| 44 | + protected function tearDown(): void |
| 45 | + { |
| 46 | + if ($this->templateProcessor !== null) { |
| 47 | + $filename = $this->templateProcessor->getTempDocumentFilename(); |
| 48 | + $this->templateProcessor = null; |
| 49 | + if (file_exists($filename)) { |
| 50 | + @unlink($filename); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public function testSetComplexSection(): void |
| 56 | + { |
| 57 | + $templateProcessor = $this->getTemplateProcessor('C:/git/sectiontemplate/tests/PhpWordTests/_files/templates/document22-xml.docx'); |
| 58 | + $html = ' |
| 59 | + <p> Bug Report:</p> |
| 60 | + <p><span style="background-color: #ff0000;">BugTracker X</span> is ${facing1} an issue.</p> |
| 61 | + <p><span style="background-color: #00ff00;">BugTracker X</span> is ${facing2} an issue.</p> |
| 62 | + '; |
| 63 | + $section = new Section(0); |
| 64 | + Html::addHtml($section, $html, false, false); |
| 65 | + $templateProcessor->setComplexBlock('test', $section); |
| 66 | + $facing1 = new TextRun(); |
| 67 | + $facing1->addText('facing', ['bold' => true]); |
| 68 | + $facing2 = new TextRun(); |
| 69 | + $facing2->addText('facing', ['italic' => true]); |
| 70 | + |
| 71 | + $templateProcessor->setComplexBlock('test', $section); |
| 72 | + $templateProcessor->setComplexValue('facing1', $facing1); |
| 73 | + $templateProcessor->setComplexValue('facing2', $facing2); |
| 74 | + |
| 75 | + $docName = $templateProcessor->save(); |
| 76 | + $docFound = file_exists($docName); |
| 77 | + self::assertTrue($docFound); |
| 78 | + $contents = file_get_contents("zip://$docName#word/document2.xml"); |
| 79 | + unlink($docName); |
| 80 | + self::assertNotFalse($contents); |
| 81 | + $contents = preg_replace('/>\s+</', '><', $contents) ?? ''; |
| 82 | + self::assertStringContainsString('<w:t>Test</w:t>', $contents); |
| 83 | + self::assertStringContainsString('<w:r><w:rPr><w:b w:val="1"/><w:bCs w:val="1"/></w:rPr><w:t xml:space="preserve">facing</w:t></w:r>', $contents, 'bold string found'); |
| 84 | + self::assertStringContainsString('<w:r><w:rPr><w:i w:val="1"/><w:iCs w:val="1"/></w:rPr><w:t xml:space="preserve">facing</w:t></w:r>', $contents, 'italic string found'); |
| 85 | + self::assertStringNotContainsString('$', $contents, 'no leftover macros'); |
| 86 | + self::assertStringNotContainsString('facing1', $contents, 'no leftover replaced string1'); |
| 87 | + self::assertStringNotContainsString('facing2', $contents, 'no leftover replaced string2'); |
| 88 | + } |
| 89 | +} |
0 commit comments