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