]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/Url/UrlTest.php
Add exclusion in tag search
[github/shaarli/Shaarli.git] / tests / Url / UrlTest.php
CommitLineData
d9d776af
V
1<?php
2/**
3 * Url's tests
4 */
5
6require_once 'application/Url.php';
7
d9d776af
V
8/**
9 * Unitary tests for URL utilities
10 */
11class 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();
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'
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
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 }
89
90 /**
91 * URL cleanup - multiple annoying query parameters
92 */
93 public function testCleanupMultipleQueryParams()
94 {
95 $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng');
96 $this->assertUrlIsCleaned(
97 '?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3'
98 );
99 }
100
101 /**
102 * URL cleanup - multiple annoying query parameters, annoying fragment
103 */
104 public function testCleanupMultipleQueryParamsAndFragment()
105 {
106 $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng', '#tk.rss_all');
107 }
108
109 /**
110 * Nominal case - the URL contains both useful and annoying parameters
111 */
112 public function testCleanupMixedContent()
113 {
114 // ditch annoying query params and fragment, keep useful params
115 $url = new Url(
116 self::$baseUrl
117 .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
118 );
119 $this->assertEquals(self::$baseUrl.'?my=stuff&is=kept', $url->cleanup());
120
121
122 // ditch annoying query params, keep useful params and fragment
123 $url = new Url(
124 self::$baseUrl
125 .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
126 );
127 $this->assertEquals(
128 self::$baseUrl.'?my=stuff&is=kept#again',
129 $url->cleanup()
130 );
131 }
9e1724f1
A
132
133 /**
134 * Test default http scheme.
135 */
136 public function testDefaultScheme() {
137 $url = new Url(self::$baseUrl);
138 $this->assertEquals('http', $url->getScheme());
139 $url = new Url('domain.tld');
140 $this->assertEquals('http', $url->getScheme());
141 $url = new Url('ssh://domain.tld');
142 $this->assertEquals('ssh', $url->getScheme());
143 $url = new Url('ftp://domain.tld');
144 $this->assertEquals('ftp', $url->getScheme());
145 $url = new Url('git://domain.tld/push?pull=clone#checkout');
146 $this->assertEquals('git', $url->getScheme());
147 }
938d9cce
A
148
149 /**
150 * Test add trailing slash.
151 */
152 function testAddTrailingSlash()
153 {
154 $strOn = 'http://randomstr.com/test/';
155 $strOff = 'http://randomstr.com/test';
156 $this->assertEquals($strOn, add_trailing_slash($strOn));
157 $this->assertEquals($strOn, add_trailing_slash($strOff));
158 }
1557cefb
A
159
160 /**
161 * Test valid HTTP url.
162 */
163 function testUrlIsHttp()
164 {
165 $url = new Url(self::$baseUrl);
166 $this->assertTrue($url->isHttp());
167 }
168
169 /**
170 * Test non HTTP url.
171 */
172 function testUrlIsNotHttp()
173 {
174 $url = new Url('ftp://save.tld/mysave');
175 $this->assertFalse($url->isHttp());
176 }
d9d776af 177}