[DomCrawler] Added more tests for the XPath filtering

This will ensure we don't introduce regressions again when fixing #10206.
This commit is contained in:
Christophe Coevoet 2014-05-18 02:01:43 +02:00
parent 125b9e7347
commit 8f706c97b9
2 changed files with 103 additions and 0 deletions

View File

@ -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 = <<<HTML
<!DOCTYPE html>
<html lang="en">
<body>
<div id="action">
<a href="/index.php?r=site/login">Login</a>
</div>
<form id="login-form" action="/index.php?r=site/login" method="post">
<button type="submit">Submit</button>
</form>
</body>
</html>
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('<div name="a"><div name="b"><div name="c"></div></div></div>');
$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
<li>Two Bis</li>
<li>Three Bis</li>
</ul>
<div id="parent">
<div id="child"></div>
</div>
<div id="sibling"><img /></div>
</body>
</html>
');

View File

@ -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('
<html>
<h1>Hello form</h1>
<form action="" method="POST">
<div><input type="checkbox" name="apples[]" value="1" checked /></div>
<input type="checkbox" name="oranges[]" value="1" checked />
<div><label></label><input type="hidden" name="form_name" value="form-1" /></div>
<input type="submit" name="button_1" value="Capture fields" />
<button type="submit" name="button_2">Submit form_2</button>
</form>
<form action="" method="POST">
<div><div><input type="checkbox" name="oranges[]" value="2" checked />
<input type="checkbox" name="oranges[]" value="3" checked /></div></div>
<input type="hidden" name="form_name" value="form_2" />
<input type="hidden" name="outer_field" value="success" />
<button type="submit" name="button_3">Submit from outside the form</button>
</form>
<button />
</html>');
return $dom;
}
public function testgetPhpValuesWithEmptyTextarea()
{
$dom = new \DOMDocument();