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 text2clickable.
394 public function testText2clickable()
396 $text = 'stuff http://hello.there/is=someone#here otherstuff';
397 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
398 . 'http://hello.there/is=someone#here</a> otherstuff';
399 $processedText = text2clickable($text);
400 $this->assertEquals($expectedText, $processedText);
402 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
403 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
404 . 'http://hello.there/is=someone#here(please)</a> otherstuff';
405 $processedText = text2clickable($text);
406 $this->assertEquals($expectedText, $processedText);
408 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
409 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
410 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
411 . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
412 $processedText = text2clickable($text);
413 $this->assertEquals($expectedText, $processedText);
417 * Test testSpace2nbsp.
419 public function testSpace2nbsp()
421 $text = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
422 $expectedText = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
423 $processedText = space2nbsp($text);
424 $this->assertEquals($expectedText, $processedText);
428 * Test hashtags auto-link.
430 public function testHashtagAutolink()
432 $index = 'http://domain.tld/';
433 $rawDescription = '#hashtag\n
435 test#nothashtag #hashtag \#nothashtag\n
436 test #hashtag #hashtag test #hashtag.test\n
437 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
438 What is #ашок anyway?\n
440 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
442 $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
443 $this->assertNotContains(' #hashtag', $autolinkedDescription);
444 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
445 $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
446 $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
447 $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
448 $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
452 * Test hashtags auto-link without index URL.
454 public function testHashtagAutolinkNoIndex()
456 $rawDescription = 'blabla #hashtag x#nothashtag';
457 $autolinkedDescription = hashtag_autolink($rawDescription);
459 $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
460 $this->assertNotContains(' #hashtag', $autolinkedDescription);
461 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
465 * Test is_note with note URLs.
467 public function testIsNote()
469 $this->assertTrue(is_note('?'));
470 $this->assertTrue(is_note('?abcDEf'));
471 $this->assertTrue(is_note('?_abcDEf#123'));
475 * Test is_note with non note URLs.
477 public function testIsNotNote()
479 $this->assertFalse(is_note(''));
480 $this->assertFalse(is_note('nope'));
481 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
485 * Util function to build an hashtag link.
487 * @param string $hashtag Hashtag name.
488 * @param string $index Index URL.
490 * @return string HTML hashtag link.
492 private function getHashtagLink($hashtag, $index = '')
494 $hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>';
495 return str_replace('$1', $hashtag, $hashtagLink);