Added the Porter Stemmer module to improve searches. This doesn't deal with some...
[yaffs-website] / vendor / symfony / dom-crawler / Tests / LinkTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\DomCrawler\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DomCrawler\Link;
16
17 class LinkTest extends TestCase
18 {
19     /**
20      * @expectedException \LogicException
21      */
22     public function testConstructorWithANonATag()
23     {
24         $dom = new \DOMDocument();
25         $dom->loadHTML('<html><div><div></html>');
26
27         new Link($dom->getElementsByTagName('div')->item(0), 'http://www.example.com/');
28     }
29
30     /**
31      * @expectedException \InvalidArgumentException
32      */
33     public function testConstructorWithAnInvalidCurrentUri()
34     {
35         $dom = new \DOMDocument();
36         $dom->loadHTML('<html><a href="/foo">foo</a></html>');
37
38         new Link($dom->getElementsByTagName('a')->item(0), 'example.com');
39     }
40
41     public function testGetNode()
42     {
43         $dom = new \DOMDocument();
44         $dom->loadHTML('<html><a href="/foo">foo</a></html>');
45
46         $node = $dom->getElementsByTagName('a')->item(0);
47         $link = new Link($node, 'http://example.com/');
48
49         $this->assertEquals($node, $link->getNode(), '->getNode() returns the node associated with the link');
50     }
51
52     public function testGetMethod()
53     {
54         $dom = new \DOMDocument();
55         $dom->loadHTML('<html><a href="/foo">foo</a></html>');
56
57         $node = $dom->getElementsByTagName('a')->item(0);
58         $link = new Link($node, 'http://example.com/');
59
60         $this->assertEquals('GET', $link->getMethod(), '->getMethod() returns the method of the link');
61
62         $link = new Link($node, 'http://example.com/', 'post');
63         $this->assertEquals('POST', $link->getMethod(), '->getMethod() returns the method of the link');
64     }
65
66     /**
67      * @dataProvider getGetUriTests
68      */
69     public function testGetUri($url, $currentUri, $expected)
70     {
71         $dom = new \DOMDocument();
72         $dom->loadHTML(sprintf('<html><a href="%s">foo</a></html>', $url));
73         $link = new Link($dom->getElementsByTagName('a')->item(0), $currentUri);
74
75         $this->assertEquals($expected, $link->getUri());
76     }
77
78     /**
79      * @dataProvider getGetUriTests
80      */
81     public function testGetUriOnArea($url, $currentUri, $expected)
82     {
83         $dom = new \DOMDocument();
84         $dom->loadHTML(sprintf('<html><map><area href="%s" /></map></html>', $url));
85         $link = new Link($dom->getElementsByTagName('area')->item(0), $currentUri);
86
87         $this->assertEquals($expected, $link->getUri());
88     }
89
90     /**
91      * @dataProvider getGetUriTests
92      */
93     public function testGetUriOnLink($url, $currentUri, $expected)
94     {
95         $dom = new \DOMDocument();
96         $dom->loadHTML(sprintf('<html><head><link href="%s" /></head></html>', $url));
97         $link = new Link($dom->getElementsByTagName('link')->item(0), $currentUri);
98
99         $this->assertEquals($expected, $link->getUri());
100     }
101
102     public function getGetUriTests()
103     {
104         return array(
105             array('/foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
106             array('/foo', 'http://localhost/bar/foo', 'http://localhost/foo'),
107             array('
108             /foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
109             array('/foo
110             ', 'http://localhost/bar/foo', 'http://localhost/foo'),
111
112             array('foo', 'http://localhost/bar/foo/', 'http://localhost/bar/foo/foo'),
113             array('foo', 'http://localhost/bar/foo', 'http://localhost/bar/foo'),
114
115             array('', 'http://localhost/bar/', 'http://localhost/bar/'),
116             array('#', 'http://localhost/bar/', 'http://localhost/bar/#'),
117             array('#bar', 'http://localhost/bar?a=b', 'http://localhost/bar?a=b#bar'),
118             array('#bar', 'http://localhost/bar/#foo', 'http://localhost/bar/#bar'),
119             array('?a=b', 'http://localhost/bar#foo', 'http://localhost/bar?a=b'),
120             array('?a=b', 'http://localhost/bar/', 'http://localhost/bar/?a=b'),
121
122             array('http://login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
123             array('https://login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'),
124             array('mailto:foo@bar.com', 'http://localhost/foo', 'mailto:foo@bar.com'),
125
126             // tests schema relative URL (issue #7169)
127             array('//login.foo.com/foo', 'http://localhost/bar/', 'http://login.foo.com/foo'),
128             array('//login.foo.com/foo', 'https://localhost/bar/', 'https://login.foo.com/foo'),
129
130             array('?foo=2', 'http://localhost?foo=1', 'http://localhost?foo=2'),
131             array('?foo=2', 'http://localhost/?foo=1', 'http://localhost/?foo=2'),
132             array('?foo=2', 'http://localhost/bar?foo=1', 'http://localhost/bar?foo=2'),
133             array('?foo=2', 'http://localhost/bar/?foo=1', 'http://localhost/bar/?foo=2'),
134             array('?bar=2', 'http://localhost?foo=1', 'http://localhost?bar=2'),
135
136             array('foo', 'http://login.foo.com/bar/baz?/query/string', 'http://login.foo.com/bar/foo'),
137
138             array('.', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'),
139             array('./', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/'),
140             array('./foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/bar/foo'),
141             array('..', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'),
142             array('../', 'http://localhost/foo/bar/baz', 'http://localhost/foo/'),
143             array('../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo/foo'),
144             array('../..', 'http://localhost/foo/bar/baz', 'http://localhost/'),
145             array('../../', 'http://localhost/foo/bar/baz', 'http://localhost/'),
146             array('../../foo', 'http://localhost/foo/bar/baz', 'http://localhost/foo'),
147             array('../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
148             array('../bar/../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
149             array('../bar/./../../foo', 'http://localhost/bar/foo/', 'http://localhost/foo'),
150             array('../../', 'http://localhost/', 'http://localhost/'),
151             array('../../', 'http://localhost', 'http://localhost/'),
152
153             array('/foo', 'http://localhost?bar=1', 'http://localhost/foo'),
154             array('/foo', 'http://localhost#bar', 'http://localhost/foo'),
155             array('/foo', 'file:///', 'file:///foo'),
156             array('/foo', 'file:///bar/baz', 'file:///foo'),
157             array('foo', 'file:///', 'file:///foo'),
158             array('foo', 'file:///bar/baz', 'file:///bar/foo'),
159         );
160     }
161 }