3 namespace Shaarli\Bookmark
;
5 use PHPUnit\Framework\TestCase
;
7 use Shaarli\Config\ConfigManager
;
9 require_once 'tests/utils/CurlUtils.php';
12 * Class LinkUtilsTest.
14 class LinkUtilsTest
extends TestCase
17 * Test html_extract_title() when the title is found.
19 public function testHtmlExtractExistentTitle()
21 $title = 'Read me please.';
22 $html = '<html><meta>stuff</meta><title>' . $title . '</title></html>';
23 $this->assertEquals($title, html_extract_title($html));
24 $html = '<html><title>' . $title . '</title>blabla<title>another</title></html>';
25 $this->assertEquals($title, html_extract_title($html));
29 * Test html_extract_title() when the title is not found.
31 public function testHtmlExtractNonExistentTitle()
33 $html = '<html><meta>stuff</meta></html>';
34 $this->assertFalse(html_extract_title($html));
38 * Test headers_extract_charset() when the charset is found.
40 public function testHeadersExtractExistentCharset()
42 $charset = 'x-MacCroatian';
43 $headers = 'text/html; charset=' . $charset;
44 $this->assertEquals(strtolower($charset), header_extract_charset($headers));
48 * Test headers_extract_charset() when the charset is not found.
50 public function testHeadersExtractNonExistentCharset()
53 $this->assertFalse(header_extract_charset($headers));
55 $headers = 'text/html';
56 $this->assertFalse(header_extract_charset($headers));
60 * Test html_extract_charset() when the charset is found.
62 public function testHtmlExtractExistentCharset()
64 $charset = 'x-MacCroatian';
65 $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>';
66 $this->assertEquals(strtolower($charset), html_extract_charset($html));
70 * Test html_extract_charset() when the charset is not found.
72 public function testHtmlExtractNonExistentCharset()
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));
81 * Test html_extract_tag() when the tag <meta name= is found.
83 public function testHtmlExtractExistentNameTag()
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));
91 * Test html_extract_tag() when the tag <meta name= is not found.
93 public function testHtmlExtractNonExistentNameTag()
95 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
96 $this->assertFalse(html_extract_tag('description', $html));
100 * Test html_extract_tag() when the tag <meta property="og: is found.
102 public function testHtmlExtractExistentOgTag()
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));
110 * Test html_extract_tag() when the tag <meta property="og: is not found.
112 public function testHtmlExtractNonExistentOgTag()
114 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
115 $this->assertFalse(html_extract_tag('description', $html));
119 * Test the download callback with valid value
121 public function testCurlDownloadCallbackOk()
123 $callback = get_curl_download_callback(
133 'Server: GitHub.com',
134 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
135 'Content-Type: text/html; charset=utf-8',
137 'end' => 'th=device-width">'
138 . '<title>Refactoring · GitHub</title>'
139 . '<link rel="search" type="application/opensea',
140 '<title>ignored</title>'
141 . '<meta name="description" content="desc" />'
142 . '<meta name="keywords" content="key1,key2" />',
144 foreach ($data as $key => $line) {
146 $expected = $key !== 'end' ? strlen($line) : false;
147 $this->assertEquals($expected, $callback($ignore, $line));
148 if ($expected === false) {
152 $this->assertEquals('utf-8', $charset);
153 $this->assertEquals('Refactoring · GitHub', $title);
154 $this->assertEmpty($desc);
155 $this->assertEmpty($keywords);
159 * Test the download callback with valid values and no charset
161 public function testCurlDownloadCallbackOkNoCharset()
163 $callback = get_curl_download_callback(
169 'ut_curl_getinfo_no_charset'
173 'end' => 'th=device-width">'
174 . '<title>Refactoring · GitHub</title>'
175 . '<link rel="search" type="application/opensea',
176 '<title>ignored</title>'
177 . '<meta name="description" content="desc" />'
178 . '<meta name="keywords" content="key1,key2" />',
180 foreach ($data as $key => $line) {
182 $this->assertEquals(strlen($line), $callback($ignore, $line));
184 $this->assertEmpty($charset);
185 $this->assertEquals('Refactoring · GitHub', $title);
186 $this->assertEmpty($desc);
187 $this->assertEmpty($keywords);
191 * Test the download callback with valid values and no charset
193 public function testCurlDownloadCallbackOkHtmlCharset()
195 $callback = get_curl_download_callback(
201 'ut_curl_getinfo_no_charset'
205 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
206 'end' => 'th=device-width">'
207 . '<title>Refactoring · GitHub</title>'
208 . '<link rel="search" type="application/opensea',
209 '<title>ignored</title>'
210 . '<meta name="description" content="desc" />'
211 . '<meta name="keywords" content="key1,key2" />',
213 foreach ($data as $key => $line) {
215 $expected = $key !== 'end' ? strlen($line) : false;
216 $this->assertEquals($expected, $callback($ignore, $line));
217 if ($expected === false) {
221 $this->assertEquals('utf-8', $charset);
222 $this->assertEquals('Refactoring · GitHub', $title);
223 $this->assertEmpty($desc);
224 $this->assertEmpty($keywords);
228 * Test the download callback with valid values and no title
230 public function testCurlDownloadCallbackOkNoTitle()
232 $callback = get_curl_download_callback(
242 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
245 foreach ($data as $key => $line) {
247 $this->assertEquals(strlen($line), $callback($ignore, $line));
249 $this->assertEquals('utf-8', $charset);
250 $this->assertEmpty($title);
251 $this->assertEmpty($desc);
252 $this->assertEmpty($keywords);
256 * Test the download callback with an invalid content type.
258 public function testCurlDownloadCallbackInvalidContentType()
260 $callback = get_curl_download_callback(
266 'ut_curl_getinfo_ct_ko'
269 $this->assertFalse($callback($ignore, ''));
270 $this->assertEmpty($charset);
271 $this->assertEmpty($title);
275 * Test the download callback with an invalid response code.
277 public function testCurlDownloadCallbackInvalidResponseCode()
279 $callback = $callback = get_curl_download_callback(
285 'ut_curl_getinfo_rc_ko'
288 $this->assertFalse($callback($ignore, ''));
289 $this->assertEmpty($charset);
290 $this->assertEmpty($title);
294 * Test the download callback with an invalid content type and response code.
296 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
298 $callback = $callback = get_curl_download_callback(
304 'ut_curl_getinfo_rs_ct_ko'
307 $this->assertFalse($callback($ignore, ''));
308 $this->assertEmpty($charset);
309 $this->assertEmpty($title);
313 * Test the download callback with valid value, and retrieve_description option enabled.
315 public function testCurlDownloadCallbackOkWithDesc()
317 $callback = get_curl_download_callback(
327 'Server: GitHub.com',
328 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
329 'Content-Type: text/html; charset=utf-8',
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" />',
338 foreach ($data as $key => $line) {
340 $expected = $key !== 'end' ? strlen($line) : false;
341 $this->assertEquals($expected, $callback($ignore, $line));
342 if ($expected === false) {
346 $this->assertEquals('utf-8', $charset);
347 $this->assertEquals('Refactoring · GitHub', $title);
348 $this->assertEquals('link desc', $desc);
349 $this->assertEquals('key1 key2', $keywords);
353 * Test the download callback with valid value, and retrieve_description option enabled,
354 * but no desc or keyword defined in the page.
356 public function testCurlDownloadCallbackOkWithDescNotFound()
358 $callback = get_curl_download_callback(
368 'Server: GitHub.com',
369 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
370 'Content-Type: text/html; charset=utf-8',
373 . '<title>Refactoring · GitHub</title>'
374 . '<link rel="search" type="application/opensea',
375 'end' => '<title>ignored</title>',
377 foreach ($data as $key => $line) {
379 $expected = $key !== 'end' ? strlen($line) : false;
380 $this->assertEquals($expected, $callback($ignore, $line));
381 if ($expected === false) {
385 $this->assertEquals('utf-8', $charset);
386 $this->assertEquals('Refactoring · GitHub', $title);
387 $this->assertEmpty($desc);
388 $this->assertEmpty($keywords);
392 * Test count_private.
394 public function testCountPrivateLinks()
396 $refDB = new ReferenceLinkDB();
397 $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks()));
401 * Test text2clickable.
403 public function testText2clickable()
405 $text = 'stuff http://hello.there/is=someone#here otherstuff';
406 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
407 . 'http://hello.there/is=someone#here</a> otherstuff';
408 $processedText = text2clickable($text);
409 $this->assertEquals($expectedText, $processedText);
411 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
412 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
413 . 'http://hello.there/is=someone#here(please)</a> otherstuff';
414 $processedText = text2clickable($text);
415 $this->assertEquals($expectedText, $processedText);
417 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
418 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
419 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
420 . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
421 $processedText = text2clickable($text);
422 $this->assertEquals($expectedText, $processedText);
426 * Test testSpace2nbsp.
428 public function testSpace2nbsp()
430 $text = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
431 $expectedText = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
432 $processedText = space2nbsp($text);
433 $this->assertEquals($expectedText, $processedText);
437 * Test hashtags auto-link.
439 public function testHashtagAutolink()
441 $index = 'http://domain.tld/';
442 $rawDescription = '#hashtag\n
444 test#nothashtag #hashtag \#nothashtag\n
445 test #hashtag #hashtag test #hashtag.test\n
446 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
447 What is #ашок anyway?\n
449 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
451 $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
452 $this->assertNotContains(' #hashtag', $autolinkedDescription);
453 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
454 $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
455 $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
456 $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
457 $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
461 * Test hashtags auto-link without index URL.
463 public function testHashtagAutolinkNoIndex()
465 $rawDescription = 'blabla #hashtag x#nothashtag';
466 $autolinkedDescription = hashtag_autolink($rawDescription);
468 $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
469 $this->assertNotContains(' #hashtag', $autolinkedDescription);
470 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
474 * Test is_note with note URLs.
476 public function testIsNote()
478 $this->assertTrue(is_note('?'));
479 $this->assertTrue(is_note('?abcDEf'));
480 $this->assertTrue(is_note('?_abcDEf#123'));
484 * Test is_note with non note URLs.
486 public function testIsNotNote()
488 $this->assertFalse(is_note(''));
489 $this->assertFalse(is_note('nope'));
490 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
494 * Util function to build an hashtag link.
496 * @param string $hashtag Hashtag name.
497 * @param string $index Index URL.
499 * @return string HTML hashtag link.
501 private function getHashtagLink($hashtag, $index = '')
503 $hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>';
504 return str_replace('$1', $hashtag, $hashtagLink);