]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/UrlTest.php
Links: refactor & improve URL cleanup
[github/shaarli/Shaarli.git] / tests / UrlTest.php
CommitLineData
d9d776af
V
1<?php
2/**
3 * Url's tests
4 */
5
6require_once 'application/Url.php';
7
8/**
9 * Unitary tests for unparse_url()
10 */
11class 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}
31
32/**
33 * Unitary tests for URL utilities
34 */
35class UrlTest extends PHPUnit_Framework_TestCase
36{
37 // base URL for tests
38 protected static $baseUrl = 'http://domain.tld:3000';
39
40 /**
41 * Helper method
42 */
43 private function assertUrlIsCleaned($query='', $fragment='')
44 {
45 $url = new Url(self::$baseUrl.$query.$fragment);
46 $url->cleanup();
47 $this->assertEquals(self::$baseUrl, $url->__toString());
48 }
49
50 /**
51 * Instantiate an empty URL
52 */
53 public function testEmptyConstruct()
54 {
55 $this->assertEquals('', new Url(''));
56 }
57
58 /**
59 * Instantiate a URL
60 */
61 public function testConstruct()
62 {
63 $ref = 'http://username:password@hostname:9090/path'
64 .'?arg1=value1&arg2=value2#anchor';
65 $this->assertEquals($ref, new Url($ref));
66 }
67
68 /**
69 * URL cleanup - nothing to do
70 */
71 public function testNoCleanup()
72 {
73 // URL with no query nor fragment
74 $this->assertUrlIsCleaned();
75
76 // URL with no annoying elements
77 $ref = self::$baseUrl.'?p1=val1&p2=1234#edit';
78 $url = new Url($ref);
79 $this->assertEquals($ref, $url->cleanup());
80 }
81
82 /**
83 * URL cleanup - annoying fragment
84 */
85 public function testCleanupFragment()
86 {
87 $this->assertUrlIsCleaned('', '#tk.rss_all');
88 $this->assertUrlIsCleaned('', '#xtor=RSS-');
89 $this->assertUrlIsCleaned('', '#xtor=RSS-U3ht0tkc4b');
90 }
91
92 /**
93 * URL cleanup - single annoying query parameter
94 */
95 public function testCleanupSingleQueryParam()
96 {
97 $this->assertUrlIsCleaned('?action_object_map=junk');
98 $this->assertUrlIsCleaned('?action_ref_map=Cr4p!');
99 $this->assertUrlIsCleaned('?action_type_map=g4R84g3');
100
101 $this->assertUrlIsCleaned('?fb_stuff=v41u3');
102 $this->assertUrlIsCleaned('?fb=71m3w4573');
103
104 $this->assertUrlIsCleaned('?utm_campaign=zomg');
105 $this->assertUrlIsCleaned('?utm_medium=numnum');
106 $this->assertUrlIsCleaned('?utm_source=c0d3');
107 $this->assertUrlIsCleaned('?utm_term=1n4l');
108
109 $this->assertUrlIsCleaned('?xtor=some-url');
110 }
111
112 /**
113 * URL cleanup - multiple annoying query parameters
114 */
115 public function testCleanupMultipleQueryParams()
116 {
117 $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng');
118 $this->assertUrlIsCleaned(
119 '?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3'
120 );
121 }
122
123 /**
124 * URL cleanup - multiple annoying query parameters, annoying fragment
125 */
126 public function testCleanupMultipleQueryParamsAndFragment()
127 {
128 $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng', '#tk.rss_all');
129 }
130
131 /**
132 * Nominal case - the URL contains both useful and annoying parameters
133 */
134 public function testCleanupMixedContent()
135 {
136 // ditch annoying query params and fragment, keep useful params
137 $url = new Url(
138 self::$baseUrl
139 .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
140 );
141 $this->assertEquals(self::$baseUrl.'?my=stuff&is=kept', $url->cleanup());
142
143
144 // ditch annoying query params, keep useful params and fragment
145 $url = new Url(
146 self::$baseUrl
147 .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
148 );
149 $this->assertEquals(
150 self::$baseUrl.'?my=stuff&is=kept#again',
151 $url->cleanup()
152 );
153 }
154}