-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
TemplateProcessorTest.php
324 lines (286 loc) · 12.2 KB
/
TemplateProcessorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
/**
* This file is part of PHPWord - A pure PHP library for reading and writing
* word processing documents.
*
* PHPWord is free software distributed under the terms of the GNU Lesser
* General Public License version 3 as published by the Free Software Foundation.
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code. For the full list of
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
*
* @link https://github.com/PHPOffice/PHPWord
* @copyright 2010-2016 PHPWord contributors
* @license http:https://www.gnu.org/licenses/lgpl.txt LGPL version 3
*/
namespace PhpOffice\PhpWord;
/**
* @covers \PhpOffice\PhpWord\TemplateProcessor
* @coversDefaultClass \PhpOffice\PhpWord\TemplateProcessor
* @runTestsInSeparateProcesses
*/
final class TemplateProcessorTest extends \PHPUnit_Framework_TestCase
{
/**
* Template can be saved in temporary location.
*
* @covers ::save
* @test
*/
final public function testTemplateCanBeSavedInTemporaryLocation()
{
$templateFqfn = __DIR__ . '/_files/templates/with_table_macros.docx';
$templateProcessor = new TemplateProcessor($templateFqfn);
$xslDomDocument = new \DOMDocument();
$xslDomDocument->load(__DIR__ . "/_files/xsl/remove_tables_by_needle.xsl");
foreach (array('${employee.', '${scoreboard.', '${reference.') as $needle) {
$templateProcessor->applyXslStyleSheet($xslDomDocument, array('needle' => $needle));
}
$documentFqfn = $templateProcessor->save();
$this->assertNotEmpty($documentFqfn, 'FQFN of the saved document is empty.');
$this->assertFileExists($documentFqfn, "The saved document \"{$documentFqfn}\" doesn't exist.");
$templateZip = new \ZipArchive();
$templateZip->open($templateFqfn);
$templateHeaderXml = $templateZip->getFromName('word/header1.xml');
$templateMainPartXml = $templateZip->getFromName('word/document.xml');
$templateFooterXml = $templateZip->getFromName('word/footer1.xml');
if (false === $templateZip->close()) {
throw new \Exception("Could not close zip file \"{$templateZip}\".");
}
$documentZip = new \ZipArchive();
$documentZip->open($documentFqfn);
$documentHeaderXml = $documentZip->getFromName('word/header1.xml');
$documentMainPartXml = $documentZip->getFromName('word/document.xml');
$documentFooterXml = $documentZip->getFromName('word/footer1.xml');
if (false === $documentZip->close()) {
throw new \Exception("Could not close zip file \"{$documentZip}\".");
}
$this->assertNotEquals($templateHeaderXml, $documentHeaderXml);
$this->assertNotEquals($templateMainPartXml, $documentMainPartXml);
$this->assertNotEquals($templateFooterXml, $documentFooterXml);
return $documentFqfn;
}
/**
* XSL stylesheet can be applied.
*
* @test
* @covers ::applyXslStyleSheet
* @depends testTemplateCanBeSavedInTemporaryLocation
*
* @param string $actualDocumentFqfn
*
* @throws \Exception
*/
final public function testXslStyleSheetCanBeApplied($actualDocumentFqfn)
{
$expectedDocumentFqfn = __DIR__ . '/_files/documents/without_table_macros.docx';
$actualDocumentZip = new \ZipArchive();
$actualDocumentZip->open($actualDocumentFqfn);
$actualHeaderXml = $actualDocumentZip->getFromName('word/header1.xml');
$actualMainPartXml = $actualDocumentZip->getFromName('word/document.xml');
$actualFooterXml = $actualDocumentZip->getFromName('word/footer1.xml');
if (false === $actualDocumentZip->close()) {
throw new \Exception("Could not close zip file \"{$actualDocumentFqfn}\".");
}
$expectedDocumentZip = new \ZipArchive();
$expectedDocumentZip->open($expectedDocumentFqfn);
$expectedHeaderXml = $expectedDocumentZip->getFromName('word/header1.xml');
$expectedMainPartXml = $expectedDocumentZip->getFromName('word/document.xml');
$expectedFooterXml = $expectedDocumentZip->getFromName('word/footer1.xml');
if (false === $expectedDocumentZip->close()) {
throw new \Exception("Could not close zip file \"{$expectedDocumentFqfn}\".");
}
$this->assertXmlStringEqualsXmlString($expectedHeaderXml, $actualHeaderXml);
$this->assertXmlStringEqualsXmlString($expectedMainPartXml, $actualMainPartXml);
$this->assertXmlStringEqualsXmlString($expectedFooterXml, $actualFooterXml);
}
/**
* XSL stylesheet cannot be applied on failure in setting parameter value.
*
* @covers ::applyXslStyleSheet
* @expectedException \PhpOffice\PhpWord\Exception\Exception
* @expectedExceptionMessage Could not set values for the given XSL style sheet parameters.
* @test
*/
final public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParameterValue()
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/blank.docx');
$xslDomDocument = new \DOMDocument();
$xslDomDocument->load(__DIR__ . '/_files/xsl/passthrough.xsl');
/*
* We have to use error control below, because \XSLTProcessor::setParameter omits warning on failure.
* This warning fails the test.
*/
@$templateProcessor->applyXslStyleSheet($xslDomDocument, array(1 => 'somevalue'));
}
/**
* XSL stylesheet can be applied on failure of loading XML from template.
*
* @covers ::applyXslStyleSheet
* @expectedException \PhpOffice\PhpWord\Exception\Exception
* @expectedExceptionMessage Could not load the given XML document.
* @test
*/
final public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplate()
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/corrupted_main_document_part.docx');
$xslDomDocument = new \DOMDocument();
$xslDomDocument->load(__DIR__ . '/_files/xsl/passthrough.xsl');
/*
* We have to use error control below, because \DOMDocument::loadXML omits warning on failure.
* This warning fails the test.
*/
@$templateProcessor->applyXslStyleSheet($xslDomDocument);
}
/**
* @covers ::setValue
* @covers ::cloneRow
* @covers ::saveAs
* @test
*/
public function testCloneRow()
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-merge.docx');
$this->assertEquals(
array('tableHeader', 'userId', 'userName', 'userLocation'),
$templateProcessor->getVariables()
);
$docName = 'clone-test-result.docx';
$templateProcessor->setValue('tableHeader', utf8_decode('ééé'));
$templateProcessor->cloneRow('userId', 1);
$templateProcessor->setValue('userId#1', 'Test');
$templateProcessor->saveAs($docName);
$docFound = file_exists($docName);
unlink($docName);
$this->assertTrue($docFound);
}
/**
* @covers ::setValue
* @covers ::saveAs
* @test
*/
public function testMacrosCanBeReplacedInHeaderAndFooter()
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/header-footer.docx');
$this->assertEquals(array('documentContent', 'headerValue', 'footerValue'), $templateProcessor->getVariables());
$macroNames = array('headerValue', 'documentContent', 'footerValue');
$macroValues = array('Header Value', 'Document text.', 'Footer Value');
$templateProcessor->setValue($macroNames, $macroValues);
$docName = 'header-footer-test-result.docx';
$templateProcessor->saveAs($docName);
$docFound = file_exists($docName);
unlink($docName);
$this->assertTrue($docFound);
}
/**
* @covers ::cloneBlock
* @covers ::deleteBlock
* @covers ::saveAs
* @test
*/
public function testCloneDeleteBlock()
{
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/clone-delete-block.docx');
$this->assertEquals(
array('DELETEME', '/DELETEME', 'CLONEME', '/CLONEME'),
$templateProcessor->getVariables()
);
$docName = 'clone-delete-block-result.docx';
$templateProcessor->cloneBlock('CLONEME', 3);
$templateProcessor->deleteBlock('DELETEME');
$templateProcessor->saveAs($docName);
$docFound = file_exists($docName);
unlink($docName);
$this->assertTrue($docFound);
}
/**
* @covers ::insertTable
* @covers ::saveAs
* @test
*/
public function testInsertTable()
{
$docName = 'table-insert.docx';
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/' . $docName);
$this->assertEquals(
array('myTable', 'otherContent'),
$templateProcessor->getVariables()
);
$templateProcessor->insertTable('myTable', array('myCol1#' => 6000, 'myCol2#' => '1in', 'myCol3#' => '72pt'));
$this->assertEquals(
array('myCol1#', 'myCol2#', 'myCol3#', 'otherContent'),
$templateProcessor->getVariables()
);
$templateProcessor->saveAs($docName);
$docFound = file_exists($docName);
$zip = new ZipArchive();
$zip->open($docName);
$mainDoc = $zip->getFromName('word/document.xml');
$zip->close();
unlink($docName);
$this->assertTrue($docFound);
$this->assertTrue(false !== strpos($mainDoc, '<w:gridCol w:w="6000"/><w:gridCol w:w="1440"/><w:gridCol w:w="1440"/>'));
$this->assertTrue(false !== strpos($mainDoc, '<w:tc><w:tcPr><w:tcW w:w="6000" w:type="dxa"/></w:tcPr><w:p><w:r><w:t>${myCol1#}</w:t></w:r></w:p></w:tc>'));
$this->assertTrue(false !== strpos($mainDoc, '<w:tc><w:tcPr><w:tcW w:w="1440" w:type="dxa"/></w:tcPr><w:p><w:r><w:t>${myCol2#}</w:t></w:r></w:p></w:tc>'));
$this->assertTrue(false !== strpos($mainDoc, '<w:tc><w:tcPr><w:tcW w:w="1440" w:type="dxa"/></w:tcPr><w:p><w:r><w:t>${myCol3#}</w:t></w:r></w:p></w:tc>'));
}
/**
* @covers ::deleteTable
* @covers ::saveAs
* @test
*/
public function testDeleteTable()
{
$docName = 'table-delete.docx';
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/' . $docName);
$this->assertEquals(
array('KEEP_THIS_TABLE', 'DELETE_THIS_TABLE'),
$templateProcessor->getVariables()
);
$templateProcessor->deleteTable('DELETE_THIS_TABLE');
$this->assertEquals(
array('KEEP_THIS_TABLE'),
$templateProcessor->getVariables()
);
$templateProcessor->saveAs($docName);
$docFound = file_exists($docName);
$zip = new ZipArchive();
$zip->open($docName);
$mainDoc = $zip->getFromName('word/document.xml');
$zip->close();
unlink($docName);
$this->assertTrue($docFound);
$numMatches = preg_match_all('/<w:tbl>/u', $mainDoc);
$this->assertTrue($numMatches === 1);
}
/**
* @covers ::deleteRow
* @covers ::saveAs
* @test
*/
public function testDeleteRow()
{
$docName = 'table-delete-row.docx';
$templateProcessor = new TemplateProcessor(__DIR__ . '/_files/templates/' . $docName);
$this->assertEquals(
array('KEEP_THE_HEADER', 'DELETE_THIS_ROW', 'RETAIN_THIS_ROW'),
$templateProcessor->getVariables()
);
$templateProcessor->deleteRow('DELETE_THIS_ROW');
$this->assertEquals(
array('KEEP_THE_HEADER', 'RETAIN_THIS_ROW'),
$templateProcessor->getVariables()
);
$templateProcessor->saveAs($docName);
$docFound = file_exists($docName);
$zip = new ZipArchive();
$zip->open($docName);
$mainDoc = $zip->getFromName('word/document.xml');
$zip->close();
unlink($docName);
$this->assertTrue($docFound);
$numMatches = preg_match_all('/<w:tr /u', $mainDoc);
$this->assertTrue($numMatches === 2);
}
}