text_processor_test.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * ezcDocumentOdtTextProcessorTest.
  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 ezcDocumentOdtTextProcessorTest extends ezcTestCase
  34. {
  35. protected $sourceRoot;
  36. protected $targetRoot;
  37. protected $proc;
  38. public static function suite()
  39. {
  40. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  41. }
  42. public function setUp()
  43. {
  44. $sourceDoc = new DOMDocument();
  45. $sourceDoc->registerNodeClass(
  46. 'DOMElement',
  47. 'ezcDocumentLocateableDomElement'
  48. );
  49. $this->sourceRoot = $sourceDoc->appendChild(
  50. $sourceDoc->createElement(
  51. 'docbook'
  52. )
  53. );
  54. $targetDoc = new DOMDocument();
  55. $this->targetRoot = $targetDoc->appendChild(
  56. $targetDoc->createElementNS(
  57. ezcDocumentOdt::NS_ODT_TEXT,
  58. 'text'
  59. )
  60. );
  61. $this->proc = new ezcDocumentOdtTextProcessor();
  62. }
  63. public function testNoLiteral()
  64. {
  65. $in = $this->sourceRoot->appendChild(
  66. new DOMText( "Some text with multiple\t\t\twhitespaces in\n\n \t it." )
  67. );
  68. $res = $this->proc->processText( $in, $this->targetRoot );
  69. $this->assertTrue(
  70. is_array( $res )
  71. );
  72. $this->assertEquals(
  73. 1,
  74. count( $res )
  75. );
  76. $this->assertTrue(
  77. ( $res = $res[0] ) instanceof DOMNode
  78. );
  79. $this->assertEquals(
  80. XML_TEXT_NODE,
  81. $res->nodeType
  82. );
  83. $this->assertEquals(
  84. $in->wholeText,
  85. $res->wholeText
  86. );
  87. }
  88. public function testLiteralNoReplacement()
  89. {
  90. $ll = $this->sourceRoot->appendChild(
  91. $this->sourceRoot->ownerDocument->createElement(
  92. 'literallayout'
  93. )
  94. );
  95. $in = $ll->appendChild(
  96. new DOMText( "Some text without multiple whitespaces in it." )
  97. );
  98. $res = $this->proc->processText( $in, $this->targetRoot );
  99. $this->assertTrue(
  100. is_array( $res )
  101. );
  102. $this->assertEquals(
  103. 1,
  104. count( $res )
  105. );
  106. $this->assertTrue(
  107. ( $res[0] instanceof DOMNode )
  108. );
  109. $this->assertEquals(
  110. XML_TEXT_NODE,
  111. $res[0]->nodeType
  112. );
  113. $this->assertEquals(
  114. $in->wholeText,
  115. $res[0]->wholeText
  116. );
  117. }
  118. public function testLiteralReplacement()
  119. {
  120. $ll = $this->sourceRoot->appendChild(
  121. $this->sourceRoot->ownerDocument->createElement(
  122. 'literallayout'
  123. )
  124. );
  125. $in = $ll->appendChild(
  126. new DOMText( "Some text with multiple\t\t\twhitespaces in\n\n \t it." )
  127. );
  128. $res = $this->proc->processText( $in, $this->targetRoot );
  129. $this->assertTrue(
  130. is_array( $res )
  131. );
  132. // 0 => "Some text "
  133. // 1 => <text:s c="2"/>
  134. // 2 => "with multiple"
  135. // 3 => <text:tab/>
  136. // 4 => <text:tab/>
  137. // 5 => <text:tab/>
  138. // 6 => "whitespaces in"
  139. // 7 => <text:line-break/>
  140. // 8 => <text:line-break/>
  141. // 9 => " "
  142. // 10 => <text:tab/>
  143. // 11 => " "
  144. // 12 => <text:s c="1"/>
  145. // 13 => "it."
  146. $this->assertEquals(
  147. 14,
  148. count( $res )
  149. );
  150. $this->assertTextNode( $res[0], "Some text " );
  151. $this->assertSpaceNode( $res[1], 2 );
  152. $this->assertTextNode( $res[2], "with multiple" );
  153. $this->assertTabNode( $res[3] );
  154. $this->assertTabNode( $res[4] );
  155. $this->assertTabNode( $res[5] );
  156. $this->assertTextNode( $res[6], "whitespaces in" );
  157. $this->assertBreakNode( $res[7] );
  158. $this->assertBreakNode( $res[8] );
  159. $this->assertTextNode( $res[9], " " );
  160. $this->assertTabNode( $res[10] );
  161. $this->assertTextNode( $res[11], " " );
  162. $this->assertSpaceNode( $res[12], 1 );
  163. $this->assertTextNode( $res[13], "it." );
  164. }
  165. protected function dumpDom( DOMNode $node, $indent = '' )
  166. {
  167. echo $indent;
  168. switch ( $node->nodeType )
  169. {
  170. case XML_ELEMENT_NODE:
  171. echo "-- <{$node->tagName}>";
  172. break;
  173. case XML_TEXT_NODE:
  174. echo '-- "' . $node->nodeValue . '"';
  175. break;
  176. }
  177. echo "\n";
  178. if ( $node->childNodes !== null )
  179. {
  180. foreach ( $node->childNodes as $child )
  181. {
  182. $this->dumpDom( $child );
  183. }
  184. }
  185. }
  186. protected function assertTextNode( DOMNode $node, $text )
  187. {
  188. $this->assertEquals(
  189. XML_TEXT_NODE,
  190. $node->nodeType
  191. );
  192. $this->assertEquals(
  193. $text,
  194. $node->wholeText
  195. );
  196. }
  197. protected function assertSpaceNode( $node, $count )
  198. {
  199. $this->assertEquals(
  200. XML_ELEMENT_NODE,
  201. $node->nodeType
  202. );
  203. $this->assertEquals(
  204. 's',
  205. $node->localName
  206. );
  207. $this->assertEquals(
  208. (string) $count,
  209. $node->getAttributeNS(
  210. ezcDocumentOdt::NS_ODT_TEXT,
  211. 'c'
  212. )
  213. );
  214. }
  215. protected function assertTabNode( $node )
  216. {
  217. $this->assertEquals(
  218. XML_ELEMENT_NODE,
  219. $node->nodeType
  220. );
  221. $this->assertEquals(
  222. 'tab',
  223. $node->localName
  224. );
  225. // Not allowed, must be repeated!
  226. $this->assertFalse(
  227. $node->hasAttributeNS(
  228. ezcDocumentOdt::NS_ODT_TEXT,
  229. 'c'
  230. )
  231. );
  232. }
  233. protected function assertBreakNode( $node )
  234. {
  235. $this->assertEquals(
  236. XML_ELEMENT_NODE,
  237. $node->nodeType
  238. );
  239. $this->assertEquals(
  240. 'line-break',
  241. $node->localName
  242. );
  243. // Not allowed, must be repeated!
  244. $this->assertFalse(
  245. $node->hasAttributeNS(
  246. ezcDocumentOdt::NS_ODT_TEXT,
  247. 'c'
  248. )
  249. );
  250. }
  251. }
  252. ?>