]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/LinkUtilsTest.php
Compatibility with PHPUnit 9
[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 not found.
47 */
48 public function testHeadersExtractNonExistentCharset()
49 {
50 $headers = '';
51 $this->assertFalse(header_extract_charset($headers));
52
53 $headers = 'text/html';
54 $this->assertFalse(header_extract_charset($headers));
55 }
56
57 /**
58 * Test html_extract_charset() when the charset is found.
59 */
60 public function testHtmlExtractExistentCharset()
61 {
62 $charset = 'x-MacCroatian';
63 $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>';
64 $this->assertEquals(strtolower($charset), html_extract_charset($html));
65 }
66
67 /**
68 * Test html_extract_charset() when the charset is not found.
69 */
70 public function testHtmlExtractNonExistentCharset()
71 {
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));
76 }
77
78 /**
79 * Test html_extract_tag() when the tag <meta name= is found.
80 */
81 public function testHtmlExtractExistentNameTag()
82 {
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));
86 }
87
88 /**
89 * Test html_extract_tag() when the tag <meta name= is not found.
90 */
91 public function testHtmlExtractNonExistentNameTag()
92 {
93 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
94 $this->assertFalse(html_extract_tag('description', $html));
95 }
96
97 /**
98 * Test html_extract_tag() when the tag <meta property="og: is found.
99 */
100 public function testHtmlExtractExistentOgTag()
101 {
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));
105 }
106
107 /**
108 * Test html_extract_tag() when the tag <meta property="og: is not found.
109 */
110 public function testHtmlExtractNonExistentOgTag()
111 {
112 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
113 $this->assertFalse(html_extract_tag('description', $html));
114 }
115
116 /**
117 * Test the download callback with valid value
118 */
119 public function testCurlDownloadCallbackOk()
120 {
121 $callback = get_curl_download_callback(
122 $charset,
123 $title,
124 $desc,
125 $keywords,
126 false,
127 'ut_curl_getinfo_ok'
128 );
129 $data = [
130 'HTTP/1.1 200 OK',
131 'Server: GitHub.com',
132 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
133 'Content-Type: text/html; charset=utf-8',
134 'Status: 200 OK',
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" />',
141 ];
142 foreach ($data as $key => $line) {
143 $ignore = null;
144 $expected = $key !== 'end' ? strlen($line) : false;
145 $this->assertEquals($expected, $callback($ignore, $line));
146 if ($expected === false) {
147 break;
148 }
149 }
150 $this->assertEquals('utf-8', $charset);
151 $this->assertEquals('Refactoring · GitHub', $title);
152 $this->assertEmpty($desc);
153 $this->assertEmpty($keywords);
154 }
155
156 /**
157 * Test the download callback with valid values and no charset
158 */
159 public function testCurlDownloadCallbackOkNoCharset()
160 {
161 $callback = get_curl_download_callback(
162 $charset,
163 $title,
164 $desc,
165 $keywords,
166 false,
167 'ut_curl_getinfo_no_charset'
168 );
169 $data = [
170 'HTTP/1.1 200 OK',
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" />',
177 ];
178 foreach ($data as $key => $line) {
179 $ignore = null;
180 $this->assertEquals(strlen($line), $callback($ignore, $line));
181 }
182 $this->assertEmpty($charset);
183 $this->assertEquals('Refactoring · GitHub', $title);
184 $this->assertEmpty($desc);
185 $this->assertEmpty($keywords);
186 }
187
188 /**
189 * Test the download callback with valid values and no charset
190 */
191 public function testCurlDownloadCallbackOkHtmlCharset()
192 {
193 $callback = get_curl_download_callback(
194 $charset,
195 $title,
196 $desc,
197 $keywords,
198 false,
199 'ut_curl_getinfo_no_charset'
200 );
201 $data = [
202 'HTTP/1.1 200 OK',
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" />',
210 ];
211 foreach ($data as $key => $line) {
212 $ignore = null;
213 $expected = $key !== 'end' ? strlen($line) : false;
214 $this->assertEquals($expected, $callback($ignore, $line));
215 if ($expected === false) {
216 break;
217 }
218 }
219 $this->assertEquals('utf-8', $charset);
220 $this->assertEquals('Refactoring · GitHub', $title);
221 $this->assertEmpty($desc);
222 $this->assertEmpty($keywords);
223 }
224
225 /**
226 * Test the download callback with valid values and no title
227 */
228 public function testCurlDownloadCallbackOkNoTitle()
229 {
230 $callback = get_curl_download_callback(
231 $charset,
232 $title,
233 $desc,
234 $keywords,
235 false,
236 'ut_curl_getinfo_ok'
237 );
238 $data = [
239 'HTTP/1.1 200 OK',
240 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
241 'ignored',
242 ];
243 foreach ($data as $key => $line) {
244 $ignore = null;
245 $this->assertEquals(strlen($line), $callback($ignore, $line));
246 }
247 $this->assertEquals('utf-8', $charset);
248 $this->assertEmpty($title);
249 $this->assertEmpty($desc);
250 $this->assertEmpty($keywords);
251 }
252
253 /**
254 * Test the download callback with an invalid content type.
255 */
256 public function testCurlDownloadCallbackInvalidContentType()
257 {
258 $callback = get_curl_download_callback(
259 $charset,
260 $title,
261 $desc,
262 $keywords,
263 false,
264 'ut_curl_getinfo_ct_ko'
265 );
266 $ignore = null;
267 $this->assertFalse($callback($ignore, ''));
268 $this->assertEmpty($charset);
269 $this->assertEmpty($title);
270 }
271
272 /**
273 * Test the download callback with an invalid response code.
274 */
275 public function testCurlDownloadCallbackInvalidResponseCode()
276 {
277 $callback = $callback = get_curl_download_callback(
278 $charset,
279 $title,
280 $desc,
281 $keywords,
282 false,
283 'ut_curl_getinfo_rc_ko'
284 );
285 $ignore = null;
286 $this->assertFalse($callback($ignore, ''));
287 $this->assertEmpty($charset);
288 $this->assertEmpty($title);
289 }
290
291 /**
292 * Test the download callback with an invalid content type and response code.
293 */
294 public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode()
295 {
296 $callback = $callback = get_curl_download_callback(
297 $charset,
298 $title,
299 $desc,
300 $keywords,
301 false,
302 'ut_curl_getinfo_rs_ct_ko'
303 );
304 $ignore = null;
305 $this->assertFalse($callback($ignore, ''));
306 $this->assertEmpty($charset);
307 $this->assertEmpty($title);
308 }
309
310 /**
311 * Test the download callback with valid value, and retrieve_description option enabled.
312 */
313 public function testCurlDownloadCallbackOkWithDesc()
314 {
315 $callback = get_curl_download_callback(
316 $charset,
317 $title,
318 $desc,
319 $keywords,
320 true,
321 'ut_curl_getinfo_ok'
322 );
323 $data = [
324 'HTTP/1.1 200 OK',
325 'Server: GitHub.com',
326 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
327 'Content-Type: text/html; charset=utf-8',
328 'Status: 200 OK',
329 'th=device-width">'
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" />',
335 ];
336 foreach ($data as $key => $line) {
337 $ignore = null;
338 $expected = $key !== 'end' ? strlen($line) : false;
339 $this->assertEquals($expected, $callback($ignore, $line));
340 if ($expected === false) {
341 break;
342 }
343 }
344 $this->assertEquals('utf-8', $charset);
345 $this->assertEquals('Refactoring · GitHub', $title);
346 $this->assertEquals('link desc', $desc);
347 $this->assertEquals('key1 key2', $keywords);
348 }
349
350 /**
351 * Test the download callback with valid value, and retrieve_description option enabled,
352 * but no desc or keyword defined in the page.
353 */
354 public function testCurlDownloadCallbackOkWithDescNotFound()
355 {
356 $callback = get_curl_download_callback(
357 $charset,
358 $title,
359 $desc,
360 $keywords,
361 true,
362 'ut_curl_getinfo_ok'
363 );
364 $data = [
365 'HTTP/1.1 200 OK',
366 'Server: GitHub.com',
367 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
368 'Content-Type: text/html; charset=utf-8',
369 'Status: 200 OK',
370 'th=device-width">'
371 . '<title>Refactoring · GitHub</title>'
372 . '<link rel="search" type="application/opensea',
373 'end' => '<title>ignored</title>',
374 ];
375 foreach ($data as $key => $line) {
376 $ignore = null;
377 $expected = $key !== 'end' ? strlen($line) : false;
378 $this->assertEquals($expected, $callback($ignore, $line));
379 if ($expected === false) {
380 break;
381 }
382 }
383 $this->assertEquals('utf-8', $charset);
384 $this->assertEquals('Refactoring · GitHub', $title);
385 $this->assertEmpty($desc);
386 $this->assertEmpty($keywords);
387 }
388
389 /**
390 * Test text2clickable.
391 */
392 public function testText2clickable()
393 {
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);
399
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);
405
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);
412 }
413
414 /**
415 * Test testSpace2nbsp.
416 */
417 public function testSpace2nbsp()
418 {
419 $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?';
420 $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?' . PHP_EOL . '&nbsp;Really?';
421 $processedText = space2nbsp($text);
422 $this->assertEquals($expectedText, $processedText);
423 }
424
425 /**
426 * Test hashtags auto-link.
427 */
428 public function testHashtagAutolink()
429 {
430 $index = 'http://domain.tld/';
431 $rawDescription = '#hashtag\n
432 # nothashtag\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
437 カタカナ #カタカナ」カタカナ\n';
438 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
439
440 $this->assertContainsPolyfill($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
441 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
442 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
443 $this->assertContainsPolyfill($this->getHashtagLink('ашок', $index), $autolinkedDescription);
444 $this->assertContainsPolyfill($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
445 $this->assertContainsPolyfill($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
446 $this->assertNotContainsPolyfill($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
447 }
448
449 /**
450 * Test hashtags auto-link without index URL.
451 */
452 public function testHashtagAutolinkNoIndex()
453 {
454 $rawDescription = 'blabla #hashtag x#nothashtag';
455 $autolinkedDescription = hashtag_autolink($rawDescription);
456
457 $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription);
458 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
459 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
460 }
461
462 /**
463 * Test is_note with note URLs.
464 */
465 public function testIsNote()
466 {
467 $this->assertTrue(is_note('?'));
468 $this->assertTrue(is_note('?abcDEf'));
469 $this->assertTrue(is_note('?_abcDEf#123'));
470 }
471
472 /**
473 * Test is_note with non note URLs.
474 */
475 public function testIsNotNote()
476 {
477 $this->assertFalse(is_note(''));
478 $this->assertFalse(is_note('nope'));
479 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
480 }
481
482 /**
483 * Util function to build an hashtag link.
484 *
485 * @param string $hashtag Hashtag name.
486 * @param string $index Index URL.
487 *
488 * @return string HTML hashtag link.
489 */
490 private function getHashtagLink($hashtag, $index = '')
491 {
492 $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>';
493 return str_replace('$1', $hashtag, $hashtagLink);
494 }
495 }