converter_docbook_odt_test.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. require_once 'odt/test_classes/styler.php';
  28. /**
  29. * Test suite for class.
  30. *
  31. * @package Document
  32. * @subpackage Tests
  33. */
  34. class ezcDocumentConverterDocbookToOdtTests extends ezcTestCase
  35. {
  36. const WRITE_RESULTS = true;
  37. protected static $testDocuments = null;
  38. protected static $css;
  39. public static function suite()
  40. {
  41. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  42. }
  43. public static function getTestDocuments()
  44. {
  45. if ( self::$testDocuments === null )
  46. {
  47. // Get a list of all test files from the respektive folder
  48. $testFiles = glob( dirname( __FILE__ ) . '/files/docbook/odt/s_*.xml' );
  49. // Create array with the test file and the expected result file
  50. foreach ( $testFiles as $file )
  51. {
  52. self::$testDocuments[] = array(
  53. $file,
  54. substr( $file, 0, -3 ) . 'fodt'
  55. );
  56. }
  57. }
  58. return self::$testDocuments;
  59. }
  60. /**
  61. * @dataProvider getTestDocuments
  62. */
  63. public function testConvertDocBookOdt( $from, $to )
  64. {
  65. if ( !is_file( $to ) )
  66. {
  67. $this->markTestSkipped( "Comparision file '$to' not yet defined." );
  68. }
  69. $doc = new ezcDocumentDocbook();
  70. $doc->loadFile( $from );
  71. $converter = new ezcDocumentDocbookToOdtConverter();
  72. $converter->options->styler->addStylesheetFile( dirname( __FILE__ ) . '/odt/test_data/test_styles.pcss' );
  73. $created = $converter->convert( $doc );
  74. $this->assertTrue(
  75. $created instanceof ezcDocumentOdt
  76. );
  77. $this->adjustMetaDate( $created );
  78. // Store test file, to have something to compare on failure
  79. $tempDir = $this->createTempDir( 'docbook_odt_custom_' ) . '/';
  80. $tempFile = $tempDir . basename( $to );
  81. file_put_contents( $tempFile, ( $xml = $created->save() ) );
  82. if ( self::WRITE_RESULTS )
  83. {
  84. copy( $tempFile, $to );
  85. }
  86. $this->assertTrue(
  87. ( $errors = $created->validateString( $xml ) ),
  88. ( is_array( $errors ) ? implode( PHP_EOL, $errors ) : 'Expected true' )
  89. );
  90. $this->assertEquals(
  91. file_get_contents( $to ),
  92. $xml
  93. );
  94. // Remove tempdir, when nothing failed.
  95. $this->removeTempDir();
  96. }
  97. private function adjustMetaDate( ezcDocumentOdt $odt )
  98. {
  99. $fakeDate = '2009-12-04T10:14:00+01:00';
  100. $creationDate = $odt->getDomDocument()->getElementsByTagnameNS(
  101. ezcDocumentOdt::NS_ODT_META,
  102. 'creation-date'
  103. )->item( 0 );
  104. $creationDate->nodeValue = $fakeDate;
  105. $date = $odt->getDomDocument()->getElementsByTagnameNS(
  106. ezcDocumentOdt::NS_DC,
  107. 'date'
  108. )->item( 0 );
  109. $date->nodeValue = $fakeDate;
  110. }
  111. /**
  112. * @dataProvider getTestDocuments
  113. */
  114. public function testStylerCalls()
  115. {
  116. $testDocs = self::getTestDocuments();
  117. if ( count( $testDocs ) < 1 )
  118. {
  119. throw new RuntimeException( 'Missing test documents.' );
  120. }
  121. $doc = new ezcDocumentDocbook();
  122. $doc->loadFile( $testDocs[0][0] );
  123. $stylerMock = new ezcDocumentOdtTestStyler();
  124. $converter = new ezcDocumentDocbookToOdtConverter(
  125. new ezcDocumentDocbookToOdtConverterOptions(
  126. array( 'styler' => $stylerMock )
  127. )
  128. );
  129. $created = $converter->convert( $doc );
  130. $this->assertTrue(
  131. $stylerMock->odtDocument instanceof DOMDocument
  132. );
  133. $this->assertEquals(
  134. 38,
  135. count( $stylerMock->seenElements )
  136. );
  137. }
  138. }
  139. ?>