From cb7d8bfce8d30b26e75f89d470cfb94d6de037ca Mon Sep 17 00:00:00 2001 From: Progi1984 Date: Wed, 7 Jul 2021 10:02:40 +0200 Subject: [PATCH] #634 : Added ODFValidator --- .github/workflows/odfvalidator.yml | 26 ++++ composer.json | 26 ++++ samples/Sample_01_Simple.php | 2 +- samples/Sample_02_Serialized.php | 2 +- samples/Sample_03_Image.php | 6 +- samples/Sample_03_Video.php | 5 +- samples/Sample_08_Group.php | 2 +- samples/Sample_09_SlideNote.php | 2 +- samples/Sample_10_Transition.php | 2 +- samples/Sample_13_MarkAsFinal.php | 2 +- samples/Sample_15_Background.php | 2 +- samples/Sample_19_SlideMaster.php | 2 +- samples/Sample_Header.php | 14 +-- samples/results/.gitkeep | 0 src/PhpPresentation/DocumentProperties.php | 118 ++++++++++-------- src/PhpPresentation/Writer/ODPresentation.php | 34 ++--- .../Writer/ODPresentation/Meta.php | 2 + .../Tests/DocumentPropertiesTest.php | 3 +- .../Tests/Writer/ODPresentation/MetaTest.php | 91 ++++++++++++++ .../Tests/Writer/ODPresentationTest.php | 6 + .../_includes/PhpPresentationTestCase.php | 45 ++++--- 21 files changed, 283 insertions(+), 109 deletions(-) create mode 100644 .github/workflows/odfvalidator.yml create mode 100644 samples/results/.gitkeep create mode 100644 tests/PhpPresentation/Tests/Writer/ODPresentation/MetaTest.php diff --git a/.github/workflows/odfvalidator.yml b/.github/workflows/odfvalidator.yml new file mode 100644 index 0000000000..a39fd9bec0 --- /dev/null +++ b/.github/workflows/odfvalidator.yml @@ -0,0 +1,26 @@ +name: ODFValidator +on: [push, pull_request] +jobs: + php-cs-fixer: + name: ODFValidator + runs-on: ubuntu-latest + steps: + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '7.4' + extensions: mbstring, intl, gd, xml, dom, json, fileinfo, curl, zip, iconv + - uses: actions/checkout@v2 + + - name: Composer Install + run: composer install --ansi --prefer-dist --no-interaction --no-progress + + - name: Generate samples files + run: composer run samples + + - name: Download ODFValidator + run: wget https://repo1.maven.org/maven2/org/odftoolkit/odfvalidator/1.0.0-BETA1/odfvalidator-1.0.0-BETA1-jar-with-dependencies.jar + + # https://odftoolkit.org/conformance/ODFValidator.html#what-is-checked + - name: Validate documents (ODF 1.2) + run: java -jar odfvalidator-1.0.0-BETA1-jar-with-dependencies.jar -1.2 -r samples/results/ \ No newline at end of file diff --git a/composer.json b/composer.json index 2f1e0f3a84..b0ca476f4b 100644 --- a/composer.json +++ b/composer.json @@ -37,5 +37,31 @@ "psr-4": { "PhpOffice\\PhpPresentation\\": "src/PhpPresentation/" } + }, + "scripts": { + "samples": [ + "php samples/Sample_01_Complex.php", + "php samples/Sample_01_Simple.php", + "php samples/Sample_03_Image.php", + "php samples/Sample_03_Video.php", + "php samples/Sample_04_Table.php", + "php samples/Sample_05_Chart_Line.php", + "php samples/Sample_05_Chart.php", + "php samples/Sample_05_Chart_with_PhpSpreadsheet.php", + "php samples/Sample_06_Fill.php", + "php samples/Sample_07_Border.php", + "php samples/Sample_08_Group.php", + "php samples/Sample_09_SlideNote.php", + "php samples/Sample_10_Transition.php", + "php samples/Sample_11_Shape.php", + "php samples/Sample_13_MarkAsFinal.php", + "php samples/Sample_14_Zoom.php", + "php samples/Sample_15_Background.php", + "php samples/Sample_16_Thumbnail.php", + "php samples/Sample_17_Comment.php", + "php samples/Sample_18_Animation.php", + "php samples/Sample_19_SlideMaster.php", + "php samples/Sample_20_ExternalSlide.php" + ] } } diff --git a/samples/Sample_01_Simple.php b/samples/Sample_01_Simple.php index e7ebd0b9a5..1cf2d764c0 100644 --- a/samples/Sample_01_Simple.php +++ b/samples/Sample_01_Simple.php @@ -29,7 +29,7 @@ $shape = $currentSlide->createDrawingShape(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('./resources/phppowerpoint_logo.gif') + ->setPath(__DIR__ . '/resources/phppowerpoint_logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); diff --git a/samples/Sample_02_Serialized.php b/samples/Sample_02_Serialized.php index b2101c440e..61806e57c2 100644 --- a/samples/Sample_02_Serialized.php +++ b/samples/Sample_02_Serialized.php @@ -30,7 +30,7 @@ $shape = $currentSlide->createDrawingShape(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('./resources/phppowerpoint_logo.gif') + ->setPath(__DIR__ . '/resources/phppowerpoint_logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); diff --git a/samples/Sample_03_Image.php b/samples/Sample_03_Image.php index 7c6ab70c8d..f9aa30f28d 100644 --- a/samples/Sample_03_Image.php +++ b/samples/Sample_03_Image.php @@ -37,7 +37,7 @@ $shape = new Drawing\File(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('./resources/phppowerpoint_logo.gif') + ->setPath(__DIR__ . '/resources/phppowerpoint_logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(100); @@ -47,7 +47,7 @@ $shape = new Drawing\ZipFile(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('zip://./resources/Sample_12.pptx#ppt/media/phppowerpoint_logo1.gif') + ->setPath('zip://' . __DIR__ . '/resources/Sample_12.pptx#ppt/media/phppowerpoint_logo1.gif') ->setResizeProportional(false) ->setHeight(36) ->setWidth(36) @@ -74,7 +74,7 @@ $shape = new Drawing\File(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('./resources/logo_ubuntu_transparent.png') + ->setPath(__DIR__ . '/resources/logo_ubuntu_transparent.png') ->setHeight(100) ->setOffsetX(10) ->setOffsetY(250) diff --git a/samples/Sample_03_Video.php b/samples/Sample_03_Video.php index 3998f750ce..c28892c0f7 100644 --- a/samples/Sample_03_Video.php +++ b/samples/Sample_03_Video.php @@ -17,7 +17,10 @@ $shape = new Media(); $shape->setName('Video') ->setDescription('Video') - ->setPath('WIN' === strtoupper(substr(PHP_OS, 0, 3)) ? './resources/sintel_trailer-480p.wmv' : './resources/sintel_trailer-480p.ogv') + ->setPath( + __DIR__ . '/resources/sintel_trailer-480p' . + ('WIN' === strtoupper(substr(PHP_OS, 0, 3)) ? '.wmv' : '.ogv') + ) ->setResizeProportional(false) ->setHeight(90) ->setWidth(90) diff --git a/samples/Sample_08_Group.php b/samples/Sample_08_Group.php index b87b50b7b5..f22196696f 100644 --- a/samples/Sample_08_Group.php +++ b/samples/Sample_08_Group.php @@ -29,7 +29,7 @@ $shape = $currentGroup->createDrawingShape(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('./resources/phppowerpoint_logo.gif') + ->setPath(__DIR__ . '/resources/phppowerpoint_logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); diff --git a/samples/Sample_09_SlideNote.php b/samples/Sample_09_SlideNote.php index 8a597dfa08..641c778636 100644 --- a/samples/Sample_09_SlideNote.php +++ b/samples/Sample_09_SlideNote.php @@ -24,7 +24,7 @@ $shape = $currentSlide->createDrawingShape(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('./resources/phppowerpoint_logo.gif') + ->setPath(__DIR__ . '/resources/phppowerpoint_logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); diff --git a/samples/Sample_10_Transition.php b/samples/Sample_10_Transition.php index 58dcbab34b..7cb732ecb9 100644 --- a/samples/Sample_10_Transition.php +++ b/samples/Sample_10_Transition.php @@ -30,7 +30,7 @@ $shapeDrawing = $slide0->createDrawingShape(); $shapeDrawing->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('./resources/phppowerpoint_logo.gif') + ->setPath(__DIR__ . '/resources/phppowerpoint_logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); diff --git a/samples/Sample_13_MarkAsFinal.php b/samples/Sample_13_MarkAsFinal.php index e2637ac6bc..f8670eba5a 100644 --- a/samples/Sample_13_MarkAsFinal.php +++ b/samples/Sample_13_MarkAsFinal.php @@ -9,7 +9,7 @@ $objPHPPresentation = new PhpPresentation(); // Mark the document as final -$objPHPPresentation->markAsFinal(true); +$objPHPPresentation->getPresentationProperties()->markAsFinal(true); // Create slide echo date('H:i:s') . ' Create slide' . EOL; diff --git a/samples/Sample_15_Background.php b/samples/Sample_15_Background.php index 8c78d2d501..74135eeceb 100644 --- a/samples/Sample_15_Background.php +++ b/samples/Sample_15_Background.php @@ -33,7 +33,7 @@ * @link : http://publicdomainarchive.com/public-domain-images-cave-red-rocks-light-beam-cavern/ */ $oBkgImage = new Image(); -$oBkgImage->setPath('./resources/background.jpg'); +$oBkgImage->setPath(__DIR__ . '/resources/background.jpg'); $oSlide2->setBackground($oBkgImage); // Save file diff --git a/samples/Sample_19_SlideMaster.php b/samples/Sample_19_SlideMaster.php index ef1ac4b52b..d72b2488b4 100644 --- a/samples/Sample_19_SlideMaster.php +++ b/samples/Sample_19_SlideMaster.php @@ -80,7 +80,7 @@ $shape = $currentSlide->createDrawingShape(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('./resources/phppowerpoint_logo.gif') + ->setPath(__DIR__ . '/resources/phppowerpoint_logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); diff --git a/samples/Sample_Header.php b/samples/Sample_Header.php index 40356df18d..1f6e299142 100644 --- a/samples/Sample_Header.php +++ b/samples/Sample_Header.php @@ -45,11 +45,6 @@ // Set writers $writers = ['PowerPoint2007' => 'pptx', 'ODPresentation' => 'odp']; -// Return to the caller script when runs by CLI -if (CLI) { - return; -} - // Set titles and names $pageHeading = str_replace('_', ' ', SCRIPT_FILENAME); $pageTitle = IS_INDEX ? 'Welcome to ' : "{$pageHeading} - "; @@ -59,7 +54,7 @@ $oShapeDrawing = new Drawing\File(); $oShapeDrawing->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('./resources/phppowerpoint_logo.gif') + ->setPath(__DIR__ . '/resources/phppowerpoint_logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); @@ -80,6 +75,11 @@ ->setSize(60) ->setColor(new Color('FFE06B20')); +// Return to the caller script when runs by CLI +if (CLI) { + return; +} + // Populate samples $files = []; if ($handle = opendir('.')) { @@ -183,7 +183,7 @@ function createTemplatedSlide(PhpOffice\PhpPresentation\PhpPresentation $objPHPP $shape = $slide->createDrawingShape(); $shape->setName('PHPPresentation logo') ->setDescription('PHPPresentation logo') - ->setPath('./resources/phppowerpoint_logo.gif') + ->setPath(__DIR__ . '/resources/phppowerpoint_logo.gif') ->setHeight(36) ->setOffsetX(10) ->setOffsetY(10); diff --git a/samples/results/.gitkeep b/samples/results/.gitkeep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/PhpPresentation/DocumentProperties.php b/src/PhpPresentation/DocumentProperties.php index cc1e61f9da..59f258d515 100644 --- a/src/PhpPresentation/DocumentProperties.php +++ b/src/PhpPresentation/DocumentProperties.php @@ -18,9 +18,6 @@ namespace PhpOffice\PhpPresentation; -/** - * \PhpOffice\PhpPresentation\DocumentProperties. - */ class DocumentProperties { /** @@ -28,14 +25,14 @@ class DocumentProperties * * @var string */ - private $creator; + private $creator = 'Unknown Creator'; /** * LastModifiedBy. * * @var string */ - private $lastModifiedBy; + private $lastModifiedBy = 'Unknown Creator'; /** * Created. @@ -56,59 +53,54 @@ class DocumentProperties * * @var string */ - private $title; + private $title = 'Untitled Presentation'; /** * Description. * * @var string */ - private $description; + private $description = ''; /** * Subject. * * @var string */ - private $subject; + private $subject = ''; /** * Keywords. * * @var string */ - private $keywords; + private $keywords = ''; /** * Category. * * @var string */ - private $category; + private $category = ''; /** * Company. * * @var string */ - private $company; + private $company = 'Unknown Company'; /** - * Create a new \PhpOffice\PhpPresentation\DocumentProperties. + * Generator. + * + * @var string */ + private $generator = ''; + public function __construct() { - // Initialise values - $this->creator = 'Unknown Creator'; - $this->lastModifiedBy = $this->creator; $this->created = time(); $this->modified = time(); - $this->title = 'Untitled Presentation'; - $this->subject = ''; - $this->description = ''; - $this->keywords = ''; - $this->category = ''; - $this->company = 'Microsoft Corporation'; } /** @@ -116,7 +108,7 @@ public function __construct() * * @return string */ - public function getCreator() + public function getCreator(): string { return $this->creator; } @@ -126,9 +118,9 @@ public function getCreator() * * @param string $pValue * - * @return \PhpOffice\PhpPresentation\DocumentProperties + * @return self */ - public function setCreator($pValue = '') + public function setCreator(string $pValue = ''): self { $this->creator = $pValue; @@ -140,7 +132,7 @@ public function setCreator($pValue = '') * * @return string */ - public function getLastModifiedBy() + public function getLastModifiedBy(): string { return $this->lastModifiedBy; } @@ -150,9 +142,9 @@ public function getLastModifiedBy() * * @param string $pValue * - * @return \PhpOffice\PhpPresentation\DocumentProperties + * @return self */ - public function setLastModifiedBy($pValue = '') + public function setLastModifiedBy(string $pValue = ''): self { $this->lastModifiedBy = $pValue; @@ -164,7 +156,7 @@ public function setLastModifiedBy($pValue = '') * * @return int */ - public function getCreated() + public function getCreated(): int { return $this->created; } @@ -174,9 +166,9 @@ public function getCreated() * * @param int $pValue * - * @return \PhpOffice\PhpPresentation\DocumentProperties + * @return self */ - public function setCreated($pValue = null) + public function setCreated(int $pValue = null): self { if (is_null($pValue)) { $pValue = time(); @@ -191,7 +183,7 @@ public function setCreated($pValue = null) * * @return int */ - public function getModified() + public function getModified(): int { return $this->modified; } @@ -201,9 +193,9 @@ public function getModified() * * @param int $pValue * - * @return \PhpOffice\PhpPresentation\DocumentProperties + * @return self */ - public function setModified($pValue = null) + public function setModified(int $pValue = null): self { if (is_null($pValue)) { $pValue = time(); @@ -218,7 +210,7 @@ public function setModified($pValue = null) * * @return string */ - public function getTitle() + public function getTitle(): string { return $this->title; } @@ -228,9 +220,9 @@ public function getTitle() * * @param string $pValue * - * @return \PhpOffice\PhpPresentation\DocumentProperties + * @return self */ - public function setTitle($pValue = '') + public function setTitle(string $pValue = ''): self { $this->title = $pValue; @@ -242,7 +234,7 @@ public function setTitle($pValue = '') * * @return string */ - public function getDescription() + public function getDescription(): string { return $this->description; } @@ -252,9 +244,9 @@ public function getDescription() * * @param string $pValue * - * @return \PhpOffice\PhpPresentation\DocumentProperties + * @return self */ - public function setDescription($pValue = '') + public function setDescription(string $pValue = ''): self { $this->description = $pValue; @@ -266,7 +258,7 @@ public function setDescription($pValue = '') * * @return string */ - public function getSubject() + public function getSubject(): string { return $this->subject; } @@ -276,9 +268,9 @@ public function getSubject() * * @param string $pValue * - * @return \PhpOffice\PhpPresentation\DocumentProperties + * @return self */ - public function setSubject($pValue = '') + public function setSubject(string $pValue = ''): self { $this->subject = $pValue; @@ -290,7 +282,7 @@ public function setSubject($pValue = '') * * @return string */ - public function getKeywords() + public function getKeywords(): string { return $this->keywords; } @@ -300,9 +292,9 @@ public function getKeywords() * * @param string $pValue * - * @return \PhpOffice\PhpPresentation\DocumentProperties + * @return self */ - public function setKeywords($pValue = '') + public function setKeywords(string $pValue = ''): self { $this->keywords = $pValue; @@ -314,7 +306,7 @@ public function setKeywords($pValue = '') * * @return string */ - public function getCategory() + public function getCategory(): string { return $this->category; } @@ -324,9 +316,9 @@ public function getCategory() * * @param string $pValue * - * @return \PhpOffice\PhpPresentation\DocumentProperties + * @return self */ - public function setCategory($pValue = '') + public function setCategory(string $pValue = ''): self { $this->category = $pValue; @@ -338,7 +330,7 @@ public function setCategory($pValue = '') * * @return string */ - public function getCompany() + public function getCompany(): string { return $this->company; } @@ -348,12 +340,36 @@ public function getCompany() * * @param string $pValue * - * @return \PhpOffice\PhpPresentation\DocumentProperties + * @return self */ - public function setCompany($pValue = '') + public function setCompany(string $pValue = ''): self { $this->company = $pValue; return $this; } + + /** + * Get Generator. + * + * @return string + */ + public function getGenerator(): string + { + return $this->generator; + } + + /** + * Set Generator. + * + * @param string $pValue + * + * @return self + */ + public function setGenerator(string $pValue = ''): self + { + $this->generator = $pValue; + + return $this; + } } diff --git a/src/PhpPresentation/Writer/ODPresentation.php b/src/PhpPresentation/Writer/ODPresentation.php index 0c45098cbd..1a74402e35 100644 --- a/src/PhpPresentation/Writer/ODPresentation.php +++ b/src/PhpPresentation/Writer/ODPresentation.php @@ -18,7 +18,6 @@ namespace PhpOffice\PhpPresentation\Writer; -use DirectoryIterator; use PhpOffice\Common\Adapter\Zip\ZipArchiveAdapter; use PhpOffice\PhpPresentation\HashTable; use PhpOffice\PhpPresentation\PhpPresentation; @@ -98,26 +97,19 @@ public function save(string $pFilename): void $oPresentation = $this->getPhpPresentation(); $arrayChart = []; - $arrayFiles = []; - $oDir = new DirectoryIterator(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ODPresentation'); - foreach ($oDir as $oFile) { - if (!$oFile->isFile()) { - continue; - } - - $class = __NAMESPACE__ . '\\ODPresentation\\' . $oFile->getBasename('.php'); - $class = new \ReflectionClass($class); - - if ($class->isAbstract() || !$class->isSubclassOf('PhpOffice\PhpPresentation\Writer\ODPresentation\AbstractDecoratorWriter')) { - continue; - } - $arrayFiles[$oFile->getBasename('.php')] = $class; - } - - ksort($arrayFiles); - - foreach ($arrayFiles as $o) { - $oService = $o->newInstance(); + $arrayFiles = [ + __CLASS__ . '\Mimetype', + __CLASS__ . '\Content', + __CLASS__ . '\Meta', + __CLASS__ . '\MetaInfManifest', + __CLASS__ . '\ObjectsChart', + __CLASS__ . '\Pictures', + __CLASS__ . '\Styles', + __CLASS__ . '\ThumbnailsThumbnail', + ]; + + foreach ($arrayFiles as $class) { + $oService = new $class(); $oService->setZip($oZip); $oService->setPresentation($oPresentation); $oService->setDrawingHashTable($this->getDrawingHashTable()); diff --git a/src/PhpPresentation/Writer/ODPresentation/Meta.php b/src/PhpPresentation/Writer/ODPresentation/Meta.php index 2c742ce61b..22df6676aa 100644 --- a/src/PhpPresentation/Writer/ODPresentation/Meta.php +++ b/src/PhpPresentation/Writer/ODPresentation/Meta.php @@ -51,6 +51,8 @@ public function render() $objWriter->writeElement('meta:initial-creator', $this->getPresentation()->getDocumentProperties()->getCreator()); // meta:keyword $objWriter->writeElement('meta:keyword', $this->getPresentation()->getDocumentProperties()->getKeywords()); + // meta:generator + $objWriter->writeElement('meta:generator', $this->getPresentation()->getDocumentProperties()->getGenerator()); // @todo : Where these properties are written ? // $this->getPresentation()->getDocumentProperties()->getCategory() diff --git a/tests/PhpPresentation/Tests/DocumentPropertiesTest.php b/tests/PhpPresentation/Tests/DocumentPropertiesTest.php index c89d745b1d..8ffa112e4d 100644 --- a/tests/PhpPresentation/Tests/DocumentPropertiesTest.php +++ b/tests/PhpPresentation/Tests/DocumentPropertiesTest.php @@ -37,14 +37,13 @@ public function testGetSet(): void $properties = [ 'creator' => '', 'lastModifiedBy' => '', - 'created' => '', - 'modified' => '', 'title' => '', 'description' => '', 'subject' => '', 'keywords' => '', 'category' => '', 'company' => '', + 'generator' => '', ]; foreach ($properties as $key => $val) { diff --git a/tests/PhpPresentation/Tests/Writer/ODPresentation/MetaTest.php b/tests/PhpPresentation/Tests/Writer/ODPresentation/MetaTest.php new file mode 100644 index 0000000000..d6e65e64f8 --- /dev/null +++ b/tests/PhpPresentation/Tests/Writer/ODPresentation/MetaTest.php @@ -0,0 +1,91 @@ +assertZipXmlElementExists('meta.xml', $element); + $element = '/office:document-meta/office:meta/dc:creator'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, 'Unknown Creator'); + $element = '/office:document-meta/office:meta/dc:date'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, gmdate('Y-m-d\TH:i:s.000', $this->oPresentation->getDocumentProperties()->getModified())); + $element = '/office:document-meta/office:meta/dc:description'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, ''); + $element = '/office:document-meta/office:meta/dc:subject'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, ''); + $element = '/office:document-meta/office:meta/dc:title'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, 'Untitled Presentation'); + $element = '/office:document-meta/office:meta/meta:creation-date'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, gmdate('Y-m-d\TH:i:s.000', $this->oPresentation->getDocumentProperties()->getCreated())); + $element = '/office:document-meta/office:meta/meta:initial-creator'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, 'Unknown Creator'); + $element = '/office:document-meta/office:meta/meta:keyword'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, ''); + $element = '/office:document-meta/office:meta/meta:generator'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, ''); + + $this->assertIsSchemaOpenDocumentValid('1.2'); + + $this->oPresentation->getDocumentProperties() + ->setCreator('AlphaCreator') + ->setDescription('BetaDescription') + ->setSubject('GammaSubject') + ->setTitle('DeltaTitle') + ->setKeywords('EpsilonKeyword') + ->setGenerator('ZĂȘtaGenerator') + ->setLastModifiedBy('ÊtaModifier') + ; + $this->resetPresentationFile(); + + $element = '/office:document-meta/office:meta'; + $this->assertZipXmlElementExists('meta.xml', $element); + $element = '/office:document-meta/office:meta/dc:creator'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, $this->oPresentation->getDocumentProperties()->getLastModifiedBy()); + $element = '/office:document-meta/office:meta/dc:date'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, gmdate('Y-m-d\TH:i:s.000', $this->oPresentation->getDocumentProperties()->getModified())); + $element = '/office:document-meta/office:meta/dc:description'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, $this->oPresentation->getDocumentProperties()->getDescription()); + $element = '/office:document-meta/office:meta/dc:subject'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, $this->oPresentation->getDocumentProperties()->getSubject()); + $element = '/office:document-meta/office:meta/dc:title'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, $this->oPresentation->getDocumentProperties()->getTitle()); + $element = '/office:document-meta/office:meta/meta:creation-date'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, gmdate('Y-m-d\TH:i:s.000', $this->oPresentation->getDocumentProperties()->getCreated())); + $element = '/office:document-meta/office:meta/meta:initial-creator'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, $this->oPresentation->getDocumentProperties()->getCreator()); + $element = '/office:document-meta/office:meta/meta:keyword'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, $this->oPresentation->getDocumentProperties()->getKeywords()); + $element = '/office:document-meta/office:meta/meta:generator'; + $this->assertZipXmlElementExists('meta.xml', $element); + $this->assertZipXmlElementEquals('meta.xml', $element, $this->oPresentation->getDocumentProperties()->getGenerator()); + } +} diff --git a/tests/PhpPresentation/Tests/Writer/ODPresentationTest.php b/tests/PhpPresentation/Tests/Writer/ODPresentationTest.php index cd0cf0a849..1f25e675de 100644 --- a/tests/PhpPresentation/Tests/Writer/ODPresentationTest.php +++ b/tests/PhpPresentation/Tests/Writer/ODPresentationTest.php @@ -116,4 +116,10 @@ public function testFeatureThumbnail(): void $this->assertZipFileExists('META-INF/manifest.xml'); $this->assertZipXmlElementExists('META-INF/manifest.xml', $xPathManifest); } + + public function testOrderFileInZip(): void + { + $this->assertZipFileExists('mimetype'); + $this->assertZipFilePositionEquals('mimetype', 0); + } } diff --git a/tests/PhpPresentation/Tests/_includes/PhpPresentationTestCase.php b/tests/PhpPresentation/Tests/_includes/PhpPresentationTestCase.php index b06b85514d..3724e4cb80 100644 --- a/tests/PhpPresentation/Tests/_includes/PhpPresentationTestCase.php +++ b/tests/PhpPresentation/Tests/_includes/PhpPresentationTestCase.php @@ -174,10 +174,7 @@ private function getXmlNodeList(string $file, string $xpath): DOMNodeList return $this->xmlXPath->query($xpath); } - /** - * @param string $writerName - */ - protected function writePresentationFile(PhpPresentation $oPhpPresentation, $writerName): void + protected function writePresentationFile(PhpPresentation $oPhpPresentation, string $writerName): void { if (is_file($this->filePath)) { return; @@ -213,7 +210,7 @@ protected function resetPresentationFile(): void /** * @param string $filePath */ - public function assertZipFileExists($filePath): void + public function assertZipFileExists(string $filePath): void { $this->writePresentationFile($this->oPresentation, $this->writerName); self::assertTrue(is_file($this->workDirectory . $filePath)); @@ -222,17 +219,33 @@ public function assertZipFileExists($filePath): void /** * @param string $filePath */ - public function assertZipFileNotExists($filePath): void + public function assertZipFileNotExists(string $filePath): void { $this->writePresentationFile($this->oPresentation, $this->writerName); self::assertFalse(is_file($this->workDirectory . $filePath)); } + /** + * @param string $filePath + */ + public function assertZipFilePositionEquals(string $filePath, int $position): void + { + $xmlWriter = IOFactory::createWriter($this->oPresentation, $this->writerName); + $xmlWriter->save($this->filePath); + + $zip = new \ZipArchive(); + $res = $zip->open($this->filePath); + self::assertTrue($res); + $actualPosition = $zip->locateName($filePath); + self::assertIsInt($actualPosition); + self::assertSame($actualPosition, $position); + } + /** * @param string $filePath * @param string $xPath */ - public function assertZipXmlElementExists($filePath, $xPath): void + public function assertZipXmlElementExists(string $filePath, string $xPath): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath); @@ -243,7 +256,7 @@ public function assertZipXmlElementExists($filePath, $xPath): void * @param string $filePath * @param string $xPath */ - public function assertZipXmlElementNotExists($filePath, $xPath): void + public function assertZipXmlElementNotExists(string $filePath, string $xPath): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath); @@ -255,7 +268,7 @@ public function assertZipXmlElementNotExists($filePath, $xPath): void * @param string $xPath * @param mixed $value */ - public function assertZipXmlElementEquals($filePath, $xPath, $value): void + public function assertZipXmlElementEquals(string $filePath, string $xPath, $value): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath); @@ -267,7 +280,7 @@ public function assertZipXmlElementEquals($filePath, $xPath, $value): void * @param string $xPath * @param int $num */ - public function assertZipXmlElementCount($filePath, $xPath, $num): void + public function assertZipXmlElementCount(string $filePath, string $xPath, int $num): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath); @@ -280,7 +293,7 @@ public function assertZipXmlElementCount($filePath, $xPath, $num): void * @param string $attribute * @param mixed $value */ - public function assertZipXmlAttributeEquals($filePath, $xPath, $attribute, $value): void + public function assertZipXmlAttributeEquals(string $filePath, string $xPath, string $attribute, $value): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath); @@ -296,7 +309,7 @@ public function assertZipXmlAttributeEquals($filePath, $xPath, $attribute, $valu * @param string $attribute * @param mixed $value */ - public function assertZipXmlAttributeStartsWith($filePath, $xPath, $attribute, $value): void + public function assertZipXmlAttributeStartsWith(string $filePath, string $xPath, string $attribute, $value): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath); @@ -312,7 +325,7 @@ public function assertZipXmlAttributeStartsWith($filePath, $xPath, $attribute, $ * @param string $attribute * @param mixed $value */ - public function assertZipXmlAttributeEndsWith($filePath, $xPath, $attribute, $value): void + public function assertZipXmlAttributeEndsWith(string $filePath, string $xPath, string $attribute, $value): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath); @@ -328,7 +341,7 @@ public function assertZipXmlAttributeEndsWith($filePath, $xPath, $attribute, $va * @param string $attribute * @param mixed $value */ - public function assertZipXmlAttributeContains($filePath, $xPath, $attribute, $value): void + public function assertZipXmlAttributeContains(string $filePath, string $xPath, string $attribute, $value): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath); @@ -343,7 +356,7 @@ public function assertZipXmlAttributeContains($filePath, $xPath, $attribute, $va * @param string $xPath * @param string $attribute */ - public function assertZipXmlAttributeExists($filePath, $xPath, $attribute): void + public function assertZipXmlAttributeExists(string $filePath, string $xPath, string $attribute): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath); @@ -358,7 +371,7 @@ public function assertZipXmlAttributeExists($filePath, $xPath, $attribute): void * @param string $xPath * @param string $attribute */ - public function assertZipXmlAttributeNotExists($filePath, $xPath, $attribute): void + public function assertZipXmlAttributeNotExists(string $filePath, string $xPath, string $attribute): void { $this->writePresentationFile($this->oPresentation, $this->writerName); $nodeList = $this->getXmlNodeList($filePath, $xPath);