]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/bookmark/LinkUtilsTest.php
Feature: support any tag separator
[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
98 // Simple one line
99 $html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>';
100 $this->assertEquals($description, html_extract_tag('description', $html));
101
102 // Simple OpenGraph
103 $html = '<meta property="og:description" content="' . $description . '">';
104 $this->assertEquals($description, html_extract_tag('description', $html));
105
106 // Simple reversed OpenGraph
107 $html = '<meta content="' . $description . '" property="og:description">';
108 $this->assertEquals($description, html_extract_tag('description', $html));
109
110 // ItemProp OpenGraph
111 $html = '<meta itemprop="og:description" content="' . $description . '">';
112 $this->assertEquals($description, html_extract_tag('description', $html));
113
114 // OpenGraph without quotes
115 $html = '<meta property=og:description content="' . $description . '">';
116 $this->assertEquals($description, html_extract_tag('description', $html));
117
118 // OpenGraph reversed without quotes
119 $html = '<meta content="' . $description . '" property=og:description>';
120 $this->assertEquals($description, html_extract_tag('description', $html));
121
122 // OpenGraph with noise
123 $html = '<meta tag1="content1" property="og:description" tag2="content2" content="' .
124 $description . '" tag3="content3">';
125 $this->assertEquals($description, html_extract_tag('description', $html));
126
127 // OpenGraph reversed with noise
128 $html = '<meta tag1="content1" content="' . $description . '" ' .
129 'tag3="content3" tag2="content2" property="og:description">';
130 $this->assertEquals($description, html_extract_tag('description', $html));
131
132 // OpenGraph multiple properties start
133 $html = '<meta property="unrelated og:description" content="' . $description . '">';
134 $this->assertEquals($description, html_extract_tag('description', $html));
135
136 // OpenGraph multiple properties end
137 $html = '<meta property="og:description unrelated" content="' . $description . '">';
138 $this->assertEquals($description, html_extract_tag('description', $html));
139
140 // OpenGraph multiple properties both end
141 $html = '<meta property="og:unrelated1 og:description og:unrelated2" content="' . $description . '">';
142 $this->assertEquals($description, html_extract_tag('description', $html));
143
144 // OpenGraph multiple properties both end with noise
145 $html = '<meta tag1="content1" property="og:unrelated1 og:description og:unrelated2" '.
146 'tag2="content2" content="' . $description . '" tag3="content3">';
147 $this->assertEquals($description, html_extract_tag('description', $html));
148
149 // OpenGraph reversed multiple properties start
150 $html = '<meta content="' . $description . '" property="unrelated og:description">';
151 $this->assertEquals($description, html_extract_tag('description', $html));
152
153 // OpenGraph reversed multiple properties end
154 $html = '<meta content="' . $description . '" property="og:description unrelated">';
155 $this->assertEquals($description, html_extract_tag('description', $html));
156
157 // OpenGraph reversed multiple properties both end
158 $html = '<meta content="' . $description . '" property="og:unrelated1 og:description og:unrelated2">';
159 $this->assertEquals($description, html_extract_tag('description', $html));
160
161 // OpenGraph reversed multiple properties both end with noise
162 $html = '<meta tag1="content1" content="' . $description . '" tag2="content2" '.
163 'property="og:unrelated1 og:description og:unrelated2" tag3="content3">';
164 $this->assertEquals($description, html_extract_tag('description', $html));
165
166 // Suggestion from #1375
167 $html = '<meta property="og:description" name="description" content="' . $description . '">';
168 $this->assertEquals($description, html_extract_tag('description', $html));
169 }
170
171 /**
172 * Test html_extract_tag() when the tag <meta name= is not found.
173 */
174 public function testHtmlExtractNonExistentNameTag()
175 {
176 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
177 $this->assertFalse(html_extract_tag('description', $html));
178
179 // Partial meta tag
180 $html = '<meta content="Brief description">';
181 $this->assertFalse(html_extract_tag('description', $html));
182
183 $html = '<meta property="og:description">';
184 $this->assertFalse(html_extract_tag('description', $html));
185
186 $html = '<meta tag1="content1" property="og:description">';
187 $this->assertFalse(html_extract_tag('description', $html));
188
189 $html = '<meta property="og:description" tag1="content1">';
190 $this->assertFalse(html_extract_tag('description', $html));
191
192 $html = '<meta tag1="content1" content="Brief description">';
193 $this->assertFalse(html_extract_tag('description', $html));
194
195 $html = '<meta content="Brief description" tag1="content1">';
196 $this->assertFalse(html_extract_tag('description', $html));
197 }
198
199 /**
200 * Test html_extract_tag() when the tag <meta property="og: is found.
201 */
202 public function testHtmlExtractExistentOgTag()
203 {
204 $description = 'Bob and Alice share cookies.';
205 $html = '<html><meta>stuff2</meta><meta property="og:description" content="' . $description . '"/></html>';
206 $this->assertEquals($description, html_extract_tag('description', $html));
207 }
208
209 /**
210 * Test html_extract_tag() when the tag <meta property="og: is not found.
211 */
212 public function testHtmlExtractNonExistentOgTag()
213 {
214 $html = '<html><meta>stuff2</meta><meta name="image" content="img"/></html>';
215 $this->assertFalse(html_extract_tag('description', $html));
216 }
217
218 /**
219 * Test the header callback with valid value
220 */
221 public function testCurlHeaderCallbackOk(): void
222 {
223 $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_ok');
224 $data = [
225 'HTTP/1.1 200 OK',
226 'Server: GitHub.com',
227 'Date: Sat, 28 Oct 2017 12:01:33 GMT',
228 'Content-Type: text/html; charset=utf-8',
229 'Status: 200 OK',
230 ];
231
232 foreach ($data as $chunk) {
233 static::assertIsInt($callback(null, $chunk));
234 }
235
236 static::assertSame('utf-8', $charset);
237 }
238
239 /**
240 * Test the download callback with valid value
241 */
242 public function testCurlDownloadCallbackOk(): void
243 {
244 $charset = 'utf-8';
245 $callback = get_curl_download_callback(
246 $charset,
247 $title,
248 $desc,
249 $keywords,
250 false,
251 ' '
252 );
253
254 $data = [
255 'th=device-width">'
256 . '<title>Refactoring · GitHub</title>'
257 . '<link rel="search" type="application/opensea',
258 '<title>ignored</title>'
259 . '<meta name="description" content="desc" />'
260 . '<meta name="keywords" content="key1,key2" />',
261 ];
262
263 foreach ($data as $chunk) {
264 static::assertSame(strlen($chunk), $callback(null, $chunk));
265 }
266
267 static::assertSame('utf-8', $charset);
268 static::assertSame('Refactoring · GitHub', $title);
269 static::assertEmpty($desc);
270 static::assertEmpty($keywords);
271 }
272
273 /**
274 * Test the header callback with valid value
275 */
276 public function testCurlHeaderCallbackNoCharset(): void
277 {
278 $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_no_charset');
279 $data = [
280 'HTTP/1.1 200 OK',
281 ];
282
283 foreach ($data as $chunk) {
284 static::assertSame(strlen($chunk), $callback(null, $chunk));
285 }
286
287 static::assertFalse($charset);
288 }
289
290 /**
291 * Test the download callback with valid values and no charset
292 */
293 public function testCurlDownloadCallbackOkNoCharset(): void
294 {
295 $charset = null;
296 $callback = get_curl_download_callback(
297 $charset,
298 $title,
299 $desc,
300 $keywords,
301 false,
302 ' '
303 );
304
305 $data = [
306 'end' => 'th=device-width">'
307 . '<title>Refactoring · GitHub</title>'
308 . '<link rel="search" type="application/opensea',
309 '<title>ignored</title>'
310 . '<meta name="description" content="desc" />'
311 . '<meta name="keywords" content="key1,key2" />',
312 ];
313
314 foreach ($data as $chunk) {
315 static::assertSame(strlen($chunk), $callback(null, $chunk));
316 }
317
318 $this->assertEmpty($charset);
319 $this->assertEquals('Refactoring · GitHub', $title);
320 $this->assertEmpty($desc);
321 $this->assertEmpty($keywords);
322 }
323
324 /**
325 * Test the download callback with valid values and no charset
326 */
327 public function testCurlDownloadCallbackOkHtmlCharset(): void
328 {
329 $charset = null;
330 $callback = get_curl_download_callback(
331 $charset,
332 $title,
333 $desc,
334 $keywords,
335 false,
336 ' '
337 );
338
339 $data = [
340 '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />',
341 'end' => 'th=device-width">'
342 . '<title>Refactoring · GitHub</title>'
343 . '<link rel="search" type="application/opensea',
344 '<title>ignored</title>'
345 . '<meta name="description" content="desc" />'
346 . '<meta name="keywords" content="key1,key2" />',
347 ];
348 foreach ($data as $chunk) {
349 static::assertSame(strlen($chunk), $callback(null, $chunk));
350 }
351
352 $this->assertEquals('utf-8', $charset);
353 $this->assertEquals('Refactoring · GitHub', $title);
354 $this->assertEmpty($desc);
355 $this->assertEmpty($keywords);
356 }
357
358 /**
359 * Test the download callback with valid values and no title
360 */
361 public function testCurlDownloadCallbackOkNoTitle(): void
362 {
363 $charset = 'utf-8';
364 $callback = get_curl_download_callback(
365 $charset,
366 $title,
367 $desc,
368 $keywords,
369 false,
370 ' '
371 );
372
373 $data = [
374 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea',
375 'ignored',
376 ];
377
378 foreach ($data as $chunk) {
379 static::assertSame(strlen($chunk), $callback(null, $chunk));
380 }
381
382 $this->assertEquals('utf-8', $charset);
383 $this->assertEmpty($title);
384 $this->assertEmpty($desc);
385 $this->assertEmpty($keywords);
386 }
387
388 /**
389 * Test the header callback with an invalid content type.
390 */
391 public function testCurlHeaderCallbackInvalidContentType(): void
392 {
393 $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_ct_ko');
394 $data = [
395 'HTTP/1.1 200 OK',
396 ];
397
398 static::assertFalse($callback(null, $data[0]));
399 static::assertNull($charset);
400 }
401
402 /**
403 * Test the header callback with an invalid response code.
404 */
405 public function testCurlHeaderCallbackInvalidResponseCode(): void
406 {
407 $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rc_ko');
408
409 static::assertFalse($callback(null, ''));
410 static::assertNull($charset);
411 }
412
413 /**
414 * Test the header callback with an invalid content type and response code.
415 */
416 public function testCurlHeaderCallbackInvalidContentTypeAndResponseCode(): void
417 {
418 $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rs_ct_ko');
419
420 static::assertFalse($callback(null, ''));
421 static::assertNull($charset);
422 }
423
424 /**
425 * Test the download callback with valid value, and retrieve_description option enabled.
426 */
427 public function testCurlDownloadCallbackOkWithDesc(): void
428 {
429 $charset = 'utf-8';
430 $callback = get_curl_download_callback(
431 $charset,
432 $title,
433 $desc,
434 $keywords,
435 true,
436 ' '
437 );
438 $data = [
439 'th=device-width">'
440 . '<title>Refactoring · GitHub</title>'
441 . '<link rel="search" type="application/opensea',
442 'end' => '<title>ignored</title>'
443 . '<meta name="description" content="link desc" />'
444 . '<meta name="keywords" content="key1,key2" />',
445 ];
446
447 foreach ($data as $chunk) {
448 static::assertSame(strlen($chunk), $callback(null, $chunk));
449 }
450
451 $this->assertEquals('utf-8', $charset);
452 $this->assertEquals('Refactoring · GitHub', $title);
453 $this->assertEquals('link desc', $desc);
454 $this->assertEquals('key1 key2', $keywords);
455 }
456
457 /**
458 * Test the download callback with valid value, and retrieve_description option enabled,
459 * but no desc or keyword defined in the page.
460 */
461 public function testCurlDownloadCallbackOkWithDescNotFound(): void
462 {
463 $charset = 'utf-8';
464 $callback = get_curl_download_callback(
465 $charset,
466 $title,
467 $desc,
468 $keywords,
469 true,
470 'ut_curl_getinfo_ok'
471 );
472 $data = [
473 'th=device-width">'
474 . '<title>Refactoring · GitHub</title>'
475 . '<link rel="search" type="application/opensea',
476 'end' => '<title>ignored</title>',
477 ];
478
479 foreach ($data as $chunk) {
480 static::assertSame(strlen($chunk), $callback(null, $chunk));
481 }
482
483 $this->assertEquals('utf-8', $charset);
484 $this->assertEquals('Refactoring · GitHub', $title);
485 $this->assertEmpty($desc);
486 $this->assertEmpty($keywords);
487 }
488
489 /**
490 * Test text2clickable.
491 */
492 public function testText2clickable()
493 {
494 $text = 'stuff http://hello.there/is=someone#here otherstuff';
495 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">'
496 . 'http://hello.there/is=someone#here</a> otherstuff';
497 $processedText = text2clickable($text);
498 $this->assertEquals($expectedText, $processedText);
499
500 $text = 'stuff http://hello.there/is=someone#here(please) otherstuff';
501 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">'
502 . 'http://hello.there/is=someone#here(please)</a> otherstuff';
503 $processedText = text2clickable($text);
504 $this->assertEquals($expectedText, $processedText);
505
506 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
507 $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff';
508 $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">'
509 . 'http://hello.there/is=someone#here(please)&no</a> otherstuff';
510 $processedText = text2clickable($text);
511 $this->assertEquals($expectedText, $processedText);
512 }
513
514 /**
515 * Test testSpace2nbsp.
516 */
517 public function testSpace2nbsp()
518 {
519 $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?';
520 $expectedText = '&nbsp; Are you &nbsp; thrilled &nbsp;by flags &nbsp; ?' . PHP_EOL . '&nbsp;Really?';
521 $processedText = space2nbsp($text);
522 $this->assertEquals($expectedText, $processedText);
523 }
524
525 /**
526 * Test hashtags auto-link.
527 */
528 public function testHashtagAutolink()
529 {
530 $index = 'http://domain.tld/';
531 $rawDescription = '#hashtag\n
532 # nothashtag\n
533 test#nothashtag #hashtag \#nothashtag\n
534 test #hashtag #hashtag test #hashtag.test\n
535 #hashtag #hashtag-nothashtag #hashtag_hashtag\n
536 What is #ашок anyway?\n
537 カタカナ #カタカナ」カタカナ\n';
538 $autolinkedDescription = hashtag_autolink($rawDescription, $index);
539
540 $this->assertContainsPolyfill($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
541 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
542 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
543 $this->assertContainsPolyfill($this->getHashtagLink('ашок', $index), $autolinkedDescription);
544 $this->assertContainsPolyfill($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
545 $this->assertContainsPolyfill($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
546 $this->assertNotContainsPolyfill($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
547 }
548
549 /**
550 * Test hashtags auto-link without index URL.
551 */
552 public function testHashtagAutolinkNoIndex()
553 {
554 $rawDescription = 'blabla #hashtag x#nothashtag';
555 $autolinkedDescription = hashtag_autolink($rawDescription);
556
557 $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription);
558 $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
559 $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
560 }
561
562 /**
563 * Test is_note with note URLs.
564 */
565 public function testIsNote()
566 {
567 $this->assertTrue(is_note('?'));
568 $this->assertTrue(is_note('?abcDEf'));
569 $this->assertTrue(is_note('?_abcDEf#123'));
570 }
571
572 /**
573 * Test is_note with non note URLs.
574 */
575 public function testIsNotNote()
576 {
577 $this->assertFalse(is_note(''));
578 $this->assertFalse(is_note('nope'));
579 $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
580 }
581
582 /**
583 * Test tags_str2array with whitespace separator.
584 */
585 public function testTagsStr2ArrayWithSpaceSeparator(): void
586 {
587 $separator = ' ';
588
589 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1 tag2 tag3', $separator));
590 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1 tag2 tag3', $separator));
591 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array(' tag1 tag2 tag3 ', $separator));
592 static::assertSame(['tag1@', 'tag2,', '.tag3'], tags_str2array(' tag1@ tag2, .tag3 ', $separator));
593 static::assertSame([], tags_str2array('', $separator));
594 static::assertSame([], tags_str2array(' ', $separator));
595 static::assertSame([], tags_str2array(null, $separator));
596 }
597
598 /**
599 * Test tags_str2array with @ separator.
600 */
601 public function testTagsStr2ArrayWithCharSeparator(): void
602 {
603 $separator = '@';
604
605 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1@tag2@tag3', $separator));
606 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('tag1@@@@tag2@@@@tag3', $separator));
607 static::assertSame(['tag1', 'tag2', 'tag3'], tags_str2array('@@@tag1@@@tag2@@@@tag3@@', $separator));
608 static::assertSame(
609 ['tag1#', 'tag2, and other', '.tag3'],
610 tags_str2array('@@@ tag1# @@@ tag2, and other @@@@.tag3@@', $separator)
611 );
612 static::assertSame([], tags_str2array('', $separator));
613 static::assertSame([], tags_str2array(' ', $separator));
614 static::assertSame([], tags_str2array(null, $separator));
615 }
616
617 /**
618 * Test tags_array2str with ' ' separator.
619 */
620 public function testTagsArray2StrWithSpaceSeparator(): void
621 {
622 $separator = ' ';
623
624 static::assertSame('tag1 tag2 tag3', tags_array2str(['tag1', 'tag2', 'tag3'], $separator));
625 static::assertSame('tag1, tag2@ tag3', tags_array2str(['tag1,', 'tag2@', 'tag3'], $separator));
626 static::assertSame('tag1 tag2 tag3', tags_array2str([' tag1 ', 'tag2', 'tag3 '], $separator));
627 static::assertSame('tag1 tag2 tag3', tags_array2str([' tag1 ', ' ', 'tag2', ' ', 'tag3 '], $separator));
628 static::assertSame('tag1', tags_array2str([' tag1 '], $separator));
629 static::assertSame('', tags_array2str([' '], $separator));
630 static::assertSame('', tags_array2str([], $separator));
631 static::assertSame('', tags_array2str(null, $separator));
632 }
633
634 /**
635 * Test tags_array2str with @ separator.
636 */
637 public function testTagsArray2StrWithCharSeparator(): void
638 {
639 $separator = '@';
640
641 static::assertSame('tag1@tag2@tag3', tags_array2str(['tag1', 'tag2', 'tag3'], $separator));
642 static::assertSame('tag1,@tag2@tag3', tags_array2str(['tag1,', 'tag2@', 'tag3'], $separator));
643 static::assertSame(
644 'tag1@tag2, and other@tag3',
645 tags_array2str(['@@@@ tag1@@@', ' @tag2, and other @', 'tag3@@@@'], $separator)
646 );
647 static::assertSame('tag1@tag2@tag3', tags_array2str(['@@@tag1@@@', '@', 'tag2', '@@@', 'tag3@@@'], $separator));
648 static::assertSame('tag1', tags_array2str(['@@@@tag1@@@@'], $separator));
649 static::assertSame('', tags_array2str(['@@@'], $separator));
650 static::assertSame('', tags_array2str([], $separator));
651 static::assertSame('', tags_array2str(null, $separator));
652 }
653
654 /**
655 * Test tags_array2str with @ separator.
656 */
657 public function testTagsFilterWithSpaceSeparator(): void
658 {
659 $separator = ' ';
660
661 static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['tag1', 'tag2', 'tag3'], $separator));
662 static::assertSame(['tag1,', 'tag2@', 'tag3'], tags_filter(['tag1,', 'tag2@', 'tag3'], $separator));
663 static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter([' tag1 ', 'tag2', 'tag3 '], $separator));
664 static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter([' tag1 ', ' ', 'tag2', ' ', 'tag3 '], $separator));
665 static::assertSame(['tag1'], tags_filter([' tag1 '], $separator));
666 static::assertSame([], tags_filter([' '], $separator));
667 static::assertSame([], tags_filter([], $separator));
668 static::assertSame([], tags_filter(null, $separator));
669 }
670
671 /**
672 * Test tags_array2str with @ separator.
673 */
674 public function testTagsArrayFilterWithSpaceSeparator(): void
675 {
676 $separator = '@';
677
678 static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['tag1', 'tag2', 'tag3'], $separator));
679 static::assertSame(['tag1,', 'tag2#', 'tag3'], tags_filter(['tag1,', 'tag2#', 'tag3'], $separator));
680 static::assertSame(
681 ['tag1', 'tag2, and other', 'tag3'],
682 tags_filter(['@@@@ tag1@@@', ' @tag2, and other @', 'tag3@@@@'], $separator)
683 );
684 static::assertSame(['tag1', 'tag2', 'tag3'], tags_filter(['@@@tag1@@@', '@', 'tag2', '@@@', 'tag3@@@'], $separator));
685 static::assertSame(['tag1'], tags_filter(['@@@@tag1@@@@'], $separator));
686 static::assertSame([], tags_filter(['@@@'], $separator));
687 static::assertSame([], tags_filter([], $separator));
688 static::assertSame([], tags_filter(null, $separator));
689 }
690
691 /**
692 * Util function to build an hashtag link.
693 *
694 * @param string $hashtag Hashtag name.
695 * @param string $index Index URL.
696 *
697 * @return string HTML hashtag link.
698 */
699 private function getHashtagLink($hashtag, $index = '')
700 {
701 $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>';
702 return str_replace('$1', $hashtag, $hashtagLink);
703 }
704 }