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