PageRenderTime 77ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/tests/ZendTest/Dom/QueryTest.php

https://bitbucket.org/pcelta/zf2
PHP | 359 lines | 274 code | 38 blank | 47 comment | 1 complexity | d00690c77082d64c9e6a7dbbbe1f32ee MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Dom
  9. */
  10. namespace ZendTest\Dom;
  11. use Zend\Dom\Query;
  12. use Zend\Dom\NodeList;
  13. use Zend\Dom\Exception\ExceptionInterface as DOMException;
  14. /**
  15. * Test class for Zend_Dom_Query.
  16. *
  17. * @category Zend
  18. * @package Zend_Dom
  19. * @subpackage UnitTests
  20. * @group Zend_Dom
  21. */
  22. class QueryTest extends \PHPUnit_Framework_TestCase
  23. {
  24. public $html;
  25. public $query;
  26. /**
  27. * Sets up the fixture, for example, open a network connection.
  28. * This method is called before a test is executed.
  29. *
  30. * @return void
  31. */
  32. public function setUp()
  33. {
  34. $this->query = new Query();
  35. }
  36. public function getHtml()
  37. {
  38. if (null === $this->html) {
  39. $this->html = file_get_contents(__DIR__ . '/_files/sample.xhtml');
  40. }
  41. return $this->html;
  42. }
  43. public function loadHtml()
  44. {
  45. $this->query->setDocument($this->getHtml());
  46. }
  47. public function handleError($msg, $code = 0)
  48. {
  49. $this->error = $msg;
  50. }
  51. public function testConstructorShouldNotRequireArguments()
  52. {
  53. $query = new Query();
  54. }
  55. public function testConstructorShouldAcceptDocumentString()
  56. {
  57. $html = $this->getHtml();
  58. $query = new Query($html);
  59. $this->assertSame($html, $query->getDocument());
  60. }
  61. public function testDocShouldBeNullByDefault()
  62. {
  63. $this->assertNull($this->query->getDocument());
  64. }
  65. public function testDocShouldBeNullByEmptyStringConstructor()
  66. {
  67. $emptyStr = "";
  68. $query = new Query($emptyStr);
  69. $this->assertNull($this->query->getDocument());
  70. }
  71. public function testDocShouldBeNullByEmptyStringSet()
  72. {
  73. $emptyStr = "";
  74. $this->query->setDocument($emptyStr);
  75. $this->assertNull($this->query->getDocument());
  76. }
  77. public function testDocTypeShouldBeNullByDefault()
  78. {
  79. $this->assertNull($this->query->getDocumentType());
  80. }
  81. public function testShouldAllowSettingDocument()
  82. {
  83. $this->testDocShouldBeNullByDefault();
  84. $this->loadHtml();
  85. $this->assertEquals($this->getHtml(), $this->query->getDocument());
  86. }
  87. public function testDocumentTypeShouldBeAutomaticallyDiscovered()
  88. {
  89. $this->loadHtml();
  90. $this->assertEquals(Query::DOC_XHTML, $this->query->getDocumentType());
  91. $this->query->setDocument('<?xml version="1.0"?><root></root>');
  92. $this->assertEquals(Query::DOC_XML, $this->query->getDocumentType());
  93. $this->query->setDocument('<html><body></body></html>');
  94. $this->assertEquals(Query::DOC_HTML, $this->query->getDocumentType());
  95. }
  96. public function testQueryingWithoutRegisteringDocumentShouldThrowException()
  97. {
  98. $this->setExpectedException('\Zend\Dom\Exception\RuntimeException', 'no document');
  99. $this->query->execute('.foo');
  100. }
  101. public function testQueryingInvalidDocumentShouldThrowException()
  102. {
  103. set_error_handler(array($this, 'handleError'));
  104. $this->query->setDocumentXml('some bogus string');
  105. try {
  106. $this->query->execute('.foo');
  107. restore_error_handler();
  108. $this->fail('Querying invalid document should throw exception');
  109. } catch (DOMException $e) {
  110. restore_error_handler();
  111. $this->assertContains('Error parsing', $e->getMessage());
  112. }
  113. }
  114. public function testQueryShouldReturnResultObject()
  115. {
  116. $this->loadHtml();
  117. $test = $this->query->execute('.foo');
  118. $this->assertTrue($test instanceof NodeList);
  119. }
  120. public function testResultShouldIndicateNumberOfFoundNodes()
  121. {
  122. $this->loadHtml();
  123. $result = $this->query->execute('.foo');
  124. $message = 'Xpath: ' . $result->getXpathQuery() . "\n";
  125. $this->assertEquals(3, count($result), $message);
  126. }
  127. public function testResultShouldAllowIteratingOverFoundNodes()
  128. {
  129. $this->loadHtml();
  130. $result = $this->query->execute('.foo');
  131. $this->assertEquals(3, count($result));
  132. foreach ($result as $node) {
  133. $this->assertTrue($node instanceof \DOMNode, var_export($result, 1));
  134. }
  135. }
  136. public function testQueryShouldFindNodesWithMultipleClasses()
  137. {
  138. $this->loadHtml();
  139. $result = $this->query->execute('.footerblock .last');
  140. $this->assertEquals(1, count($result), $result->getXpathQuery());
  141. }
  142. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsExactly()
  143. {
  144. $this->loadHtml();
  145. $result = $this->query->execute('div[dojoType="FilteringSelect"]');
  146. $this->assertEquals(1, count($result), $result->getXpathQuery());
  147. }
  148. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAsDiscreteWords()
  149. {
  150. $this->loadHtml();
  151. $result = $this->query->execute('li[dojoType~="bar"]');
  152. $this->assertEquals(2, count($result), $result->getXpathQuery());
  153. }
  154. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAndAttributeValue()
  155. {
  156. $this->loadHtml();
  157. $result = $this->query->execute('li[dojoType*="bar"]');
  158. $this->assertEquals(2, count($result), $result->getXpathQuery());
  159. }
  160. public function testQueryXpathShouldAllowQueryingArbitraryUsingXpath()
  161. {
  162. $this->loadHtml();
  163. $result = $this->query->queryXpath('//li[contains(@dojotype, "bar")]');
  164. $this->assertEquals(2, count($result), $result->getXpathQuery());
  165. }
  166. public function testXpathPhpFunctionsShouldBeDisableByDefault()
  167. {
  168. $this->loadHtml();
  169. try {
  170. $this->query->queryXpath('//meta[php:functionString("strtolower", @http-equiv) = "content-type"]');
  171. } catch (\Exception $e) {
  172. return ;
  173. }
  174. $this->assertFails('XPath PHPFunctions should be disable by default');
  175. }
  176. public function testXpathPhpFunctionsShouldBeEnableWithoutParameter()
  177. {
  178. $this->loadHtml();
  179. $this->query->registerXpathPhpFunctions();
  180. $result = $this->query->queryXpath('//meta[php:functionString("strtolower", @http-equiv) = "content-type"]');
  181. $this->assertEquals('content-type',
  182. strtolower($result->current()->getAttribute('http-equiv')),
  183. $result->getXpathQuery());
  184. }
  185. public function testXpathPhpFunctionsShouldBeNotCalledWhenSpecifiedFunction()
  186. {
  187. $this->loadHtml();
  188. try {
  189. $this->query->registerXpathPhpFunctions('stripos');
  190. $this->query->queryXpath('//meta[php:functionString("strtolower", @http-equiv) = "content-type"]');
  191. } catch (\Exception $e) {
  192. // $e->getMessage() - Not allowed to call handler 'strtolower()
  193. return ;
  194. }
  195. $this->assertFails('Not allowed to call handler strtolower()');
  196. }
  197. /**
  198. * @group ZF-9243
  199. */
  200. public function testLoadingDocumentWithErrorsShouldNotRaisePhpErrors()
  201. {
  202. $file = file_get_contents(__DIR__ . '/_files/bad-sample.html');
  203. $this->query->setDocument($file);
  204. $this->query->execute('p');
  205. $errors = $this->query->getDocumentErrors();
  206. $this->assertTrue(is_array($errors));
  207. $this->assertTrue(0 < count($errors));
  208. }
  209. /**
  210. * @group ZF-9765
  211. */
  212. public function testCssSelectorShouldFindNodesWhenMatchingMultipleAttributes()
  213. {
  214. $html = <<<EOF
  215. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  216. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  217. <html>
  218. <body>
  219. <form action="#" method="get">
  220. <input type="hidden" name="foo" value="1" id="foo"/>
  221. <input type="hidden" name="bar" value="0" id="bar"/>
  222. <input type="hidden" name="baz" value="1" id="baz"/>
  223. </form>
  224. </body>
  225. </html>
  226. EOF;
  227. $this->query->setDocument($html);
  228. $results = $this->query->execute('input[type="hidden"][value="1"]');
  229. $this->assertEquals(2, count($results), $results->getXpathQuery());
  230. $results = $this->query->execute('input[value="1"][type~="hidden"]');
  231. $this->assertEquals(2, count($results), $results->getXpathQuery());
  232. $results = $this->query->execute('input[type="hidden"][value="0"]');
  233. $this->assertEquals(1, count($results));
  234. }
  235. /**
  236. * @group ZF-3938
  237. */
  238. public function testAllowsSpecifyingEncodingAtConstruction()
  239. {
  240. $doc = new Query($this->getHtml(), 'iso-8859-1');
  241. $this->assertEquals('iso-8859-1', $doc->getEncoding());
  242. }
  243. /**
  244. * @group ZF-3938
  245. */
  246. public function testAllowsSpecifyingEncodingWhenSettingDocument()
  247. {
  248. $this->query->setDocument($this->getHtml(), 'iso-8859-1');
  249. $this->assertEquals('iso-8859-1', $this->query->getEncoding());
  250. }
  251. /**
  252. * @group ZF-3938
  253. */
  254. public function testAllowsSpecifyingEncodingViaSetter()
  255. {
  256. $this->query->setEncoding('iso-8859-1');
  257. $this->assertEquals('iso-8859-1', $this->query->getEncoding());
  258. }
  259. /**
  260. * @group ZF-3938
  261. */
  262. public function testSpecifyingEncodingSetsEncodingOnDomDocument()
  263. {
  264. $this->query->setDocument($this->getHtml(), 'utf-8');
  265. $test = $this->query->execute('.foo');
  266. $this->assertInstanceof('\\Zend\\Dom\\NodeList', $test);
  267. $doc = $test->getDocument();
  268. $this->assertInstanceof('\\DOMDocument', $doc);
  269. $this->assertEquals('utf-8', $doc->encoding);
  270. }
  271. /**
  272. * @group ZF-11376
  273. */
  274. public function testXhtmlDocumentWithXmlDeclaration()
  275. {
  276. $xhtmlWithXmlDecl = <<<EOB
  277. <?xml version="1.0" encoding="UTF-8" ?>
  278. <html xmlns="http://www.w3.org/1999/xhtml">
  279. <head><title /></head>
  280. <body><p>Test paragraph.</p></body>
  281. </html>
  282. EOB;
  283. $this->query->setDocument($xhtmlWithXmlDecl, 'utf-8');
  284. $this->assertEquals(1, $this->query->execute('//p')->count());
  285. }
  286. /**
  287. * @group ZF-12106
  288. */
  289. public function testXhtmlDocumentWithXmlAndDoctypeDeclaration()
  290. {
  291. $xhtmlWithXmlDecl = <<<EOB
  292. <?xml version="1.0" encoding="UTF-8"?>
  293. <!DOCTYPE html
  294. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  295. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  296. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  297. <head>
  298. <title>Virtual Library</title>
  299. </head>
  300. <body>
  301. <p>Moved to <a href="http://example.org/">example.org</a>.</p>
  302. </body>
  303. </html>
  304. EOB;
  305. $this->query->setDocument($xhtmlWithXmlDecl, 'utf-8');
  306. $this->assertEquals(1, $this->query->execute('//p')->count());
  307. }
  308. public function testLoadingXmlContainingDoctypeShouldFailToPreventXxeAndXeeAttacks()
  309. {
  310. $xml = <<<XML
  311. <?xml version="1.0"?>
  312. <!DOCTYPE results [<!ENTITY harmless "completely harmless">]>
  313. <results>
  314. <result>This result is &harmless;</result>
  315. </results>
  316. XML;
  317. $this->query->setDocumentXml($xml);
  318. $this->setExpectedException("\Zend\Dom\Exception\RuntimeException");
  319. $this->query->queryXpath('/');
  320. }
  321. }