3 namespace Shaarli\Bookmark
;
7 require_once 'tests/utils/CurlUtils.php';
10 * Class LinkUtilsTest.
12 class LinkUtilsTest
extends \PHPUnit\Framework\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 the download callback with valid value
81 public function testCurlDownloadCallbackOk()
83 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok');
87 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
88 'Content-Type: text/html; charset=utf-8',
90 'end' => 'th=device-width">'
91 . '<title>Refactoring · GitHub</title>'
92 . '<link rel="search" type="application/opensea',
93 '<title>ignored</title>',
95 foreach ($data as $key => $line) {
97 $expected = $key !== 'end' ? strlen($line) : false;
98 $this->assertEquals($expected, $callback($ignore, $line));
99 if ($expected === false) {
103 $this->assertEquals('utf-8', $charset);
104 $this->assertEquals('Refactoring · GitHub', $title);
108 * Test the download callback with valid values and no charset
110 public function testCurlDownloadCallbackOkNoCharset()
112 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset');
115 'end' => 'th=device-width">'
116 . '<title>Refactoring · GitHub</title>'
117 . '<link rel="search" type="application/opensea',
118 '<title>ignored</title>',
120 foreach ($data as $key => $line) {
122 $this->assertEquals(strlen($line), $callback($ignore, $line));
124 $this->assertEmpty($charset);
125 $this->assertEquals('Refactoring · GitHub', $title);
129 * Test the download callback with valid values and no charset
131 public function testCurlDownloadCallbackOkHtmlCharset()
133 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset');
136 '<meta http-equiv="Content-Type" content="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>',
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);
155 * Test the download callback with valid values and no title
157 public function testCurlDownloadCallbackOkNoTitle()
159 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok');
162 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
165 foreach ($data as $key => $line) {
167 $this->assertEquals(strlen($line), $callback($ignore, $line));
169 $this->assertEquals('utf-8', $charset);
170 $this->assertEmpty($title);
174 * Test the download callback with an invalid content type.
176 public function testCurlDownloadCallbackInvalidContentType()
178 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ct_ko');
180 $this->assertFalse($callback($ignore, ''));
181 $this->assertEmpty($charset);
182 $this->assertEmpty($title);
186 * Test the download callback with an invalid response code.
188 public function testCurlDownloadCallbackInvalidResponseCode()
190 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rc_ko');
192 $this->assertFalse($callback($ignore, ''));
193 $this->assertEmpty($charset);
194 $this->assertEmpty($title);
198 * Test the download callback with an invalid content type and response code.
200 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
202 $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rs_ct_ko');
204 $this->assertFalse($callback($ignore, ''));
205 $this->assertEmpty($charset);
206 $this->assertEmpty($title);
210 * Test count_private.
212 public function testCountPrivateLinks()
214 $refDB = new ReferenceLinkDB();
215 $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks()));
219 * Test text2clickable.
221 public function testText2clickable()
223 $text = 'stuff http://hello.there/is=someone#here otherstuff';
224 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
225 . 'http://hello.there/is=someone#here</a> otherstuff';
226 $processedText = text2clickable($text);
227 $this->assertEquals($expectedText, $processedText);
229 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
230 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
231 . 'http://hello.there/is=someone#here(please)</a> otherstuff';
232 $processedText = text2clickable($text);
233 $this->assertEquals($expectedText, $processedText);
235 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
236 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
237 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
238 . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
239 $processedText = text2clickable($text);
240 $this->assertEquals($expectedText, $processedText);
244 * Test testSpace2nbsp.
246 public function testSpace2nbsp()
248 $text = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
249 $expectedText = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
250 $processedText = space2nbsp($text);
251 $this->assertEquals($expectedText, $processedText);
255 * Test hashtags auto-link.
257 public function testHashtagAutolink()
259 $index = 'http://domain.tld/';
260 $rawDescription = '#hashtag\n
262 test#nothashtag #hashtag \#nothashtag\n
263 test #hashtag #hashtag test #hashtag.test\n
264 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
265 What is #ашок anyway?\n
267 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
269 $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
270 $this->assertNotContains(' #hashtag', $autolinkedDescription);
271 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
272 $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
273 $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
274 $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
275 $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
279 * Test hashtags auto-link without index URL.
281 public function testHashtagAutolinkNoIndex()
283 $rawDescription = 'blabla #hashtag x#nothashtag';
284 $autolinkedDescription = hashtag_autolink($rawDescription);
286 $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
287 $this->assertNotContains(' #hashtag', $autolinkedDescription);
288 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
292 * Test is_note with note URLs.
294 public function testIsNote()
296 $this->assertTrue(is_note('?'));
297 $this->assertTrue(is_note('?abcDEf'));
298 $this->assertTrue(is_note('?_abcDEf#123'));
302 * Test is_note with non note URLs.
304 public function testIsNotNote()
306 $this->assertFalse(is_note(''));
307 $this->assertFalse(is_note('nope'));
308 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
312 * Util function to build an hashtag link.
314 * @param string $hashtag Hashtag name.
315 * @param string $index Index URL.
317 * @return string HTML hashtag link.
319 private function getHashtagLink($hashtag, $index = '')
321 $hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>';
322 return str_replace('$1', $hashtag, $hashtagLink);