document_xhtml_docbook_test.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 ezcDocumentXhtmlDocbookTests extends ezcTestCase
  35. {
  36. protected static $rstTestDocuments = null;
  37. protected static $metadataTestDocuments = null;
  38. protected static $badTestDocuments = null;
  39. protected static $tableTestDocuments = null;
  40. protected static $testDocuments = null;
  41. public static function suite()
  42. {
  43. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  44. }
  45. public function testCreateFromDocbook()
  46. {
  47. $from = dirname( __FILE__ ) . '/files/docbook/xhtml/s_001_empty.xml';
  48. $to = dirname( __FILE__ ) . '/files/docbook/xhtml/s_001_empty.html';
  49. $docbook = new ezcDocumentDocbook();
  50. $docbook->loadFile( $from );
  51. $document = new ezcDocumentXhtml();
  52. $document->createFromDocbook( $docbook );
  53. // Store test file, to have something to compare on failure
  54. $tempDir = $this->createTempDir( 'docbook_xhtml_' ) . '/';
  55. file_put_contents( $tempDir . basename( $to ), $xml = $document->save() );
  56. $this->assertEquals(
  57. file_get_contents( $to ),
  58. $xml,
  59. 'Document not visited as expected.'
  60. );
  61. // Remove tempdir, when nothing failed.
  62. $this->removeTempDir();
  63. }
  64. public static function getRstTestDocuments()
  65. {
  66. if ( self::$rstTestDocuments === null )
  67. {
  68. // Get a list of all test files from the respektive folder
  69. $testFiles = glob( dirname( __FILE__ ) . '/files/xhtml/rst_html/s_*.html' );
  70. // Create array with the test file and the expected result file
  71. foreach ( $testFiles as $file )
  72. {
  73. self::$rstTestDocuments[] = array(
  74. $file,
  75. substr( $file, 0, -4 ) . 'xml'
  76. );
  77. }
  78. }
  79. return self::$rstTestDocuments;
  80. return array_slice( self::$rstTestDocuments, 0, 10 );
  81. }
  82. public static function getMetadataTestDocuments()
  83. {
  84. if ( self::$metadataTestDocuments === null )
  85. {
  86. // Get a list of all test files from the respektive folder
  87. $testFiles = glob( dirname( __FILE__ ) . '/files/xhtml/metadata/s_*.html' );
  88. // Create array with the test file and the expected result file
  89. foreach ( $testFiles as $file )
  90. {
  91. self::$metadataTestDocuments[] = array(
  92. $file,
  93. substr( $file, 0, -4 ) . 'xml'
  94. );
  95. }
  96. }
  97. return self::$metadataTestDocuments;
  98. return array_slice( self::$metadataTestDocuments, 0, 0 );
  99. }
  100. public static function getBadTestDocuments()
  101. {
  102. if ( self::$badTestDocuments === null )
  103. {
  104. // Get a list of all test files from the respektive folder
  105. $testFiles = glob( dirname( __FILE__ ) . '/files/xhtml/bad_markup/s_*.html' );
  106. // Create array with the test file and the expected result file
  107. foreach ( $testFiles as $file )
  108. {
  109. self::$badTestDocuments[] = array(
  110. $file,
  111. substr( $file, 0, -4 ) . 'xml'
  112. );
  113. }
  114. }
  115. return self::$badTestDocuments;
  116. return array_slice( self::$badTestDocuments, 0, 0 );
  117. }
  118. public static function getTableTestDocuments()
  119. {
  120. if ( self::$tableTestDocuments === null )
  121. {
  122. // Get a list of all test files from the respektive folder
  123. $testFiles = glob( dirname( __FILE__ ) . '/files/xhtml/table/s_*.html' );
  124. // Create array with the test file and the expected result file
  125. foreach ( $testFiles as $file )
  126. {
  127. self::$tableTestDocuments[] = array(
  128. $file,
  129. substr( $file, 0, -4 ) . 'xml'
  130. );
  131. }
  132. }
  133. return self::$tableTestDocuments;
  134. return array_slice( self::$tableTestDocuments, 0, 0 );
  135. }
  136. public static function getTestDocuments()
  137. {
  138. if ( self::$testDocuments === null )
  139. {
  140. // Get a list of all test files from the respektive folder
  141. $testFiles = glob( dirname( __FILE__ ) . '/files/xhtml/tests/s_*.html' );
  142. // Create array with the test file and the expected result file
  143. foreach ( $testFiles as $file )
  144. {
  145. self::$testDocuments[] = array(
  146. $file,
  147. substr( $file, 0, -4 ) . 'xml'
  148. );
  149. }
  150. }
  151. return self::$testDocuments;
  152. return array_slice( self::$tableTestDocuments, 0, 0 );
  153. }
  154. /**
  155. * @dataProvider getRstTestDocuments
  156. */
  157. public function testParseRstHtmlFile( $from, $to )
  158. {
  159. if ( !is_file( $to ) )
  160. {
  161. $this->markTestSkipped( "Comparision file '$to' not yet defined." );
  162. }
  163. $document = new ezcDocumentXhtml();
  164. $document->loadFile( $from );
  165. $docbook = $document->getAsDocbook();
  166. $xml = $docbook->save();
  167. // Store test file, to have something to compare on failure
  168. $tempDir = $this->createTempDir( 'xhtml_rst_' ) . '/';
  169. file_put_contents( $tempDir . basename( $to ), $xml );
  170. // Validate generated docbook
  171. $this->assertTrue( $docbook->validateString( $xml ) );
  172. $this->assertEquals(
  173. file_get_contents( $to ),
  174. $xml,
  175. 'Document not visited as expected.'
  176. );
  177. // Remove tempdir, when nothing failed.
  178. $this->removeTempDir();
  179. }
  180. /**
  181. * @dataProvider getMetadataTestDocuments
  182. */
  183. public function testExtractMetadata( $from, $to )
  184. {
  185. if ( !is_file( $to ) )
  186. {
  187. $this->markTestSkipped( "Comparision file '$to' not yet defined." );
  188. }
  189. $document = new ezcDocumentXhtml();
  190. $document->loadFile( $from );
  191. $docbook = $document->getAsDocbook();
  192. $xml = $docbook->save();
  193. // Store test file, to have something to compare on failure
  194. $tempDir = $this->createTempDir( 'xhtml_metadata_' ) . '/';
  195. file_put_contents( $tempDir . basename( $to ), $xml );
  196. // Validate generated docbook
  197. $this->assertTrue( $docbook->validateString( $xml ) );
  198. $this->assertEquals(
  199. file_get_contents( $to ),
  200. $xml,
  201. 'Document not visited as expected.'
  202. );
  203. // Remove tempdir, when nothing failed.
  204. $this->removeTempDir();
  205. }
  206. /**
  207. * @dataProvider getBadTestDocuments
  208. */
  209. public function testConvertBadMarkup( $from, $to )
  210. {
  211. if ( !is_file( $to ) )
  212. {
  213. $this->markTestSkipped( "Comparision file '$to' not yet defined." );
  214. }
  215. $document = new ezcDocumentXhtml();
  216. $document->setFilters( array(
  217. new ezcDocumentXhtmlElementFilter(),
  218. new ezcDocumentXhtmlMetadataFilter(),
  219. new ezcDocumentXhtmlContentLocatorFilter(),
  220. ) );
  221. $document->loadFile( $from );
  222. $docbook = $document->getAsDocbook();
  223. $xml = $docbook->save();
  224. // Store test file, to have something to compare on failure
  225. $tempDir = $this->createTempDir( 'xhtml_bad_' ) . '/';
  226. file_put_contents( $tempDir . basename( $to ), $xml );
  227. // Do not validate the converted "bad" markup.
  228. // $this->assertTrue( $docbook->validateString( $xml ) );
  229. $this->assertEquals(
  230. file_get_contents( $to ),
  231. $xml,
  232. 'Document not visited as expected.'
  233. );
  234. // Remove tempdir, when nothing failed.
  235. $this->removeTempDir();
  236. }
  237. public function testXpathFilter()
  238. {
  239. $from = dirname( __FILE__ ) . '/files/xhtml/xpath/s_004_detect_url_in_texts.html';
  240. $to = dirname( __FILE__ ) . '/files/xhtml/xpath/s_004_detect_url_in_texts.xml';
  241. $document = new ezcDocumentXhtml();
  242. $document->setFilters( array(
  243. new ezcDocumentXhtmlXpathFilter(
  244. '//div[@class = "content"]'
  245. ),
  246. new ezcDocumentXhtmlElementFilter(),
  247. new ezcDocumentXhtmlMetadataFilter(),
  248. ) );
  249. $document->loadFile( $from );
  250. $docbook = $document->getAsDocbook();
  251. $xml = $docbook->save();
  252. // Store test file, to have something to compare on failure
  253. $tempDir = $this->createTempDir( 'xpath_filter_' ) . '/';
  254. file_put_contents( $tempDir . basename( $to ), $xml );
  255. // Do not validate the converted "bad" markup.
  256. // $this->assertTrue( $docbook->validateString( $xml ) );
  257. $this->assertEquals(
  258. file_get_contents( $to ),
  259. $xml,
  260. 'Document not visited as expected.'
  261. );
  262. // Remove tempdir, when nothing failed.
  263. $this->removeTempDir();
  264. }
  265. /**
  266. * @dataProvider getTableTestDocuments
  267. */
  268. public function testTableDetector( $from, $to )
  269. {
  270. if ( !is_file( $to ) )
  271. {
  272. $this->markTestSkipped( "Comparision file '$to' not yet defined." );
  273. }
  274. $document = new ezcDocumentXhtml();
  275. $document->setFilters( array(
  276. new ezcDocumentXhtmlElementFilter(),
  277. new ezcDocumentXhtmlMetadataFilter(),
  278. new ezcDocumentXhtmlContentLocatorFilter(),
  279. new ezcDocumentXhtmlTablesFilter(),
  280. ) );
  281. $document->loadFile( $from );
  282. $docbook = $document->getAsDocbook();
  283. $xml = $docbook->save();
  284. // Store test file, to have something to compare on failure
  285. $tempDir = $this->createTempDir( 'xhtml_table_' ) . '/';
  286. file_put_contents( $tempDir . basename( $to ), $xml );
  287. // Do not validate the converted "bad" markup.
  288. // $this->assertTrue( $docbook->validateString( $xml ) );
  289. $this->assertEquals(
  290. file_get_contents( $to ),
  291. $xml,
  292. 'Document not visited as expected.'
  293. );
  294. // Remove tempdir, when nothing failed.
  295. $this->removeTempDir();
  296. }
  297. /**
  298. * @dataProvider getTestDocuments
  299. */
  300. public function testCommonConversions( $from, $to )
  301. {
  302. if ( !is_file( $to ) )
  303. {
  304. $this->markTestSkipped( "Comparision file '$to' not yet defined." );
  305. }
  306. $document = new ezcDocumentXhtml();
  307. $document->setFilters( array(
  308. new ezcDocumentXhtmlElementFilter(),
  309. new ezcDocumentXhtmlMetadataFilter(),
  310. new ezcDocumentXhtmlTablesFilter(),
  311. ) );
  312. $document->loadFile( $from );
  313. $docbook = $document->getAsDocbook();
  314. $xml = $docbook->save();
  315. // Store test file, to have something to compare on failure
  316. $tempDir = $this->createTempDir( 'xhtml_tests_' ) . '/';
  317. file_put_contents( $tempDir . basename( $to ), $xml );
  318. $this->assertTrue( $docbook->validateString( $xml ) );
  319. $this->assertEquals(
  320. file_get_contents( $to ),
  321. $xml,
  322. 'Document not visited as expected.'
  323. );
  324. // Remove tempdir, when nothing failed.
  325. $this->removeTempDir();
  326. }
  327. }
  328. ?>