From 8f706c97b9a15a4b10dde0ae51aab5069f4151b0 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Sun, 18 May 2014 02:01:43 +0200 Subject: [PATCH 1/3] [DomCrawler] Added more tests for the XPath filtering This will ensure we don't introduce regressions again when fixing #10206. --- .../DomCrawler/Tests/CrawlerTest.php | 63 +++++++++++++++++++ .../Component/DomCrawler/Tests/FormTest.php | 40 ++++++++++++ 2 files changed, 103 insertions(+) diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index 07a08d0d04..8c90caea08 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -368,6 +368,27 @@ EOF $this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->extract('_text'), '->extract() returns an empty array if the node list is empty'); } + public function testFilterXpathComplexQueries() + { + $crawler = $this->createTestCrawler()->filterXPath('//body'); + + $this->assertCount(0, $crawler->filterXPath('/input')); + $this->assertCount(0, $crawler->filterXPath('/body')); + $this->assertCount(1, $crawler->filterXPath('/_root/body')); + $this->assertCount(1, $crawler->filterXPath('./body')); + $this->assertCount(4, $crawler->filterXPath('//form')->filterXPath('//button | //input')); + $this->assertCount(1, $crawler->filterXPath('body')); + $this->assertCount(6, $crawler->filterXPath('//button | //input')); + $this->assertCount(1, $crawler->filterXPath('//body')); + $this->assertCount(1, $crawler->filterXPath('descendant-or-self::body')); + $this->assertCount(1, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('./div'), 'A child selection finds only the current div'); + $this->assertCount(2, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('descendant::div'), 'A descendant selector matches the current div and its child'); + $this->assertCount(2, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('//div'), 'A descendant selector matches the current div and its child'); + $this->assertCount(5, $crawler->filterXPath('(//a | //div)//img')); + $this->assertCount(7, $crawler->filterXPath('((//a | //div)//img | //ul)')); + $this->assertCount(7, $crawler->filterXPath('( ( //a | //div )//img | //ul )')); + } + /** * @covers Symfony\Component\DomCrawler\Crawler::filterXPath */ @@ -455,6 +476,44 @@ EOF } } + public function testSelectLinkAndLinkFiltered() + { + $html = << + + +
+ Login +
+
+ +
+ + +HTML; + + $crawler = new Crawler($html); + $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'login-form']"); + + $this->assertCount(0, $filtered->selectLink('Login')); + $this->assertCount(1, $filtered->selectButton('Submit')); + + $filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'action']"); + + $this->assertCount(1, $filtered->selectLink('Login')); + $this->assertCount(0, $filtered->selectButton('Submit')); + + $this->assertCount(1, $crawler->selectLink('Login')->selectLink('Login')); + $this->assertCount(1, $crawler->selectButton('Submit')->selectButton('Submit')); + } + + public function testChaining() + { + $crawler = new Crawler('
'); + + $this->assertEquals('a', $crawler->filterXPath('//div')->filterXPath('div')->filterXPath('div')->attr('name')); + } + public function testLinks() { $crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo'); @@ -665,6 +724,10 @@ EOF
  • Two Bis
  • Three Bis
  • +
    +
    +
    +
    '); diff --git a/src/Symfony/Component/DomCrawler/Tests/FormTest.php b/src/Symfony/Component/DomCrawler/Tests/FormTest.php index 02e7fbba4b..0c12b08dc8 100644 --- a/src/Symfony/Component/DomCrawler/Tests/FormTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/FormTest.php @@ -85,6 +85,20 @@ class FormTest extends \PHPUnit_Framework_TestCase $form = new Form($nodes->item(1), 'http://example.com'); } + public function testConstructorLoadsOnlyFieldsOfTheRightForm() + { + $dom = $this->createTestMultipleForm(); + + $nodes = $dom->getElementsByTagName('form'); + $buttonElements = $dom->getElementsByTagName('button'); + + $form = new Form($nodes->item(0), 'http://example.com'); + $this->assertCount(3, $form->all()); + + $form = new Form($buttonElements->item(1), 'http://example.com'); + $this->assertCount(5, $form->all()); + } + public function testConstructorHandlesFormAttribute() { $dom = $this->createTestHtml5Form(); @@ -840,6 +854,32 @@ class FormTest extends \PHPUnit_Framework_TestCase return $dom; } + protected function createTestMultipleForm() + { + $dom = new \DOMDocument(); + $dom->loadHTML(' + +

    Hello form

    +
    +
    + +
    + + +
    +
    +
    +
    + + + +
    +