Skip to content

Commit

Permalink
[DomCrawler] Fixed Form::getUri() and Link::getUri() issue if the for…
Browse files Browse the repository at this point in the history
…m action attribute is an absolute url
  • Loading branch information
nfabre authored and fabpot committed Jul 7, 2010
1 parent 4b24544 commit 6613555
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Symfony/Components/DomCrawler/Form.php
Expand Up @@ -169,17 +169,18 @@ public function getPhpFiles()
public function getUri($absolute = true)
{
$uri = $this->node->getAttribute('action');
$urlHaveScheme = 'http' === substr($uri, 0, 4);

if (!in_array($this->getMethod(), array('post', 'put', 'delete')) && $queryString = http_build_query($this->getValues(), null, '&')) {
$sep = false === strpos($uri, '?') ? '?' : '&';
$uri .= $sep.$queryString;
}

if ($uri && '/' !== $uri[0]) {
if ($uri && '/' !== $uri[0] && !$urlHaveScheme) {
$uri = $this->path.$uri;
}

if ($absolute && null !== $this->host) {
if ($absolute && null !== $this->host && !$urlHaveScheme) {
return $this->host.$uri;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Components/DomCrawler/Link.php
Expand Up @@ -67,12 +67,13 @@ public function getNode()
public function getUri($absolute = true)
{
$uri = $this->node->getAttribute('href');
$urlHaveScheme = 'http' === substr($uri, 0, 4);

if ($uri && '/' !== $uri[0]) {
if ($uri && '/' !== $uri[0] && !$urlHaveScheme) {
$uri = $this->path.$uri;
}

if ($absolute && null !== $this->host) {
if ($absolute && null !== $this->host && !$urlHaveScheme) {
return $this->host.$uri;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Symfony/Tests/Components/DomCrawler/FormTest.php
Expand Up @@ -244,6 +244,27 @@ public function testGetUri($message, $form, $values, $uri)
$this->assertEquals($uri, $form->getUri(), '->getUri() '.$message);
}

public function testGetUriActionAbsolute()
{
$formHtml='<form id="login_form" action="https://login.foo.com/login.php?login_attempt=1" method="POST"><input type="text" name="foo" value="foo" /><input type="submit" /></form>';

$form = $this->createForm($formHtml);
$this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');

$form = $this->createForm($formHtml, null, 'https://login.foo.com');
$this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');

$form = $this->createForm($formHtml, null, 'https://login.foo.com', '/bar/');
$this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');

// The action URI haven't the same domain Host have an another domain as Host
$form = $this->createForm($formHtml, null, 'https://www.foo.com');
$this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');

$form = $this->createForm($formHtml, null, 'https://www.foo.com', '/bar/');
$this->assertEquals('https://login.foo.com/login.php?login_attempt=1', $form->getUri(), '->getUri() returns absolute URIs set in the action form');
}

public function testGetUriAbsolute()
{
$form = $this->createForm('<form action="foo"><input type="submit" /></form>', null, 'http://localhost', '/foo/');
Expand Down
16 changes: 16 additions & 0 deletions tests/Symfony/Tests/Components/DomCrawler/LinkTest.php
Expand Up @@ -56,5 +56,21 @@ public function testGetters()
$link = new Link($node, 'get', 'http://localhost', '/bar/');
$this->assertEquals('http://localhost/bar/foo', $link->getUri(), '->getUri() returns the absolute URI of the link for relative hrefs');
$this->assertEquals('/bar/foo', $link->getUri(false), '->getUri() returns the relative URI of the link if false is the first argument');

$dom = new \DOMDocument();
$dom->loadHTML('<html><a href="http://login.foo.com/foo">foo</a></html>');
$node = $dom->getElementsByTagName('a')->item(0);

$link = new Link($node, 'get', 'http://www.foo.com');
$this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');

$link = new Link($node, 'get');
$this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');

$link = new Link($node, 'get', null, '/bar/');
$this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');

$link = new Link($node, 'get','http://www.foo.com','/bar/');
$this->assertEquals('http://login.foo.com/foo', $link->getUri(), '->getUri() returns the absolute URI of the link, regardless of the context of the object');
}
}

0 comments on commit 6613555

Please sign in to comment.