From 9e7e07d0bc5399d11a061a6baef314b8706d4242 Mon Sep 17 00:00:00 2001 From: troosan Date: Thu, 9 Nov 2017 00:41:56 +0100 Subject: [PATCH] Add unit tests for Html parser --- src/PhpWord/Shared/Html.php | 41 ++++----- tests/PhpWord/Reader/MsDocTest.php | 20 +++++ tests/PhpWord/Shared/HtmlTest.php | 110 +++++++++++++++++++++++- tests/PhpWord/_includes/XmlDocument.php | 12 ++- 4 files changed, 156 insertions(+), 27 deletions(-) diff --git a/src/PhpWord/Shared/Html.php b/src/PhpWord/Shared/Html.php index f2282499..620839b4 100644 --- a/src/PhpWord/Shared/Html.php +++ b/src/PhpWord/Shared/Html.php @@ -18,6 +18,7 @@ namespace PhpOffice\PhpWord\Shared; use PhpOffice\PhpWord\Element\AbstractContainer; +use PhpOffice\PhpWord\SimpleType\Jc; /** * Common Html functions @@ -120,9 +121,10 @@ class Html 'b' => array('Property', null, null, $styles, null, 'bold', true), 'em' => array('Property', null, null, $styles, null, 'italic', true), 'i' => array('Property', null, null, $styles, null, 'italic', true), + 'u' => array('Property', null, null, $styles, null, 'underline', 'single'), 'sup' => array('Property', null, null, $styles, null, 'superScript', true), 'sub' => array('Property', null, null, $styles, null, 'subScript', true), - 'span' => array('Property', null, null, $styles, null, 'span', $node), + 'span' => array('Property', null, null, $styles, null, 'span', $node), 'table' => array('Table', $node, $element, $styles, null, 'addTable', true), 'tr' => array('Table', $node, $element, $styles, null, 'addRow', true), 'td' => array('Table', $node, $element, $styles, null, 'addCell', true), @@ -236,8 +238,6 @@ class Html // if (method_exists($element, 'addText')) { $element->addText($node->nodeValue, $styles['font'], $styles['paragraph']); // } - - return null; } /** @@ -259,8 +259,6 @@ class Html } } } - - return null; } /** @@ -310,8 +308,6 @@ class Html $data['listdepth'] = 0; } $styles['list']['listType'] = $argument1; - - return null; } /** @@ -337,8 +333,6 @@ class Html } $element->addListItem($text, $data['listdepth'], $styles['font'], $styles['list'], $styles['paragraph']); } - - return null; } /** @@ -366,7 +360,20 @@ class Html } break; case 'text-align': - $styles['alignment'] = $cValue; // todo: any mapping? + switch ($cValue) { + case 'left': + $styles['alignment'] = Jc::START; + break; + case 'right': + $styles['alignment'] = Jc::END; + break; + case 'center': + $styles['alignment'] = Jc::CENTER; + break; + case 'justify': + $styles['alignment'] = Jc::BOTH; + break; + } break; case 'color': $styles['color'] = trim($cValue, '#'); @@ -388,20 +395,6 @@ class Html } $styles['italic'] = $tValue; break; - case 'font-weight': - $tValue = false; - if (preg_match('#bold#', $cValue)) { - $tValue = true; // also match bolder - } - $styles['bold'] = $tValue; - break; - case 'font-style': - $tValue = false; - if (preg_match('#(?:italic|oblique)#', $cValue)) { - $tValue = true; - } - $styles['italic'] = $tValue; - break; } } diff --git a/tests/PhpWord/Reader/MsDocTest.php b/tests/PhpWord/Reader/MsDocTest.php index ed16e64b..b57f7e44 100644 --- a/tests/PhpWord/Reader/MsDocTest.php +++ b/tests/PhpWord/Reader/MsDocTest.php @@ -56,4 +56,24 @@ class MsDocTest extends \PHPUnit_Framework_TestCase $phpWord = IOFactory::load($filename, 'MsDoc'); $this->assertInstanceOf('PhpOffice\\PhpWord\\PhpWord', $phpWord); } + + /** + * Test exception on not existing file + * @expectedException \Exception + */ + public function testFailIfFileNotReadable() + { + $filename = __DIR__ . '/../_files/documents/not_existing_reader.doc'; + IOFactory::load($filename, 'MsDoc'); + } + + /** + * Test exception on non OLE document + * @expectedException \Exception + */ + public function testFailIfFileNotOle() + { + $filename = __DIR__ . '/../_files/documents/reader.odt'; + IOFactory::load($filename, 'MsDoc'); + } } diff --git a/tests/PhpWord/Shared/HtmlTest.php b/tests/PhpWord/Shared/HtmlTest.php index 602b644d..b1a9e31c 100644 --- a/tests/PhpWord/Shared/HtmlTest.php +++ b/tests/PhpWord/Shared/HtmlTest.php @@ -18,9 +18,12 @@ namespace PhpOffice\PhpWord\Shared; use PhpOffice\PhpWord\Element\Section; +use PhpOffice\PhpWord\SimpleType\Jc; +use PhpOffice\PhpWord\TestHelperDOCX; /** * Test class for PhpOffice\PhpWord\Shared\Html + * @coversDefaultClass \PhpOffice\PhpWord\Shared\Html */ class HtmlTest extends \PHPUnit_Framework_TestCase { @@ -43,7 +46,7 @@ class HtmlTest extends \PHPUnit_Framework_TestCase // Styles $content .= '

'; + . 'text-align: center; color: #999; background-color: #000; font-weight: bold; font-style: italic;">'; foreach ($styles as $style) { $content .= "<{$style}>{$style}"; } @@ -67,4 +70,109 @@ class HtmlTest extends \PHPUnit_Framework_TestCase $content .= '–   ²³¼½¾'; Html::addHtml($section, $content); } + + /** + * Test that html already in body element can be read + * @ignore + */ + public function testParseFullHtml() + { + $section = new Section(1); + Html::addHtml($section, '

test paragraph1

test paragraph2

', true); + + $this->assertCount(2, $section->getElements()); + } + + /** + * Test underline + */ + public function testParseUnderline() + { + $html = 'test'; + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, $html); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u')); + $this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val')); + } + + /** + * Test text-decoration style + */ + public function testParseTextDecoration() + { + $html = 'test'; + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, $html); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:r/w:rPr/w:u')); + $this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p/w:r/w:rPr/w:u', 'w:val')); + } + + /** + * Test text-align style + */ + public function testParseTextAlign() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, '

