Positive test.
This commit is contained in:
Roman Syroeshko 2014-03-07 17:47:23 +04:00
parent 670765f80f
commit 60c21a2fee
2 changed files with 80 additions and 9 deletions

View File

@ -14,7 +14,46 @@ class PHPWord_TemplateTest extends \PHPUnit_Framework_TestCase
*/ */
final public function testXslStyleSheetCanBeApplied() final public function testXslStyleSheetCanBeApplied()
{ {
// TODO: implement after merge of the issue https://github.com/PHPOffice/PHPWord/issues/56 $template = new PHPWord_Template(
\join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'with_table_macros.docx')
)
);
$xslDOMDocument = new \DOMDocument();
$xslDOMDocument->load(
\join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'remove_tables_by_needle.xsl')
)
);
foreach (array('${employee.', '${scoreboard.') as $needle) {
$template->applyXslStyleSheet($xslDOMDocument, array('needle' => $needle));
}
$actualDocument = $template->save();
$expectedDocument = \join(
\DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'documents', 'without_table_macros.docx')
);
$actualZip = new ZipArchive();
$actualZip->open($actualDocument);
$actualXml = $zip->getFromName('word/document.xml');
if ($actualZip->close() === false) {
throw new \Exception('Could not close zip file "' . $actualDocument . '".');
}
$expectedZip = new ZipArchive();
$expectedZip->open($expectedDocument);
$expectedXml = $zip->getFromName('word/document.xml');
if ($expectedZip->close() === false) {
throw new \Exception('Could not close zip file "' . $expectedDocument . '".');
}
$this->assertXmlStringEqualsXmlString(expectedXml, actualXml);
} }
/** /**
@ -26,14 +65,18 @@ class PHPWord_TemplateTest extends \PHPUnit_Framework_TestCase
final public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParameterValue() final public function testXslStyleSheetCanNotBeAppliedOnFailureOfSettingParameterValue()
{ {
$template = new PHPWord_Template( $template = new PHPWord_Template(
\join(\DIRECTORY_SEPARATOR, \join(
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blank.docx')) \DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'blank.docx')
)
); );
$xslDOMDocument = new \DOMDocument(); $xslDOMDocument = new \DOMDocument();
$xslDOMDocument->load( $xslDOMDocument->load(
\join(\DIRECTORY_SEPARATOR, \join(
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl')) \DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl')
)
); );
/* /*
@ -52,14 +95,18 @@ class PHPWord_TemplateTest extends \PHPUnit_Framework_TestCase
final public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplate() final public function testXslStyleSheetCanNotBeAppliedOnFailureOfLoadingXmlFromTemplate()
{ {
$template = new PHPWord_Template( $template = new PHPWord_Template(
\join(\DIRECTORY_SEPARATOR, \join(
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'corrupted_main_document_part.docx')) \DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'templates', 'corrupted_main_document_part.docx')
)
); );
$xslDOMDocument = new \DOMDocument(); $xslDOMDocument = new \DOMDocument();
$xslDOMDocument->load( $xslDOMDocument->load(
\join(\DIRECTORY_SEPARATOR, \join(
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl')) \DIRECTORY_SEPARATOR,
array(\PHPWORD_TESTS_DIR_ROOT, '_files', 'xsl', 'passthrough.xsl')
)
); );
/* /*

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<xsl:template match='@*|node()'>
<xsl:copy>
<xsl:apply-templates select='@*|node()'/>
</xsl:copy>
</xsl:template>
<xsl:template match="//w:tbl">
<xsl:choose>
<xsl:when test="w:tr/w:tc/w:p/w:r/w:t[contains(., $needle)]"/>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select='@*|node()'/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>