Skip to content

Commit 8f706c9

Browse files
committed
[DomCrawler] Added more tests for the XPath filtering
This will ensure we don't introduce regressions again when fixing symfony#10206.
1 parent 125b9e7 commit 8f706c9

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,27 @@ public function testExtract()
368368
$this->assertEquals(array(), $this->createTestCrawler()->filterXPath('//ol')->extract('_text'), '->extract() returns an empty array if the node list is empty');
369369
}
370370

371+
public function testFilterXpathComplexQueries()
372+
{
373+
$crawler = $this->createTestCrawler()->filterXPath('//body');
374+
375+
$this->assertCount(0, $crawler->filterXPath('/input'));
376+
$this->assertCount(0, $crawler->filterXPath('/body'));
377+
$this->assertCount(1, $crawler->filterXPath('/_root/body'));
378+
$this->assertCount(1, $crawler->filterXPath('./body'));
379+
$this->assertCount(4, $crawler->filterXPath('//form')->filterXPath('//button | //input'));
380+
$this->assertCount(1, $crawler->filterXPath('body'));
381+
$this->assertCount(6, $crawler->filterXPath('//button | //input'));
382+
$this->assertCount(1, $crawler->filterXPath('//body'));
383+
$this->assertCount(1, $crawler->filterXPath('descendant-or-self::body'));
384+
$this->assertCount(1, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('./div'), 'A child selection finds only the current div');
385+
$this->assertCount(2, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('descendant::div'), 'A descendant selector matches the current div and its child');
386+
$this->assertCount(2, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('//div'), 'A descendant selector matches the current div and its child');
387+
$this->assertCount(5, $crawler->filterXPath('(//a | //div)//img'));
388+
$this->assertCount(7, $crawler->filterXPath('((//a | //div)//img | //ul)'));
389+
$this->assertCount(7, $crawler->filterXPath('( ( //a | //div )//img | //ul )'));
390+
}
391+
371392
/**
372393
* @covers Symfony\Component\DomCrawler\Crawler::filterXPath
373394
*/
@@ -455,6 +476,44 @@ public function testLink()
455476
}
456477
}
457478

479+
public function testSelectLinkAndLinkFiltered()
480+
{
481+
$html = <<<HTML
482+
<!DOCTYPE html>
483+
<html lang="en">
484+
<body>
485+
<div id="action">
486+
<a href="/index.php?r=site/login">Login</a>
487+
</div>
488+
<form id="login-form" action="/index.php?r=site/login" method="post">
489+
<button type="submit">Submit</button>
490+
</form>
491+
</body>
492+
</html>
493+
HTML;
494+
495+
$crawler = new Crawler($html);
496+
$filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'login-form']");
497+
498+
$this->assertCount(0, $filtered->selectLink('Login'));
499+
$this->assertCount(1, $filtered->selectButton('Submit'));
500+
501+
$filtered = $crawler->filterXPath("descendant-or-self::*[@id = 'action']");
502+
503+
$this->assertCount(1, $filtered->selectLink('Login'));
504+
$this->assertCount(0, $filtered->selectButton('Submit'));
505+
506+
$this->assertCount(1, $crawler->selectLink('Login')->selectLink('Login'));
507+
$this->assertCount(1, $crawler->selectButton('Submit')->selectButton('Submit'));
508+
}
509+
510+
public function testChaining()
511+
{
512+
$crawler = new Crawler('<div name="a"><div name="b"><div name="c"></div></div></div>');
513+
514+
$this->assertEquals('a', $crawler->filterXPath('//div')->filterXPath('div')->filterXPath('div')->attr('name'));
515+
}
516+
458517
public function testLinks()
459518
{
460519
$crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');
@@ -665,6 +724,10 @@ public function createTestCrawler($uri = null)
665724
<li>Two Bis</li>
666725
<li>Three Bis</li>
667726
</ul>
727+
<div id="parent">
728+
<div id="child"></div>
729+
</div>
730+
<div id="sibling"><img /></div>
668731
</body>
669732
</html>
670733
');

src/Symfony/Component/DomCrawler/Tests/FormTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ public function testConstructorThrowsExceptionIfNoRelatedForm()
8585
$form = new Form($nodes->item(1), 'http://example.com');
8686
}
8787

88+
public function testConstructorLoadsOnlyFieldsOfTheRightForm()
89+
{
90+
$dom = $this->createTestMultipleForm();
91+
92+
$nodes = $dom->getElementsByTagName('form');
93+
$buttonElements = $dom->getElementsByTagName('button');
94+
95+
$form = new Form($nodes->item(0), 'http://example.com');
96+
$this->assertCount(3, $form->all());
97+
98+
$form = new Form($buttonElements->item(1), 'http://example.com');
99+
$this->assertCount(5, $form->all());
100+
}
101+
88102
public function testConstructorHandlesFormAttribute()
89103
{
90104
$dom = $this->createTestHtml5Form();
@@ -840,6 +854,32 @@ protected function createTestHtml5Form()
840854
return $dom;
841855
}
842856

857+
protected function createTestMultipleForm()
858+
{
859+
$dom = new \DOMDocument();
860+
$dom->loadHTML('
861+
<html>
862+
<h1>Hello form</h1>
863+
<form action="" method="POST">
864+
<div><input type="checkbox" name="apples[]" value="1" checked /></div>
865+
<input type="checkbox" name="oranges[]" value="1" checked />
866+
<div><label></label><input type="hidden" name="form_name" value="form-1" /></div>
867+
<input type="submit" name="button_1" value="Capture fields" />
868+
<button type="submit" name="button_2">Submit form_2</button>
869+
</form>
870+
<form action="" method="POST">
871+
<div><div><input type="checkbox" name="oranges[]" value="2" checked />
872+
<input type="checkbox" name="oranges[]" value="3" checked /></div></div>
873+
<input type="hidden" name="form_name" value="form_2" />
874+
<input type="hidden" name="outer_field" value="success" />
875+
<button type="submit" name="button_3">Submit from outside the form</button>
876+
</form>
877+
<button />
878+
</html>');
879+
880+
return $dom;
881+
}
882+
843883
public function testgetPhpValuesWithEmptyTextarea()
844884
{
845885
$dom = new \DOMDocument();

0 commit comments

Comments
 (0)