diff options
Diffstat (limited to 'tests/bookmark/LinkUtilsTest.php')
-rw-r--r-- | tests/bookmark/LinkUtilsTest.php | 506 |
1 files changed, 506 insertions, 0 deletions
diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php new file mode 100644 index 00000000..78cb8f2a --- /dev/null +++ b/tests/bookmark/LinkUtilsTest.php | |||
@@ -0,0 +1,506 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Bookmark; | ||
4 | |||
5 | use PHPUnit\Framework\TestCase; | ||
6 | use ReferenceLinkDB; | ||
7 | use Shaarli\Config\ConfigManager; | ||
8 | |||
9 | require_once 'tests/utils/CurlUtils.php'; | ||
10 | |||
11 | /** | ||
12 | * Class LinkUtilsTest. | ||
13 | */ | ||
14 | class LinkUtilsTest extends TestCase | ||
15 | { | ||
16 | /** | ||
17 | * Test html_extract_title() when the title is found. | ||
18 | */ | ||
19 | public function testHtmlExtractExistentTitle() | ||
20 | { | ||
21 | $title = 'Read me please.'; | ||
22 | $html = '<html><meta>stuff</meta><title>' . $title . '</title></html>'; | ||
23 | $this->assertEquals($title, html_extract_title($html)); | ||
24 | $html = '<html><title>' . $title . '</title>blabla<title>another</title></html>'; | ||
25 | $this->assertEquals($title, html_extract_title($html)); | ||
26 | } | ||
27 | |||
28 | /** | ||
29 | * Test html_extract_title() when the title is not found. | ||
30 | */ | ||
31 | public function testHtmlExtractNonExistentTitle() | ||
32 | { | ||
33 | $html = '<html><meta>stuff</meta></html>'; | ||
34 | $this->assertFalse(html_extract_title($html)); | ||
35 | } | ||
36 | |||
37 | /** | ||
38 | * Test headers_extract_charset() when the charset is found. | ||
39 | */ | ||
40 | public function testHeadersExtractExistentCharset() | ||
41 | { | ||
42 | $charset = 'x-MacCroatian'; | ||
43 | $headers = 'text/html; charset=' . $charset; | ||
44 | $this->assertEquals(strtolower($charset), header_extract_charset($headers)); | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * Test headers_extract_charset() when the charset is not found. | ||
49 | */ | ||
50 | public function testHeadersExtractNonExistentCharset() | ||
51 | { | ||
52 | $headers = ''; | ||
53 | $this->assertFalse(header_extract_charset($headers)); | ||
54 | |||
55 | $headers = 'text/html'; | ||
56 | $this->assertFalse(header_extract_charset($headers)); | ||
57 | } | ||
58 | |||
59 | /** | ||
60 | * Test html_extract_charset() when the charset is found. | ||
61 | */ | ||
62 | public function testHtmlExtractExistentCharset() | ||
63 | { | ||
64 | $charset = 'x-MacCroatian'; | ||
65 | $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>'; | ||
66 | $this->assertEquals(strtolower($charset), html_extract_charset($html)); | ||
67 | } | ||
68 | |||
69 | /** | ||
70 | * Test html_extract_charset() when the charset is not found. | ||
71 | */ | ||
72 | public function testHtmlExtractNonExistentCharset() | ||
73 | { | ||
74 | $html = '<html><meta>stuff</meta></html>'; | ||
75 | $this->assertFalse(html_extract_charset($html)); | ||
76 | $html = '<html><meta>stuff</meta><meta charset=""/></html>'; | ||
77 | $this->assertFalse(html_extract_charset($html)); | ||
78 | } | ||
79 | |||
80 | /** | ||
81 | * Test html_extract_tag() when the tag <meta name= is found. | ||
82 | */ | ||
83 | public function testHtmlExtractExistentNameTag() | ||
84 | { | ||
85 | $description = 'Bob and Alice share cookies.'; | ||
86 | $html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>'; | ||
87 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Test html_extract_tag() when the tag <meta name= is not found. | ||
92 | */ | ||
93 | public function testHtmlExtractNonExistentNameTag() | ||
94 | { | ||
95 | $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>'; | ||
96 | $this->assertFalse(html_extract_tag('description', $html)); | ||
97 | } | ||
98 | |||
99 | /** | ||
100 | * Test html_extract_tag() when the tag <meta property="og: is found. | ||
101 | */ | ||
102 | public function testHtmlExtractExistentOgTag() | ||
103 | { | ||
104 | $description = 'Bob and Alice share cookies.'; | ||
105 | $html = '<html><meta>stuff2</meta><meta property="og:description" content="' . $description . '"/></html>'; | ||
106 | $this->assertEquals($description, html_extract_tag('description', $html)); | ||
107 | } | ||
108 | |||
109 | /** | ||
110 | * Test html_extract_tag() when the tag <meta property="og: is not found. | ||
111 | */ | ||
112 | public function testHtmlExtractNonExistentOgTag() | ||
113 | { | ||
114 | $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>'; | ||
115 | $this->assertFalse(html_extract_tag('description', $html)); | ||
116 | } | ||
117 | |||
118 | /** | ||
119 | * Test the download callback with valid value | ||
120 | */ | ||
121 | public function testCurlDownloadCallbackOk() | ||
122 | { | ||
123 | $callback = get_curl_download_callback( | ||
124 | $charset, | ||
125 | $title, | ||
126 | $desc, | ||
127 | $keywords, | ||
128 | false, | ||
129 | 'ut_curl_getinfo_ok' | ||
130 | ); | ||
131 | $data = [ | ||
132 | 'HTTP/1.1 200 OK', | ||
133 | 'Server: GitHub.com', | ||
134 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
135 | 'Content-Type: text/html; charset=utf-8', | ||
136 | 'Status: 200 OK', | ||
137 | 'end' => 'th=device-width">' | ||
138 | . '<title>Refactoring · GitHub</title>' | ||
139 | . '<link rel="search" type="application/opensea', | ||
140 | '<title>ignored</title>' | ||
141 | . '<meta name="description" content="desc" />' | ||
142 | . '<meta name="keywords" content="key1,key2" />', | ||
143 | ]; | ||
144 | foreach ($data as $key => $line) { | ||
145 | $ignore = null; | ||
146 | $expected = $key !== 'end' ? strlen($line) : false; | ||
147 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
148 | if ($expected === false) { | ||
149 | break; | ||
150 | } | ||
151 | } | ||
152 | $this->assertEquals('utf-8', $charset); | ||
153 | $this->assertEquals('Refactoring · GitHub', $title); | ||
154 | $this->assertEmpty($desc); | ||
155 | $this->assertEmpty($keywords); | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * Test the download callback with valid values and no charset | ||
160 | */ | ||
161 | public function testCurlDownloadCallbackOkNoCharset() | ||
162 | { | ||
163 | $callback = get_curl_download_callback( | ||
164 | $charset, | ||
165 | $title, | ||
166 | $desc, | ||
167 | $keywords, | ||
168 | false, | ||
169 | 'ut_curl_getinfo_no_charset' | ||
170 | ); | ||
171 | $data = [ | ||
172 | 'HTTP/1.1 200 OK', | ||
173 | 'end' => 'th=device-width">' | ||
174 | . '<title>Refactoring · GitHub</title>' | ||
175 | . '<link rel="search" type="application/opensea', | ||
176 | '<title>ignored</title>' | ||
177 | . '<meta name="description" content="desc" />' | ||
178 | . '<meta name="keywords" content="key1,key2" />', | ||
179 | ]; | ||
180 | foreach ($data as $key => $line) { | ||
181 | $ignore = null; | ||
182 | $this->assertEquals(strlen($line), $callback($ignore, $line)); | ||
183 | } | ||
184 | $this->assertEmpty($charset); | ||
185 | $this->assertEquals('Refactoring · GitHub', $title); | ||
186 | $this->assertEmpty($desc); | ||
187 | $this->assertEmpty($keywords); | ||
188 | } | ||
189 | |||
190 | /** | ||
191 | * Test the download callback with valid values and no charset | ||
192 | */ | ||
193 | public function testCurlDownloadCallbackOkHtmlCharset() | ||
194 | { | ||
195 | $callback = get_curl_download_callback( | ||
196 | $charset, | ||
197 | $title, | ||
198 | $desc, | ||
199 | $keywords, | ||
200 | false, | ||
201 | 'ut_curl_getinfo_no_charset' | ||
202 | ); | ||
203 | $data = [ | ||
204 | 'HTTP/1.1 200 OK', | ||
205 | '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', | ||
206 | 'end' => 'th=device-width">' | ||
207 | . '<title>Refactoring · GitHub</title>' | ||
208 | . '<link rel="search" type="application/opensea', | ||
209 | '<title>ignored</title>' | ||
210 | . '<meta name="description" content="desc" />' | ||
211 | . '<meta name="keywords" content="key1,key2" />', | ||
212 | ]; | ||
213 | foreach ($data as $key => $line) { | ||
214 | $ignore = null; | ||
215 | $expected = $key !== 'end' ? strlen($line) : false; | ||
216 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
217 | if ($expected === false) { | ||
218 | break; | ||
219 | } | ||
220 | } | ||
221 | $this->assertEquals('utf-8', $charset); | ||
222 | $this->assertEquals('Refactoring · GitHub', $title); | ||
223 | $this->assertEmpty($desc); | ||
224 | $this->assertEmpty($keywords); | ||
225 | } | ||
226 | |||
227 | /** | ||
228 | * Test the download callback with valid values and no title | ||
229 | */ | ||
230 | public function testCurlDownloadCallbackOkNoTitle() | ||
231 | { | ||
232 | $callback = get_curl_download_callback( | ||
233 | $charset, | ||
234 | $title, | ||
235 | $desc, | ||
236 | $keywords, | ||
237 | false, | ||
238 | 'ut_curl_getinfo_ok' | ||
239 | ); | ||
240 | $data = [ | ||
241 | 'HTTP/1.1 200 OK', | ||
242 | 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea', | ||
243 | 'ignored', | ||
244 | ]; | ||
245 | foreach ($data as $key => $line) { | ||
246 | $ignore = null; | ||
247 | $this->assertEquals(strlen($line), $callback($ignore, $line)); | ||
248 | } | ||
249 | $this->assertEquals('utf-8', $charset); | ||
250 | $this->assertEmpty($title); | ||
251 | $this->assertEmpty($desc); | ||
252 | $this->assertEmpty($keywords); | ||
253 | } | ||
254 | |||
255 | /** | ||
256 | * Test the download callback with an invalid content type. | ||
257 | */ | ||
258 | public function testCurlDownloadCallbackInvalidContentType() | ||
259 | { | ||
260 | $callback = get_curl_download_callback( | ||
261 | $charset, | ||
262 | $title, | ||
263 | $desc, | ||
264 | $keywords, | ||
265 | false, | ||
266 | 'ut_curl_getinfo_ct_ko' | ||
267 | ); | ||
268 | $ignore = null; | ||
269 | $this->assertFalse($callback($ignore, '')); | ||
270 | $this->assertEmpty($charset); | ||
271 | $this->assertEmpty($title); | ||
272 | } | ||
273 | |||
274 | /** | ||
275 | * Test the download callback with an invalid response code. | ||
276 | */ | ||
277 | public function testCurlDownloadCallbackInvalidResponseCode() | ||
278 | { | ||
279 | $callback = $callback = get_curl_download_callback( | ||
280 | $charset, | ||
281 | $title, | ||
282 | $desc, | ||
283 | $keywords, | ||
284 | false, | ||
285 | 'ut_curl_getinfo_rc_ko' | ||
286 | ); | ||
287 | $ignore = null; | ||
288 | $this->assertFalse($callback($ignore, '')); | ||
289 | $this->assertEmpty($charset); | ||
290 | $this->assertEmpty($title); | ||
291 | } | ||
292 | |||
293 | /** | ||
294 | * Test the download callback with an invalid content type and response code. | ||
295 | */ | ||
296 | public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode() | ||
297 | { | ||
298 | $callback = $callback = get_curl_download_callback( | ||
299 | $charset, | ||
300 | $title, | ||
301 | $desc, | ||
302 | $keywords, | ||
303 | false, | ||
304 | 'ut_curl_getinfo_rs_ct_ko' | ||
305 | ); | ||
306 | $ignore = null; | ||
307 | $this->assertFalse($callback($ignore, '')); | ||
308 | $this->assertEmpty($charset); | ||
309 | $this->assertEmpty($title); | ||
310 | } | ||
311 | |||
312 | /** | ||
313 | * Test the download callback with valid value, and retrieve_description option enabled. | ||
314 | */ | ||
315 | public function testCurlDownloadCallbackOkWithDesc() | ||
316 | { | ||
317 | $callback = get_curl_download_callback( | ||
318 | $charset, | ||
319 | $title, | ||
320 | $desc, | ||
321 | $keywords, | ||
322 | true, | ||
323 | 'ut_curl_getinfo_ok' | ||
324 | ); | ||
325 | $data = [ | ||
326 | 'HTTP/1.1 200 OK', | ||
327 | 'Server: GitHub.com', | ||
328 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
329 | 'Content-Type: text/html; charset=utf-8', | ||
330 | 'Status: 200 OK', | ||
331 | 'th=device-width">' | ||
332 | . '<title>Refactoring · GitHub</title>' | ||
333 | . '<link rel="search" type="application/opensea', | ||
334 | 'end' => '<title>ignored</title>' | ||
335 | . '<meta name="description" content="link desc" />' | ||
336 | . '<meta name="keywords" content="key1,key2" />', | ||
337 | ]; | ||
338 | foreach ($data as $key => $line) { | ||
339 | $ignore = null; | ||
340 | $expected = $key !== 'end' ? strlen($line) : false; | ||
341 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
342 | if ($expected === false) { | ||
343 | break; | ||
344 | } | ||
345 | } | ||
346 | $this->assertEquals('utf-8', $charset); | ||
347 | $this->assertEquals('Refactoring · GitHub', $title); | ||
348 | $this->assertEquals('link desc', $desc); | ||
349 | $this->assertEquals('key1 key2', $keywords); | ||
350 | } | ||
351 | |||
352 | /** | ||
353 | * Test the download callback with valid value, and retrieve_description option enabled, | ||
354 | * but no desc or keyword defined in the page. | ||
355 | */ | ||
356 | public function testCurlDownloadCallbackOkWithDescNotFound() | ||
357 | { | ||
358 | $callback = get_curl_download_callback( | ||
359 | $charset, | ||
360 | $title, | ||
361 | $desc, | ||
362 | $keywords, | ||
363 | true, | ||
364 | 'ut_curl_getinfo_ok' | ||
365 | ); | ||
366 | $data = [ | ||
367 | 'HTTP/1.1 200 OK', | ||
368 | 'Server: GitHub.com', | ||
369 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
370 | 'Content-Type: text/html; charset=utf-8', | ||
371 | 'Status: 200 OK', | ||
372 | 'th=device-width">' | ||
373 | . '<title>Refactoring · GitHub</title>' | ||
374 | . '<link rel="search" type="application/opensea', | ||
375 | 'end' => '<title>ignored</title>', | ||
376 | ]; | ||
377 | foreach ($data as $key => $line) { | ||
378 | $ignore = null; | ||
379 | $expected = $key !== 'end' ? strlen($line) : false; | ||
380 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
381 | if ($expected === false) { | ||
382 | break; | ||
383 | } | ||
384 | } | ||
385 | $this->assertEquals('utf-8', $charset); | ||
386 | $this->assertEquals('Refactoring · GitHub', $title); | ||
387 | $this->assertEmpty($desc); | ||
388 | $this->assertEmpty($keywords); | ||
389 | } | ||
390 | |||
391 | /** | ||
392 | * Test count_private. | ||
393 | */ | ||
394 | public function testCountPrivateLinks() | ||
395 | { | ||
396 | $refDB = new ReferenceLinkDB(); | ||
397 | $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks())); | ||
398 | } | ||
399 | |||
400 | /** | ||
401 | * Test text2clickable. | ||
402 | */ | ||
403 | public function testText2clickable() | ||
404 | { | ||
405 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | ||
406 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here">' | ||
407 | . 'http://hello.there/is=someone#here</a> otherstuff'; | ||
408 | $processedText = text2clickable($text); | ||
409 | $this->assertEquals($expectedText, $processedText); | ||
410 | |||
411 | $text = 'stuff http://hello.there/is=someone#here(please) otherstuff'; | ||
412 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">' | ||
413 | . 'http://hello.there/is=someone#here(please)</a> otherstuff'; | ||
414 | $processedText = text2clickable($text); | ||
415 | $this->assertEquals($expectedText, $processedText); | ||
416 | |||
417 | $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; | ||
418 | $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; | ||
419 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">' | ||
420 | . 'http://hello.there/is=someone#here(please)&no</a> otherstuff'; | ||
421 | $processedText = text2clickable($text); | ||
422 | $this->assertEquals($expectedText, $processedText); | ||
423 | } | ||
424 | |||
425 | /** | ||
426 | * Test testSpace2nbsp. | ||
427 | */ | ||
428 | public function testSpace2nbsp() | ||
429 | { | ||
430 | $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?'; | ||
431 | $expectedText = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?'; | ||
432 | $processedText = space2nbsp($text); | ||
433 | $this->assertEquals($expectedText, $processedText); | ||
434 | } | ||
435 | |||
436 | /** | ||
437 | * Test hashtags auto-link. | ||
438 | */ | ||
439 | public function testHashtagAutolink() | ||
440 | { | ||
441 | $index = 'http://domain.tld/'; | ||
442 | $rawDescription = '#hashtag\n | ||
443 | # nothashtag\n | ||
444 | test#nothashtag #hashtag \#nothashtag\n | ||
445 | test #hashtag #hashtag test #hashtag.test\n | ||
446 | #hashtag #hashtag-nothashtag #hashtag_hashtag\n | ||
447 | What is #ашок anyway?\n | ||
448 | カタカナ #カタカナ」カタカナ\n'; | ||
449 | $autolinkedDescription = hashtag_autolink($rawDescription, $index); | ||
450 | |||
451 | $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription); | ||
452 | $this->assertNotContains(' #hashtag', $autolinkedDescription); | ||
453 | $this->assertNotContains('>#nothashtag', $autolinkedDescription); | ||
454 | $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription); | ||
455 | $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription); | ||
456 | $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription); | ||
457 | $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription); | ||
458 | } | ||
459 | |||
460 | /** | ||
461 | * Test hashtags auto-link without index URL. | ||
462 | */ | ||
463 | public function testHashtagAutolinkNoIndex() | ||
464 | { | ||
465 | $rawDescription = 'blabla #hashtag x#nothashtag'; | ||
466 | $autolinkedDescription = hashtag_autolink($rawDescription); | ||
467 | |||
468 | $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription); | ||
469 | $this->assertNotContains(' #hashtag', $autolinkedDescription); | ||
470 | $this->assertNotContains('>#nothashtag', $autolinkedDescription); | ||
471 | } | ||
472 | |||
473 | /** | ||
474 | * Test is_note with note URLs. | ||
475 | */ | ||
476 | public function testIsNote() | ||
477 | { | ||
478 | $this->assertTrue(is_note('?')); | ||
479 | $this->assertTrue(is_note('?abcDEf')); | ||
480 | $this->assertTrue(is_note('?_abcDEf#123')); | ||
481 | } | ||
482 | |||
483 | /** | ||
484 | * Test is_note with non note URLs. | ||
485 | */ | ||
486 | public function testIsNotNote() | ||
487 | { | ||
488 | $this->assertFalse(is_note('')); | ||
489 | $this->assertFalse(is_note('nope')); | ||
490 | $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi')); | ||
491 | } | ||
492 | |||
493 | /** | ||
494 | * Util function to build an hashtag link. | ||
495 | * | ||
496 | * @param string $hashtag Hashtag name. | ||
497 | * @param string $index Index URL. | ||
498 | * | ||
499 | * @return string HTML hashtag link. | ||
500 | */ | ||
501 | private function getHashtagLink($hashtag, $index = '') | ||
502 | { | ||
503 | $hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>'; | ||
504 | return str_replace('$1', $hashtag, $hashtagLink); | ||
505 | } | ||
506 | } | ||