test

'); + Html::addHtml($section, '

test

'); + Html::addHtml($section, '

test

'); + Html::addHtml($section, '

test

'); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc')); + $this->assertEquals('start', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('end', $doc->getElementAttribute('/w:document/w:body/w:p[2]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('center', $doc->getElementAttribute('/w:document/w:body/w:p[3]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('both', $doc->getElementAttribute('/w:document/w:body/w:p[4]/w:pPr/w:jc', 'w:val')); + } + + /** + * Test parsing paragraph and span styles + */ + public function testParseParagraphAndSpanStyle() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + Html::addHtml($section, '

test

'); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); + $this->assertTrue($doc->elementExists('/w:document/w:body/w:p/w:pPr/w:jc')); + $this->assertEquals('center', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:pPr/w:jc', 'w:val')); + $this->assertEquals('single', $doc->getElementAttribute('/w:document/w:body/w:p[1]/w:r/w:rPr/w:u', 'w:val')); + } + + /** + * Test parsing table + */ + public function testParseTable() + { + $phpWord = new \PhpOffice\PhpWord\PhpWord(); + $section = $phpWord->addSection(); + $html = ' + + + + + + + + + + + + +
abc
12
456
'; + Html::addHtml($section, $html); + + $doc = TestHelperDOCX::getDocument($phpWord, 'Word2007'); +// echo $doc->printXml(); +// $this->assertTrue($doc->elementExists('/w:document/w:body/w:tbl/w:tr/w:tc')); + } } diff --git a/tests/PhpWord/_includes/XmlDocument.php b/tests/PhpWord/_includes/XmlDocument.php index eb335278..c82c5a8e 100644 --- a/tests/PhpWord/_includes/XmlDocument.php +++ b/tests/PhpWord/_includes/XmlDocument.php @@ -170,12 +170,20 @@ class XmlDocument * @param string $file * @return string */ - public function printXml($path = '/w:document', $file = 'word/document.xml') + public function printXml($path = '/', $file = 'word/document.xml') { + $element = $this->getElement($path, $file); + if ($element instanceof \DOMDocument) { + $element->formatOutput = true; + $element->preserveWhiteSpace = false; + + return $element->saveXML(); + } + $newdoc = new \DOMDocument(); $newdoc->formatOutput = true; $newdoc->preserveWhiteSpace = false; - $node = $newdoc->importNode($this->getElement($path, $file), true); + $node = $newdoc->importNode($element, true); $newdoc->appendChild($node); return $newdoc->saveXML($node);