document_odt_docbook_test.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * ezcDocumentRstParserTests
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Document
  23. * @version //autogen//
  24. * @subpackage Tests
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. require_once 'helper/rst_dummy_directives.php';
  28. /**
  29. * Test suite for class.
  30. *
  31. * @package Document
  32. * @subpackage Tests
  33. */
  34. class ezcDocumentOdtDocbookTests extends ezcTestCase
  35. {
  36. public static $testDocuments;
  37. protected $cwd;
  38. public static function suite()
  39. {
  40. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  41. }
  42. public function setUp()
  43. {
  44. // The pathes in the processed files are relativ to one directory level
  45. // above, so we just change the curretn working dir.
  46. $this->cwd = getcwd();
  47. chdir( dirname( $this->cwd ) );
  48. }
  49. public function tearDown()
  50. {
  51. chdir( $this->cwd );
  52. }
  53. public static function getTestDocuments()
  54. {
  55. if ( self::$testDocuments === null )
  56. {
  57. // Get a list of all test files from the respektive folder
  58. $testFiles = glob( dirname( __FILE__ ) . '/files/odt/tests/s_*.fodt' );
  59. // Create array with the test file and the expected result file
  60. foreach ( $testFiles as $file )
  61. {
  62. self::$testDocuments[] = array(
  63. $file,
  64. substr( $file, 0, -4 ) . 'xml'
  65. );
  66. }
  67. }
  68. return self::$testDocuments;
  69. }
  70. public function testBadMarkup()
  71. {
  72. $document = new ezcDocumentOdt();
  73. try
  74. {
  75. $document->loadString(
  76. file_get_contents(
  77. dirname( __FILE__ ) . '/files/odt/bad_markup/broken_xml.fodt'
  78. )
  79. );
  80. $this->fail( 'Exception not thrown on load of invalid markup.' );
  81. }
  82. catch ( ezcDocumentErroneousXmlException $e )
  83. {
  84. $this->assertEquals(
  85. 'Errors occured while parsing the XML.',
  86. $e->getMessage()
  87. );
  88. }
  89. }
  90. public function testInvalidDocBook()
  91. {
  92. $docbook = new ezcDocumentDocbook();
  93. $docbook->options->validate = false;
  94. $docbook->loadFile( dirname( __FILE__ ) . '/files/docbook/invalid.xml' );
  95. $document = new ezcDocumentOdt();
  96. $document->options->validate = true;
  97. try
  98. {
  99. $document->createFromDocbook( $docbook );
  100. $this->fail( 'Exception not thrown on conversion of invalid docbook.' );
  101. }
  102. catch ( ezcDocumentVisitException $e ) {}
  103. }
  104. public function testValidateFileSuccess()
  105. {
  106. $document = new ezcDocumentOdt();
  107. $actRes = $document->validateFile(
  108. dirname( __FILE__ ) . '/files/odt/tests/s_000_simple.fodt'
  109. );
  110. $this->assertTrue( $actRes );
  111. }
  112. public function testValidateFileFailure()
  113. {
  114. $document = new ezcDocumentOdt();
  115. $actRes = $document->validateFile(
  116. dirname( __FILE__ ) . '/files/odt/invalid/s_000_simple.fodt'
  117. );
  118. $this->assertInternalType(
  119. 'array',
  120. $actRes
  121. );
  122. $this->assertEquals(
  123. 1,
  124. count( $actRes )
  125. );
  126. }
  127. /**
  128. * @dataProvider getTestDocuments
  129. */
  130. public function testCreateFromDocbook( $to, $from )
  131. {
  132. // Tested for correctness in converter tests!
  133. $docbook = new ezcDocumentDocbook();
  134. $docbook->loadFile( $from );
  135. $document = new ezcDocumentOdt();
  136. $document->createFromDocbook( $docbook );
  137. $this->assertNotNull(
  138. $document->getDomDocument()
  139. );
  140. }
  141. /**
  142. * @dataProvider getTestDocuments
  143. */
  144. public function testCommonConversions( $from, $to )
  145. {
  146. $tempDir = $this->createTempDir( 'odt_tests_' ) . '/';
  147. $imgDir = $tempDir . 'img';
  148. mkdir( $imgDir );
  149. $options = new ezcDocumentOdtOptions();
  150. $options->imageDir = $imgDir;
  151. $document = new ezcDocumentOdt();
  152. $document->setFilters(
  153. array(
  154. new ezcDocumentOdtImageFilter( $options ),
  155. new ezcDocumentOdtElementFilter(),
  156. new ezcDocumentOdtStyleFilter(),
  157. )
  158. );
  159. $document->loadFile( $from );
  160. $docbook = $document->getAsDocbook();
  161. $xml = $docbook->save();
  162. $xml = $this->verifyAndReplaceImages( basename( $to, '.xml' ), $xml );
  163. // Store test file, to have something to compare on failure
  164. file_put_contents( $tempDir . basename( $to ), $xml );
  165. $this->assertTrue( $docbook->validateString( $xml ) );
  166. if ( !is_file( $to ) )
  167. {
  168. $this->fail( "Missing comparison file '$to'." );
  169. }
  170. $this->assertEquals(
  171. file_get_contents( $to ),
  172. $xml,
  173. 'Document not visited as expected.'
  174. );
  175. // Remove tempdir, when nothing failed.
  176. $this->removeTempDir();
  177. }
  178. /**
  179. * Verify extracted images from an FODT and replace their links for
  180. * comparison.
  181. *
  182. * @param string $testDir Name of the current test sub-dir
  183. * @param string $xml
  184. * @return string XML with image refs replaced
  185. */
  186. protected function verifyAndReplaceImages( $testDir, $xml )
  187. {
  188. $dom = new DOMDocument();
  189. $dom->loadXml( $xml );
  190. $xpath = new DOMXPath( $dom );
  191. $xpath->registerNamespace( 'doc', 'http://docbook.org/ns/docbook' );
  192. $images = $xpath->query( '//doc:imagedata' );
  193. $i = 1;
  194. foreach ( $images as $image )
  195. {
  196. $refFile = "Document/tests/files/odt/tests/$testDir/$i.png";
  197. if ( !file_exists( $refFile ) )
  198. {
  199. $this->fail( "Image reference with '$refFile' does not exist." );
  200. }
  201. $imageFile = $image->getAttribute( 'fileref' );
  202. $this->assertFileEquals(
  203. $refFile,
  204. $imageFile,
  205. "Extracted image $i did not match ref file '$refFile'."
  206. );
  207. $image->setAttribute( 'fileref', $refFile );
  208. ++$i;
  209. }
  210. return $dom->saveXml();
  211. }
  212. }
  213. ?>