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