]>
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 download callback with valid value
221 public function testCurlDownloadCallbackOk()
223 $callback = get_curl_download_callback(
233 'Server: GitHub.com',
234 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
235 'Content-Type: text/html; charset=utf-8',
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" />',
244 foreach ($data as $key => $line) {
246 $expected = $key !== 'end' ? strlen($line) : false;
247 $this->assertEquals($expected, $callback($ignore, $line));
248 if ($expected === false) {
252 $this->assertEquals('utf-8', $charset);
253 $this->assertEquals('Refactoring · GitHub', $title);
254 $this->assertEmpty($desc);
255 $this->assertEmpty($keywords);
259 * Test the download callback with valid values and no charset
261 public function testCurlDownloadCallbackOkNoCharset()
263 $callback = get_curl_download_callback(
269 'ut_curl_getinfo_no_charset'
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" />',
280 foreach ($data as $key => $line) {
282 $this->assertEquals(strlen($line), $callback($ignore, $line));
284 $this->assertEmpty($charset);
285 $this->assertEquals('Refactoring · GitHub', $title);
286 $this->assertEmpty($desc);
287 $this->assertEmpty($keywords);
291 * Test the download callback with valid values and no charset
293 public function testCurlDownloadCallbackOkHtmlCharset()
295 $callback = get_curl_download_callback(
301 'ut_curl_getinfo_no_charset'
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" />',
313 foreach ($data as $key => $line) {
315 $expected = $key !== 'end' ? strlen($line) : false;
316 $this->assertEquals($expected, $callback($ignore, $line));
317 if ($expected === false) {
321 $this->assertEquals('utf-8', $charset);
322 $this->assertEquals('Refactoring · GitHub', $title);
323 $this->assertEmpty($desc);
324 $this->assertEmpty($keywords);
328 * Test the download callback with valid values and no title
330 public function testCurlDownloadCallbackOkNoTitle()
332 $callback = get_curl_download_callback(
342 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
345 foreach ($data as $key => $line) {
347 $this->assertEquals(strlen($line), $callback($ignore, $line));
349 $this->assertEquals('utf-8', $charset);
350 $this->assertEmpty($title);
351 $this->assertEmpty($desc);
352 $this->assertEmpty($keywords);
356 * Test the download callback with an invalid content type.
358 public function testCurlDownloadCallbackInvalidContentType()
360 $callback = get_curl_download_callback(
366 'ut_curl_getinfo_ct_ko'
369 $this->assertFalse($callback($ignore, ''));
370 $this->assertEmpty($charset);
371 $this->assertEmpty($title);
375 * Test the download callback with an invalid response code.
377 public function testCurlDownloadCallbackInvalidResponseCode()
379 $callback = $callback = get_curl_download_callback(
385 'ut_curl_getinfo_rc_ko'
388 $this->assertFalse($callback($ignore, ''));
389 $this->assertEmpty($charset);
390 $this->assertEmpty($title);
394 * Test the download callback with an invalid content type and response code.
396 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
398 $callback = $callback = get_curl_download_callback(
404 'ut_curl_getinfo_rs_ct_ko'
407 $this->assertFalse($callback($ignore, ''));
408 $this->assertEmpty($charset);
409 $this->assertEmpty($title);
413 * Test the download callback with valid value, and retrieve_description option enabled.
415 public function testCurlDownloadCallbackOkWithDesc()
417 $callback = get_curl_download_callback(
427 'Server: GitHub.com',
428 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
429 'Content-Type: text/html; charset=utf-8',
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" />',
438 foreach ($data as $key => $line) {
440 $expected = $key !== 'end' ? strlen($line) : false;
441 $this->assertEquals($expected, $callback($ignore, $line));
442 if ($expected === false) {
446 $this->assertEquals('utf-8', $charset);
447 $this->assertEquals('Refactoring · GitHub', $title);
448 $this->assertEquals('link desc', $desc);
449 $this->assertEquals('key1 key2', $keywords);
453 * Test the download callback with valid value, and retrieve_description option enabled,
454 * but no desc or keyword defined in the page.
456 public function testCurlDownloadCallbackOkWithDescNotFound()
458 $callback = get_curl_download_callback(
468 'Server: GitHub.com',
469 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
470 'Content-Type: text/html; charset=utf-8',
473 . '<title>Refactoring · GitHub</title>'
474 . '<link rel="search" type="application/opensea',
475 'end' => '<title>ignored</title>',
477 foreach ($data as $key => $line) {
479 $expected = $key !== 'end' ? strlen($line) : false;
480 $this->assertEquals($expected, $callback($ignore, $line));
481 if ($expected === false) {
485 $this->assertEquals('utf-8', $charset);
486 $this->assertEquals('Refactoring · GitHub', $title);
487 $this->assertEmpty($desc);
488 $this->assertEmpty($keywords);
492 * Test text2clickable.
494 public function testText2clickable()
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);
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);
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);
517 * Test testSpace2nbsp.
519 public function testSpace2nbsp()
521 $text = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
522 $expectedText = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
523 $processedText = space2nbsp($text);
524 $this->assertEquals($expectedText, $processedText);
528 * Test hashtags auto-link.
530 public function testHashtagAutolink()
532 $index = 'http://domain.tld/';
533 $rawDescription = '#hashtag\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
540 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
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);
552 * Test hashtags auto-link without index URL.
554 public function testHashtagAutolinkNoIndex()
556 $rawDescription = 'blabla #hashtag x#nothashtag';
557 $autolinkedDescription = hashtag_autolink($rawDescription);
559 $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription);
560 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
561 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
565 * Test is_note with note URLs.
567 public function testIsNote()
569 $this->assertTrue(is_note('?'));
570 $this->assertTrue(is_note('?abcDEf'));
571 $this->assertTrue(is_note('?_abcDEf#123'));
575 * Test is_note with non note URLs.
577 public function testIsNotNote()
579 $this->assertFalse(is_note(''));
580 $this->assertFalse(is_note('nope'));
581 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
585 * Util function to build an hashtag link.
587 * @param string $hashtag Hashtag name.
588 * @param string $index Index URL.
590 * @return string HTML hashtag link.
592 private function getHashtagLink($hashtag, $index = '')
594 $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>';
595 return str_replace('$1', $hashtag, $hashtagLink);