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