aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Url
diff options
context:
space:
mode:
authorGuillaume Virlet <github@virlet.org>2015-09-02 13:55:39 +0200
committerGuillaume Virlet <github@virlet.org>2015-09-08 22:00:37 +0200
commitef591e7ee21435da9314c5f7f6ea983c6f423898 (patch)
treea58c1bcaab33d42161b23d55c739737526ea17e9 /tests/Url
parent0a813cfd7cdf3c81faba8568bf6e2e667aae6f13 (diff)
downloadShaarli-ef591e7ee21435da9314c5f7f6ea983c6f423898.tar.gz
Shaarli-ef591e7ee21435da9314c5f7f6ea983c6f423898.tar.zst
Shaarli-ef591e7ee21435da9314c5f7f6ea983c6f423898.zip
Url: introduce global helper functions for cleanup and scheme detection
Relates to #314 & #326 Additions: - add global `cleanup_url()` and `get_url_scheme()` functions Modifications: - replace `Url` usage in `index.php` by calls to global functions - fix `Url` tests not being run: PHPUnit expects a single test class per file - move classes to separate files
Diffstat (limited to 'tests/Url')
-rw-r--r--tests/Url/CleanupUrlTest.php76
-rw-r--r--tests/Url/GetUrlSchemeTest.php31
-rw-r--r--tests/Url/UnparseUrlTest.php31
-rwxr-xr-xtests/Url/UrlTest.php148
4 files changed, 286 insertions, 0 deletions
diff --git a/tests/Url/CleanupUrlTest.php b/tests/Url/CleanupUrlTest.php
new file mode 100644
index 00000000..ba9a0437
--- /dev/null
+++ b/tests/Url/CleanupUrlTest.php
@@ -0,0 +1,76 @@
1<?php
2/**
3 * Unitary tests for cleanup_url()
4 */
5
6require_once 'application/Url.php';
7
8class CleanupUrlTest extends PHPUnit_Framework_TestCase
9{
10 /**
11 * Clean empty UrlThanks for building nothing
12 */
13 public function testCleanupUrlEmpty()
14 {
15 $this->assertEquals('', cleanup_url(''));
16 }
17
18 /**
19 * Clean an already cleaned Url
20 */
21 public function testCleanupUrlAlreadyClean()
22 {
23 $ref = 'http://domain.tld:3000';
24 $this->assertEquals($ref, cleanup_url($ref));
25 $ref = $ref.'/path/to/dir/';
26 $this->assertEquals($ref, cleanup_url($ref));
27 }
28
29 /**
30 * Clean Url needing cleaning
31 */
32 public function testCleanupUrlNeedClean()
33 {
34 $ref = 'http://domain.tld:3000';
35 $this->assertEquals($ref, cleanup_url($ref.'#tk.rss_all'));
36 $this->assertEquals($ref, cleanup_url($ref.'#xtor=RSS-'));
37 $this->assertEquals($ref, cleanup_url($ref.'#xtor=RSS-U3ht0tkc4b'));
38 $this->assertEquals($ref, cleanup_url($ref.'?action_object_map=junk'));
39 $this->assertEquals($ref, cleanup_url($ref.'?action_ref_map=Cr4p!'));
40 $this->assertEquals($ref, cleanup_url($ref.'?action_type_map=g4R84g3'));
41
42 $this->assertEquals($ref, cleanup_url($ref.'?fb_stuff=v41u3'));
43 $this->assertEquals($ref, cleanup_url($ref.'?fb=71m3w4573'));
44
45 $this->assertEquals($ref, cleanup_url($ref.'?utm_campaign=zomg'));
46 $this->assertEquals($ref, cleanup_url($ref.'?utm_medium=numnum'));
47 $this->assertEquals($ref, cleanup_url($ref.'?utm_source=c0d3'));
48 $this->assertEquals($ref, cleanup_url($ref.'?utm_term=1n4l'));
49
50 $this->assertEquals($ref, cleanup_url($ref.'?xtor=some-url'));
51 $this->assertEquals($ref, cleanup_url($ref.'?xtor=some-url&fb=som3th1ng'));
52 $this->assertEquals($ref, cleanup_url(
53 $ref.'?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3'
54 ));
55 $this->assertEquals($ref, cleanup_url(
56 $ref.'?xtor=some-url&fb=som3th1ng#tk.rss_all'
57 ));
58
59 // ditch annoying query params and fragment, keep useful params
60 $this->assertEquals(
61 $ref.'?my=stuff&is=kept',
62 cleanup_url(
63 $ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
64 )
65 );
66
67 // ditch annoying query params, keep useful params and fragment
68 $this->assertEquals(
69 $ref.'?my=stuff&is=kept#again',
70 cleanup_url(
71 $ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
72 )
73 );
74 }
75}
76
diff --git a/tests/Url/GetUrlSchemeTest.php b/tests/Url/GetUrlSchemeTest.php
new file mode 100644
index 00000000..72d80b30
--- /dev/null
+++ b/tests/Url/GetUrlSchemeTest.php
@@ -0,0 +1,31 @@
1<?php
2/**
3 * Unitary tests for get_url_scheme()
4 */
5
6require_once 'application/Url.php';
7
8class 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}
31
diff --git a/tests/Url/UnparseUrlTest.php b/tests/Url/UnparseUrlTest.php
new file mode 100644
index 00000000..edde73e4
--- /dev/null
+++ b/tests/Url/UnparseUrlTest.php
@@ -0,0 +1,31 @@
1<?php
2/**
3 * Unpares 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
diff --git a/tests/Url/UrlTest.php b/tests/Url/UrlTest.php
new file mode 100755
index 00000000..e498d79e
--- /dev/null
+++ b/tests/Url/UrlTest.php
@@ -0,0 +1,148 @@
1<?php
2/**
3 * Url's tests
4 */
5
6require_once 'application/Url.php';
7
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();
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 }
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 }
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 }
148}