diff options
Diffstat (limited to 'tests/Url')
-rw-r--r-- | tests/Url/CleanupUrlTest.php | 109 | ||||
-rw-r--r-- | tests/Url/GetUrlSchemeTest.php | 30 | ||||
-rw-r--r-- | tests/Url/UnparseUrlTest.php | 30 | ||||
-rw-r--r-- | tests/Url/UrlTest.php | 200 | ||||
-rw-r--r-- | tests/Url/WhitelistProtocolsTest.php | 63 |
5 files changed, 0 insertions, 432 deletions
diff --git a/tests/Url/CleanupUrlTest.php b/tests/Url/CleanupUrlTest.php deleted file mode 100644 index 24791948..00000000 --- a/tests/Url/CleanupUrlTest.php +++ /dev/null | |||
@@ -1,109 +0,0 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Unitary tests for cleanup_url() | ||
4 | */ | ||
5 | |||
6 | require_once 'application/Url.php'; | ||
7 | |||
8 | class CleanupUrlTest extends PHPUnit_Framework_TestCase | ||
9 | { | ||
10 | /** | ||
11 | * @var string reference URL | ||
12 | */ | ||
13 | protected $ref = 'http://domain.tld:3000'; | ||
14 | |||
15 | |||
16 | /** | ||
17 | * Clean empty URL | ||
18 | */ | ||
19 | public function testCleanupUrlEmpty() | ||
20 | { | ||
21 | $this->assertEquals('', cleanup_url('')); | ||
22 | } | ||
23 | |||
24 | /** | ||
25 | * Clean an already cleaned URL | ||
26 | */ | ||
27 | public function testCleanupUrlAlreadyClean() | ||
28 | { | ||
29 | $this->assertEquals($this->ref, cleanup_url($this->ref)); | ||
30 | $this->ref2 = $this->ref.'/path/to/dir/'; | ||
31 | $this->assertEquals($this->ref2, cleanup_url($this->ref2)); | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * Clean URL fragments | ||
36 | */ | ||
37 | public function testCleanupUrlFragment() | ||
38 | { | ||
39 | $this->assertEquals($this->ref, cleanup_url($this->ref.'#tk.rss_all')); | ||
40 | $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-')); | ||
41 | $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-U3ht0tkc4b')); | ||
42 | } | ||
43 | |||
44 | /** | ||
45 | * Clean URL query - single annoying parameter | ||
46 | */ | ||
47 | public function testCleanupUrlQuerySingle() | ||
48 | { | ||
49 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_object_map=junk')); | ||
50 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_ref_map=Cr4p!')); | ||
51 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_type_map=g4R84g3')); | ||
52 | |||
53 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb_stuff=v41u3')); | ||
54 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb=71m3w4573')); | ||
55 | |||
56 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_campaign=zomg')); | ||
57 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_medium=numnum')); | ||
58 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_source=c0d3')); | ||
59 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_term=1n4l')); | ||
60 | |||
61 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url')); | ||
62 | |||
63 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_name=junk')); | ||
64 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_start=junk')); | ||
65 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_item_index=junk')); | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Clean URL query - multiple annoying parameters | ||
70 | */ | ||
71 | public function testCleanupUrlQueryMultiple() | ||
72 | { | ||
73 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url&fb=som3th1ng')); | ||
74 | |||
75 | $this->assertEquals($this->ref, cleanup_url( | ||
76 | $this->ref.'?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3' | ||
77 | )); | ||
78 | |||
79 | $this->assertEquals($this->ref, cleanup_url( | ||
80 | $this->ref.'?campaign_start=zomg&campaign_name=numnum' | ||
81 | )); | ||
82 | } | ||
83 | |||
84 | /** | ||
85 | * Clean URL query - multiple annoying parameters and fragment | ||
86 | */ | ||
87 | public function testCleanupUrlQueryFragment() | ||
88 | { | ||
89 | $this->assertEquals($this->ref, cleanup_url( | ||
90 | $this->ref.'?xtor=some-url&fb=som3th1ng#tk.rss_all' | ||
91 | )); | ||
92 | |||
93 | // ditch annoying query params and fragment, keep useful params | ||
94 | $this->assertEquals( | ||
95 | $this->ref.'?my=stuff&is=kept', | ||
96 | cleanup_url( | ||
97 | $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all' | ||
98 | ) | ||
99 | ); | ||
100 | |||
101 | // ditch annoying query params, keep useful params and fragment | ||
102 | $this->assertEquals( | ||
103 | $this->ref.'?my=stuff&is=kept#again', | ||
104 | cleanup_url( | ||
105 | $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again' | ||
106 | ) | ||
107 | ); | ||
108 | } | ||
109 | } | ||
diff --git a/tests/Url/GetUrlSchemeTest.php b/tests/Url/GetUrlSchemeTest.php deleted file mode 100644 index 18b932d6..00000000 --- a/tests/Url/GetUrlSchemeTest.php +++ /dev/null | |||
@@ -1,30 +0,0 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Unitary tests for get_url_scheme() | ||
4 | */ | ||
5 | |||
6 | require_once 'application/Url.php'; | ||
7 | |||
8 | class GetUrlSchemeTest extends PHPUnit_Framework_TestCase | ||
9 | { | ||
10 | /** | ||
11 | * Get empty scheme string for empty Url | ||
12 | */ | ||
13 | public function testGetUrlSchemeEmpty() | ||
14 | { | ||
15 | $this->assertEquals('', get_url_scheme('')); | ||
16 | } | ||
17 | |||
18 | /** | ||
19 | * Get normal scheme of Url | ||
20 | */ | ||
21 | public function testGetUrlScheme() | ||
22 | { | ||
23 | $this->assertEquals('http', get_url_scheme('http://domain.tld:3000')); | ||
24 | $this->assertEquals('https', get_url_scheme('https://domain.tld:3000')); | ||
25 | $this->assertEquals('http', get_url_scheme('domain.tld')); | ||
26 | $this->assertEquals('ssh', get_url_scheme('ssh://domain.tld')); | ||
27 | $this->assertEquals('ftp', get_url_scheme('ftp://domain.tld')); | ||
28 | $this->assertEquals('git', get_url_scheme('git://domain.tld/push?pull=clone#checkout')); | ||
29 | } | ||
30 | } | ||
diff --git a/tests/Url/UnparseUrlTest.php b/tests/Url/UnparseUrlTest.php deleted file mode 100644 index e314b484..00000000 --- a/tests/Url/UnparseUrlTest.php +++ /dev/null | |||
@@ -1,30 +0,0 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Unpares Url's tests | ||
4 | */ | ||
5 | |||
6 | require_once 'application/Url.php'; | ||
7 | |||
8 | /** | ||
9 | * Unitary tests for unparse_url() | ||
10 | */ | ||
11 | class UnparseUrlTest extends PHPUnit_Framework_TestCase | ||
12 | { | ||
13 | /** | ||
14 | * Thanks for building nothing | ||
15 | */ | ||
16 | public function testUnparseEmptyArray() | ||
17 | { | ||
18 | $this->assertEquals('', unparse_url(array())); | ||
19 | } | ||
20 | |||
21 | /** | ||
22 | * Rebuild a full-featured URL | ||
23 | */ | ||
24 | public function testUnparseFull() | ||
25 | { | ||
26 | $ref = 'http://username:password@hostname:9090/path' | ||
27 | .'?arg1=value1&arg2=value2#anchor'; | ||
28 | $this->assertEquals($ref, unparse_url(parse_url($ref))); | ||
29 | } | ||
30 | } | ||
diff --git a/tests/Url/UrlTest.php b/tests/Url/UrlTest.php deleted file mode 100644 index db229ce0..00000000 --- a/tests/Url/UrlTest.php +++ /dev/null | |||
@@ -1,200 +0,0 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Url's tests | ||
4 | */ | ||
5 | |||
6 | require_once 'application/Url.php'; | ||
7 | |||
8 | /** | ||
9 | * Unitary tests for URL utilities | ||
10 | */ | ||
11 | class UrlTest extends PHPUnit_Framework_TestCase | ||
12 | { | ||
13 | // base URL for tests | ||
14 | protected static $baseUrl = 'http://domain.tld:3000'; | ||
15 | |||
16 | /** | ||
17 | * Helper method | ||
18 | */ | ||
19 | private function assertUrlIsCleaned($query = '', $fragment = '') | ||
20 | { | ||
21 | $url = new Url(self::$baseUrl.$query.$fragment); | ||
22 | $url->cleanup(); | ||
23 | $this->assertEquals(self::$baseUrl, $url->toString()); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Instantiate an empty URL | ||
28 | */ | ||
29 | public function testEmptyConstruct() | ||
30 | { | ||
31 | $url = new Url(''); | ||
32 | $this->assertEquals('', $url->toString()); | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * Instantiate a URL | ||
37 | */ | ||
38 | public function testConstruct() | ||
39 | { | ||
40 | $ref = 'http://username:password@hostname:9090/path' | ||
41 | .'?arg1=value1&arg2=value2#anchor'; | ||
42 | $url = new Url($ref); | ||
43 | $this->assertEquals($ref, $url->toString()); | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * URL cleanup - nothing to do | ||
48 | */ | ||
49 | public function testNoCleanup() | ||
50 | { | ||
51 | // URL with no query nor fragment | ||
52 | $this->assertUrlIsCleaned(); | ||
53 | |||
54 | // URL with no annoying elements | ||
55 | $ref = self::$baseUrl.'?p1=val1&p2=1234#edit'; | ||
56 | $url = new Url($ref); | ||
57 | $this->assertEquals($ref, $url->cleanup()); | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * URL cleanup - annoying fragment | ||
62 | */ | ||
63 | public function testCleanupFragment() | ||
64 | { | ||
65 | $this->assertUrlIsCleaned('', '#tk.rss_all'); | ||
66 | $this->assertUrlIsCleaned('', '#xtor=RSS-'); | ||
67 | $this->assertUrlIsCleaned('', '#xtor=RSS-U3ht0tkc4b'); | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * URL cleanup - single annoying query parameter | ||
72 | */ | ||
73 | public function testCleanupSingleQueryParam() | ||
74 | { | ||
75 | $this->assertUrlIsCleaned('?action_object_map=junk'); | ||
76 | $this->assertUrlIsCleaned('?action_ref_map=Cr4p!'); | ||
77 | $this->assertUrlIsCleaned('?action_type_map=g4R84g3'); | ||
78 | |||
79 | $this->assertUrlIsCleaned('?fb_stuff=v41u3'); | ||
80 | $this->assertUrlIsCleaned('?fb=71m3w4573'); | ||
81 | |||
82 | $this->assertUrlIsCleaned('?utm_campaign=zomg'); | ||
83 | $this->assertUrlIsCleaned('?utm_medium=numnum'); | ||
84 | $this->assertUrlIsCleaned('?utm_source=c0d3'); | ||
85 | $this->assertUrlIsCleaned('?utm_term=1n4l'); | ||
86 | |||
87 | $this->assertUrlIsCleaned('?xtor=some-url'); | ||
88 | $this->assertUrlIsCleaned('?PHPSESSID=012345678910111213'); | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * URL cleanup - multiple annoying query parameters | ||
93 | */ | ||
94 | public function testCleanupMultipleQueryParams() | ||
95 | { | ||
96 | $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng'); | ||
97 | $this->assertUrlIsCleaned( | ||
98 | '?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3' | ||
99 | ); | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * URL cleanup - multiple annoying query parameters, annoying fragment | ||
104 | */ | ||
105 | public function testCleanupMultipleQueryParamsAndFragment() | ||
106 | { | ||
107 | $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng', '#tk.rss_all'); | ||
108 | } | ||
109 | |||
110 | /** | ||
111 | * Nominal case - the URL contains both useful and annoying parameters | ||
112 | */ | ||
113 | public function testCleanupMixedContent() | ||
114 | { | ||
115 | // ditch annoying query params and fragment, keep useful params | ||
116 | $url = new Url( | ||
117 | self::$baseUrl | ||
118 | .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all' | ||
119 | ); | ||
120 | $this->assertEquals(self::$baseUrl.'?my=stuff&is=kept', $url->cleanup()); | ||
121 | |||
122 | |||
123 | // ditch annoying query params, keep useful params and fragment | ||
124 | $url = new Url( | ||
125 | self::$baseUrl | ||
126 | .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again' | ||
127 | ); | ||
128 | $this->assertEquals( | ||
129 | self::$baseUrl.'?my=stuff&is=kept#again', | ||
130 | $url->cleanup() | ||
131 | ); | ||
132 | |||
133 | // test firefox reader url | ||
134 | $url = new Url( | ||
135 | 'about://reader?url=' . urlencode(self::$baseUrl .'?my=stuff&is=kept') | ||
136 | ); | ||
137 | $this->assertEquals(self::$baseUrl.'?my=stuff&is=kept', $url->cleanup()); | ||
138 | } | ||
139 | |||
140 | /** | ||
141 | * Test default http scheme. | ||
142 | */ | ||
143 | public function testDefaultScheme() | ||
144 | { | ||
145 | $url = new Url(self::$baseUrl); | ||
146 | $this->assertEquals('http', $url->getScheme()); | ||
147 | $url = new Url('domain.tld'); | ||
148 | $this->assertEquals('http', $url->getScheme()); | ||
149 | $url = new Url('ssh://domain.tld'); | ||
150 | $this->assertEquals('ssh', $url->getScheme()); | ||
151 | $url = new Url('ftp://domain.tld'); | ||
152 | $this->assertEquals('ftp', $url->getScheme()); | ||
153 | $url = new Url('git://domain.tld/push?pull=clone#checkout'); | ||
154 | $this->assertEquals('git', $url->getScheme()); | ||
155 | } | ||
156 | |||
157 | /** | ||
158 | * Test add trailing slash. | ||
159 | */ | ||
160 | public function testAddTrailingSlash() | ||
161 | { | ||
162 | $strOn = 'http://randomstr.com/test/'; | ||
163 | $strOff = 'http://randomstr.com/test'; | ||
164 | $this->assertEquals($strOn, add_trailing_slash($strOn)); | ||
165 | $this->assertEquals($strOn, add_trailing_slash($strOff)); | ||
166 | } | ||
167 | |||
168 | /** | ||
169 | * Test valid HTTP url. | ||
170 | */ | ||
171 | public function testUrlIsHttp() | ||
172 | { | ||
173 | $url = new Url(self::$baseUrl); | ||
174 | $this->assertTrue($url->isHttp()); | ||
175 | } | ||
176 | |||
177 | /** | ||
178 | * Test non HTTP url. | ||
179 | */ | ||
180 | public function testUrlIsNotHttp() | ||
181 | { | ||
182 | $url = new Url('ftp://save.tld/mysave'); | ||
183 | $this->assertFalse($url->isHttp()); | ||
184 | } | ||
185 | |||
186 | /** | ||
187 | * Test International Domain Name to ASCII conversion | ||
188 | */ | ||
189 | public function testIdnToAscii() | ||
190 | { | ||
191 | $ind = 'http://www.académie-française.fr/'; | ||
192 | $expected = 'http://www.xn--acadmie-franaise-npb1a.fr/'; | ||
193 | $url = new Url($ind); | ||
194 | $this->assertEquals($expected, $url->idnToAscii()); | ||
195 | |||
196 | $notInd = 'http://www.academie-francaise.fr/'; | ||
197 | $url = new Url($notInd); | ||
198 | $this->assertEquals($notInd, $url->idnToAscii()); | ||
199 | } | ||
200 | } | ||
diff --git a/tests/Url/WhitelistProtocolsTest.php b/tests/Url/WhitelistProtocolsTest.php deleted file mode 100644 index a3156804..00000000 --- a/tests/Url/WhitelistProtocolsTest.php +++ /dev/null | |||
@@ -1,63 +0,0 @@ | |||
1 | <?php | ||
2 | |||
3 | require_once 'application/Url.php'; | ||
4 | |||
5 | use Shaarli\Config\ConfigManager; | ||
6 | |||
7 | /** | ||
8 | * Class WhitelistProtocolsTest | ||
9 | * | ||
10 | * Test whitelist_protocols() function of Url. | ||
11 | */ | ||
12 | class WhitelistProtocolsTest extends PHPUnit_Framework_TestCase | ||
13 | { | ||
14 | /** | ||
15 | * Test whitelist_protocols() on a note (relative URL). | ||
16 | */ | ||
17 | public function testWhitelistProtocolsRelative() | ||
18 | { | ||
19 | $whitelist = ['ftp', 'magnet']; | ||
20 | $url = '?12443564'; | ||
21 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
22 | $url = '/path.jpg'; | ||
23 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Test whitelist_protocols() on a note (relative URL). | ||
28 | */ | ||
29 | public function testWhitelistProtocolMissing() | ||
30 | { | ||
31 | $whitelist = ['ftp', 'magnet']; | ||
32 | $url = 'test.tld/path/?query=value#hash'; | ||
33 | $this->assertEquals('http://'. $url, whitelist_protocols($url, $whitelist)); | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * Test whitelist_protocols() with allowed protocols. | ||
38 | */ | ||
39 | public function testWhitelistAllowedProtocol() | ||
40 | { | ||
41 | $whitelist = ['ftp', 'magnet']; | ||
42 | $url = 'http://test.tld/path/?query=value#hash'; | ||
43 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
44 | $url = 'https://test.tld/path/?query=value#hash'; | ||
45 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
46 | $url = 'ftp://test.tld/path/?query=value#hash'; | ||
47 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
48 | $url = 'magnet:test.tld/path/?query=value#hash'; | ||
49 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * Test whitelist_protocols() with allowed protocols. | ||
54 | */ | ||
55 | public function testWhitelistDisallowedProtocol() | ||
56 | { | ||
57 | $whitelist = ['ftp', 'magnet']; | ||
58 | $url = 'javascript:alert("xss");'; | ||
59 | $this->assertEquals('http://alert("xss");', whitelist_protocols($url, $whitelist)); | ||
60 | $url = 'other://test.tld/path/?query=value#hash'; | ||
61 | $this->assertEquals('http://test.tld/path/?query=value#hash', whitelist_protocols($url, $whitelist)); | ||
62 | } | ||
63 | } | ||