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 without a redirector being set.
221 public function testText2clickableWithoutRedirector()
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 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
237 . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
238 $processedText = text2clickable($text, '');
239 $this->assertEquals($expectedText, $processedText);
243 * Test text2clickable with a redirector set.
245 public function testText2clickableWithRedirector()
247 $text = 'stuff http://hello.there/is=someone#here otherstuff';
248 $redirector = 'http://redirector.to';
249 $expectedText = 'stuff <a href="' .
251 urlencode('http://hello.there/is=someone#here') .
252 '">http://hello.there/is=someone#here</a> otherstuff';
253 $processedText = text2clickable($text, $redirector);
254 $this->assertEquals($expectedText, $processedText);
258 * Test text2clickable a redirector set and without URL encode.
260 public function testText2clickableWithRedirectorDontEncode()
262 $text = 'stuff http://hello.there/?is=someone&or=something#here otherstuff';
263 $redirector = 'http://redirector.to';
264 $expectedText = 'stuff <a href="' .
266 'http://hello.there/?is=someone&or=something#here' .
267 '">http://hello.there/?is=someone&or=something#here</a> otherstuff';
268 $processedText = text2clickable($text, $redirector, false);
269 $this->assertEquals($expectedText, $processedText);
273 * Test testSpace2nbsp.
275 public function testSpace2nbsp()
277 $text = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
278 $expectedText = ' Are you thrilled by flags ?' . PHP_EOL
. ' Really?';
279 $processedText = space2nbsp($text);
280 $this->assertEquals($expectedText, $processedText);
284 * Test hashtags auto-link.
286 public function testHashtagAutolink()
288 $index = 'http://domain.tld/';
289 $rawDescription = '#hashtag\n
291 test#nothashtag #hashtag \#nothashtag\n
292 test #hashtag #hashtag test #hashtag.test\n
293 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
294 What is #ашок anyway?\n
296 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
298 $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
299 $this->assertNotContains(' #hashtag', $autolinkedDescription);
300 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
301 $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
302 $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
303 $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
304 $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
308 * Test hashtags auto-link without index URL.
310 public function testHashtagAutolinkNoIndex()
312 $rawDescription = 'blabla #hashtag x#nothashtag';
313 $autolinkedDescription = hashtag_autolink($rawDescription);
315 $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
316 $this->assertNotContains(' #hashtag', $autolinkedDescription);
317 $this->assertNotContains('>#nothashtag', $autolinkedDescription);
321 * Util function to build an hashtag link.
323 * @param string $hashtag Hashtag name.
324 * @param string $index Index URL.
326 * @return string HTML hashtag link.
328 private function getHashtagLink($hashtag, $index = '')
330 $hashtagLink = '<a href="' . $index . '?addtag=$1" title="Hashtag $1">#$1</a>';
331 return str_replace('$1', $hashtag, $hashtagLink);