QueryTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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-2012 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-2012 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. }
  130. public function testQueryingWithoutRegisteringDocumentShouldThrowException()
  131. {
  132. try {
  133. $this->query->query('.foo');
  134. $this->fail('Querying without registering document should throw exception');
  135. } catch (Zend_Dom_Exception $e) {
  136. $this->assertContains('no document', $e->getMessage());
  137. }
  138. }
  139. public function testQueryingInvalidDocumentShouldThrowException()
  140. {
  141. set_error_handler(array($this, 'handleError'));
  142. $this->query->setDocumentXml('some bogus string');
  143. try {
  144. $this->query->query('.foo');
  145. restore_error_handler();
  146. $this->fail('Querying invalid document should throw exception');
  147. } catch (Zend_Dom_Exception $e) {
  148. restore_error_handler();
  149. $this->assertContains('Error parsing', $e->getMessage());
  150. }
  151. }
  152. public function testQueryShouldReturnResultObject()
  153. {
  154. $this->loadHtml();
  155. $test = $this->query->query('.foo');
  156. $this->assertTrue($test instanceof Zend_Dom_Query_Result);
  157. }
  158. public function testResultShouldIndicateNumberOfFoundNodes()
  159. {
  160. $this->loadHtml();
  161. $result = $this->query->query('.foo');
  162. $message = 'Xpath: ' . $result->getXpathQuery() . "\n";
  163. $this->assertEquals(3, count($result), $message);
  164. }
  165. public function testResultShouldAllowIteratingOverFoundNodes()
  166. {
  167. $this->loadHtml();
  168. $result = $this->query->query('.foo');
  169. $this->assertEquals(3, count($result));
  170. foreach ($result as $node) {
  171. $this->assertTrue($node instanceof DOMNode, var_export($result, 1));
  172. }
  173. }
  174. public function testQueryShouldFindNodesWithMultipleClasses()
  175. {
  176. $this->loadHtml();
  177. $result = $this->query->query('.footerblock .last');
  178. $this->assertEquals(1, count($result), $result->getXpathQuery());
  179. }
  180. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsExactly()
  181. {
  182. $this->loadHtml();
  183. $result = $this->query->query('div[dojoType="FilteringSelect"]');
  184. $this->assertEquals(1, count($result), $result->getXpathQuery());
  185. }
  186. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAsDiscreteWords()
  187. {
  188. $this->loadHtml();
  189. $result = $this->query->query('li[dojoType~="bar"]');
  190. $this->assertEquals(2, count($result), $result->getXpathQuery());
  191. }
  192. public function testQueryShouldFindNodesWithArbitraryAttributeSelectorsAndAttributeValue()
  193. {
  194. $this->loadHtml();
  195. $result = $this->query->query('li[dojoType*="bar"]');
  196. $this->assertEquals(2, count($result), $result->getXpathQuery());
  197. }
  198. public function testQueryXpathShouldAllowQueryingArbitraryUsingXpath()
  199. {
  200. $this->loadHtml();
  201. $result = $this->query->queryXpath('//li[contains(@dojotype, "bar")]');
  202. $this->assertEquals(2, count($result), $result->getXpathQuery());
  203. }
  204. /**
  205. * @group ZF-9243
  206. */
  207. public function testLoadingDocumentWithErrorsShouldNotRaisePhpErrors()
  208. {
  209. $file = file_get_contents(dirname(__FILE__) . '/_files/bad-sample.html');
  210. $this->query->setDocument($file);
  211. $this->query->query('p');
  212. $errors = $this->query->getDocumentErrors();
  213. $this->assertTrue(is_array($errors));
  214. $this->assertTrue(0 < count($errors));
  215. }
  216. /**
  217. * @group ZF-9765
  218. */
  219. public function testCssSelectorShouldFindNodesWhenMatchingMultipleAttributes()
  220. {
  221. $html = <<<EOF
  222. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  223. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  224. <html>
  225. <body>
  226. <form action="#" method="get">
  227. <input type="hidden" name="foo" value="1" id="foo"/>
  228. <input type="hidden" name="bar" value="0" id="bar"/>
  229. <input type="hidden" name="baz" value="1" id="baz"/>
  230. </form>
  231. </body>
  232. </html>
  233. EOF;
  234. $this->query->setDocument($html);
  235. $results = $this->query->query('input[type="hidden"][value="1"]');
  236. $this->assertEquals(2, count($results), $results->getXpathQuery());
  237. $results = $this->query->query('input[value="1"][type~="hidden"]');
  238. $this->assertEquals(2, count($results), $results->getXpathQuery());
  239. $results = $this->query->query('input[type="hidden"][value="0"]');
  240. $this->assertEquals(1, count($results));
  241. }
  242. /**
  243. * @group ZF-3938
  244. */
  245. public function testAllowsSpecifyingEncodingAtConstruction()
  246. {
  247. $doc = new Zend_Dom_Query($this->getHtml(), 'iso-8859-1');
  248. $this->assertEquals('iso-8859-1', $doc->getEncoding());
  249. }
  250. /**
  251. * @group ZF-3938
  252. */
  253. public function testAllowsSpecifyingEncodingWhenSettingDocument()
  254. {
  255. $this->query->setDocument($this->getHtml(), 'iso-8859-1');
  256. $this->assertEquals('iso-8859-1', $this->query->getEncoding());
  257. }
  258. /**
  259. * @group ZF-3938
  260. */
  261. public function testAllowsSpecifyingEncodingViaSetter()
  262. {
  263. $this->query->setEncoding('iso-8859-1');
  264. $this->assertEquals('iso-8859-1', $this->query->getEncoding());
  265. }
  266. /**
  267. * @group ZF-3938
  268. */
  269. public function testSpecifyingEncodingSetsEncodingOnDomDocument()
  270. {
  271. $this->query->setDocument($this->getHtml(), 'utf-8');
  272. $test = $this->query->query('.foo');
  273. $this->assertType('Zend_Dom_Query_Result', $test);
  274. $doc = $test->getDocument();
  275. $this->assertType('DOMDocument', $doc);
  276. $this->assertEquals('utf-8', $doc->encoding);
  277. }
  278. /**
  279. * @group ZF-11376
  280. */
  281. public function testXhtmlDocumentWithXmlDeclaration()
  282. {
  283. $xhtmlWithXmlDecl = <<<EOB
  284. <?xml version="1.0" encoding="UTF-8" ?>
  285. <html xmlns="http://www.w3.org/1999/xhtml">
  286. <head><title /></head>
  287. <body><p>Test paragraph.</p></body>
  288. </html>
  289. EOB;
  290. $this->query->setDocument($xhtmlWithXmlDecl, 'utf-8');
  291. $this->assertEquals(1, $this->query->query('//p')->count());
  292. }
  293. /**
  294. * @group ZF-12106
  295. */
  296. public function testXhtmlDocumentWithXmlAndDoctypeDeclaration()
  297. {
  298. $xhtmlWithXmlDecl = <<<EOB
  299. <?xml version="1.0" encoding="UTF-8"?>
  300. <!DOCTYPE html
  301. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  302. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  303. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  304. <head>
  305. <title>Virtual Library</title>
  306. </head>
  307. <body>
  308. <p>Moved to <a href="http://example.org/">example.org</a>.</p>
  309. </body>
  310. </html>
  311. EOB;
  312. $this->query->setDocument($xhtmlWithXmlDecl, 'utf-8');
  313. $this->assertEquals(1, $this->query->query('//p')->count());
  314. }
  315. public function testLoadingXmlContainingDoctypeShouldFailToPreventXxeAndXeeAttacks()
  316. {
  317. $xml = <<<XML
  318. <?xml version="1.0"?>
  319. <!DOCTYPE results [<!ENTITY harmless "completely harmless">]>
  320. <results>
  321. <result>This result is &harmless;</result>
  322. </results>
  323. XML;
  324. $this->query->setDocumentXml($xml);
  325. $this->setExpectedException("Zend_Dom_Exception");
  326. $this->query->queryXpath('/');
  327. }
  328. }
  329. // Call Zend_Dom_QueryTest::main() if this source file is executed directly.
  330. if (PHPUnit_MAIN_METHOD == "Zend_Dom_QueryTest::main") {
  331. Zend_Dom_QueryTest::main();
  332. }