]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/LinkUtilsTest.php
Merge pull request #1575 from ArthurHoaro/feature/php8
[github/shaarli/Shaarli.git] / tests / bookmark / LinkUtilsTest.php
1 <?php
2
3 namespace Shaarli\Bookmark;
4
5 use Shaarli\TestCase;
6
7 require_once 'tests/utils/CurlUtils.php';
8
9 /**
10 * Class LinkUtilsTest.
11 */
12 class LinkUtilsTest extends TestCase
13 {
14 /**
15 * Test html_extract_title() when the title is found.
16 */
17 public function testHtmlExtractExistentTitle()
18 {
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));
24 }
25
26 /**
27 * Test html_extract_title() when the title is not found.
28 */
29 public function testHtmlExtractNonExistentTitle()
30 {
31 $html = '<html><meta>stuff</meta></html>';
32 $this->assertFalse(html_extract_title($html));
33 }
34
35 /**
36 * Test headers_extract_charset() when the charset is found.
37 */
38 public function testHeadersExtractExistentCharset()
39 {
40 $charset = 'x-MacCroatian';
41 $headers = 'text/html; charset=' . $charset;
42 $this->assertEquals(strtolower($charset), header_extract_charset($headers));
43 }
44
45 /**
46 * Test headers_extract_charset() when the charset is found with odd quotes.
47 */
48 public function testHeadersExtractExistentCharsetWithQuotes()
49 {
50 $charset = 'x-MacCroatian';
51 $headers = 'text/html; charset="' . $charset . '"otherstuff="test"';
52 $this->assertEquals(strtolower($charset), header_extract_charset($headers));
53
54 $headers = 'text/html; charset=\'' . $charset . '\'otherstuff="test"';
55 $this->assertEquals(strtolower($charset), header_extract_charset($headers));
56 }
57
58 /**
59 * Test headers_extract_charset() when the charset is not found.
60 */
61 public function testHeadersExtractNonExistentCharset()
62 {
63 $headers = '';
64 $this->assertFalse(header_extract_charset($headers));
65
66 $headers = 'text/html';
67 $this->assertFalse(header_extract_charset($headers));
68 }
69
70 /**
71 * Test html_extract_charset() when the charset is found.
72 */
73 public function testHtmlExtractExistentCharset()
74 {
75 $charset = 'x-MacCroatian';
76 $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>';
77 $this->assertEquals(strtolower($charset), html_extract_charset($html));
78 }
79
80 /**
81 * Test html_extract_charset() when the charset is not found.
82 */
83 public function testHtmlExtractNonExistentCharset()
84 {
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));
89 }
90
91 /**
92 * Test html_extract_tag() when the tag <meta name= is found.
93 */
94 public function testHtmlExtractExistentNameTag()
95 {
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));
99 }
100
101 /**
102 * Test html_extract_tag() when the tag <meta name= is not found.
103 */
104 public function testHtmlExtractNonExistentNameTag()
105 {
106 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
107 $this->assertFalse(html_extract_tag('description', $html));
108 }
109
110 /**
111 * Test html_extract_tag() when the tag <meta property="og: is found.
112 */
113 public function testHtmlExtractExistentOgTag()
114 {
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));
118 }
119
120 /**
121 * Test html_extract_tag() when the tag <meta property="og: is not found.
122 */
123 public function testHtmlExtractNonExistentOgTag()
124 {
125 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
126 $this->assertFalse(html_extract_tag('description', $html));
127 }
128
129 /**
130 * Test the download callback with valid value
131 */
132 public function testCurlDownloadCallbackOk()
133 {
134 $callback = get_curl_download_callback(
135 $charset,
136 $title,
137 $desc,
138 $keywords,
139 false,
140 'ut_curl_getinfo_ok'
141 );
142 $data = [
143 'HTTP/1.1 200 OK',
144 'Server: GitHub.com',
145 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
146 'Content-Type: text/html; charset=utf-8',
147 'Status: 200 OK',
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" />',
154 ];
155 foreach ($data as $key => $line) {
156 $ignore = null;
157 $expected = $key !== 'end' ? strlen($line) : false;
158 $this->assertEquals($expected, $callback($ignore, $line));
159 if ($expected === false) {
160 break;
161 }
162 }
163 $this->assertEquals('utf-8', $charset);
164 $this->assertEquals('Refactoring · GitHub', $title);
165 $this->assertEmpty($desc);
166 $this->assertEmpty($keywords);
167 }
168
169 /**
170 * Test the download callback with valid values and no charset
171 */
172 public function testCurlDownloadCallbackOkNoCharset()
173 {
174 $callback = get_curl_download_callback(
175 $charset,
176 $title,
177 $desc,
178 $keywords,
179 false,
180 'ut_curl_getinfo_no_charset'
181 );
182 $data = [
183 'HTTP/1.1 200 OK',
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" />',
190 ];
191 foreach ($data as $key => $line) {
192 $ignore = null;
193 $this->assertEquals(strlen($line), $callback($ignore, $line));
194 }
195 $this->assertEmpty($charset);
196 $this->assertEquals('Refactoring · GitHub', $title);
197 $this->assertEmpty($desc);
198 $this->assertEmpty($keywords);
199 }
200
201 /**
202 * Test the download callback with valid values and no charset
203 */
204 public function testCurlDownloadCallbackOkHtmlCharset()
205 {
206 $callback = get_curl_download_callback(
207 $charset,
208 $title,
209 $desc,
210 $keywords,
211 false,
212 'ut_curl_getinfo_no_charset'
213 );
214 $data = [
215 'HTTP/1.1 200 OK',
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" />',
223 ];
224 foreach ($data as $key => $line) {
225 $ignore = null;
226 $expected = $key !== 'end' ? strlen($line) : false;
227 $this->assertEquals($expected, $callback($ignore, $line));
228 if ($expected === false) {
229 break;
230 }
231 }
232 $this->assertEquals('utf-8', $charset);
233 $this->assertEquals('Refactoring · GitHub', $title);
234 $this->assertEmpty($desc);
235 $this->assertEmpty($keywords);
236 }
237
238 /**
239 * Test the download callback with valid values and no title
240 */
241 public function testCurlDownloadCallbackOkNoTitle()
242 {
243 $callback = get_curl_download_callback(
244 $charset,
245 $title,
246 $desc,
247 $keywords,
248 false,
249 'ut_curl_getinfo_ok'
250 );
251 $data = [
252 'HTTP/1.1 200 OK',
253 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
254 'ignored',
255 ];
256 foreach ($data as $key => $line) {
257 $ignore = null;
258 $this->assertEquals(strlen($line), $callback($ignore, $line));
259 }
260 $this->assertEquals('utf-8', $charset);
261 $this->assertEmpty($title);
262 $this->assertEmpty($desc);
263 $this->assertEmpty($keywords);
264 }
265
266 /**
267 * Test the download callback with an invalid content type.
268 */
269 public function testCurlDownloadCallbackInvalidContentType()
270 {
271 $callback = get_curl_download_callback(
272 $charset,
273 $title,
274 $desc,
275 $keywords,
276 false,
277 'ut_curl_getinfo_ct_ko'
278 );
279 $ignore = null;
280 $this->assertFalse($callback($ignore, ''));
281 $this->assertEmpty($charset);
282 $this->assertEmpty($title);
283 }
284
285 /**
286 * Test the download callback with an invalid response code.
287 */
288 public function testCurlDownloadCallbackInvalidResponseCode()
289 {
290 $callback = $callback = get_curl_download_callback(
291 $charset,
292 $title,
293 $desc,
294 $keywords,
295 false,
296 'ut_curl_getinfo_rc_ko'
297 );
298 $ignore = null;
299 $this->assertFalse($callback($ignore, ''));
300 $this->assertEmpty($charset);
301 $this->assertEmpty($title);
302 }
303
304 /**
305 * Test the download callback with an invalid content type and response code.
306 */
307 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
308 {
309 $callback = $callback = get_curl_download_callback(
310 $charset,
311 $title,
312 $desc,
313 $keywords,
314 false,
315 'ut_curl_getinfo_rs_ct_ko'
316 );
317 $ignore = null;
318 $this->assertFalse($callback($ignore, ''));
319 $this->assertEmpty($charset);
320 $this->assertEmpty($title);
321 }
322
323 /**
324 * Test the download callback with valid value, and retrieve_description option enabled.
325 */
326 public function testCurlDownloadCallbackOkWithDesc()
327 {
328 $callback = get_curl_download_callback(
329 $charset,
330 $title,
331 $desc,
332 $keywords,
333 true,
334 'ut_curl_getinfo_ok'
335 );
336 $data = [
337 'HTTP/1.1 200 OK',
338 'Server: GitHub.com',
339 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
340 'Content-Type: text/html; charset=utf-8',
341 'Status: 200 OK',
342 'th=device-width">'
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" />',
348 ];
349 foreach ($data as $key => $line) {
350 $ignore = null;
351 $expected = $key !== 'end' ? strlen($line) : false;
352 $this->assertEquals($expected, $callback($ignore, $line));
353 if ($expected === false) {
354 break;
355 }
356 }
357 $this->assertEquals('utf-8', $charset);
358 $this->assertEquals('Refactoring · GitHub', $title);
359 $this->assertEquals('link desc', $desc);
360 $this->assertEquals('key1 key2', $keywords);
361 }
362
363 /**
364 * Test the download callback with valid value, and retrieve_description option enabled,
365 * but no desc or keyword defined in the page.
366 */
367 public function testCurlDownloadCallbackOkWithDescNotFound()
368 {
369 $callback = get_curl_download_callback(
370 $charset,
371 $title,
372 $desc,
373 $keywords,
374 true,
375 'ut_curl_getinfo_ok'
376 );
377 $data = [
378 'HTTP/1.1 200 OK',
379 'Server: GitHub.com',
380 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
381 'Content-Type: text/html; charset=utf-8',
382 'Status: 200 OK',
383 'th=device-width">'
384 . '<title>Refactoring · GitHub</title>'
385 . '<link rel="search" type="application/opensea',
386 'end' => '<title>ignored</title>',
387 ];
388 foreach ($data as $key => $line) {
389 $ignore = null;
390 $expected = $key !== 'end' ? strlen($line) : false;
391 $this->assertEquals($expected, $callback($ignore, $line));
392 if ($expected === false) {
393 break;
394 }
395 }
396 $this->assertEquals('utf-8', $charset);
397 $this->assertEquals('Refactoring · GitHub', $title);
398 $this->assertEmpty($desc);
399 $this->assertEmpty($keywords);
400 }
401
402 /**
403 * Test text2clickable.
404 */
405 public function testText2clickable()
406 {
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);
412
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);
418
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);
425 }
426
427 /**
428 * Test testSpace2nbsp.
429 */
430 public function testSpace2nbsp()
431 {
432 $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?';
433 $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?' . PHP_EOL . '&nbsp;Really?';
434 $processedText = space2nbsp($text);
435 $this->assertEquals($expectedText, $processedText);
436 }
437
438 /**
439 * Test hashtags auto-link.
440 */
441 public function testHashtagAutolink()
442 {
443 $index = 'http://domain.tld/';
444 $rawDescription = '#hashtag\n
445 # nothashtag\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
450 カタカナ #カタカナ」カタカナ\n';
451 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
452
453 $this->assertContainsPolyfill($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
454 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
455 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
456 $this->assertContainsPolyfill($this->getHashtagLink('ашок', $index), $autolinkedDescription);
457 $this->assertContainsPolyfill($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
458 $this->assertContainsPolyfill($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
459 $this->assertNotContainsPolyfill($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
460 }
461
462 /**
463 * Test hashtags auto-link without index URL.
464 */
465 public function testHashtagAutolinkNoIndex()
466 {
467 $rawDescription = 'blabla #hashtag x#nothashtag';
468 $autolinkedDescription = hashtag_autolink($rawDescription);
469
470 $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription);
471 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
472 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
473 }
474
475 /**
476 * Test is_note with note URLs.
477 */
478 public function testIsNote()
479 {
480 $this->assertTrue(is_note('?'));
481 $this->assertTrue(is_note('?abcDEf'));
482 $this->assertTrue(is_note('?_abcDEf#123'));
483 }
484
485 /**
486 * Test is_note with non note URLs.
487 */
488 public function testIsNotNote()
489 {
490 $this->assertFalse(is_note(''));
491 $this->assertFalse(is_note('nope'));
492 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
493 }
494
495 /**
496 * Util function to build an hashtag link.
497 *
498 * @param string $hashtag Hashtag name.
499 * @param string $index Index URL.
500 *
501 * @return string HTML hashtag link.
502 */
503 private function getHashtagLink($hashtag, $index = '')
504 {
505 $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>';
506 return str_replace('$1', $hashtag, $hashtagLink);
507 }
508 }