QueryTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Dojo
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. // Call Zend_Dom_QueryTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Dom_QueryTest::main");
  25. }
  26. /** Zend_Dom_Query */
  27. require_once 'Zend/Dom/Query.php';
  28. /**
  29. * Test class for Zend_Dom_Query.
  30. *
  31. * @category Zend
  32. * @package Zend_Dom
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Dom
  37. */
  38. class Zend_Dom_QueryTest extends PHPUnit_Framework_TestCase
  39. {
  40. public $html;
  41. /**
  42. * Runs the test methods of this class.
  43. *
  44. * @return void
  45. */
  46. public static function main()
  47. {
  48. $suite = new PHPUnit_Framework_TestSuite("Zend_Dom_QueryTest");
  49. $result = PHPUnit_TextUI_TestRunner::run($suite);
  50. }
  51. /**
  52. * Sets up the fixture, for example, open a network connection.
  53. * This method is called before a test is executed.
  54. *
  55. * @return void
  56. */
  57. public function setUp()
  58. {
  59. $this->query = new Zend_Dom_Query();
  60. }
  61. /**
  62. * Tears down the fixture, for example, close a network connection.
  63. * This method is called after a test is executed.
  64. *
  65. * @return void
  66. */
  67. public function tearDown()
  68. {
  69. }
  70. public function getHtml()
  71. {
  72. if (null === $this->html) {
  73. $this->html = file_get_contents(dirname(__FILE__) . '/_files/sample.xhtml');
  74. }
  75. return $this->html;
  76. }
  77. public function loadHtml()
  78. {
  79. $this->query->setDocument($this->getHtml());
  80. }
  81. public function handleError($msg, $code = 0)
  82. {
  83. $this->error = $msg;
  84. }
  85. public function testConstructorShouldNotRequireArguments()
  86. {
  87. $query = new Zend_Dom_Query();
  88. }
  89. public function testConstructorShouldAcceptDocumentString()
  90. {
  91. $html = $this->getHtml();
  92. $query = new Zend_Dom_Query($html);
  93. $this->assertSame($html, $query->getDocument());
  94. }
  95. public function testDocShouldBeNullByDefault()
  96. {
  97. $this->assertNull($this->query->getDocument());
  98. }
  99. public function testDocShouldBeNullByEmptyStringConstructor()
  100. {
  101. $emptyStr = "";
  102. $query = new Zend_Dom_Query($emptyStr);
  103. $this->assertNull($this->query->getDocument());
  104. }
  105. public function testDocShouldBeNullByEmptyStringSet()
  106. {
  107. $emptyStr = "";
  108. $this->query->setDocument($emptyStr);
  109. $this->assertNull($this->query->getDocument());
  110. }
  111. public function testDocTypeShouldBeNullByDefault()
  112. {
  113. $this->assertNull($this->query->getDocumentType());
  114. }
  115. public function testShouldAllowSettingDocument()
  116. {
  117. $this->testDocShouldBeNullByDefault();
  118. $this->loadHtml();
  119. $this->assertEquals($this->getHtml(), $this->query->getDocument());
  120. }
  121. public function testDocumentTypeShouldBeAutomaticallyDiscovered()
  122. {
  123. $this->loadHtml();
  124. $this->assertEquals(Zend_Dom_Query::DOC_XHTML, $this->query->getDocumentType());
  125. $this->query->setDocument('<?xml version="1.0"?><root></root>');
  126. $this->assertEquals(Zend_Dom_Query::DOC_XML, $this->query->getDocumentType());
  127. $this->query->setDocument('<html><body></body></html>');
  128. $this->assertEquals(Zend_Dom_Query::DOC_HTML, $this->query->getDocumentType());
  129. $this->query->setDocument(new DOMDocument());
  130. $this->assertEquals(Zend_Dom_Query::DOC_DOM, $this->query->getDocumentType());
  131. }
  132. public function testQueryingWithoutRegisteringDocumentShouldThrowException()
  133. {
  134. try {
  135. $this->query->query('.foo');
  136. $this->fail('Querying without registering document should throw exception');
  137. } catch (Zend_Dom_Exception $e) {
  138. $this->assertContains('no document', $e->getMessage());
  139. }
  140. }
  141. public function testQueryingInvalidDocumentShouldThrowException()
  142. {
  143. set_error_handler(array($this, 'handleError'));
  144. $this->query->setDocumentXml('some bogus string');
  145. try {
  146. $this->query->query('.foo');
  147. restore_error_handler();
  148. $this->fail('Querying invalid document should throw exception');
  149. } catch (Zend_Dom_Exception $e) {
  150. restore_error_handler();
  151. $this->assertContains('Error parsing', $e->getMessage());
  152. }
  153. }
  154. public function testQueryShouldReturnResultObject()
  155. {
  156. $this->loadHtml();
  157. $test = $this->query->query('.foo');
  158. $this->assertTrue($test instanceof Zend_Dom_Query_Result);
  159. }
  160. public function testResultShouldIndicateNumberOfFoundNodes()
  161. {
  162. $this->loadHtml();
  163. $result = $this->query->query('.foo');
  164. $message = 'Xpath: ' . $result->getXpathQuery() . "\n";
  165. $this->assertEquals(3, count($result), $message);
  166. }
  167. public function testResultShouldAllowIteratingOverFoundNodes()
  168. {
  169. $this->loadHtml();
  170. $result = $this->query->query('.foo');
  171. $this->assertEquals(3, count($result));
  172. foreach ($result as $node) {
  173. $this->assertTrue($node instanceof DOMNode, var_export($result, 1));
  174. }
  175. }
  176. public function testQueryShouldFindNodesWithMultipleClasses()
  177. {
  178. $this->loadHtml();
  179. $result = $this->query->query('.footerblock .last');
  180. $this->assertEquals(1, count($result), $result->getXpathQuery());
  181. }
  182. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsExactly()
  183. {
  184. $this->loadHtml();
  185. $result = $this->query->query('div[dojoType="FilteringSelect"]');
  186. $this->assertEquals(1, count($result), $result->getXpathQuery());
  187. }
  188. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAsDiscreteWords()
  189. {
  190. $this->loadHtml();
  191. $result = $this->query->query('li[dojoType~="bar"]');
  192. $this->assertEquals(2, count($result), $result->getXpathQuery());
  193. }
  194. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAndAttributeValue()
  195. {
  196. $this->loadHtml();
  197. $result = $this->query->query('li[dojoType*="bar"]');
  198. $this->assertEquals(2, count($result), $result->getXpathQuery());
  199. }
  200. public function testQueryXpathShouldAllowQueryingArbitraryUsingXpath()
  201. {
  202. $this->loadHtml();
  203. $result = $this->query->queryXpath('//li[contains(@dojotype, "bar")]');
  204. $this->assertEquals(2, count($result), $result->getXpathQuery());
  205. }
  206. public function testQueryOnDomDocument()
  207. {
  208. $xml = <<<EOF
  209. <?xml version="1.0" encoding="UTF-8" ?>
  210. <foo>
  211. <bar class="baz"/>
  212. </foo>
  213. EOF;
  214. $document = new DOMDocument();
  215. $document->loadXML($xml, 524288 /* LIBXML_PARSEHUGE */);
  216. $this->query->setDocument($document);
  217. $test = $this->query->query('.baz');
  218. $this->assertTrue($test instanceof Zend_Dom_Query_Result);
  219. $testDocument = $test->getDocument();
  220. $this->assertTrue($testDocument instanceof DOMDocument);
  221. $this->assertEquals('UTF-8', $testDocument->encoding);
  222. }
  223. /**
  224. * @group ZF-9243
  225. */
  226. public function testLoadingDocumentWithErrorsShouldNotRaisePhpErrors()
  227. {
  228. $file = file_get_contents(dirname(__FILE__) . '/_files/bad-sample.html');
  229. $this->query->setDocument($file);
  230. $this->query->query('p');
  231. $errors = $this->query->getDocumentErrors();
  232. $this->assertTrue(is_array($errors));
  233. $this->assertTrue(0 < count($errors));
  234. }
  235. /**
  236. * @group ZF-9765
  237. */
  238. public function testCssSelectorShouldFindNodesWhenMatchingMultipleAttributes()
  239. {
  240. $html = <<<EOF
  241. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  242. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  243. <html>
  244. <body>
  245. <form action="#" method="get">
  246. <input type="hidden" name="foo" value="1" id="foo"/>
  247. <input type="hidden" name="bar" value="0" id="bar"/>
  248. <input type="hidden" name="baz" value="1" id="baz"/>
  249. </form>
  250. </body>
  251. </html>
  252. EOF;
  253. $this->query->setDocument($html);
  254. $results = $this->query->query('input[type="hidden"][value="1"]');
  255. $this->assertEquals(2, count($results), $results->getXpathQuery());
  256. $results = $this->query->query('input[value="1"][type~="hidden"]');
  257. $this->assertEquals(2, count($results), $results->getXpathQuery());
  258. $results = $this->query->query('input[type="hidden"][value="0"]');
  259. $this->assertEquals(1, count($results));
  260. }
  261. /**
  262. * @group ZF-3938
  263. */
  264. public function testAllowsSpecifyingEncodingAtConstruction()
  265. {
  266. $doc = new Zend_Dom_Query($this->getHtml(), 'iso-8859-1');
  267. $this->assertEquals('iso-8859-1', $doc->getEncoding());
  268. }
  269. /**
  270. * @group ZF-3938
  271. */
  272. public function testAllowsSpecifyingEncodingWhenSettingDocument()
  273. {
  274. $this->query->setDocument($this->getHtml(), 'iso-8859-1');
  275. $this->assertEquals('iso-8859-1', $this->query->getEncoding());
  276. }
  277. /**
  278. * @group ZF-3938
  279. */
  280. public function testAllowsSpecifyingEncodingViaSetter()
  281. {
  282. $this->query->setEncoding('iso-8859-1');
  283. $this->assertEquals('iso-8859-1', $this->query->getEncoding());
  284. }
  285. /**
  286. * @group ZF-3938
  287. */
  288. public function testSpecifyingEncodingSetsEncodingOnDomDocument()
  289. {
  290. $this->query->setDocument($this->getHtml(), 'utf-8');
  291. $test = $this->query->query('.foo');
  292. $this->assertTrue($test instanceof Zend_Dom_Query_Result);
  293. $doc = $test->getDocument();
  294. $this->assertTrue($doc instanceof DOMDocument);
  295. $this->assertEquals('utf-8', $doc->encoding);
  296. }
  297. /**
  298. * @group ZF-11376
  299. */
  300. public function testXhtmlDocumentWithXmlDeclaration()
  301. {
  302. $xhtmlWithXmlDecl = <<<EOB
  303. <?xml version="1.0" encoding="UTF-8" ?>
  304. <html xmlns="http://www.w3.org/1999/xhtml">
  305. <head><title /></head>
  306. <body><p>Test paragraph.</p></body>
  307. </html>
  308. EOB;
  309. $this->query->setDocument($xhtmlWithXmlDecl, 'utf-8');
  310. $this->assertEquals(1, $this->query->query('//p')->count());
  311. }
  312. /**
  313. * @group ZF-12106
  314. */
  315. public function testXhtmlDocumentWithXmlAndDoctypeDeclaration()
  316. {
  317. $xhtmlWithXmlDecl = <<<EOB
  318. <?xml version="1.0" encoding="UTF-8"?>
  319. <!DOCTYPE html
  320. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  321. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  322. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  323. <head>
  324. <title>Virtual Library</title>
  325. </head>
  326. <body>
  327. <p>Moved to <a href="http://example.org/">example.org</a>.</p>
  328. </body>
  329. </html>
  330. EOB;
  331. $this->query->setDocument($xhtmlWithXmlDecl, 'utf-8');
  332. $this->assertEquals(1, $this->query->query('//p')->count());
  333. }
  334. public function testLoadingXmlContainingDoctypeShouldFailToPreventXxeAndXeeAttacks()
  335. {
  336. $xml = <<<XML
  337. <?xml version="1.0"?>
  338. <!DOCTYPE results [<!ENTITY harmless "completely harmless">]>
  339. <results>
  340. <result>This result is &harmless;</result>
  341. </results>
  342. XML;
  343. $this->query->setDocumentXml($xml);
  344. $this->setExpectedException("Zend_Dom_Exception");
  345. $this->query->queryXpath('/');
  346. }
  347. }
  348. // Call Zend_Dom_QueryTest::main() if this source file is executed directly.
  349. if (PHPUnit_MAIN_METHOD == "Zend_Dom_QueryTest::main") {
  350. Zend_Dom_QueryTest::main();
  351. }