diff --git a/Classes/PHPWord/Exceptions/InvalidImageException.php b/Classes/PHPWord/Exceptions/InvalidImageException.php new file mode 100644 index 00000000..2d5337b4 --- /dev/null +++ b/Classes/PHPWord/Exceptions/InvalidImageException.php @@ -0,0 +1,15 @@ +_src = $src; - $this->_isWatermark = $isWatermark; - $this->_style = new PHPWord_Style_Image(); + if (!in_array(exif_imagetype($src), $supportedImageTypes)) { + throw new UnsupportedImageTypeException; + } - if (!is_null($style) && is_array($style)) { - foreach ($style as $key => $value) { - if (substr($key, 0, 1) != '_') { - $key = '_' . $key; - } - $this->_style->setStyleValue($key, $value); + $this->_src = $src; + $this->_isWatermark = $isWatermark; + $this->_style = new PHPWord_Style_Image(); + + if (!is_null($style) && is_array($style)) { + foreach ($style as $key => $value) { + if (substr($key, 0, 1) != '_') { + $key = '_' . $key; } + $this->_style->setStyleValue($key, $value); } + } - if (isset($style['wrappingStyle'])) { - $this->_style->setWrappingStyle($style['wrappingStyle']); - } + if (isset($style['wrappingStyle'])) { + $this->_style->setWrappingStyle($style['wrappingStyle']); + } - if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) { - $imgData = getimagesize($this->_src); - $this->_style->setWidth($imgData[0]); - $this->_style->setHeight($imgData[1]); - } - - return $this; - } else { - return false; + if ($this->_style->getWidth() == null && $this->_style->getHeight() == null) { + $imgData = getimagesize($this->_src); + $this->_style->setWidth($imgData[0]); + $this->_style->setHeight($imgData[1]); } } diff --git a/Classes/PHPWord/Writer/Word2007/Base.php b/Classes/PHPWord/Writer/Word2007/Base.php index 0ab9684d..4d1c2a34 100755 --- a/Classes/PHPWord/Writer/Word2007/Base.php +++ b/Classes/PHPWord/Writer/Word2007/Base.php @@ -285,6 +285,9 @@ class PHPWord_Writer_Word2007_Base extends PHPWord_Writer_Word2007_WriterPart $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false; $arrText = $textrun->getText(); + if (!is_array($arrText)) { + $arrText = array($arrText); + } $objWriter->startElement('w:p'); diff --git a/Tests/PHPWord/Section/ImageTest.php b/Tests/PHPWord/Section/ImageTest.php index 5bffbc0c..8083673a 100644 --- a/Tests/PHPWord/Section/ImageTest.php +++ b/Tests/PHPWord/Section/ImageTest.php @@ -33,6 +33,32 @@ class ImageTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('PHPWord_Style_Image', $oImage->getStyle()); } + public function testValidImageTypes() + { + new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars_noext_jpg"); + new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mars.jpg"); + new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/mario.gif"); + new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/firefox.png"); + new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/duke_nukem.bmp"); + new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/angela_merkel.tif"); + } + + /** + * @expectedException \PhpOffice\PhpWord\Exceptions\InvalidImageException + */ + public function testImageNotFound() + { + new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/thisisnotarealimage"); + } + + /** + * @expectedException \PhpOffice\PhpWord\Exceptions\UnsupportedImageTypeException + */ + public function testInvalidImageTypes() + { + new PHPWord_Section_Image(PHPWORD_TESTS_DIR_ROOT . "/_files/images/alexz-johnson.pcx"); + } + public function testStyle() { $oImage = new PHPWord_Section_Image(\join( diff --git a/Tests/PHPWord/Writer/Word2007/BaseTest.php b/Tests/PHPWord/Writer/Word2007/BaseTest.php index e477eee8..cb8fa578 100644 --- a/Tests/PHPWord/Writer/Word2007/BaseTest.php +++ b/Tests/PHPWord/Writer/Word2007/BaseTest.php @@ -20,7 +20,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase TestHelperDOCX::clear(); } - public function testWriteImagePosition() + public function testWriteImage_Position() { $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); @@ -42,7 +42,7 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertRegExp('/position:absolute;/', $style); } - public function testWriteParagraphStyleAlign() + public function testWriteParagraphStyle_Align() { $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); @@ -55,10 +55,34 @@ class BaseTest extends \PHPUnit_Framework_TestCase $this->assertEquals('right', $element->getAttribute('w:val')); } + public function testWriteCellStyle_CellGridSpan() + { + $PHPWord = new PHPWord(); + $section = $PHPWord->createSection(); + + $table = $section->addTable(); + + $table->addRow(); + $cell = $table->addCell(200); + $cell->getStyle()->setGridSpan(5); + + $table->addRow(); + $table->addCell(40); + $table->addCell(40); + $table->addCell(40); + $table->addCell(40); + $table->addCell(40); + + $doc = TestHelperDOCX::getDocument($PHPWord); + $element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan'); + + $this->assertEquals(5, $element->getAttribute('w:val')); + } + /** * Test write paragraph pagination */ - public function testWriteParagraphStylePagination() + public function testWriteParagraphStyle_Pagination() { // Create the doc $PHPWord = new PHPWord(); @@ -85,27 +109,18 @@ class BaseTest extends \PHPUnit_Framework_TestCase } } - public function testWriteCellStyleCellGridSpan() + public function testWritePreserveText() { $PHPWord = new PHPWord(); $section = $PHPWord->createSection(); + $footer = $section->createFooter(); - $table = $section->addTable(); - - $table->addRow(); - $cell = $table->addCell(200); - $cell->getStyle()->setGridSpan(5); - - $table->addRow(); - $table->addCell(40); - $table->addCell(40); - $table->addCell(40); - $table->addCell(40); - $table->addCell(40); + $footer->addPreserveText('{PAGE}'); $doc = TestHelperDOCX::getDocument($PHPWord); - $element = $doc->getElement('/w:document/w:body/w:tbl/w:tr/w:tc/w:tcPr/w:gridSpan'); + $preserve = $doc->getElement("w:p/w:r[2]/w:instrText", 'word/footer1.xml'); - $this->assertEquals(5, $element->getAttribute('w:val')); + $this->assertEquals('PAGE', $preserve->nodeValue); + $this->assertEquals('preserve', $preserve->getAttribute('xml:space')); } -} +} \ No newline at end of file diff --git a/Tests/_files/images/alexz-johnson.pcx b/Tests/_files/images/alexz-johnson.pcx new file mode 100644 index 00000000..cffae373 Binary files /dev/null and b/Tests/_files/images/alexz-johnson.pcx differ diff --git a/Tests/_files/images/angela_merkel.tif b/Tests/_files/images/angela_merkel.tif new file mode 100644 index 00000000..236dedd1 Binary files /dev/null and b/Tests/_files/images/angela_merkel.tif differ diff --git a/Tests/_files/images/mars_noext_jpg b/Tests/_files/images/mars_noext_jpg new file mode 100644 index 00000000..584d3171 Binary files /dev/null and b/Tests/_files/images/mars_noext_jpg differ diff --git a/Tests/_inc/TestHelperDOCX.php b/Tests/_inc/TestHelperDOCX.php index 48e48ae2..965835d0 100644 --- a/Tests/_inc/TestHelperDOCX.php +++ b/Tests/_inc/TestHelperDOCX.php @@ -2,6 +2,7 @@ namespace PHPWord\Tests; use PHPWord; +use DOMDocument; class TestHelperDOCX { @@ -9,7 +10,7 @@ class TestHelperDOCX /** * @param \PHPWord $PHPWord - * @return \PHPWord\Tests\XmlDocument + * @return \PHPWord\Tests\Xml_Document */ public static function getDocument(PHPWord $PHPWord) { @@ -28,7 +29,7 @@ class TestHelperDOCX $zip->close(); } - return new XmlDocument(sys_get_temp_dir() . '/PHPWord_Unit_Test/'); + return new Xml_Document(sys_get_temp_dir() . '/PHPWord_Unit_Test/'); } public static function clear() @@ -59,3 +60,65 @@ class TestHelperDOCX rmdir($dir); } } + +class Xml_Document +{ + /** @var string $path */ + private $path; + + /** @var \DOMDocument $dom */ + private $dom; + + /** @var \DOMXpath $xpath */ + private $xpath; + + /** @var string $file */ + private $file; + + /** + * @param string $path + */ + public function __construct($path) + { + $this->path = realpath($path); + } + + /** + * @param string $file + * @return \DOMDocument + */ + public function getFileDom($file = 'word/document.xml') + { + if (null !== $this->dom && $file === $this->file) { + return $this->dom; + } + + $this->xpath = null; + $this->file = $file; + + $file = $this->path . '/' . $file; + $this->dom = new DOMDocument(); + $this->dom->load($file); + return $this->dom; + } + + /** + * @param string $path + * @param string $file + * @return \DOMElement + */ + public function getElement($path, $file = 'word/document.xml') + { + if ($this->dom === null || $file !== $this->file) { + $this->getFileDom($file); + } + + if (null === $this->xpath) { + $this->xpath = new \DOMXpath($this->dom); + + } + + $elements = $this->xpath->query($path); + return $elements->item(0); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 8a73f471..7aaeb411 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "ext-xml": "*" }, "require-dev": { - "phpunit/phpunit": "3.7.28" + "phpunit/phpunit": "3.7.*" }, "recommend": { "ext-zip": "*",