]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/LinkUtilsTest.php
Merge pull request #1540 from ArthurHoaro/fix/metadata-regexes
[github/shaarli/Shaarli.git] / tests / bookmark / LinkUtilsTest.php
1 <?php
2
3 namespace Shaarli\Bookmark;
4
5 use Shaarli\TestCase;
6
7 require_once 'tests/utils/CurlUtils.php';
8
9 /**
10 * Class LinkUtilsTest.
11 */
12 class LinkUtilsTest extends TestCase
13 {
14 /**
15 * Test html_extract_title() when the title is found.
16 */
17 public function testHtmlExtractExistentTitle()
18 {
19 $title = 'Read me please.';
20 $html = '<html><meta>stuff</meta><title>' . $title . '</title></html>';
21 $this->assertEquals($title, html_extract_title($html));
22 $html = '<html><title>' . $title . '</title>blabla<title>another</title></html>';
23 $this->assertEquals($title, html_extract_title($html));
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
35 /**
36 * Test headers_extract_charset() when the charset is found.
37 */
38 public function testHeadersExtractExistentCharset()
39 {
40 $charset = 'x-MacCroatian';
41 $headers = 'text/html; charset=' . $charset;
42 $this->assertEquals(strtolower($charset), header_extract_charset($headers));
43 }
44
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
58 /**
59 * Test headers_extract_charset() when the charset is not found.
60 */
61 public function testHeadersExtractNonExistentCharset()
62 {
63 $headers = '';
64 $this->assertFalse(header_extract_charset($headers));
65
66 $headers = 'text/html';
67 $this->assertFalse(header_extract_charset($headers));
68 }
69
70 /**
71 * Test html_extract_charset() when the charset is found.
72 */
73 public function testHtmlExtractExistentCharset()
74 {
75 $charset = 'x-MacCroatian';
76 $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>';
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 }
90
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
98 // Simple one line
99 $html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>';
100 $this->assertEquals($description, html_extract_tag('description', $html));
101
102 // Simple OpenGraph
103 $html = '<meta property="og:description" content="' . $description . '">';
104 $this->assertEquals($description, html_extract_tag('description', $html));
105
106 // Simple reversed OpenGraph
107 $html = '<meta content="' . $description . '" property="og:description">';
108 $this->assertEquals($description, html_extract_tag('description', $html));
109
110 // ItemProp OpenGraph
111 $html = '<meta itemprop="og:description" content="' . $description . '">';
112 $this->assertEquals($description, html_extract_tag('description', $html));
113
114 // OpenGraph without quotes
115 $html = '<meta property=og:description content="' . $description . '">';
116 $this->assertEquals($description, html_extract_tag('description', $html));
117
118 // OpenGraph reversed without quotes
119 $html = '<meta content="' . $description . '" property=og:description>';
120 $this->assertEquals($description, html_extract_tag('description', $html));
121
122 // OpenGraph with noise
123 $html = '<meta tag1="content1" property="og:description" tag2="content2" content="' .
124 $description . '" tag3="content3">';
125 $this->assertEquals($description, html_extract_tag('description', $html));
126
127 // OpenGraph reversed with noise
128 $html = '<meta tag1="content1" content="' . $description . '" ' .
129 'tag3="content3" tag2="content2" property="og:description">';
130 $this->assertEquals($description, html_extract_tag('description', $html));
131
132 // OpenGraph multiple properties start
133 $html = '<meta property="unrelated og:description" content="' . $description . '">';
134 $this->assertEquals($description, html_extract_tag('description', $html));
135
136 // OpenGraph multiple properties end
137 $html = '<meta property="og:description unrelated" content="' . $description . '">';
138 $this->assertEquals($description, html_extract_tag('description', $html));
139
140 // OpenGraph multiple properties both end
141 $html = '<meta property="og:unrelated1 og:description og:unrelated2" content="' . $description . '">';
142 $this->assertEquals($description, html_extract_tag('description', $html));
143
144 // OpenGraph multiple properties both end with noise
145 $html = '<meta tag1="content1" property="og:unrelated1 og:description og:unrelated2" '.
146 'tag2="content2" content="' . $description . '" tag3="content3">';
147 $this->assertEquals($description, html_extract_tag('description', $html));
148
149 // OpenGraph reversed multiple properties start
150 $html = '<meta content="' . $description . '" property="unrelated og:description">';
151 $this->assertEquals($description, html_extract_tag('description', $html));
152
153 // OpenGraph reversed multiple properties end
154 $html = '<meta content="' . $description . '" property="og:description unrelated">';
155 $this->assertEquals($description, html_extract_tag('description', $html));
156
157 // OpenGraph reversed multiple properties both end
158 $html = '<meta content="' . $description . '" property="og:unrelated1 og:description og:unrelated2">';
159 $this->assertEquals($description, html_extract_tag('description', $html));
160
161 // OpenGraph reversed multiple properties both end with noise
162 $html = '<meta tag1="content1" content="' . $description . '" tag2="content2" '.
163 'property="og:unrelated1 og:description og:unrelated2" tag3="content3">';
164 $this->assertEquals($description, html_extract_tag('description', $html));
165
166 // Suggestion from #1375
167 $html = '<meta property="og:description" name="description" content="' . $description . '">';
168 $this->assertEquals($description, html_extract_tag('description', $html));
169 }
170
171 /**
172 * Test html_extract_tag() when the tag <meta name= is not found.
173 */
174 public function testHtmlExtractNonExistentNameTag()
175 {
176 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
177 $this->assertFalse(html_extract_tag('description', $html));
178
179 // Partial meta tag
180 $html = '<meta content="Brief description">';
181 $this->assertFalse(html_extract_tag('description', $html));
182
183 $html = '<meta property="og:description">';
184 $this->assertFalse(html_extract_tag('description', $html));
185
186 $html = '<meta tag1="content1" property="og:description">';
187 $this->assertFalse(html_extract_tag('description', $html));
188
189 $html = '<meta property="og:description" tag1="content1">';
190 $this->assertFalse(html_extract_tag('description', $html));
191
192 $html = '<meta tag1="content1" content="Brief description">';
193 $this->assertFalse(html_extract_tag('description', $html));
194
195 $html = '<meta content="Brief description" tag1="content1">';
196 $this->assertFalse(html_extract_tag('description', $html));
197 }
198
199 /**
200 * Test html_extract_tag() when the tag <meta property="og: is found.
201 */
202 public function testHtmlExtractExistentOgTag()
203 {
204 $description = 'Bob and Alice share cookies.';
205 $html = '<html><meta>stuff2</meta><meta property="og:description" content="' . $description . '"/></html>';
206 $this->assertEquals($description, html_extract_tag('description', $html));
207 }
208
209 /**
210 * Test html_extract_tag() when the tag <meta property="og: is not found.
211 */
212 public function testHtmlExtractNonExistentOgTag()
213 {
214 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
215 $this->assertFalse(html_extract_tag('description', $html));
216 }
217
218 /**
219 * Test the download callback with valid value
220 */
221 public function testCurlDownloadCallbackOk()
222 {
223 $callback = get_curl_download_callback(
224 $charset,
225 $title,
226 $desc,
227 $keywords,
228 false,
229 'ut_curl_getinfo_ok'
230 );
231 $data = [
232 'HTTP/1.1 200 OK',
233 'Server: GitHub.com',
234 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
235 'Content-Type: text/html; charset=utf-8',
236 'Status: 200 OK',
237 'end' => 'th=device-width">'
238 . '<title>Refactoring · GitHub</title>'
239 . '<link rel="search" type="application/opensea',
240 '<title>ignored</title>'
241 . '<meta name="description" content="desc" />'
242 . '<meta name="keywords" content="key1,key2" />',
243 ];
244 foreach ($data as $key => $line) {
245 $ignore = null;
246 $expected = $key !== 'end' ? strlen($line) : false;
247 $this->assertEquals($expected, $callback($ignore, $line));
248 if ($expected === false) {
249 break;
250 }
251 }
252 $this->assertEquals('utf-8', $charset);
253 $this->assertEquals('Refactoring · GitHub', $title);
254 $this->assertEmpty($desc);
255 $this->assertEmpty($keywords);
256 }
257
258 /**
259 * Test the download callback with valid values and no charset
260 */
261 public function testCurlDownloadCallbackOkNoCharset()
262 {
263 $callback = get_curl_download_callback(
264 $charset,
265 $title,
266 $desc,
267 $keywords,
268 false,
269 'ut_curl_getinfo_no_charset'
270 );
271 $data = [
272 'HTTP/1.1 200 OK',
273 'end' => 'th=device-width">'
274 . '<title>Refactoring · GitHub</title>'
275 . '<link rel="search" type="application/opensea',
276 '<title>ignored</title>'
277 . '<meta name="description" content="desc" />'
278 . '<meta name="keywords" content="key1,key2" />',
279 ];
280 foreach ($data as $key => $line) {
281 $ignore = null;
282 $this->assertEquals(strlen($line), $callback($ignore, $line));
283 }
284 $this->assertEmpty($charset);
285 $this->assertEquals('Refactoring · GitHub', $title);
286 $this->assertEmpty($desc);
287 $this->assertEmpty($keywords);
288 }
289
290 /**
291 * Test the download callback with valid values and no charset
292 */
293 public function testCurlDownloadCallbackOkHtmlCharset()
294 {
295 $callback = get_curl_download_callback(
296 $charset,
297 $title,
298 $desc,
299 $keywords,
300 false,
301 'ut_curl_getinfo_no_charset'
302 );
303 $data = [
304 'HTTP/1.1 200 OK',
305 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
306 'end' => 'th=device-width">'
307 . '<title>Refactoring · GitHub</title>'
308 . '<link rel="search" type="application/opensea',
309 '<title>ignored</title>'
310 . '<meta name="description" content="desc" />'
311 . '<meta name="keywords" content="key1,key2" />',
312 ];
313 foreach ($data as $key => $line) {
314 $ignore = null;
315 $expected = $key !== 'end' ? strlen($line) : false;
316 $this->assertEquals($expected, $callback($ignore, $line));
317 if ($expected === false) {
318 break;
319 }
320 }
321 $this->assertEquals('utf-8', $charset);
322 $this->assertEquals('Refactoring · GitHub', $title);
323 $this->assertEmpty($desc);
324 $this->assertEmpty($keywords);
325 }
326
327 /**
328 * Test the download callback with valid values and no title
329 */
330 public function testCurlDownloadCallbackOkNoTitle()
331 {
332 $callback = get_curl_download_callback(
333 $charset,
334 $title,
335 $desc,
336 $keywords,
337 false,
338 'ut_curl_getinfo_ok'
339 );
340 $data = [
341 'HTTP/1.1 200 OK',
342 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
343 'ignored',
344 ];
345 foreach ($data as $key => $line) {
346 $ignore = null;
347 $this->assertEquals(strlen($line), $callback($ignore, $line));
348 }
349 $this->assertEquals('utf-8', $charset);
350 $this->assertEmpty($title);
351 $this->assertEmpty($desc);
352 $this->assertEmpty($keywords);
353 }
354
355 /**
356 * Test the download callback with an invalid content type.
357 */
358 public function testCurlDownloadCallbackInvalidContentType()
359 {
360 $callback = get_curl_download_callback(
361 $charset,
362 $title,
363 $desc,
364 $keywords,
365 false,
366 'ut_curl_getinfo_ct_ko'
367 );
368 $ignore = null;
369 $this->assertFalse($callback($ignore, ''));
370 $this->assertEmpty($charset);
371 $this->assertEmpty($title);
372 }
373
374 /**
375 * Test the download callback with an invalid response code.
376 */
377 public function testCurlDownloadCallbackInvalidResponseCode()
378 {
379 $callback = $callback = get_curl_download_callback(
380 $charset,
381 $title,
382 $desc,
383 $keywords,
384 false,
385 'ut_curl_getinfo_rc_ko'
386 );
387 $ignore = null;
388 $this->assertFalse($callback($ignore, ''));
389 $this->assertEmpty($charset);
390 $this->assertEmpty($title);
391 }
392
393 /**
394 * Test the download callback with an invalid content type and response code.
395 */
396 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
397 {
398 $callback = $callback = get_curl_download_callback(
399 $charset,
400 $title,
401 $desc,
402 $keywords,
403 false,
404 'ut_curl_getinfo_rs_ct_ko'
405 );
406 $ignore = null;
407 $this->assertFalse($callback($ignore, ''));
408 $this->assertEmpty($charset);
409 $this->assertEmpty($title);
410 }
411
412 /**
413 * Test the download callback with valid value, and retrieve_description option enabled.
414 */
415 public function testCurlDownloadCallbackOkWithDesc()
416 {
417 $callback = get_curl_download_callback(
418 $charset,
419 $title,
420 $desc,
421 $keywords,
422 true,
423 'ut_curl_getinfo_ok'
424 );
425 $data = [
426 'HTTP/1.1 200 OK',
427 'Server: GitHub.com',
428 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
429 'Content-Type: text/html; charset=utf-8',
430 'Status: 200 OK',
431 'th=device-width">'
432 . '<title>Refactoring · GitHub</title>'
433 . '<link rel="search" type="application/opensea',
434 'end' => '<title>ignored</title>'
435 . '<meta name="description" content="link desc" />'
436 . '<meta name="keywords" content="key1,key2" />',
437 ];
438 foreach ($data as $key => $line) {
439 $ignore = null;
440 $expected = $key !== 'end' ? strlen($line) : false;
441 $this->assertEquals($expected, $callback($ignore, $line));
442 if ($expected === false) {
443 break;
444 }
445 }
446 $this->assertEquals('utf-8', $charset);
447 $this->assertEquals('Refactoring · GitHub', $title);
448 $this->assertEquals('link desc', $desc);
449 $this->assertEquals('key1 key2', $keywords);
450 }
451
452 /**
453 * Test the download callback with valid value, and retrieve_description option enabled,
454 * but no desc or keyword defined in the page.
455 */
456 public function testCurlDownloadCallbackOkWithDescNotFound()
457 {
458 $callback = get_curl_download_callback(
459 $charset,
460 $title,
461 $desc,
462 $keywords,
463 true,
464 'ut_curl_getinfo_ok'
465 );
466 $data = [
467 'HTTP/1.1 200 OK',
468 'Server: GitHub.com',
469 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
470 'Content-Type: text/html; charset=utf-8',
471 'Status: 200 OK',
472 'th=device-width">'
473 . '<title>Refactoring · GitHub</title>'
474 . '<link rel="search" type="application/opensea',
475 'end' => '<title>ignored</title>',
476 ];
477 foreach ($data as $key => $line) {
478 $ignore = null;
479 $expected = $key !== 'end' ? strlen($line) : false;
480 $this->assertEquals($expected, $callback($ignore, $line));
481 if ($expected === false) {
482 break;
483 }
484 }
485 $this->assertEquals('utf-8', $charset);
486 $this->assertEquals('Refactoring · GitHub', $title);
487 $this->assertEmpty($desc);
488 $this->assertEmpty($keywords);
489 }
490
491 /**
492 * Test text2clickable.
493 */
494 public function testText2clickable()
495 {
496 $text = 'stuff http://hello.there/is=someone#here otherstuff';
497 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
498 . 'http://hello.there/is=someone#here</a> otherstuff';
499 $processedText = text2clickable($text);
500 $this->assertEquals($expectedText, $processedText);
501
502 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
503 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
504 . 'http://hello.there/is=someone#here(please)</a> otherstuff';
505 $processedText = text2clickable($text);
506 $this->assertEquals($expectedText, $processedText);
507
508 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
509 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
510 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
511 . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
512 $processedText = text2clickable($text);
513 $this->assertEquals($expectedText, $processedText);
514 }
515
516 /**
517 * Test testSpace2nbsp.
518 */
519 public function testSpace2nbsp()
520 {
521 $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?';
522 $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?' . PHP_EOL . '&nbsp;Really?';
523 $processedText = space2nbsp($text);
524 $this->assertEquals($expectedText, $processedText);
525 }
526
527 /**
528 * Test hashtags auto-link.
529 */
530 public function testHashtagAutolink()
531 {
532 $index = 'http://domain.tld/';
533 $rawDescription = '#hashtag\n
534 # nothashtag\n
535 test#nothashtag #hashtag \#nothashtag\n
536 test #hashtag #hashtag test #hashtag.test\n
537 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
538 What is #ашок anyway?\n
539 カタカナ #カタカナ」カタカナ\n';
540 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
541
542 $this->assertContainsPolyfill($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
543 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
544 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
545 $this->assertContainsPolyfill($this->getHashtagLink('ашок', $index), $autolinkedDescription);
546 $this->assertContainsPolyfill($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
547 $this->assertContainsPolyfill($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
548 $this->assertNotContainsPolyfill($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
549 }
550
551 /**
552 * Test hashtags auto-link without index URL.
553 */
554 public function testHashtagAutolinkNoIndex()
555 {
556 $rawDescription = 'blabla #hashtag x#nothashtag';
557 $autolinkedDescription = hashtag_autolink($rawDescription);
558
559 $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription);
560 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
561 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
562 }
563
564 /**
565 * Test is_note with note URLs.
566 */
567 public function testIsNote()
568 {
569 $this->assertTrue(is_note('?'));
570 $this->assertTrue(is_note('?abcDEf'));
571 $this->assertTrue(is_note('?_abcDEf#123'));
572 }
573
574 /**
575 * Test is_note with non note URLs.
576 */
577 public function testIsNotNote()
578 {
579 $this->assertFalse(is_note(''));
580 $this->assertFalse(is_note('nope'));
581 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
582 }
583
584 /**
585 * Util function to build an hashtag link.
586 *
587 * @param string $hashtag Hashtag name.
588 * @param string $index Index URL.
589 *
590 * @return string HTML hashtag link.
591 */
592 private function getHashtagLink($hashtag, $index = '')
593 {
594 $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>';
595 return str_replace('$1', $hashtag, $hashtagLink);
596 }
597 }