converter_docbook_html_test.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * ezcDocumentConverterEzp3TpEzp4Tests
  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. /**
  28. * Test suite for class.
  29. *
  30. * @package Document
  31. * @subpackage Tests
  32. */
  33. class ezcDocumentConverterDocbookToHtmlTests extends ezcTestCase
  34. {
  35. protected static $testDocuments = null;
  36. public static function suite()
  37. {
  38. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  39. }
  40. public function testHtmlConverterOptionsFormatOutput()
  41. {
  42. $options = new ezcDocumentHtmlConverterOptions();
  43. $options->formatOutput = false;
  44. try
  45. {
  46. $options->formatOutput = 0;
  47. $this->fail( 'Expected ezcBaseValueException.' );
  48. }
  49. catch ( ezcBaseValueException $e )
  50. { /* Expected */ }
  51. }
  52. public function testHtmlConverterOptionsDublinCoreMetadata()
  53. {
  54. $options = new ezcDocumentHtmlConverterOptions();
  55. $options->dublinCoreMetadata = false;
  56. try
  57. {
  58. $options->dublinCoreMetadata = 0;
  59. $this->fail( 'Expected ezcBaseValueException.' );
  60. }
  61. catch ( ezcBaseValueException $e )
  62. { /* Expected */ }
  63. }
  64. public function testHtmlConverterOptionsStyleSheets()
  65. {
  66. $options = new ezcDocumentHtmlConverterOptions();
  67. $options->styleSheets = array( 'url' );
  68. $options->styleSheets = null;
  69. try
  70. {
  71. $options->styleSheets = 0;
  72. $this->fail( 'Expected ezcBaseValueException.' );
  73. }
  74. catch ( ezcBaseValueException $e )
  75. { /* Expected */ }
  76. }
  77. public function testHtmlConverterOptionsHeaderLevel()
  78. {
  79. $options = new ezcDocumentHtmlConverterOptions();
  80. $options->headerLevel = 1;
  81. $options->headerLevel = 2;
  82. try
  83. {
  84. $options->headerLevel = 7;
  85. $this->fail( 'Expected ezcBaseValueException.' );
  86. }
  87. catch ( ezcBaseValueException $e )
  88. { /* Expected */ }
  89. }
  90. public function testHtmlConverterOptionsUnknownOption()
  91. {
  92. $options = new ezcDocumentHtmlConverterOptions();
  93. try
  94. {
  95. $options->notExistingOption = 0;
  96. $this->fail( 'Expected ezcBasePropertyNotFoundException.' );
  97. }
  98. catch ( ezcBasePropertyNotFoundException $e )
  99. { /* Expected */ }
  100. }
  101. public static function getTestDocuments()
  102. {
  103. if ( self::$testDocuments === null )
  104. {
  105. // Get a list of all test files from the respektive folder
  106. $testFiles = glob( dirname( __FILE__ ) . '/files/docbook/xhtml/s_*.xml' );
  107. // Create array with the test file and the expected result file
  108. foreach ( $testFiles as $file )
  109. {
  110. self::$testDocuments[] = array(
  111. $file,
  112. substr( $file, 0, -3 ) . 'html'
  113. );
  114. }
  115. }
  116. return self::$testDocuments;
  117. return array_slice( self::$testDocuments, 0, 3 );
  118. }
  119. /**
  120. * @dataProvider getTestDocuments
  121. */
  122. public function testLoadXmlDocumentFromFile( $from, $to )
  123. {
  124. if ( !is_file( $to ) )
  125. {
  126. $this->markTestSkipped( "Comparision file '$to' not yet defined." );
  127. }
  128. $doc = new ezcDocumentDocbook();
  129. $doc->loadFile( $from );
  130. $converter = new ezcDocumentDocbookToHtmlConverter();
  131. $converter->options->formatOutput = true;
  132. $created = $converter->convert( $doc );
  133. $this->assertTrue(
  134. $created instanceof ezcDocumentXhtml
  135. );
  136. // Store test file, to have something to compare on failure
  137. $tempDir = $this->createTempDir( 'docbook_html_custom_' ) . '/';
  138. file_put_contents( $tempDir . basename( $to ), $xml = $created->save() );
  139. $this->assertTrue(
  140. ( $errors = $created->validateString( $xml ) ) === true,
  141. ( is_array( $errors ) ? implode( PHP_EOL, $errors ) : 'Expected true' )
  142. );
  143. $this->assertEquals(
  144. file_get_contents( $to ),
  145. $xml
  146. );
  147. // Remove tempdir, when nothing failed.
  148. $this->removeTempDir();
  149. }
  150. public function testDublinCoreMetadata()
  151. {
  152. $from = dirname( __FILE__ ) . '/files/docbook/xhtml/s_021_field_list.xml';
  153. $to = dirname( __FILE__ ) . '/files/docbook/xhtml/s_021_field_list_dc.html';
  154. $doc = new ezcDocumentDocbook();
  155. $doc->loadFile( $from );
  156. $converter = new ezcDocumentDocbookToHtmlConverter();
  157. $converter->options->formatOutput = true;
  158. $converter->options->dublinCoreMetadata = true;
  159. $created = $converter->convert( $doc );
  160. $this->assertTrue(
  161. $created instanceof ezcDocumentXhtml
  162. );
  163. // Store test file, to have something to compare on failure
  164. $tempDir = $this->createTempDir( 'docbook_html_custom_' ) . '/';
  165. file_put_contents( $tempDir . basename( $to ), $xml = $created->save() );
  166. $this->assertTrue(
  167. ( $errors = $created->validateString( $xml ) ) === true,
  168. ( is_array( $errors ) ? implode( PHP_EOL, $errors ) : 'Expected true' )
  169. );
  170. $this->assertEquals(
  171. file_get_contents( $to ),
  172. $xml
  173. );
  174. // Remove tempdir, when nothing failed.
  175. $this->removeTempDir();
  176. }
  177. public function testWithStylesheets()
  178. {
  179. $from = dirname( __FILE__ ) . '/files/docbook/xhtml/s_021_field_list.xml';
  180. $to = dirname( __FILE__ ) . '/files/docbook/xhtml/s_021_field_list_stylesheets.html';
  181. $doc = new ezcDocumentDocbook();
  182. $doc->loadFile( $from );
  183. $converter = new ezcDocumentDocbookToHtmlConverter();
  184. $converter->options->formatOutput = true;
  185. $converter->options->dublinCoreMetadata = true;
  186. $converter->options->styleSheets = array(
  187. 'foo.css',
  188. 'http://example.org/bar.css',
  189. );
  190. $created = $converter->convert( $doc );
  191. $this->assertTrue(
  192. $created instanceof ezcDocumentXhtml
  193. );
  194. // Store test file, to have something to compare on failure
  195. $tempDir = $this->createTempDir( 'docbook_html_custom_' ) . '/';
  196. file_put_contents( $tempDir . basename( $to ), $xml = $created->save() );
  197. $this->assertEquals(
  198. file_get_contents( $to ),
  199. $xml
  200. );
  201. // Remove tempdir, when nothing failed.
  202. $this->removeTempDir();
  203. }
  204. }
  205. ?>