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 not found.
48 public function testHeadersExtractNonExistentCharset()
51 $this->assertFalse(header_extract_charset($headers));
53 $headers = 'text/html';
54 $this->assertFalse(header_extract_charset($headers));
58 * Test html_extract_charset() when the charset is found.
60 public function testHtmlExtractExistentCharset()
62 $charset = 'x-MacCroatian';
63 $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>';
64 $this->assertEquals(strtolower($charset), html_extract_charset($html));
68 * Test html_extract_charset() when the charset is not found.
70 public function testHtmlExtractNonExistentCharset()
72 $html = '<html><meta>stuff</meta></html>';
73 $this->assertFalse(html_extract_charset($html));
74 $html = '<html><meta>stuff</meta><meta charset=""/></html>';
75 $this->assertFalse(html_extract_charset($html));
79 * Test html_extract_tag() when the tag <meta name= is found.
81 public function testHtmlExtractExistentNameTag()
83 $description = 'Bob and Alice share cookies.';
84 $html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>';
85 $this->assertEquals($description, html_extract_tag('description', $html));
89 * Test html_extract_tag() when the tag <meta name= is not found.
91 public function testHtmlExtractNonExistentNameTag()
93 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
94 $this->assertFalse(html_extract_tag('description', $html));
98 * Test html_extract_tag() when the tag <meta property="og: is found.
100 public function testHtmlExtractExistentOgTag()
102 $description = 'Bob and Alice share cookies.';
103 $html = '<html><meta>stuff2</meta><meta property="og:description" content="' . $description . '"/></html>';
104 $this->assertEquals($description, html_extract_tag('description', $html));
108 * Test html_extract_tag() when the tag <meta property="og: is not found.
110 public function testHtmlExtractNonExistentOgTag()
112 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
113 $this->assertFalse(html_extract_tag('description', $html));
117 * Test the download callback with valid value
119 public function testCurlDownloadCallbackOk()
121 $callback = get_curl_download_callback(
131 'Server: GitHub.com',
132 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
133 'Content-Type: text/html; charset=utf-8',
135 'end' => 'th=device-width">'
136 . '<title>Refactoring · GitHub</title>'
137 . '<link rel="search" type="application/opensea',
138 '<title>ignored</title>'
139 . '<meta name="description" content="desc" />'
140 . '<meta name="keywords" content="key1,key2" />',
142 foreach ($data as $key => $line) {
144 $expected = $key !== 'end' ? strlen($line) : false;
145 $this->assertEquals($expected, $callback($ignore, $line));
146 if ($expected === false) {
150 $this->assertEquals('utf-8', $charset);
151 $this->assertEquals('Refactoring · GitHub', $title);
152 $this->assertEmpty($desc);
153 $this->assertEmpty($keywords);
157 * Test the download callback with valid values and no charset
159 public function testCurlDownloadCallbackOkNoCharset()
161 $callback = get_curl_download_callback(
167 'ut_curl_getinfo_no_charset'
171 'end' => 'th=device-width">'
172 . '<title>Refactoring · GitHub</title>'
173 . '<link rel="search" type="application/opensea',
174 '<title>ignored</title>'
175 . '<meta name="description" content="desc" />'
176 . '<meta name="keywords" content="key1,key2" />',
178 foreach ($data as $key => $line) {
180 $this->assertEquals(strlen($line), $callback($ignore, $line));
182 $this->assertEmpty($charset);
183 $this->assertEquals('Refactoring · GitHub', $title);
184 $this->assertEmpty($desc);
185 $this->assertEmpty($keywords);
189 * Test the download callback with valid values and no charset
191 public function testCurlDownloadCallbackOkHtmlCharset()
193 $callback = get_curl_download_callback(
199 'ut_curl_getinfo_no_charset'
203 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
204 'end' => 'th=device-width">'
205 . '<title>Refactoring · GitHub</title>'
206 . '<link rel="search" type="application/opensea',
207 '<title>ignored</title>'
208 . '<meta name="description" content="desc" />'
209 . '<meta name="keywords" content="key1,key2" />',
211 foreach ($data as $key => $line) {
213 $expected = $key !== 'end' ? strlen($line) : false;
214 $this->assertEquals($expected, $callback($ignore, $line));
215 if ($expected === false) {
219 $this->assertEquals('utf-8', $charset);
220 $this->assertEquals('Refactoring · GitHub', $title);
221 $this->assertEmpty($desc);
222 $this->assertEmpty($keywords);
226 * Test the download callback with valid values and no title
228 public function testCurlDownloadCallbackOkNoTitle()
230 $callback = get_curl_download_callback(
240 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
243 foreach ($data as $key => $line) {
245 $this->assertEquals(strlen($line), $callback($ignore, $line));
247 $this->assertEquals('utf-8', $charset);
248 $this->assertEmpty($title);
249 $this->assertEmpty($desc);
250 $this->assertEmpty($keywords);
254 * Test the download callback with an invalid content type.
256 public function testCurlDownloadCallbackInvalidContentType()
258 $callback = get_curl_download_callback(
264 'ut_curl_getinfo_ct_ko'
267 $this->assertFalse($callback($ignore, ''));
268 $this->assertEmpty($charset);
269 $this->assertEmpty($title);
273 * Test the download callback with an invalid response code.
275 public function testCurlDownloadCallbackInvalidResponseCode()
277 $callback = $callback = get_curl_download_callback(
283 'ut_curl_getinfo_rc_ko'
286 $this->assertFalse($callback($ignore, ''));
287 $this->assertEmpty($charset);
288 $this->assertEmpty($title);
292 * Test the download callback with an invalid content type and response code.
294 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
296 $callback = $callback = get_curl_download_callback(
302 'ut_curl_getinfo_rs_ct_ko'
305 $this->assertFalse($callback($ignore, ''));
306 $this->assertEmpty($charset);
307 $this->assertEmpty($title);
311 * Test the download callback with valid value, and retrieve_description option enabled.
313 public function testCurlDownloadCallbackOkWithDesc()
315 $callback = get_curl_download_callback(
325 'Server: GitHub.com',
326 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
327 'Content-Type: text/html; charset=utf-8',
330 . '<title>Refactoring · GitHub</title>'
331 . '<link rel="search" type="application/opensea',
332 'end' => '<title>ignored</title>'
333 . '<meta name="description" content="link desc" />'
334 . '<meta name="keywords" content="key1,key2" />',
336 foreach ($data as $key => $line) {
338 $expected = $key !== 'end' ? strlen($line) : false;
339 $this->assertEquals($expected, $callback($ignore, $line));
340 if ($expected === false) {
344 $this->assertEquals('utf-8', $charset);
345 $this->assertEquals('Refactoring · GitHub', $title);
346 $this->assertEquals('link desc', $desc);
347 $this->assertEquals('key1 key2', $keywords);
351 * Test the download callback with valid value, and retrieve_description option enabled,
352 * but no desc or keyword defined in the page.
354 public function testCurlDownloadCallbackOkWithDescNotFound()
356 $callback = get_curl_download_callback(
366 'Server: GitHub.com',
367 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
368 'Content-Type: text/html; charset=utf-8',
371 . '<title>Refactoring · GitHub</title>'
372 . '<link rel="search" type="application/opensea',
373 'end' => '<title>ignored</title>',
375 foreach ($data as $key => $line) {
377 $expected = $key !== 'end' ? strlen($line) : false;
378 $this->assertEquals($expected, $callback($ignore, $line));
379 if ($expected === false) {
383 $this->assertEquals('utf-8', $charset);
384 $this->assertEquals('Refactoring · GitHub', $title);
385 $this->assertEmpty($desc);
386 $this->assertEmpty($keywords);
390 * Test text2clickable.
392 public function testText2clickable()
394 $text = 'stuff http://hello.there/is=someone#here otherstuff';
395 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
396 . 'http://hello.there/is=someone#here</a> otherstuff';
397 $processedText = text2clickable($text);
398 $this->assertEquals($expectedText, $processedText);
400 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
401 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
402 . 'http://hello.there/is=someone#here(please)</a> otherstuff';
403 $processedText = text2clickable($text);
404 $this->assertEquals($expectedText, $processedText);
406 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
407 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
408 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
409 . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
410 $processedText = text2clickable($text);
411 $this->assertEquals($expectedText, $processedText);
415 * Test testSpace2nbsp.
417 public function testSpace2nbsp()
419 $text = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
420 $expectedText = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
421 $processedText = space2nbsp($text);
422 $this->assertEquals($expectedText, $processedText);
426 * Test hashtags auto-link.
428 public function testHashtagAutolink()
430 $index = 'http://domain.tld/';
431 $rawDescription = '#hashtag\n
433 test#nothashtag #hashtag \#nothashtag\n
434 test #hashtag #hashtag test #hashtag.test\n
435 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
436 What is #ашок anyway?\n
438 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
440 $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
441 $this->assertNotContains(' #hashtag', $autolinkedDescription);
442 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
443 $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
444 $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
445 $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
446 $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
450 * Test hashtags auto-link without index URL.
452 public function testHashtagAutolinkNoIndex()
454 $rawDescription = 'blabla #hashtag x#nothashtag';
455 $autolinkedDescription = hashtag_autolink($rawDescription);
457 $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
458 $this->assertNotContains(' #hashtag', $autolinkedDescription);
459 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
463 * Test is_note with note URLs.
465 public function testIsNote()
467 $this->assertTrue(is_note('?'));
468 $this->assertTrue(is_note('?abcDEf'));
469 $this->assertTrue(is_note('?_abcDEf#123'));
473 * Test is_note with non note URLs.
475 public function testIsNotNote()
477 $this->assertFalse(is_note(''));
478 $this->assertFalse(is_note('nope'));
479 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
483 * Util function to build an hashtag link.
485 * @param string $hashtag Hashtag name.
486 * @param string $index Index URL.
488 * @return string HTML hashtag link.
490 private function getHashtagLink($hashtag, $index = '')
492 $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>';
493 return str_replace('$1', $hashtag, $hashtagLink);