3 namespace Shaarli\Bookmark
;
5 use PHPUnit\Framework\TestCase
;
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.';
97 $html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>';
98 $this->assertEquals($description, html_extract_tag('description', $html));
102 * Test html_extract_tag() when the tag <meta name= is not found.
104 public function testHtmlExtractNonExistentNameTag()
106 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
107 $this->assertFalse(html_extract_tag('description', $html));
111 * Test html_extract_tag() when the tag <meta property="og: is found.
113 public function testHtmlExtractExistentOgTag()
115 $description = 'Bob and Alice share cookies.';
116 $html = '<html><meta>stuff2</meta><meta property="og:description" content="' . $description . '"/></html>';
117 $this->assertEquals($description, html_extract_tag('description', $html));
121 * Test html_extract_tag() when the tag <meta property="og: is not found.
123 public function testHtmlExtractNonExistentOgTag()
125 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
126 $this->assertFalse(html_extract_tag('description', $html));
130 * Test the download callback with valid value
132 public function testCurlDownloadCallbackOk()
134 $callback = get_curl_download_callback(
144 'Server: GitHub.com',
145 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
146 'Content-Type: text/html; charset=utf-8',
148 'end' => 'th=device-width">'
149 . '<title>Refactoring · GitHub</title>'
150 . '<link rel="search" type="application/opensea',
151 '<title>ignored</title>'
152 . '<meta name="description" content="desc" />'
153 . '<meta name="keywords" content="key1,key2" />',
155 foreach ($data as $key => $line) {
157 $expected = $key !== 'end' ? strlen($line) : false;
158 $this->assertEquals($expected, $callback($ignore, $line));
159 if ($expected === false) {
163 $this->assertEquals('utf-8', $charset);
164 $this->assertEquals('Refactoring · GitHub', $title);
165 $this->assertEmpty($desc);
166 $this->assertEmpty($keywords);
170 * Test the download callback with valid values and no charset
172 public function testCurlDownloadCallbackOkNoCharset()
174 $callback = get_curl_download_callback(
180 'ut_curl_getinfo_no_charset'
184 'end' => 'th=device-width">'
185 . '<title>Refactoring · GitHub</title>'
186 . '<link rel="search" type="application/opensea',
187 '<title>ignored</title>'
188 . '<meta name="description" content="desc" />'
189 . '<meta name="keywords" content="key1,key2" />',
191 foreach ($data as $key => $line) {
193 $this->assertEquals(strlen($line), $callback($ignore, $line));
195 $this->assertEmpty($charset);
196 $this->assertEquals('Refactoring · GitHub', $title);
197 $this->assertEmpty($desc);
198 $this->assertEmpty($keywords);
202 * Test the download callback with valid values and no charset
204 public function testCurlDownloadCallbackOkHtmlCharset()
206 $callback = get_curl_download_callback(
212 'ut_curl_getinfo_no_charset'
216 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
217 'end' => 'th=device-width">'
218 . '<title>Refactoring · GitHub</title>'
219 . '<link rel="search" type="application/opensea',
220 '<title>ignored</title>'
221 . '<meta name="description" content="desc" />'
222 . '<meta name="keywords" content="key1,key2" />',
224 foreach ($data as $key => $line) {
226 $expected = $key !== 'end' ? strlen($line) : false;
227 $this->assertEquals($expected, $callback($ignore, $line));
228 if ($expected === false) {
232 $this->assertEquals('utf-8', $charset);
233 $this->assertEquals('Refactoring · GitHub', $title);
234 $this->assertEmpty($desc);
235 $this->assertEmpty($keywords);
239 * Test the download callback with valid values and no title
241 public function testCurlDownloadCallbackOkNoTitle()
243 $callback = get_curl_download_callback(
253 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
256 foreach ($data as $key => $line) {
258 $this->assertEquals(strlen($line), $callback($ignore, $line));
260 $this->assertEquals('utf-8', $charset);
261 $this->assertEmpty($title);
262 $this->assertEmpty($desc);
263 $this->assertEmpty($keywords);
267 * Test the download callback with an invalid content type.
269 public function testCurlDownloadCallbackInvalidContentType()
271 $callback = get_curl_download_callback(
277 'ut_curl_getinfo_ct_ko'
280 $this->assertFalse($callback($ignore, ''));
281 $this->assertEmpty($charset);
282 $this->assertEmpty($title);
286 * Test the download callback with an invalid response code.
288 public function testCurlDownloadCallbackInvalidResponseCode()
290 $callback = $callback = get_curl_download_callback(
296 'ut_curl_getinfo_rc_ko'
299 $this->assertFalse($callback($ignore, ''));
300 $this->assertEmpty($charset);
301 $this->assertEmpty($title);
305 * Test the download callback with an invalid content type and response code.
307 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
309 $callback = $callback = get_curl_download_callback(
315 'ut_curl_getinfo_rs_ct_ko'
318 $this->assertFalse($callback($ignore, ''));
319 $this->assertEmpty($charset);
320 $this->assertEmpty($title);
324 * Test the download callback with valid value, and retrieve_description option enabled.
326 public function testCurlDownloadCallbackOkWithDesc()
328 $callback = get_curl_download_callback(
338 'Server: GitHub.com',
339 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
340 'Content-Type: text/html; charset=utf-8',
343 . '<title>Refactoring · GitHub</title>'
344 . '<link rel="search" type="application/opensea',
345 'end' => '<title>ignored</title>'
346 . '<meta name="description" content="link desc" />'
347 . '<meta name="keywords" content="key1,key2" />',
349 foreach ($data as $key => $line) {
351 $expected = $key !== 'end' ? strlen($line) : false;
352 $this->assertEquals($expected, $callback($ignore, $line));
353 if ($expected === false) {
357 $this->assertEquals('utf-8', $charset);
358 $this->assertEquals('Refactoring · GitHub', $title);
359 $this->assertEquals('link desc', $desc);
360 $this->assertEquals('key1 key2', $keywords);
364 * Test the download callback with valid value, and retrieve_description option enabled,
365 * but no desc or keyword defined in the page.
367 public function testCurlDownloadCallbackOkWithDescNotFound()
369 $callback = get_curl_download_callback(
379 'Server: GitHub.com',
380 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
381 'Content-Type: text/html; charset=utf-8',
384 . '<title>Refactoring · GitHub</title>'
385 . '<link rel="search" type="application/opensea',
386 'end' => '<title>ignored</title>',
388 foreach ($data as $key => $line) {
390 $expected = $key !== 'end' ? strlen($line) : false;
391 $this->assertEquals($expected, $callback($ignore, $line));
392 if ($expected === false) {
396 $this->assertEquals('utf-8', $charset);
397 $this->assertEquals('Refactoring · GitHub', $title);
398 $this->assertEmpty($desc);
399 $this->assertEmpty($keywords);
403 * Test text2clickable.
405 public function testText2clickable()
407 $text = 'stuff http://hello.there/is=someone#here otherstuff';
408 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
409 . 'http://hello.there/is=someone#here</a> otherstuff';
410 $processedText = text2clickable($text);
411 $this->assertEquals($expectedText, $processedText);
413 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
414 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
415 . 'http://hello.there/is=someone#here(please)</a> otherstuff';
416 $processedText = text2clickable($text);
417 $this->assertEquals($expectedText, $processedText);
419 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
420 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
421 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
422 . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
423 $processedText = text2clickable($text);
424 $this->assertEquals($expectedText, $processedText);
428 * Test testSpace2nbsp.
430 public function testSpace2nbsp()
432 $text = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
433 $expectedText = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
434 $processedText = space2nbsp($text);
435 $this->assertEquals($expectedText, $processedText);
439 * Test hashtags auto-link.
441 public function testHashtagAutolink()
443 $index = 'http://domain.tld/';
444 $rawDescription = '#hashtag\n
446 test#nothashtag #hashtag \#nothashtag\n
447 test #hashtag #hashtag test #hashtag.test\n
448 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
449 What is #ашок anyway?\n
451 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
453 $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
454 $this->assertNotContains(' #hashtag', $autolinkedDescription);
455 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
456 $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
457 $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
458 $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
459 $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
463 * Test hashtags auto-link without index URL.
465 public function testHashtagAutolinkNoIndex()
467 $rawDescription = 'blabla #hashtag x#nothashtag';
468 $autolinkedDescription = hashtag_autolink($rawDescription);
470 $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
471 $this->assertNotContains(' #hashtag', $autolinkedDescription);
472 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
476 * Test is_note with note URLs.
478 public function testIsNote()
480 $this->assertTrue(is_note('?'));
481 $this->assertTrue(is_note('?abcDEf'));
482 $this->assertTrue(is_note('?_abcDEf#123'));
486 * Test is_note with non note URLs.
488 public function testIsNotNote()
490 $this->assertFalse(is_note(''));
491 $this->assertFalse(is_note('nope'));
492 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
496 * Util function to build an hashtag link.
498 * @param string $hashtag Hashtag name.
499 * @param string $index Index URL.
501 * @return string HTML hashtag link.
503 private function getHashtagLink($hashtag, $index = '')
505 $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>';
506 return str_replace('$1', $hashtag, $hashtagLink);