]>
Commit | Line | Data |
---|---|---|
1557cefb A |
1 | <?php |
2 | ||
fe3713d2 V |
3 | namespace Shaarli\Bookmark; |
4 | ||
a5a9cf23 | 5 | use Shaarli\TestCase; |
fe3713d2 V |
6 | |
7 | require_once 'tests/utils/CurlUtils.php'; | |
1557cefb A |
8 | |
9 | /** | |
fe3713d2 V |
10 | * Class LinkUtilsTest. |
11 | */ | |
6a487252 | 12 | class LinkUtilsTest extends TestCase |
1557cefb A |
13 | { |
14 | /** | |
15 | * Test html_extract_title() when the title is found. | |
16 | */ | |
17 | public function testHtmlExtractExistentTitle() | |
18 | { | |
19 | $title = 'Read me please.'; | |
fe3713d2 | 20 | $html = '<html><meta>stuff</meta><title>' . $title . '</title></html>'; |
1557cefb | 21 | $this->assertEquals($title, html_extract_title($html)); |
fe3713d2 | 22 | $html = '<html><title>' . $title . '</title>blabla<title>another</title></html>'; |
68ea1d2b | 23 | $this->assertEquals($title, html_extract_title($html)); |
1557cefb A |
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 | ||
1557cefb A |
35 | /** |
36 | * Test headers_extract_charset() when the charset is found. | |
37 | */ | |
38 | public function testHeadersExtractExistentCharset() | |
39 | { | |
40 | $charset = 'x-MacCroatian'; | |
fe3713d2 | 41 | $headers = 'text/html; charset=' . $charset; |
d65342e3 | 42 | $this->assertEquals(strtolower($charset), header_extract_charset($headers)); |
1557cefb A |
43 | } |
44 | ||
1ea09a1b A |
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 | ||
1557cefb A |
58 | /** |
59 | * Test headers_extract_charset() when the charset is not found. | |
60 | */ | |
61 | public function testHeadersExtractNonExistentCharset() | |
62 | { | |
d65342e3 A |
63 | $headers = ''; |
64 | $this->assertFalse(header_extract_charset($headers)); | |
1557cefb | 65 | |
d65342e3 A |
66 | $headers = 'text/html'; |
67 | $this->assertFalse(header_extract_charset($headers)); | |
1557cefb A |
68 | } |
69 | ||
70 | /** | |
71 | * Test html_extract_charset() when the charset is found. | |
72 | */ | |
73 | public function testHtmlExtractExistentCharset() | |
74 | { | |
75 | $charset = 'x-MacCroatian'; | |
fe3713d2 | 76 | $html = '<html><meta>stuff2</meta><meta charset="' . $charset . '"/></html>'; |
1557cefb A |
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 | } | |
141a86c5 | 90 | |
6a487252 A |
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.'; | |
2cd0509b A |
97 | |
98 | // Simple one line | |
6a487252 A |
99 | $html = '<html><meta>stuff2</meta><meta name="description" content="' . $description . '"/></html>'; |
100 | $this->assertEquals($description, html_extract_tag('description', $html)); | |
2cd0509b A |
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)); | |
6a487252 A |
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)); | |
2cd0509b A |
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)); | |
6a487252 A |
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 | ||
5334090b A |
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 | ||
d65342e3 A |
239 | /** |
240 | * Test the download callback with valid value | |
241 | */ | |
5334090b | 242 | public function testCurlDownloadCallbackOk(): void |
d65342e3 | 243 | { |
5334090b | 244 | $charset = 'utf-8'; |
6a487252 A |
245 | $callback = get_curl_download_callback( |
246 | $charset, | |
247 | $title, | |
248 | $desc, | |
249 | $keywords, | |
b3bd8c3e A |
250 | false, |
251 | ' ' | |
6a487252 | 252 | ); |
5334090b | 253 | |
d65342e3 | 254 | $data = [ |
5334090b | 255 | 'th=device-width">' |
fe3713d2 V |
256 | . '<title>Refactoring · GitHub</title>' |
257 | . '<link rel="search" type="application/opensea', | |
6a487252 A |
258 | '<title>ignored</title>' |
259 | . '<meta name="description" content="desc" />' | |
260 | . '<meta name="keywords" content="key1,key2" />', | |
d65342e3 | 261 | ]; |
5334090b A |
262 | |
263 | foreach ($data as $chunk) { | |
264 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
d65342e3 | 265 | } |
5334090b A |
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); | |
d65342e3 A |
288 | } |
289 | ||
290 | /** | |
291 | * Test the download callback with valid values and no charset | |
292 | */ | |
5334090b | 293 | public function testCurlDownloadCallbackOkNoCharset(): void |
d65342e3 | 294 | { |
5334090b | 295 | $charset = null; |
6a487252 A |
296 | $callback = get_curl_download_callback( |
297 | $charset, | |
298 | $title, | |
299 | $desc, | |
300 | $keywords, | |
b3bd8c3e A |
301 | false, |
302 | ' ' | |
6a487252 | 303 | ); |
5334090b | 304 | |
d65342e3 | 305 | $data = [ |
9d9f6d75 | 306 | 'end' => 'th=device-width">' |
fe3713d2 V |
307 | . '<title>Refactoring · GitHub</title>' |
308 | . '<link rel="search" type="application/opensea', | |
6a487252 A |
309 | '<title>ignored</title>' |
310 | . '<meta name="description" content="desc" />' | |
311 | . '<meta name="keywords" content="key1,key2" />', | |
d65342e3 | 312 | ]; |
5334090b A |
313 | |
314 | foreach ($data as $chunk) { | |
315 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
d65342e3 | 316 | } |
5334090b | 317 | |
d65342e3 A |
318 | $this->assertEmpty($charset); |
319 | $this->assertEquals('Refactoring · GitHub', $title); | |
6a487252 A |
320 | $this->assertEmpty($desc); |
321 | $this->assertEmpty($keywords); | |
d65342e3 A |
322 | } |
323 | ||
324 | /** | |
325 | * Test the download callback with valid values and no charset | |
326 | */ | |
5334090b | 327 | public function testCurlDownloadCallbackOkHtmlCharset(): void |
d65342e3 | 328 | { |
5334090b | 329 | $charset = null; |
6a487252 A |
330 | $callback = get_curl_download_callback( |
331 | $charset, | |
332 | $title, | |
333 | $desc, | |
334 | $keywords, | |
b3bd8c3e A |
335 | false, |
336 | ' ' | |
6a487252 | 337 | ); |
5334090b | 338 | |
d65342e3 | 339 | $data = [ |
d65342e3 | 340 | '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', |
9d9f6d75 | 341 | 'end' => 'th=device-width">' |
fe3713d2 V |
342 | . '<title>Refactoring · GitHub</title>' |
343 | . '<link rel="search" type="application/opensea', | |
6a487252 A |
344 | '<title>ignored</title>' |
345 | . '<meta name="description" content="desc" />' | |
346 | . '<meta name="keywords" content="key1,key2" />', | |
d65342e3 | 347 | ]; |
5334090b A |
348 | foreach ($data as $chunk) { |
349 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
d65342e3 | 350 | } |
5334090b | 351 | |
d65342e3 A |
352 | $this->assertEquals('utf-8', $charset); |
353 | $this->assertEquals('Refactoring · GitHub', $title); | |
6a487252 A |
354 | $this->assertEmpty($desc); |
355 | $this->assertEmpty($keywords); | |
d65342e3 A |
356 | } |
357 | ||
358 | /** | |
359 | * Test the download callback with valid values and no title | |
360 | */ | |
5334090b | 361 | public function testCurlDownloadCallbackOkNoTitle(): void |
d65342e3 | 362 | { |
5334090b | 363 | $charset = 'utf-8'; |
6a487252 A |
364 | $callback = get_curl_download_callback( |
365 | $charset, | |
366 | $title, | |
367 | $desc, | |
368 | $keywords, | |
b3bd8c3e A |
369 | false, |
370 | ' ' | |
6a487252 | 371 | ); |
5334090b | 372 | |
d65342e3 | 373 | $data = [ |
d65342e3 A |
374 | 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea', |
375 | 'ignored', | |
376 | ]; | |
5334090b A |
377 | |
378 | foreach ($data as $chunk) { | |
379 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
d65342e3 | 380 | } |
5334090b | 381 | |
d65342e3 A |
382 | $this->assertEquals('utf-8', $charset); |
383 | $this->assertEmpty($title); | |
6a487252 A |
384 | $this->assertEmpty($desc); |
385 | $this->assertEmpty($keywords); | |
d65342e3 A |
386 | } |
387 | ||
388 | /** | |
5334090b | 389 | * Test the header callback with an invalid content type. |
d65342e3 | 390 | */ |
5334090b | 391 | public function testCurlHeaderCallbackInvalidContentType(): void |
d65342e3 | 392 | { |
5334090b A |
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); | |
d65342e3 A |
400 | } |
401 | ||
402 | /** | |
5334090b | 403 | * Test the header callback with an invalid response code. |
d65342e3 | 404 | */ |
5334090b | 405 | public function testCurlHeaderCallbackInvalidResponseCode(): void |
d65342e3 | 406 | { |
5334090b A |
407 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rc_ko'); |
408 | ||
409 | static::assertFalse($callback(null, '')); | |
410 | static::assertNull($charset); | |
d65342e3 A |
411 | } |
412 | ||
413 | /** | |
5334090b | 414 | * Test the header callback with an invalid content type and response code. |
d65342e3 | 415 | */ |
5334090b | 416 | public function testCurlHeaderCallbackInvalidContentTypeAndResponseCode(): void |
d65342e3 | 417 | { |
5334090b A |
418 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rs_ct_ko'); |
419 | ||
420 | static::assertFalse($callback(null, '')); | |
421 | static::assertNull($charset); | |
d65342e3 A |
422 | } |
423 | ||
6a487252 A |
424 | /** |
425 | * Test the download callback with valid value, and retrieve_description option enabled. | |
426 | */ | |
5334090b | 427 | public function testCurlDownloadCallbackOkWithDesc(): void |
6a487252 | 428 | { |
5334090b | 429 | $charset = 'utf-8'; |
6a487252 A |
430 | $callback = get_curl_download_callback( |
431 | $charset, | |
432 | $title, | |
433 | $desc, | |
434 | $keywords, | |
b3bd8c3e A |
435 | true, |
436 | ' ' | |
6a487252 A |
437 | ); |
438 | $data = [ | |
6a487252 A |
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 | ]; | |
5334090b A |
446 | |
447 | foreach ($data as $chunk) { | |
448 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
6a487252 | 449 | } |
5334090b | 450 | |
6a487252 A |
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 | */ | |
5334090b | 461 | public function testCurlDownloadCallbackOkWithDescNotFound(): void |
6a487252 | 462 | { |
5334090b | 463 | $charset = 'utf-8'; |
6a487252 A |
464 | $callback = get_curl_download_callback( |
465 | $charset, | |
466 | $title, | |
467 | $desc, | |
468 | $keywords, | |
469 | true, | |
470 | 'ut_curl_getinfo_ok' | |
471 | ); | |
472 | $data = [ | |
6a487252 A |
473 | 'th=device-width">' |
474 | . '<title>Refactoring · GitHub</title>' | |
475 | . '<link rel="search" type="application/opensea', | |
476 | 'end' => '<title>ignored</title>', | |
477 | ]; | |
5334090b A |
478 | |
479 | foreach ($data as $chunk) { | |
480 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
6a487252 | 481 | } |
5334090b | 482 | |
6a487252 A |
483 | $this->assertEquals('utf-8', $charset); |
484 | $this->assertEquals('Refactoring · GitHub', $title); | |
485 | $this->assertEmpty($desc); | |
486 | $this->assertEmpty($keywords); | |
487 | } | |
488 | ||
9ccca401 | 489 | /** |
520d2957 | 490 | * Test text2clickable. |
9ccca401 | 491 | */ |
520d2957 | 492 | public function testText2clickable() |
9ccca401 A |
493 | { |
494 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
9d9f6d75 | 495 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here">' |
fe3713d2 | 496 | . 'http://hello.there/is=someone#here</a> otherstuff'; |
520d2957 | 497 | $processedText = text2clickable($text); |
9ccca401 | 498 | $this->assertEquals($expectedText, $processedText); |
601faf97 A |
499 | |
500 | $text = 'stuff http://hello.there/is=someone#here(please) otherstuff'; | |
9d9f6d75 | 501 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">' |
fe3713d2 | 502 | . 'http://hello.there/is=someone#here(please)</a> otherstuff'; |
520d2957 | 503 | $processedText = text2clickable($text); |
601faf97 A |
504 | $this->assertEquals($expectedText, $processedText); |
505 | ||
520d2957 | 506 | $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; |
601faf97 | 507 | $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; |
9d9f6d75 | 508 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">' |
fe3713d2 | 509 | . 'http://hello.there/is=someone#here(please)&no</a> otherstuff'; |
520d2957 | 510 | $processedText = text2clickable($text); |
fd08b50a A |
511 | $this->assertEquals($expectedText, $processedText); |
512 | } | |
513 | ||
9ccca401 A |
514 | /** |
515 | * Test testSpace2nbsp. | |
516 | */ | |
517 | public function testSpace2nbsp() | |
518 | { | |
fe3713d2 V |
519 | $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?'; |
520 | $expectedText = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?'; | |
9ccca401 A |
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 | ||
a5a9cf23 A |
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); | |
9ccca401 A |
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 | ||
a5a9cf23 A |
557 | $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription); |
558 | $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription); | |
559 | $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription); | |
9ccca401 A |
560 | } |
561 | ||
a8e7da01 A |
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 | ||
b3bd8c3e A |
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 | ||
9ccca401 A |
691 | /** |
692 | * Util function to build an hashtag link. | |
693 | * | |
694 | * @param string $hashtag Hashtag name. | |
fe3713d2 | 695 | * @param string $index Index URL. |
9ccca401 A |
696 | * |
697 | * @return string HTML hashtag link. | |
698 | */ | |
699 | private function getHashtagLink($hashtag, $index = '') | |
700 | { | |
03340c18 | 701 | $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>'; |
9ccca401 A |
702 | return str_replace('$1', $hashtag, $hashtagLink); |
703 | } | |
1557cefb | 704 | } |