2010-04-15 13:41:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-25 16:06:54 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-15 13:41:42 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-04-15 13:41:42 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2012-03-28 14:43:52 +01:00
|
|
|
namespace Symfony\Component\DomCrawler\Tests;
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\DomCrawler\Link;
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2017-02-08 07:24:27 +00:00
|
|
|
class LinkTest extends TestCase
|
2010-04-15 13:41:42 +01:00
|
|
|
{
|
2011-04-23 15:25:21 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \LogicException
|
|
|
|
*/
|
|
|
|
public function testConstructorWithANonATag()
|
2010-04-15 13:41:42 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML('<html><div><div></html>');
|
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
new Link($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
|
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function testConstructorWithAnInvalidCurrentUri()
|
|
|
|
{
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
|
|
|
|
|
|
|
|
new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
|
2010-04-15 13:41:42 +01:00
|
|
|
}
|
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
public function testGetNode()
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$node = $dom->getElementsByTagName('a')->item(0);
|
2011-04-23 15:25:21 +01:00
|
|
|
$link = new Link($node, 'http://example.com/');
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link');
|
2011-04-23 15:25:21 +01:00
|
|
|
}
|
2010-04-15 13:41:42 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
public function testGetMethod()
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$dom = new \DOMDocument();
|
2011-04-23 15:25:21 +01:00
|
|
|
$dom->loadHTML('<html><a href="/foo">foo</a></html>');
|
2010-07-06 16:39:11 +01:00
|
|
|
|
|
|
|
$node = $dom->getElementsByTagName('a')->item(0);
|
2011-04-23 15:25:21 +01:00
|
|
|
$link = new Link($node, 'http://example.com/');
|
2010-07-06 16:39:11 +01:00
|
|
|
|
2011-05-25 14:38:02 +01:00
|
|
|
$this->assertEquals('GET', $link->getMethod(), '->getMethod() returns the method of the link');
|
2010-07-06 16:39:11 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
$link = new Link($node, 'http://example.com/', 'post');
|
2011-05-25 14:38:02 +01:00
|
|
|
$this->assertEquals('POST', $link->getMethod(), '->getMethod() returns the method of the link');
|
2011-04-23 15:25:21 +01:00
|
|
|
}
|
2010-07-06 16:39:11 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
/**
|
|
|
|
* @dataProvider getGetUriTests
|
|
|
|
*/
|
|
|
|
public function testGetUri($url, $currentUri, $expected)
|
|
|
|
{
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML(sprintf('<html><a href="%s">foo</a></html>', $url));
|
|
|
|
$link = new Link($dom->getElementsByTagName('a')->item(0), $currentUri);
|
2010-07-06 16:39:11 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
$this->assertEquals($expected, $link->getUri());
|
|
|
|
}
|
2010-09-02 19:47:15 +01:00
|
|
|
|
2014-02-11 14:26:57 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getGetUriTests
|
|
|
|
*/
|
|
|
|
public function testGetUriOnArea($url, $currentUri, $expected)
|
|
|
|
{
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML(sprintf('<html><map><area href="%s" /></map></html>', $url));
|
|
|
|
$link = new Link($dom->getElementsByTagName('area')->item(0), $currentUri);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $link->getUri());
|
|
|
|
}
|
|
|
|
|
2014-11-15 13:11:27 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getGetUriTests
|
|
|
|
*/
|
|
|
|
public function testGetUriOnLink($url, $currentUri, $expected)
|
|
|
|
{
|
|
|
|
$dom = new \DOMDocument();
|
|
|
|
$dom->loadHTML(sprintf('<html><head><link href="%s" /></head></html>', $url));
|
|
|
|
$link = new Link($dom->getElementsByTagName('link')->item(0), $currentUri);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $link->getUri());
|
|
|
|
}
|
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
public function getGetUriTests()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
|
|
|
|
array('/foo', 'http://localhost/bar/foo', 'http://localhost/foo'),
|
2011-11-10 14:38:28 +00:00
|
|
|
array('
|
|
|
|
/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
|
|
|
|
array('/foo
|
|
|
|
', 'http://localhost/bar/foo', 'http://localhost/foo'),
|
2010-09-02 19:47:15 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
array('foo', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo'),
|
|
|
|
array('foo', 'http://localhost/bar/foo', 'http://localhost/bar/foo'),
|
2010-09-02 19:47:15 +01:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
array('', 'http://localhost/bar/', 'http://localhost/bar/'),
|
|
|
|
array('#', 'http://localhost/bar/', 'http://localhost/bar/#'),
|
2014-06-21 21:15:52 +01:00
|
|
|
array('#bar', 'http://localhost/bar?a=b', 'http://localhost/bar?a=b#bar'),
|
2011-06-23 07:18:40 +01:00
|
|
|
array('#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'),
|
2014-06-21 21:15:52 +01:00
|
|
|
array('?a=b', 'http://localhost/bar#foo', 'http://localhost/bar?a=b'),
|
2011-04-23 15:25:21 +01:00
|
|
|
array('?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'),
|
2011-01-13 01:05:26 +00:00
|
|
|
|
2011-04-23 15:25:21 +01:00
|
|
|
array('http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
|
2013-02-28 11:27:10 +00:00
|
|
|
array('https://login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'),
|
|
|
|
array('mailto:foo@bar.com', 'http://localhost/foo', 'mailto:foo@bar.com'),
|
2011-06-22 20:42:59 +01:00
|
|
|
|
2013-02-25 14:57:52 +00:00
|
|
|
// tests schema relative URL (issue #7169)
|
|
|
|
array('//login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
|
|
|
|
array('//login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'),
|
|
|
|
|
2011-06-22 20:42:59 +01:00
|
|
|
array('?foo=2', 'http://localhost?foo=1', 'http://localhost?foo=2'),
|
|
|
|
array('?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'),
|
|
|
|
array('?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'),
|
|
|
|
array('?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'),
|
2011-06-23 07:18:40 +01:00
|
|
|
array('?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'),
|
2013-03-02 14:09:29 +00:00
|
|
|
|
2013-03-20 08:46:19 +00:00
|
|
|
array('foo', 'http://login.foo.com/bar/baz?/query/string', 'http://login.foo.com/bar/foo'),
|
|
|
|
|
2013-03-02 14:09:29 +00:00
|
|
|
array('.', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'),
|
|
|
|
array('./', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'),
|
|
|
|
array('./foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/foo'),
|
|
|
|
array('..', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'),
|
|
|
|
array('../', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'),
|
|
|
|
array('../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/foo'),
|
|
|
|
array('../..', 'http://localhost/foo/bar/baz', 'http://localhost/'),
|
|
|
|
array('../../', 'http://localhost/foo/bar/baz', 'http://localhost/'),
|
|
|
|
array('../../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo'),
|
|
|
|
array('../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
|
|
|
|
array('../bar/../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
|
|
|
|
array('../bar/./../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
|
|
|
|
array('../../', 'http://localhost/', 'http://localhost/'),
|
|
|
|
array('../../', 'http://localhost', 'http://localhost/'),
|
2013-07-01 13:15:46 +01:00
|
|
|
|
2014-06-21 21:15:52 +01:00
|
|
|
array('/foo', 'http://localhost?bar=1', 'http://localhost/foo'),
|
|
|
|
array('/foo', 'http://localhost#bar', 'http://localhost/foo'),
|
2013-06-13 00:54:36 +01:00
|
|
|
array('/foo', 'file:///', 'file:///foo'),
|
|
|
|
array('/foo', 'file:///bar/baz', 'file:///foo'),
|
|
|
|
array('foo', 'file:///', 'file:///foo'),
|
|
|
|
array('foo', 'file:///bar/baz', 'file:///bar/foo'),
|
2011-04-23 15:25:21 +01:00
|
|
|
);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-04-15 13:41:42 +01:00
|
|
|
}
|