]>
Commit | Line | Data |
---|---|---|
1557cefb A |
1 | <?php |
2 | ||
fe3713d2 V |
3 | namespace Shaarli\Bookmark; |
4 | ||
6a487252 | 5 | use PHPUnit\Framework\TestCase; |
fe3713d2 | 6 | use ReferenceLinkDB; |
6a487252 | 7 | use Shaarli\Config\ConfigManager; |
fe3713d2 V |
8 | |
9 | require_once 'tests/utils/CurlUtils.php'; | |
1557cefb A |
10 | |
11 | /** | |
fe3713d2 V |
12 | * Class LinkUtilsTest. |
13 | */ | |
6a487252 | 14 | class LinkUtilsTest extends TestCase |
1557cefb A |
15 | { |
16 | /** | |
17 | * Test html_extract_title() when the title is found. | |
18 | */ | |
19 | public function testHtmlExtractExistentTitle() | |
20 | { | |
21 | $title = 'Read me please.'; | |
fe3713d2 | 22 | $html = '<html><meta>stuff</meta><title>' . $title . '</title></html>'; |
1557cefb | 23 | $this->assertEquals($title, html_extract_title($html)); |
fe3713d2 | 24 | $html = '<html><title>' . $title . '</title>blabla<title>another</title></html>'; |
68ea1d2b | 25 | $this->assertEquals($title, html_extract_title($html)); |
1557cefb A |
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 | ||
1557cefb A |
37 | /** |
38 | * Test headers_extract_charset() when the charset is found. | |
39 | */ | |
40 | public function testHeadersExtractExistentCharset() | |
41 | { | |
42 | $charset = 'x-MacCroatian'; | |
fe3713d2 | 43 | $headers = 'text/html; charset=' . $charset; |
d65342e3 | 44 | $this->assertEquals(strtolower($charset), header_extract_charset($headers)); |
1557cefb A |
45 | } |
46 | ||
47 | /** | |
48 | * Test headers_extract_charset() when the charset is not found. | |
49 | */ | |
50 | public function testHeadersExtractNonExistentCharset() | |
51 | { | |
d65342e3 A |
52 | $headers = ''; |
53 | $this->assertFalse(header_extract_charset($headers)); | |
1557cefb | 54 | |
d65342e3 A |
55 | $headers = 'text/html'; |
56 | $this->assertFalse(header_extract_charset($headers)); | |
1557cefb A |
57 | } |
58 | ||
59 | /** | |
60 | * Test html_extract_charset() when the charset is found. | |
61 | */ | |
62 | public function testHtmlExtractExistentCharset() | |
63 | { | |
64 | $charset = 'x-MacCroatian'; | |
fe3713d2 | 65 | $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>'; |
1557cefb A |
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 | } | |
141a86c5 | 79 | |
6a487252 A |
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 | ||
d65342e3 A |
118 | /** |
119 | * Test the download callback with valid value | |
120 | */ | |
121 | public function testCurlDownloadCallbackOk() | |
122 | { | |
6a487252 A |
123 | $callback = get_curl_download_callback( |
124 | $charset, | |
125 | $title, | |
126 | $desc, | |
127 | $keywords, | |
128 | false, | |
129 | 'ut_curl_getinfo_ok' | |
130 | ); | |
d65342e3 A |
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', | |
9d9f6d75 | 137 | 'end' => 'th=device-width">' |
fe3713d2 V |
138 | . '<title>Refactoring · GitHub</title>' |
139 | . '<link rel="search" type="application/opensea', | |
6a487252 A |
140 | '<title>ignored</title>' |
141 | . '<meta name="description" content="desc" />' | |
142 | . '<meta name="keywords" content="key1,key2" />', | |
d65342e3 A |
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); | |
6a487252 A |
154 | $this->assertEmpty($desc); |
155 | $this->assertEmpty($keywords); | |
d65342e3 A |
156 | } |
157 | ||
158 | /** | |
159 | * Test the download callback with valid values and no charset | |
160 | */ | |
161 | public function testCurlDownloadCallbackOkNoCharset() | |
162 | { | |
6a487252 A |
163 | $callback = get_curl_download_callback( |
164 | $charset, | |
165 | $title, | |
166 | $desc, | |
167 | $keywords, | |
168 | false, | |
169 | 'ut_curl_getinfo_no_charset' | |
170 | ); | |
d65342e3 A |
171 | $data = [ |
172 | 'HTTP/1.1 200 OK', | |
9d9f6d75 | 173 | 'end' => 'th=device-width">' |
fe3713d2 V |
174 | . '<title>Refactoring · GitHub</title>' |
175 | . '<link rel="search" type="application/opensea', | |
6a487252 A |
176 | '<title>ignored</title>' |
177 | . '<meta name="description" content="desc" />' | |
178 | . '<meta name="keywords" content="key1,key2" />', | |
d65342e3 A |
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); | |
6a487252 A |
186 | $this->assertEmpty($desc); |
187 | $this->assertEmpty($keywords); | |
d65342e3 A |
188 | } |
189 | ||
190 | /** | |
191 | * Test the download callback with valid values and no charset | |
192 | */ | |
193 | public function testCurlDownloadCallbackOkHtmlCharset() | |
194 | { | |
6a487252 A |
195 | $callback = get_curl_download_callback( |
196 | $charset, | |
197 | $title, | |
198 | $desc, | |
199 | $keywords, | |
200 | false, | |
201 | 'ut_curl_getinfo_no_charset' | |
202 | ); | |
d65342e3 A |
203 | $data = [ |
204 | 'HTTP/1.1 200 OK', | |
205 | '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', | |
9d9f6d75 | 206 | 'end' => 'th=device-width">' |
fe3713d2 V |
207 | . '<title>Refactoring · GitHub</title>' |
208 | . '<link rel="search" type="application/opensea', | |
6a487252 A |
209 | '<title>ignored</title>' |
210 | . '<meta name="description" content="desc" />' | |
211 | . '<meta name="keywords" content="key1,key2" />', | |
d65342e3 A |
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); | |
6a487252 A |
223 | $this->assertEmpty($desc); |
224 | $this->assertEmpty($keywords); | |
d65342e3 A |
225 | } |
226 | ||
227 | /** | |
228 | * Test the download callback with valid values and no title | |
229 | */ | |
230 | public function testCurlDownloadCallbackOkNoTitle() | |
231 | { | |
6a487252 A |
232 | $callback = get_curl_download_callback( |
233 | $charset, | |
234 | $title, | |
235 | $desc, | |
236 | $keywords, | |
237 | false, | |
238 | 'ut_curl_getinfo_ok' | |
239 | ); | |
d65342e3 A |
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); | |
6a487252 A |
251 | $this->assertEmpty($desc); |
252 | $this->assertEmpty($keywords); | |
d65342e3 A |
253 | } |
254 | ||
255 | /** | |
256 | * Test the download callback with an invalid content type. | |
257 | */ | |
258 | public function testCurlDownloadCallbackInvalidContentType() | |
259 | { | |
6a487252 A |
260 | $callback = get_curl_download_callback( |
261 | $charset, | |
262 | $title, | |
263 | $desc, | |
264 | $keywords, | |
265 | false, | |
266 | 'ut_curl_getinfo_ct_ko' | |
267 | ); | |
d65342e3 A |
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 | { | |
6a487252 A |
279 | $callback = $callback = get_curl_download_callback( |
280 | $charset, | |
281 | $title, | |
282 | $desc, | |
283 | $keywords, | |
284 | false, | |
285 | 'ut_curl_getinfo_rc_ko' | |
286 | ); | |
d65342e3 A |
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 | { | |
6a487252 A |
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 | ); | |
d65342e3 A |
306 | $ignore = null; |
307 | $this->assertFalse($callback($ignore, '')); | |
308 | $this->assertEmpty($charset); | |
309 | $this->assertEmpty($title); | |
310 | } | |
311 | ||
6a487252 A |
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 | ||
9ccca401 | 391 | /** |
520d2957 | 392 | * Test text2clickable. |
9ccca401 | 393 | */ |
520d2957 | 394 | public function testText2clickable() |
9ccca401 A |
395 | { |
396 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
9d9f6d75 | 397 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here">' |
fe3713d2 | 398 | . 'http://hello.there/is=someone#here</a> otherstuff'; |
520d2957 | 399 | $processedText = text2clickable($text); |
9ccca401 | 400 | $this->assertEquals($expectedText, $processedText); |
601faf97 A |
401 | |
402 | $text = 'stuff http://hello.there/is=someone#here(please) otherstuff'; | |
9d9f6d75 | 403 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">' |
fe3713d2 | 404 | . 'http://hello.there/is=someone#here(please)</a> otherstuff'; |
520d2957 | 405 | $processedText = text2clickable($text); |
601faf97 A |
406 | $this->assertEquals($expectedText, $processedText); |
407 | ||
520d2957 | 408 | $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; |
601faf97 | 409 | $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; |
9d9f6d75 | 410 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">' |
fe3713d2 | 411 | . 'http://hello.there/is=someone#here(please)&no</a> otherstuff'; |
520d2957 | 412 | $processedText = text2clickable($text); |
fd08b50a A |
413 | $this->assertEquals($expectedText, $processedText); |
414 | } | |
415 | ||
9ccca401 A |
416 | /** |
417 | * Test testSpace2nbsp. | |
418 | */ | |
419 | public function testSpace2nbsp() | |
420 | { | |
fe3713d2 V |
421 | $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?'; |
422 | $expectedText = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?'; | |
9ccca401 A |
423 | $processedText = space2nbsp($text); |
424 | $this->assertEquals($expectedText, $processedText); | |
425 | } | |
426 | ||
427 | /** | |
428 | * Test hashtags auto-link. | |
429 | */ | |
430 | public function testHashtagAutolink() | |
431 | { | |
432 | $index = 'http://domain.tld/'; | |
433 | $rawDescription = '#hashtag\n | |
434 | # nothashtag\n | |
435 | test#nothashtag #hashtag \#nothashtag\n | |
436 | test #hashtag #hashtag test #hashtag.test\n | |
437 | #hashtag #hashtag-nothashtag #hashtag_hashtag\n | |
438 | What is #ашок anyway?\n | |
439 | カタカナ #カタカナ」カタカナ\n'; | |
440 | $autolinkedDescription = hashtag_autolink($rawDescription, $index); | |
441 | ||
442 | $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription); | |
443 | $this->assertNotContains(' #hashtag', $autolinkedDescription); | |
444 | $this->assertNotContains('>#nothashtag', $autolinkedDescription); | |
445 | $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription); | |
446 | $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription); | |
447 | $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription); | |
448 | $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription); | |
449 | } | |
450 | ||
451 | /** | |
452 | * Test hashtags auto-link without index URL. | |
453 | */ | |
454 | public function testHashtagAutolinkNoIndex() | |
455 | { | |
456 | $rawDescription = 'blabla #hashtag x#nothashtag'; | |
457 | $autolinkedDescription = hashtag_autolink($rawDescription); | |
458 | ||
459 | $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription); | |
460 | $this->assertNotContains(' #hashtag', $autolinkedDescription); | |
461 | $this->assertNotContains('>#nothashtag', $autolinkedDescription); | |
462 | } | |
463 | ||
a8e7da01 A |
464 | /** |
465 | * Test is_note with note URLs. | |
466 | */ | |
467 | public function testIsNote() | |
468 | { | |
469 | $this->assertTrue(is_note('?')); | |
470 | $this->assertTrue(is_note('?abcDEf')); | |
471 | $this->assertTrue(is_note('?_abcDEf#123')); | |
472 | } | |
473 | ||
474 | /** | |
475 | * Test is_note with non note URLs. | |
476 | */ | |
477 | public function testIsNotNote() | |
478 | { | |
479 | $this->assertFalse(is_note('')); | |
480 | $this->assertFalse(is_note('nope')); | |
481 | $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi')); | |
482 | } | |
483 | ||
9ccca401 A |
484 | /** |
485 | * Util function to build an hashtag link. | |
486 | * | |
487 | * @param string $hashtag Hashtag name. | |
fe3713d2 | 488 | * @param string $index Index URL. |
9ccca401 A |
489 | * |
490 | * @return string HTML hashtag link. | |
491 | */ | |
492 | private function getHashtagLink($hashtag, $index = '') | |
493 | { | |
fe3713d2 | 494 | $hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>'; |
9ccca401 A |
495 | return str_replace('$1', $hashtag, $hashtagLink); |
496 | } | |
1557cefb | 497 | } |