Skip to content

Commit 60c21a2

Browse files
author
Roman Syroeshko
committed
https://github.com/PHPOffice/PHPWord/issues/46
Positive test.
1 parent 670765f commit 60c21a2

File tree

2 files changed

+80
-9
lines changed

2 files changed

+80
-9
lines changed

Tests/PHPWord/TemplateTest.php

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,46 @@ class PHPWord_TemplateTest extends \PHPUnit_Framework_TestCase
1414
*/
1515
final public function testXslStyleSheetCanBeApplied()
1616
{
17-
// TODO: implement after merge of the issue https://github.com/PHPOffice/PHPWord/issues/56
17+
$template = new PHPWord_Template(
18+
\join(
19+
\DIRECTORY_SEPARATOR,
20+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'with_table_macros.docx')
21+
)
22+
);
23+
24+
$xslDOMDocument = new \DOMDocument();
25+
$xslDOMDocument->load(
26+
\join(
27+
\DIRECTORY_SEPARATOR,
28+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'remove_tables_by_needle.xsl')
29+
)
30+
);
31+
32+
foreach (array('${employee.', '${scoreboard.') as $needle) {
33+
$template->applyXslStyleSheet($xslDOMDocument, array('needle' => $needle));
34+
}
35+
36+
$actualDocument = $template->save();
37+
$expectedDocument = \join(
38+
\DIRECTORY_SEPARATOR,
39+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'without_table_macros.docx')
40+
);
41+
42+
$actualZip = new ZipArchive();
43+
$actualZip->open($actualDocument);
44+
$actualXml = $zip->getFromName('word/document.xml');
45+
if ($actualZip->close() === false) {
46+
throw new \Exception('Could not close zip file "' . $actualDocument . '".');
47+
}
48+
49+
$expectedZip = new ZipArchive();
50+
$expectedZip->open($expectedDocument);
51+
$expectedXml = $zip->getFromName('word/document.xml');
52+
if ($expectedZip->close() === false) {
53+
throw new \Exception('Could not close zip file "' . $expectedDocument . '".');
54+
}
55+
56+
$this->assertXmlStringEqualsXmlString(expectedXml, actualXml);
1857
}
1958

2059
/**
@@ -26,14 +65,18 @@ final public function testXslStyleSheetCanBeApplied()
2665
final public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParameterValue()
2766
{
2867
$template = new PHPWord_Template(
29-
\join(\DIRECTORY_SEPARATOR,
30-
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blank.docx'))
68+
\join(
69+
\DIRECTORY_SEPARATOR,
70+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blank.docx')
71+
)
3172
);
3273

3374
$xslDOMDocument = new \DOMDocument();
3475
$xslDOMDocument->load(
35-
\join(\DIRECTORY_SEPARATOR,
36-
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl'))
76+
\join(
77+
\DIRECTORY_SEPARATOR,
78+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl')
79+
)
3780
);
3881

3982
/*
@@ -52,14 +95,18 @@ final public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParamete
5295
final public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplate()
5396
{
5497
$template = new PHPWord_Template(
55-
\join(\DIRECTORY_SEPARATOR,
56-
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'corrupted_main_document_part.docx'))
98+
\join(
99+
\DIRECTORY_SEPARATOR,
100+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'corrupted_main_document_part.docx')
101+
)
57102
);
58103

59104
$xslDOMDocument = new \DOMDocument();
60105
$xslDOMDocument->load(
61-
\join(\DIRECTORY_SEPARATOR,
62-
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl'))
106+
\join(
107+
\DIRECTORY_SEPARATOR,
108+
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl')
109+
)
63110
);
64111

65112
/*
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xsl:stylesheet
3+
version="1.0"
4+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
5+
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
6+
7+
<xsl:template match='@*|node()'>
8+
<xsl:copy>
9+
<xsl:apply-templates select='@*|node()'/>
10+
</xsl:copy>
11+
</xsl:template>
12+
13+
<xsl:template match="//w:tbl">
14+
<xsl:choose>
15+
<xsl:when test="w:tr/w:tc/w:p/w:r/w:t[contains(., $needle)]"/>
16+
<xsl:otherwise>
17+
<xsl:copy>
18+
<xsl:apply-templates select='@*|node()'/>
19+
</xsl:copy>
20+
</xsl:otherwise>
21+
</xsl:choose>
22+
</xsl:template>
23+
24+
</xsl:stylesheet>

0 commit comments

Comments
 (0)