match_location_id_test.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. /**
  3. * ezcDocumentPdfStyleInferenceTests
  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 ezcDocumentPcssMatchLocationIdTests extends ezcTestCase
  34. {
  35. protected $document;
  36. protected $xpath;
  37. public static function suite()
  38. {
  39. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  40. }
  41. public function setUp()
  42. {
  43. $this->document = new DOMDocument();
  44. $this->document->registerNodeClass( 'DOMElement', 'ezcDocumentLocateableDomElement' );
  45. $this->document->load( dirname( __FILE__ ) . '/../files/docbook/pdf/location_ids.xml' );
  46. $this->xpath = new DOMXPath( $this->document );
  47. $this->xpath->registerNamespace( 'doc', 'http://docbook.org/ns/docbook' );
  48. }
  49. public function testMatchCommonRootNode()
  50. {
  51. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  52. $directive = new ezcDocumentPcssLayoutDirective(
  53. array( 'article' ),
  54. array()
  55. );
  56. $this->assertEquals(
  57. true,
  58. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  59. "Directive $regexp was expected to match elements location id: \"$id\"."
  60. );
  61. }
  62. public function testMatchExplicitRootNode()
  63. {
  64. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  65. $directive = new ezcDocumentPcssLayoutDirective(
  66. array( '> article' ),
  67. array()
  68. );
  69. $this->assertEquals(
  70. true,
  71. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  72. "Directive $regexp was expected to match elements location id: \"$id\"."
  73. );
  74. }
  75. public function testNoMatchExplicitRootNode()
  76. {
  77. $element = $this->xpath->query( '//doc:section' )->item( 0 );
  78. $directive = new ezcDocumentPcssLayoutDirective(
  79. array( '> section' ),
  80. array()
  81. );
  82. $this->assertEquals(
  83. false,
  84. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  85. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  86. );
  87. }
  88. public function testNotMatchChildWithParentAssertion()
  89. {
  90. $element = $this->xpath->query( '//doc:section' )->item( 0 );
  91. $directive = new ezcDocumentPcssLayoutDirective(
  92. array( 'article' ),
  93. array()
  94. );
  95. $this->assertEquals(
  96. false,
  97. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  98. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  99. );
  100. }
  101. public function testNoMatchRequiredId()
  102. {
  103. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  104. $directive = new ezcDocumentPcssLayoutDirective(
  105. array( 'article', '#some_id' ),
  106. array()
  107. );
  108. $this->assertEquals(
  109. false,
  110. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  111. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  112. );
  113. }
  114. public function testNoMatchRequiredClass()
  115. {
  116. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  117. $directive = new ezcDocumentPcssLayoutDirective(
  118. array( 'article', '.class' ),
  119. array()
  120. );
  121. $this->assertEquals(
  122. false,
  123. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  124. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  125. );
  126. }
  127. public function testNoMatchRequiredClassAndId()
  128. {
  129. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  130. $directive = new ezcDocumentPcssLayoutDirective(
  131. array( 'article', '.class', '#some_id' ),
  132. array()
  133. );
  134. $this->assertEquals(
  135. false,
  136. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  137. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  138. );
  139. }
  140. public function testMatchNodeWithId()
  141. {
  142. $element = $this->xpath->query( '//doc:section' )->item( 0 );
  143. $directive = new ezcDocumentPcssLayoutDirective(
  144. array( 'section', '#paragraph_with_inline_markup' ),
  145. array()
  146. );
  147. $this->assertEquals(
  148. true,
  149. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  150. "Directive $regexp was expected to match elements location id: \"$id\"."
  151. );
  152. }
  153. public function testMatchAnyDescendant()
  154. {
  155. $element = $this->xpath->query( '//doc:section' )->item( 0 );
  156. $directive = new ezcDocumentPcssLayoutDirective(
  157. array( 'article', 'section' ),
  158. array()
  159. );
  160. $this->assertEquals(
  161. true,
  162. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  163. "Directive $regexp was expected to match elements location id: \"$id\"."
  164. );
  165. }
  166. public function testMatchDirectDescendant()
  167. {
  168. $element = $this->xpath->query( '//doc:section' )->item( 0 );
  169. $directive = new ezcDocumentPcssLayoutDirective(
  170. array( 'article', 'section' ),
  171. array()
  172. );
  173. $this->assertEquals(
  174. true,
  175. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  176. "Directive $regexp was expected to match elements location id: \"$id\"."
  177. );
  178. }
  179. public function testMatchAnyDescendentIgnoreId()
  180. {
  181. $element = $this->xpath->query( '//doc:sectioninfo' )->item( 0 );
  182. $directive = new ezcDocumentPcssLayoutDirective(
  183. array( 'article', 'sectioninfo' ),
  184. array()
  185. );
  186. $this->assertEquals(
  187. true,
  188. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  189. "Directive $regexp was expected to match elements location id: \"$id\"."
  190. );
  191. }
  192. public function testNotMatchDirectDescendent()
  193. {
  194. $element = $this->xpath->query( '//doc:sectioninfo' )->item( 0 );
  195. $directive = new ezcDocumentPcssLayoutDirective(
  196. array( 'article', '> sectioninfo' ),
  197. array()
  198. );
  199. $this->assertEquals(
  200. false,
  201. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  202. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  203. );
  204. }
  205. public function testNotMatchPartialId()
  206. {
  207. $element = $this->xpath->query( '//doc:sectioninfo' )->item( 0 );
  208. $directive = new ezcDocumentPcssLayoutDirective(
  209. array( 'section', '#paragraph' ),
  210. array()
  211. );
  212. $this->assertEquals(
  213. false,
  214. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  215. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  216. );
  217. }
  218. public function testMatchByClassName()
  219. {
  220. $element = $this->xpath->query( '//doc:para' )->item( 1 );
  221. $directive = new ezcDocumentPcssLayoutDirective(
  222. array( 'para', '.note_warning' ),
  223. array()
  224. );
  225. $this->assertEquals(
  226. true,
  227. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  228. "Directive $regexp was expected to match elements location id: \"$id\"."
  229. );
  230. }
  231. public function testMatchByPartialClassName()
  232. {
  233. $element = $this->xpath->query( '//doc:para' )->item( 1 );
  234. $directive = new ezcDocumentPcssLayoutDirective(
  235. array( 'para', '.note' ),
  236. array()
  237. );
  238. $this->assertEquals(
  239. true,
  240. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  241. "Directive $regexp was expected to match elements location id: \"$id\"."
  242. );
  243. }
  244. public function testMatchByPartialClassName2()
  245. {
  246. $element = $this->xpath->query( '//doc:para' )->item( 1 );
  247. $directive = new ezcDocumentPcssLayoutDirective(
  248. array( 'para', '.warning' ),
  249. array()
  250. );
  251. $this->assertEquals(
  252. true,
  253. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  254. "Directive $regexp was expected to match elements location id: \"$id\"."
  255. );
  256. }
  257. public function testNotMatchByPartialClassName()
  258. {
  259. $element = $this->xpath->query( '//doc:para' )->item( 1 );
  260. $directive = new ezcDocumentPcssLayoutDirective(
  261. array( 'para', '.not' ),
  262. array()
  263. );
  264. $this->assertEquals(
  265. false,
  266. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  267. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  268. );
  269. }
  270. public function testMatchOnlyByClassName()
  271. {
  272. $element = $this->xpath->query( '//doc:para' )->item( 1 );
  273. $directive = new ezcDocumentPcssLayoutDirective(
  274. array( '.note' ),
  275. array()
  276. );
  277. $this->assertEquals(
  278. true,
  279. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  280. "Directive $regexp was expected to match elements location id: \"$id\"."
  281. );
  282. }
  283. public function testNotMatchOnlyByClassName()
  284. {
  285. $element = $this->xpath->query( '//doc:para' )->item( 0 );
  286. $directive = new ezcDocumentPcssLayoutDirective(
  287. array( '.note' ),
  288. array()
  289. );
  290. $this->assertEquals(
  291. false,
  292. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  293. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  294. );
  295. }
  296. public function testMatchOnlyById()
  297. {
  298. $element = $this->xpath->query( '//doc:section' )->item( 0 );
  299. $directive = new ezcDocumentPcssLayoutDirective(
  300. array( '#paragraph_with_inline_markup' ),
  301. array()
  302. );
  303. $this->assertEquals(
  304. true,
  305. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  306. "Directive $regexp was expected to match elements location id: \"$id\"."
  307. );
  308. }
  309. public function testNotMatchOnlyById()
  310. {
  311. $element = $this->xpath->query( '//doc:article' )->item( 0 );
  312. $directive = new ezcDocumentPcssLayoutDirective(
  313. array( '#paragraph_with_inline_markup' ),
  314. array()
  315. );
  316. $this->assertEquals(
  317. false,
  318. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  319. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  320. );
  321. }
  322. public function testNotMatchChildOnlyById()
  323. {
  324. $element = $this->xpath->query( '//doc:para' )->item( 0 );
  325. $directive = new ezcDocumentPcssLayoutDirective(
  326. array( '#paragraph_with_inline_markup' ),
  327. array()
  328. );
  329. $this->assertEquals(
  330. false,
  331. (bool) preg_match( $regexp = $directive->getRegularExpression(), $id = $element->getLocationId() ),
  332. "Directive $regexp was expected to NOT match elements location id: \"$id\"."
  333. );
  334. }
  335. }
  336. ?>