]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/LinkUtilsTest.php
3 namespace Shaarli\Bookmark
;
7 require_once 'tests/utils/CurlUtils.php';
10 * Class LinkUtilsTest.
12 class LinkUtilsTest
extends TestCase
15 * Test html_extract_title() when the title is found.
17 public function testHtmlExtractExistentTitle()
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));
27 * Test html_extract_title() when the title is not found.
29 public function testHtmlExtractNonExistentTitle()
31 $html = '<html><meta>stuff</meta></html>';
32 $this->assertFalse(html_extract_title($html));
36 * Test headers_extract_charset() when the charset is found.
38 public function testHeadersExtractExistentCharset()
40 $charset = 'x-MacCroatian';
41 $headers = 'text/html; charset=' . $charset;
42 $this->assertEquals(strtolower($charset), header_extract_charset($headers));
46 * Test headers_extract_charset() when the charset is found with odd quotes.
48 public function testHeadersExtractExistentCharsetWithQuotes()
50 $charset = 'x-MacCroatian';
51 $headers = 'text/html; charset="' . $charset . '"otherstuff="test"';
52 $this->assertEquals(strtolower($charset), header_extract_charset($headers));
54 $headers = 'text/html; charset=\'' . $charset . '\'otherstuff="test"';
55 $this->assertEquals(strtolower($charset), header_extract_charset($headers));
59 * Test headers_extract_charset() when the charset is not found.
61 public function testHeadersExtractNonExistentCharset()
64 $this->assertFalse(header_extract_charset($headers));
66 $headers = 'text/html';
67 $this->assertFalse(header_extract_charset($headers));
71 * Test html_extract_charset() when the charset is found.
73 public function testHtmlExtractExistentCharset()
75 $charset = 'x-MacCroatian';
76 $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>';
77 $this->assertEquals(strtolower($charset), html_extract_charset($html));
81 * Test html_extract_charset() when the charset is not found.
83 public function testHtmlExtractNonExistentCharset()
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));
92 * Test html_extract_tag() when the tag <meta name= is found.
94 public function testHtmlExtractExistentNameTag()
96 $description = 'Bob and Alice share cookies.';
99 $html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>';
100 $this->assertEquals($description, html_extract_tag('description', $html));
103 $html = '<meta property="og:description" content="' . $description . '">';
104 $this->assertEquals($description, html_extract_tag('description', $html));
106 // Simple reversed OpenGraph
107 $html = '<meta content="' . $description . '" property="og:description">';
108 $this->assertEquals($description, html_extract_tag('description', $html));
110 // ItemProp OpenGraph
111 $html = '<meta itemprop="og:description" content="' . $description . '">';
112 $this->assertEquals($description, html_extract_tag('description', $html));
114 // OpenGraph without quotes
115 $html = '<meta property=og:description content="' . $description . '">';
116 $this->assertEquals($description, html_extract_tag('description', $html));
118 // OpenGraph reversed without quotes
119 $html = '<meta content="' . $description . '" property=og:description>';
120 $this->assertEquals($description, html_extract_tag('description', $html));
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));
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));
132 // OpenGraph multiple properties start
133 $html = '<meta property="unrelated og:description" content="' . $description . '">';
134 $this->assertEquals($description, html_extract_tag('description', $html));
136 // OpenGraph multiple properties end
137 $html = '<meta property="og:description unrelated" content="' . $description . '">';
138 $this->assertEquals($description, html_extract_tag('description', $html));
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));
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));
149 // OpenGraph reversed multiple properties start
150 $html = '<meta content="' . $description . '" property="unrelated og:description">';
151 $this->assertEquals($description, html_extract_tag('description', $html));
153 // OpenGraph reversed multiple properties end
154 $html = '<meta content="' . $description . '" property="og:description unrelated">';
155 $this->assertEquals($description, html_extract_tag('description', $html));
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));
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));
166 // Suggestion from #1375
167 $html = '<meta property="og:description" name="description" content="' . $description . '">';
168 $this->assertEquals($description, html_extract_tag('description', $html));
172 * Test html_extract_tag() when the tag <meta name= is not found.
174 public function testHtmlExtractNonExistentNameTag()
176 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
177 $this->assertFalse(html_extract_tag('description', $html));
180 $html = '<meta content="Brief description">';
181 $this->assertFalse(html_extract_tag('description', $html));
183 $html = '<meta property="og:description">';
184 $this->assertFalse(html_extract_tag('description', $html));
186 $html = '<meta tag1="content1" property="og:description">';
187 $this->assertFalse(html_extract_tag('description', $html));
189 $html = '<meta property="og:description" tag1="content1">';
190 $this->assertFalse(html_extract_tag('description', $html));
192 $html = '<meta tag1="content1" content="Brief description">';
193 $this->assertFalse(html_extract_tag('description', $html));
195 $html = '<meta content="Brief description" tag1="content1">';
196 $this->assertFalse(html_extract_tag('description', $html));
200 * Test html_extract_tag() when the tag <meta property="og: is found.
202 public function testHtmlExtractExistentOgTag()
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));
210 * Test html_extract_tag() when the tag <meta property="og: is not found.
212 public function testHtmlExtractNonExistentOgTag()
214 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
215 $this->assertFalse(html_extract_tag('description', $html));
219 * Test the header callback with valid value
221 public function testCurlHeaderCallbackOk(): void
223 $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_ok');
226 'Server: GitHub.com',
227 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
228 'Content-Type: text/html; charset=utf-8',
232 foreach ($data as $chunk) {
233 static::assertIsInt($callback(null, $chunk));
236 static::assertSame('utf-8', $charset);
240 * Test the download callback with valid value
242 public function testCurlDownloadCallbackOk(): void
245 $callback = get_curl_download_callback(
256 . '<title>Refactoring · GitHub</title>'
257 . '<link rel="search" type="application/opensea',
258 '<title>ignored</title>'
259 . '<meta name="description" content="desc" />'
260 . '<meta name="keywords" content="key1,key2" />',
263 foreach ($data as $chunk) {
264 static::assertSame(strlen($chunk), $callback(null, $chunk));
267 static::assertSame('utf-8', $charset);
268 static::assertSame('Refactoring · GitHub', $title);
269 static::assertEmpty($desc);
270 static::assertEmpty($keywords);
274 * Test the header callback with valid value
276 public function testCurlHeaderCallbackNoCharset(): void
278 $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_no_charset');
283 foreach ($data as $chunk) {
284 static::assertSame(strlen($chunk), $callback(null, $chunk));
287 static::assertFalse($charset);
291 * Test the download callback with valid values and no charset
293 public function testCurlDownloadCallbackOkNoCharset(): void
296 $callback = get_curl_download_callback(
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" />',
314 foreach ($data as $chunk) {
315 static::assertSame(strlen($chunk), $callback(null, $chunk));
318 $this->assertEmpty($charset);
319 $this->assertEquals('Refactoring · GitHub', $title);
320 $this->assertEmpty($desc);
321 $this->assertEmpty($keywords);
325 * Test the download callback with valid values and no charset
327 public function testCurlDownloadCallbackOkHtmlCharset(): void
330 $callback = get_curl_download_callback(
340 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
341 'end' => 'th=device-width">'
342 . '<title>Refactoring · GitHub</title>'
343 . '<link rel="search" type="application/opensea',
344 '<title>ignored</title>'
345 . '<meta name="description" content="desc" />'
346 . '<meta name="keywords" content="key1,key2" />',
348 foreach ($data as $chunk) {
349 static::assertSame(strlen($chunk), $callback(null, $chunk));
352 $this->assertEquals('utf-8', $charset);
353 $this->assertEquals('Refactoring · GitHub', $title);
354 $this->assertEmpty($desc);
355 $this->assertEmpty($keywords);
359 * Test the download callback with valid values and no title
361 public function testCurlDownloadCallbackOkNoTitle(): void
364 $callback = get_curl_download_callback(
374 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
378 foreach ($data as $chunk) {
379 static::assertSame(strlen($chunk), $callback(null, $chunk));
382 $this->assertEquals('utf-8', $charset);
383 $this->assertEmpty($title);
384 $this->assertEmpty($desc);
385 $this->assertEmpty($keywords);
389 * Test the header callback with an invalid content type.
391 public function testCurlHeaderCallbackInvalidContentType(): void
393 $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_ct_ko');
398 static::assertFalse($callback(null, $data[0]));
399 static::assertNull($charset);
403 * Test the header callback with an invalid response code.
405 public function testCurlHeaderCallbackInvalidResponseCode(): void
407 $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rc_ko');
409 static::assertFalse($callback(null, ''));
410 static::assertNull($charset);
414 * Test the header callback with an invalid content type and response code.
416 public function testCurlHeaderCallbackInvalidContentTypeAndResponseCode(): void
418 $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rs_ct_ko');
420 static::assertFalse($callback(null, ''));
421 static::assertNull($charset);
425 * Test the download callback with valid value, and retrieve_description option enabled.
427 public function testCurlDownloadCallbackOkWithDesc(): void
430 $callback = get_curl_download_callback(
440 . '<title>Refactoring · GitHub</title>'
441 . '<link rel="search" type="application/opensea',
442 'end' => '<title>ignored</title>'
443 . '<meta name="description" content="link desc" />'
444 . '<meta name="keywords" content="key1,key2" />',
447 foreach ($data as $chunk) {
448 static::assertSame(strlen($chunk), $callback(null, $chunk));
451 $this->assertEquals('utf-8', $charset);
452 $this->assertEquals('Refactoring · GitHub', $title);
453 $this->assertEquals('link desc', $desc);
454 $this->assertEquals('key1 key2', $keywords);
458 * Test the download callback with valid value, and retrieve_description option enabled,
459 * but no desc or keyword defined in the page.
461 public function testCurlDownloadCallbackOkWithDescNotFound(): void
464 $callback = get_curl_download_callback(
474 . '<title>Refactoring · GitHub</title>'
475 . '<link rel="search" type="application/opensea',
476 'end' => '<title>ignored</title>',
479 foreach ($data as $chunk) {
480 static::assertSame(strlen($chunk), $callback(null, $chunk));
483 $this->assertEquals('utf-8', $charset);
484 $this->assertEquals('Refactoring · GitHub', $title);
485 $this->assertEmpty($desc);
486 $this->assertEmpty($keywords);
490 * Test text2clickable.
492 public function testText2clickable()
494 $text = 'stuff http://hello.there/is=someone#here otherstuff';
495 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
496 . 'http://hello.there/is=someone#here</a> otherstuff';
497 $processedText = text2clickable($text);
498 $this->assertEquals($expectedText, $processedText);
500 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
501 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
502 . 'http://hello.there/is=someone#here(please)</a> otherstuff';
503 $processedText = text2clickable($text);
504 $this->assertEquals($expectedText, $processedText);
506 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
507 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
508 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
509 . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
510 $processedText = text2clickable($text);
511 $this->assertEquals($expectedText, $processedText);
515 * Test testSpace2nbsp.
517 public function testSpace2nbsp()
519 $text = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
520 $expectedText = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
521 $processedText = space2nbsp($text);
522 $this->assertEquals($expectedText, $processedText);
526 * Test hashtags auto-link.
528 public function testHashtagAutolink()
530 $index = 'http://domain.tld/';
531 $rawDescription = '#hashtag\n
533 test#nothashtag #hashtag \#nothashtag\n
534 test #hashtag #hashtag test #hashtag.test\n
535 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
536 What is #ашок anyway?\n
538 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
540 $this->assertContainsPolyfill($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
541 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
542 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
543 $this->assertContainsPolyfill($this->getHashtagLink('ашок', $index), $autolinkedDescription);
544 $this->assertContainsPolyfill($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
545 $this->assertContainsPolyfill($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
546 $this->assertNotContainsPolyfill($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
550 * Test hashtags auto-link without index URL.
552 public function testHashtagAutolinkNoIndex()
554 $rawDescription = 'blabla #hashtag x#nothashtag';
555 $autolinkedDescription = hashtag_autolink($rawDescription);
557 $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription);
558 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
559 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
563 * Test is_note with note URLs.
565 public function testIsNote()
567 $this->assertTrue(is_note('?'));
568 $this->assertTrue(is_note('?abcDEf'));
569 $this->assertTrue(is_note('?_abcDEf#123'));
573 * Test is_note with non note URLs.
575 public function testIsNotNote()
577 $this->assertFalse(is_note(''));
578 $this->assertFalse(is_note('nope'));
579 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
583 * Test tags_str2array with whitespace separator.
585 public function testTagsStr2ArrayWithSpaceSeparator(): void
589 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1 tag2 tag3', $separator));
590 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1 tag2 tag3', $separator));
591 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array(' tag1 tag2 tag3 ', $separator));
592 static::assertSame(['tag1@', 'tag2,', '.tag3'], tags_str2array(' tag1@ tag2, .tag3 ', $separator));
593 static::assertSame([], tags_str2array('', $separator));
594 static::assertSame([], tags_str2array(' ', $separator));
595 static::assertSame([], tags_str2array(null, $separator));
599 * Test tags_str2array with @ separator.
601 public function testTagsStr2ArrayWithCharSeparator(): void
605 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1@tag2@tag3', $separator));
606 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1@@@@tag2@@@@tag3', $separator));
607 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('@@@tag1@@@tag2@@@@tag3@@', $separator));
609 ['tag1#', 'tag2, and other', '.tag3'],
610 tags_str2array('@@@ tag1# @@@ tag2, and other @@@@.tag3@@', $separator)
612 static::assertSame([], tags_str2array('', $separator));
613 static::assertSame([], tags_str2array(' ', $separator));
614 static::assertSame([], tags_str2array(null, $separator));
618 * Test tags_array2str with ' ' separator.
620 public function testTagsArray2StrWithSpaceSeparator(): void
624 static::assertSame('tag1 tag2 tag3', tags_array2str(['tag1', 'tag2', 'tag3'], $separator));
625 static::assertSame('tag1, tag2@ tag3', tags_array2str(['tag1,', 'tag2@', 'tag3'], $separator));
626 static::assertSame('tag1 tag2 tag3', tags_array2str([' tag1 ', 'tag2', 'tag3 '], $separator));
627 static::assertSame('tag1 tag2 tag3', tags_array2str([' tag1 ', ' ', 'tag2', ' ', 'tag3 '], $separator));
628 static::assertSame('tag1', tags_array2str([' tag1 '], $separator));
629 static::assertSame('', tags_array2str([' '], $separator));
630 static::assertSame('', tags_array2str([], $separator));
631 static::assertSame('', tags_array2str(null, $separator));
635 * Test tags_array2str with @ separator.
637 public function testTagsArray2StrWithCharSeparator(): void
641 static::assertSame('tag1@tag2@tag3', tags_array2str(['tag1', 'tag2', 'tag3'], $separator));
642 static::assertSame('tag1,@tag2@tag3', tags_array2str(['tag1,', 'tag2@', 'tag3'], $separator));
644 'tag1@tag2, and other@tag3',
645 tags_array2str(['@@@@ tag1@@@', ' @tag2, and other @', 'tag3@@@@'], $separator)
647 static::assertSame('tag1@tag2@tag3', tags_array2str(['@@@tag1@@@', '@', 'tag2', '@@@', 'tag3@@@'], $separator));
648 static::assertSame('tag1', tags_array2str(['@@@@tag1@@@@'], $separator));
649 static::assertSame('', tags_array2str(['@@@'], $separator));
650 static::assertSame('', tags_array2str([], $separator));
651 static::assertSame('', tags_array2str(null, $separator));
655 * Test tags_array2str with @ separator.
657 public function testTagsFilterWithSpaceSeparator(): void
661 static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['tag1', 'tag2', 'tag3'], $separator));
662 static::assertSame(['tag1,', 'tag2@', 'tag3'], tags_filter(['tag1,', 'tag2@', 'tag3'], $separator));
663 static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter([' tag1 ', 'tag2', 'tag3 '], $separator));
664 static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter([' tag1 ', ' ', 'tag2', ' ', 'tag3 '], $separator));
665 static::assertSame(['tag1'], tags_filter([' tag1 '], $separator));
666 static::assertSame([], tags_filter([' '], $separator));
667 static::assertSame([], tags_filter([], $separator));
668 static::assertSame([], tags_filter(null, $separator));
672 * Test tags_array2str with @ separator.
674 public function testTagsArrayFilterWithSpaceSeparator(): void
678 static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['tag1', 'tag2', 'tag3'], $separator));
679 static::assertSame(['tag1,', 'tag2#', 'tag3'], tags_filter(['tag1,', 'tag2#', 'tag3'], $separator));
681 ['tag1', 'tag2, and other', 'tag3'],
682 tags_filter(['@@@@ tag1@@@', ' @tag2, and other @', 'tag3@@@@'], $separator)
684 static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['@@@tag1@@@', '@', 'tag2', '@@@', 'tag3@@@'], $separator));
685 static::assertSame(['tag1'], tags_filter(['@@@@tag1@@@@'], $separator));
686 static::assertSame([], tags_filter(['@@@'], $separator));
687 static::assertSame([], tags_filter([], $separator));
688 static::assertSame([], tags_filter(null, $separator));
692 * Util function to build an hashtag link.
694 * @param string $hashtag Hashtag name.
695 * @param string $index Index URL.
697 * @return string HTML hashtag link.
699 private function getHashtagLink($hashtag, $index = '')
701 $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>';
702 return str_replace('$1', $hashtag, $hashtagLink);