]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/http/UrlTest.php
Include empty basePath in formatting
[github/shaarli/Shaarli.git] / tests / http / UrlTest.php
CommitLineData
d9d776af
V
1<?php
2/**
51753e40 3 * UrlUtils's tests
d9d776af
V
4 */
5
fb1b182f
V
6namespace Shaarli\Http;
7
d9d776af
V
8/**
9 * Unitary tests for URL utilities
10 */
fb1b182f 11class UrlTest extends \PHPUnit\Framework\TestCase
d9d776af
V
12{
13 // base URL for tests
14 protected static $baseUrl = 'http://domain.tld:3000';
15
16 /**
17 * Helper method
18 */
067c2dd8 19 private function assertUrlIsCleaned($query = '', $fragment = '')
d9d776af 20 {
fb1b182f 21 $url = new Url(self::$baseUrl . $query . $fragment);
d9d776af 22 $url->cleanup();
ef591e7e 23 $this->assertEquals(self::$baseUrl, $url->toString());
d9d776af
V
24 }
25
26 /**
27 * Instantiate an empty URL
28 */
29 public function testEmptyConstruct()
30 {
ef591e7e
GV
31 $url = new Url('');
32 $this->assertEquals('', $url->toString());
d9d776af
V
33 }
34
35 /**
36 * Instantiate a URL
37 */
38 public function testConstruct()
39 {
40 $ref = 'http://username:password@hostname:9090/path'
fb1b182f 41 . '?arg1=value1&arg2=value2#anchor';
ef591e7e
GV
42 $url = new Url($ref);
43 $this->assertEquals($ref, $url->toString());
d9d776af
V
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
fb1b182f 55 $ref = self::$baseUrl . '?p1=val1&p2=1234#edit';
d9d776af
V
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');
725ca094 88 $this->assertUrlIsCleaned('?PHPSESSID=012345678910111213');
d9d776af
V
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
fb1b182f 118 . '?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
d9d776af 119 );
fb1b182f 120 $this->assertEquals(self::$baseUrl . '?my=stuff&is=kept', $url->cleanup());
d9d776af
V
121
122
123 // ditch annoying query params, keep useful params and fragment
124 $url = new Url(
125 self::$baseUrl
fb1b182f 126 . '?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
d9d776af
V
127 );
128 $this->assertEquals(
fb1b182f 129 self::$baseUrl . '?my=stuff&is=kept#again',
d9d776af
V
130 $url->cleanup()
131 );
c9da01e7
A
132
133 // test firefox reader url
134 $url = new Url(
fb1b182f 135 'about://reader?url=' . urlencode(self::$baseUrl . '?my=stuff&is=kept')
c9da01e7 136 );
fb1b182f 137 $this->assertEquals(self::$baseUrl . '?my=stuff&is=kept', $url->cleanup());
d9d776af 138 }
9e1724f1
A
139
140 /**
141 * Test default http scheme.
142 */
067c2dd8
V
143 public function testDefaultScheme()
144 {
9e1724f1
A
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 }
938d9cce
A
156
157 /**
158 * Test add trailing slash.
159 */
93b1fe54 160 public function testAddTrailingSlash()
938d9cce
A
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 }
1557cefb
A
167
168 /**
169 * Test valid HTTP url.
170 */
93b1fe54 171 public function testUrlIsHttp()
1557cefb
A
172 {
173 $url = new Url(self::$baseUrl);
174 $this->assertTrue($url->isHttp());
175 }
176
177 /**
178 * Test non HTTP url.
179 */
93b1fe54 180 public function testUrlIsNotHttp()
1557cefb
A
181 {
182 $url = new Url('ftp://save.tld/mysave');
183 $this->assertFalse($url->isHttp());
184 }
ce7b0b64
A
185
186 /**
1336a732 187 * Test International Domain Name to ASCII conversion
ce7b0b64 188 */
93b1fe54 189 public function testIdnToAscii()
ce7b0b64
A
190 {
191 $ind = 'http://www.académie-française.fr/';
192 $expected = 'http://www.xn--acadmie-franaise-npb1a.fr/';
193 $url = new Url($ind);
caa69b58 194 $this->assertEquals($expected, $url->idnToAscii());
ce7b0b64
A
195
196 $notInd = 'http://www.academie-francaise.fr/';
197 $url = new Url($notInd);
caa69b58 198 $this->assertEquals($notInd, $url->idnToAscii());
ce7b0b64 199 }
d9d776af 200}