diff options
author | Guillaume Virlet <github@virlet.org> | 2015-09-02 13:55:39 +0200 |
---|---|---|
committer | Guillaume Virlet <github@virlet.org> | 2015-09-08 22:00:37 +0200 |
commit | ef591e7ee21435da9314c5f7f6ea983c6f423898 (patch) | |
tree | a58c1bcaab33d42161b23d55c739737526ea17e9 | |
parent | 0a813cfd7cdf3c81faba8568bf6e2e667aae6f13 (diff) | |
download | Shaarli-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
-rw-r--r-- | application/LinkDB.php | 4 | ||||
-rwxr-xr-x | application/Url.php | 30 | ||||
-rwxr-xr-x | index.php | 9 | ||||
-rw-r--r-- | tests/Url/CleanupUrlTest.php | 76 | ||||
-rw-r--r-- | tests/Url/GetUrlSchemeTest.php | 31 | ||||
-rw-r--r-- | tests/Url/UnparseUrlTest.php | 31 | ||||
-rwxr-xr-x | tests/Url/UrlTest.php (renamed from tests/UrlTest.php) | 32 |
7 files changed, 179 insertions, 34 deletions
diff --git a/application/LinkDB.php b/application/LinkDB.php index 463aa47e..84733505 100644 --- a/application/LinkDB.php +++ b/application/LinkDB.php | |||
@@ -287,6 +287,10 @@ You use the community supported version of the original Shaarli project, by Seba | |||
287 | 287 | ||
288 | /** | 288 | /** |
289 | * Returns the link for a given URL, or False if it does not exist. | 289 | * Returns the link for a given URL, or False if it does not exist. |
290 | * | ||
291 | * @param string $url URL to search for | ||
292 | * | ||
293 | * @return mixed the existing link if it exists, else 'false' | ||
290 | */ | 294 | */ |
291 | public function getLinkFromUrl($url) | 295 | public function getLinkFromUrl($url) |
292 | { | 296 | { |
diff --git a/application/Url.php b/application/Url.php index 02a4395d..af43b457 100755 --- a/application/Url.php +++ b/application/Url.php | |||
@@ -26,6 +26,32 @@ function unparse_url($parsedUrl) | |||
26 | } | 26 | } |
27 | 27 | ||
28 | /** | 28 | /** |
29 | * Removes undesired query parameters and fragments | ||
30 | * | ||
31 | * @param string url Url to be cleaned | ||
32 | * | ||
33 | * @return string the string representation of this URL after cleanup | ||
34 | */ | ||
35 | function cleanup_url($url) | ||
36 | { | ||
37 | $obj_url = new Url($url); | ||
38 | return $obj_url->cleanup(); | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * Get URL scheme. | ||
43 | * | ||
44 | * @param string url Url for which the scheme is requested | ||
45 | * | ||
46 | * @return mixed the URL scheme or false if none is provided. | ||
47 | */ | ||
48 | function get_url_scheme($url) | ||
49 | { | ||
50 | $obj_url = new Url($url); | ||
51 | return $obj_url->getScheme(); | ||
52 | } | ||
53 | |||
54 | /** | ||
29 | * URL representation and cleanup utilities | 55 | * URL representation and cleanup utilities |
30 | * | 56 | * |
31 | * Form | 57 | * Form |
@@ -90,7 +116,7 @@ class Url | |||
90 | /** | 116 | /** |
91 | * Returns a string representation of this URL | 117 | * Returns a string representation of this URL |
92 | */ | 118 | */ |
93 | public function __toString() | 119 | public function toString() |
94 | { | 120 | { |
95 | return unparse_url($this->parts); | 121 | return unparse_url($this->parts); |
96 | } | 122 | } |
@@ -149,7 +175,7 @@ class Url | |||
149 | { | 175 | { |
150 | $this->cleanupQuery(); | 176 | $this->cleanupQuery(); |
151 | $this->cleanupFragment(); | 177 | $this->cleanupFragment(); |
152 | return $this->__toString(); | 178 | return $this->toString(); |
153 | } | 179 | } |
154 | 180 | ||
155 | /** | 181 | /** |
@@ -1454,12 +1454,11 @@ function renderPage() | |||
1454 | 1454 | ||
1455 | // -------- User want to post a new link: Display link edit form. | 1455 | // -------- User want to post a new link: Display link edit form. |
1456 | if (isset($_GET['post'])) { | 1456 | if (isset($_GET['post'])) { |
1457 | $url = new Url($_GET['post']); | 1457 | $url = cleanup_url($_GET['post']); |
1458 | $url->cleanup(); | ||
1459 | 1458 | ||
1460 | $link_is_new = false; | 1459 | $link_is_new = false; |
1461 | // Check if URL is not already in database (in this case, we will edit the existing link) | 1460 | // Check if URL is not already in database (in this case, we will edit the existing link) |
1462 | $link = $LINKSDB->getLinkFromUrl((string)$url); | 1461 | $link = $LINKSDB->getLinkFromUrl($url); |
1463 | if (!$link) | 1462 | if (!$link) |
1464 | { | 1463 | { |
1465 | $link_is_new = true; | 1464 | $link_is_new = true; |
@@ -1471,7 +1470,7 @@ function renderPage() | |||
1471 | $tags = (empty($_GET['tags']) ? '' : $_GET['tags'] ); | 1470 | $tags = (empty($_GET['tags']) ? '' : $_GET['tags'] ); |
1472 | $private = (!empty($_GET['private']) && $_GET['private'] === "1" ? 1 : 0); | 1471 | $private = (!empty($_GET['private']) && $_GET['private'] === "1" ? 1 : 0); |
1473 | // If this is an HTTP(S) link, we try go get the page to extract the title (otherwise we will to straight to the edit form.) | 1472 | // If this is an HTTP(S) link, we try go get the page to extract the title (otherwise we will to straight to the edit form.) |
1474 | if (empty($title) && strpos($url->getScheme(), 'http') !== false) { | 1473 | if (empty($title) && strpos(get_url_scheme($url), 'http') !== false) { |
1475 | // Short timeout to keep the application responsive | 1474 | // Short timeout to keep the application responsive |
1476 | list($headers, $data) = get_http_url($url, 4); | 1475 | list($headers, $data) = get_http_url($url, 4); |
1477 | // FIXME: Decode charset according to specified in either 1) HTTP response headers or 2) <head> in html | 1476 | // FIXME: Decode charset according to specified in either 1) HTTP response headers or 2) <head> in html |
@@ -1505,7 +1504,7 @@ function renderPage() | |||
1505 | $link = array( | 1504 | $link = array( |
1506 | 'linkdate' => $linkdate, | 1505 | 'linkdate' => $linkdate, |
1507 | 'title' => $title, | 1506 | 'title' => $title, |
1508 | 'url' => (string)$url, | 1507 | 'url' => $url, |
1509 | 'description' => $description, | 1508 | 'description' => $description, |
1510 | 'tags' => $tags, | 1509 | 'tags' => $tags, |
1511 | 'private' => $private | 1510 | 'private' => $private |
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 | |||
6 | require_once 'application/Url.php'; | ||
7 | |||
8 | class 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 | |||
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 | } | ||
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 | |||
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 | } | ||
31 | |||
diff --git a/tests/UrlTest.php b/tests/Url/UrlTest.php index c848e88e..e498d79e 100755 --- a/tests/UrlTest.php +++ b/tests/Url/UrlTest.php | |||
@@ -6,30 +6,6 @@ | |||
6 | require_once 'application/Url.php'; | 6 | require_once 'application/Url.php'; |
7 | 7 | ||
8 | /** | 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 | } | ||
31 | |||
32 | /** | ||
33 | * Unitary tests for URL utilities | 9 | * Unitary tests for URL utilities |
34 | */ | 10 | */ |
35 | class UrlTest extends PHPUnit_Framework_TestCase | 11 | class UrlTest extends PHPUnit_Framework_TestCase |
@@ -44,7 +20,7 @@ class UrlTest extends PHPUnit_Framework_TestCase | |||
44 | { | 20 | { |
45 | $url = new Url(self::$baseUrl.$query.$fragment); | 21 | $url = new Url(self::$baseUrl.$query.$fragment); |
46 | $url->cleanup(); | 22 | $url->cleanup(); |
47 | $this->assertEquals(self::$baseUrl, $url->__toString()); | 23 | $this->assertEquals(self::$baseUrl, $url->toString()); |
48 | } | 24 | } |
49 | 25 | ||
50 | /** | 26 | /** |
@@ -52,7 +28,8 @@ class UrlTest extends PHPUnit_Framework_TestCase | |||
52 | */ | 28 | */ |
53 | public function testEmptyConstruct() | 29 | public function testEmptyConstruct() |
54 | { | 30 | { |
55 | $this->assertEquals('', new Url('')); | 31 | $url = new Url(''); |
32 | $this->assertEquals('', $url->toString()); | ||
56 | } | 33 | } |
57 | 34 | ||
58 | /** | 35 | /** |
@@ -62,7 +39,8 @@ class UrlTest extends PHPUnit_Framework_TestCase | |||
62 | { | 39 | { |
63 | $ref = 'http://username:password@hostname:9090/path' | 40 | $ref = 'http://username:password@hostname:9090/path' |
64 | .'?arg1=value1&arg2=value2#anchor'; | 41 | .'?arg1=value1&arg2=value2#anchor'; |
65 | $this->assertEquals($ref, new Url($ref)); | 42 | $url = new Url($ref); |
43 | $this->assertEquals($ref, $url->toString()); | ||
66 | } | 44 | } |
67 | 45 | ||
68 | /** | 46 | /** |