]>
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, | |
5334090b | 280 | false |
6a487252 | 281 | ); |
5334090b | 282 | |
d65342e3 | 283 | $data = [ |
5334090b | 284 | 'th=device-width">' |
fe3713d2 V |
285 | . '<title>Refactoring · GitHub</title>' |
286 | . '<link rel="search" type="application/opensea', | |
6a487252 A |
287 | '<title>ignored</title>' |
288 | . '<meta name="description" content="desc" />' | |
289 | . '<meta name="keywords" content="key1,key2" />', | |
d65342e3 | 290 | ]; |
5334090b A |
291 | |
292 | foreach ($data as $chunk) { | |
293 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
d65342e3 | 294 | } |
5334090b A |
295 | |
296 | static::assertSame('utf-8', $charset); | |
297 | static::assertSame('Refactoring · GitHub', $title); | |
298 | static::assertEmpty($desc); | |
299 | static::assertEmpty($keywords); | |
300 | } | |
301 | ||
302 | /** | |
303 | * Test the header callback with valid value | |
304 | */ | |
305 | public function testCurlHeaderCallbackNoCharset(): void | |
306 | { | |
307 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_no_charset'); | |
308 | $data = [ | |
309 | 'HTTP/1.1 200 OK', | |
310 | ]; | |
311 | ||
312 | foreach ($data as $chunk) { | |
313 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
314 | } | |
315 | ||
316 | static::assertFalse($charset); | |
d65342e3 A |
317 | } |
318 | ||
319 | /** | |
320 | * Test the download callback with valid values and no charset | |
321 | */ | |
5334090b | 322 | public function testCurlDownloadCallbackOkNoCharset(): void |
d65342e3 | 323 | { |
5334090b | 324 | $charset = null; |
6a487252 A |
325 | $callback = get_curl_download_callback( |
326 | $charset, | |
327 | $title, | |
328 | $desc, | |
329 | $keywords, | |
5334090b | 330 | false |
6a487252 | 331 | ); |
5334090b | 332 | |
d65342e3 | 333 | $data = [ |
9d9f6d75 | 334 | 'end' => 'th=device-width">' |
fe3713d2 V |
335 | . '<title>Refactoring · GitHub</title>' |
336 | . '<link rel="search" type="application/opensea', | |
6a487252 A |
337 | '<title>ignored</title>' |
338 | . '<meta name="description" content="desc" />' | |
339 | . '<meta name="keywords" content="key1,key2" />', | |
d65342e3 | 340 | ]; |
5334090b A |
341 | |
342 | foreach ($data as $chunk) { | |
343 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
d65342e3 | 344 | } |
5334090b | 345 | |
d65342e3 A |
346 | $this->assertEmpty($charset); |
347 | $this->assertEquals('Refactoring · GitHub', $title); | |
6a487252 A |
348 | $this->assertEmpty($desc); |
349 | $this->assertEmpty($keywords); | |
d65342e3 A |
350 | } |
351 | ||
352 | /** | |
353 | * Test the download callback with valid values and no charset | |
354 | */ | |
5334090b | 355 | public function testCurlDownloadCallbackOkHtmlCharset(): void |
d65342e3 | 356 | { |
5334090b | 357 | $charset = null; |
6a487252 A |
358 | $callback = get_curl_download_callback( |
359 | $charset, | |
360 | $title, | |
361 | $desc, | |
362 | $keywords, | |
5334090b | 363 | false |
6a487252 | 364 | ); |
5334090b | 365 | |
d65342e3 | 366 | $data = [ |
d65342e3 | 367 | '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', |
9d9f6d75 | 368 | 'end' => 'th=device-width">' |
fe3713d2 V |
369 | . '<title>Refactoring · GitHub</title>' |
370 | . '<link rel="search" type="application/opensea', | |
6a487252 A |
371 | '<title>ignored</title>' |
372 | . '<meta name="description" content="desc" />' | |
373 | . '<meta name="keywords" content="key1,key2" />', | |
d65342e3 | 374 | ]; |
5334090b A |
375 | foreach ($data as $chunk) { |
376 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
d65342e3 | 377 | } |
5334090b | 378 | |
d65342e3 A |
379 | $this->assertEquals('utf-8', $charset); |
380 | $this->assertEquals('Refactoring · GitHub', $title); | |
6a487252 A |
381 | $this->assertEmpty($desc); |
382 | $this->assertEmpty($keywords); | |
d65342e3 A |
383 | } |
384 | ||
385 | /** | |
386 | * Test the download callback with valid values and no title | |
387 | */ | |
5334090b | 388 | public function testCurlDownloadCallbackOkNoTitle(): void |
d65342e3 | 389 | { |
5334090b | 390 | $charset = 'utf-8'; |
6a487252 A |
391 | $callback = get_curl_download_callback( |
392 | $charset, | |
393 | $title, | |
394 | $desc, | |
395 | $keywords, | |
5334090b | 396 | false |
6a487252 | 397 | ); |
5334090b | 398 | |
d65342e3 | 399 | $data = [ |
d65342e3 A |
400 | 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea', |
401 | 'ignored', | |
402 | ]; | |
5334090b A |
403 | |
404 | foreach ($data as $chunk) { | |
405 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
d65342e3 | 406 | } |
5334090b | 407 | |
d65342e3 A |
408 | $this->assertEquals('utf-8', $charset); |
409 | $this->assertEmpty($title); | |
6a487252 A |
410 | $this->assertEmpty($desc); |
411 | $this->assertEmpty($keywords); | |
d65342e3 A |
412 | } |
413 | ||
414 | /** | |
5334090b | 415 | * Test the header callback with an invalid content type. |
d65342e3 | 416 | */ |
5334090b | 417 | public function testCurlHeaderCallbackInvalidContentType(): void |
d65342e3 | 418 | { |
5334090b A |
419 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_ct_ko'); |
420 | $data = [ | |
421 | 'HTTP/1.1 200 OK', | |
422 | ]; | |
423 | ||
424 | static::assertFalse($callback(null, $data[0])); | |
425 | static::assertNull($charset); | |
d65342e3 A |
426 | } |
427 | ||
428 | /** | |
5334090b | 429 | * Test the header callback with an invalid response code. |
d65342e3 | 430 | */ |
5334090b | 431 | public function testCurlHeaderCallbackInvalidResponseCode(): void |
d65342e3 | 432 | { |
5334090b A |
433 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rc_ko'); |
434 | ||
435 | static::assertFalse($callback(null, '')); | |
436 | static::assertNull($charset); | |
d65342e3 A |
437 | } |
438 | ||
439 | /** | |
5334090b | 440 | * Test the header callback with an invalid content type and response code. |
d65342e3 | 441 | */ |
5334090b | 442 | public function testCurlHeaderCallbackInvalidContentTypeAndResponseCode(): void |
d65342e3 | 443 | { |
5334090b A |
444 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rs_ct_ko'); |
445 | ||
446 | static::assertFalse($callback(null, '')); | |
447 | static::assertNull($charset); | |
d65342e3 A |
448 | } |
449 | ||
6a487252 A |
450 | /** |
451 | * Test the download callback with valid value, and retrieve_description option enabled. | |
452 | */ | |
5334090b | 453 | public function testCurlDownloadCallbackOkWithDesc(): void |
6a487252 | 454 | { |
5334090b | 455 | $charset = 'utf-8'; |
6a487252 A |
456 | $callback = get_curl_download_callback( |
457 | $charset, | |
458 | $title, | |
459 | $desc, | |
460 | $keywords, | |
5334090b | 461 | true |
6a487252 A |
462 | ); |
463 | $data = [ | |
6a487252 A |
464 | 'th=device-width">' |
465 | . '<title>Refactoring · GitHub</title>' | |
466 | . '<link rel="search" type="application/opensea', | |
467 | 'end' => '<title>ignored</title>' | |
468 | . '<meta name="description" content="link desc" />' | |
469 | . '<meta name="keywords" content="key1,key2" />', | |
470 | ]; | |
5334090b A |
471 | |
472 | foreach ($data as $chunk) { | |
473 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
6a487252 | 474 | } |
5334090b | 475 | |
6a487252 A |
476 | $this->assertEquals('utf-8', $charset); |
477 | $this->assertEquals('Refactoring · GitHub', $title); | |
478 | $this->assertEquals('link desc', $desc); | |
479 | $this->assertEquals('key1 key2', $keywords); | |
480 | } | |
481 | ||
482 | /** | |
483 | * Test the download callback with valid value, and retrieve_description option enabled, | |
484 | * but no desc or keyword defined in the page. | |
485 | */ | |
5334090b | 486 | public function testCurlDownloadCallbackOkWithDescNotFound(): void |
6a487252 | 487 | { |
5334090b | 488 | $charset = 'utf-8'; |
6a487252 A |
489 | $callback = get_curl_download_callback( |
490 | $charset, | |
491 | $title, | |
492 | $desc, | |
493 | $keywords, | |
494 | true, | |
495 | 'ut_curl_getinfo_ok' | |
496 | ); | |
497 | $data = [ | |
6a487252 A |
498 | 'th=device-width">' |
499 | . '<title>Refactoring · GitHub</title>' | |
500 | . '<link rel="search" type="application/opensea', | |
501 | 'end' => '<title>ignored</title>', | |
502 | ]; | |
5334090b A |
503 | |
504 | foreach ($data as $chunk) { | |
505 | static::assertSame(strlen($chunk), $callback(null, $chunk)); | |
6a487252 | 506 | } |
5334090b | 507 | |
6a487252 A |
508 | $this->assertEquals('utf-8', $charset); |
509 | $this->assertEquals('Refactoring · GitHub', $title); | |
510 | $this->assertEmpty($desc); | |
511 | $this->assertEmpty($keywords); | |
512 | } | |
513 | ||
9ccca401 | 514 | /** |
520d2957 | 515 | * Test text2clickable. |
9ccca401 | 516 | */ |
520d2957 | 517 | public function testText2clickable() |
9ccca401 A |
518 | { |
519 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
9d9f6d75 | 520 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here">' |
fe3713d2 | 521 | . 'http://hello.there/is=someone#here</a> otherstuff'; |
520d2957 | 522 | $processedText = text2clickable($text); |
9ccca401 | 523 | $this->assertEquals($expectedText, $processedText); |
601faf97 A |
524 | |
525 | $text = 'stuff http://hello.there/is=someone#here(please) otherstuff'; | |
9d9f6d75 | 526 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)">' |
fe3713d2 | 527 | . 'http://hello.there/is=someone#here(please)</a> otherstuff'; |
520d2957 | 528 | $processedText = text2clickable($text); |
601faf97 A |
529 | $this->assertEquals($expectedText, $processedText); |
530 | ||
520d2957 | 531 | $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; |
601faf97 | 532 | $text = 'stuff http://hello.there/is=someone#here(please)&no otherstuff'; |
9d9f6d75 | 533 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here(please)&no">' |
fe3713d2 | 534 | . 'http://hello.there/is=someone#here(please)&no</a> otherstuff'; |
520d2957 | 535 | $processedText = text2clickable($text); |
fd08b50a A |
536 | $this->assertEquals($expectedText, $processedText); |
537 | } | |
538 | ||
9ccca401 A |
539 | /** |
540 | * Test testSpace2nbsp. | |
541 | */ | |
542 | public function testSpace2nbsp() | |
543 | { | |
fe3713d2 V |
544 | $text = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?'; |
545 | $expectedText = ' Are you thrilled by flags ?' . PHP_EOL . ' Really?'; | |
9ccca401 A |
546 | $processedText = space2nbsp($text); |
547 | $this->assertEquals($expectedText, $processedText); | |
548 | } | |
549 | ||
550 | /** | |
551 | * Test hashtags auto-link. | |
552 | */ | |
553 | public function testHashtagAutolink() | |
554 | { | |
555 | $index = 'http://domain.tld/'; | |
556 | $rawDescription = '#hashtag\n | |
557 | # nothashtag\n | |
558 | test#nothashtag #hashtag \#nothashtag\n | |
559 | test #hashtag #hashtag test #hashtag.test\n | |
560 | #hashtag #hashtag-nothashtag #hashtag_hashtag\n | |
561 | What is #ашок anyway?\n | |
562 | カタカナ #カタカナ」カタカナ\n'; | |
563 | $autolinkedDescription = hashtag_autolink($rawDescription, $index); | |
564 | ||
a5a9cf23 A |
565 | $this->assertContainsPolyfill($this->getHashtagLink('hashtag', $index), $autolinkedDescription); |
566 | $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription); | |
567 | $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription); | |
568 | $this->assertContainsPolyfill($this->getHashtagLink('ашок', $index), $autolinkedDescription); | |
569 | $this->assertContainsPolyfill($this->getHashtagLink('カタカナ', $index), $autolinkedDescription); | |
570 | $this->assertContainsPolyfill($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription); | |
571 | $this->assertNotContainsPolyfill($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription); | |
9ccca401 A |
572 | } |
573 | ||
574 | /** | |
575 | * Test hashtags auto-link without index URL. | |
576 | */ | |
577 | public function testHashtagAutolinkNoIndex() | |
578 | { | |
579 | $rawDescription = 'blabla #hashtag x#nothashtag'; | |
580 | $autolinkedDescription = hashtag_autolink($rawDescription); | |
581 | ||
a5a9cf23 A |
582 | $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription); |
583 | $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription); | |
584 | $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription); | |
9ccca401 A |
585 | } |
586 | ||
a8e7da01 A |
587 | /** |
588 | * Test is_note with note URLs. | |
589 | */ | |
590 | public function testIsNote() | |
591 | { | |
592 | $this->assertTrue(is_note('?')); | |
593 | $this->assertTrue(is_note('?abcDEf')); | |
594 | $this->assertTrue(is_note('?_abcDEf#123')); | |
595 | } | |
596 | ||
597 | /** | |
598 | * Test is_note with non note URLs. | |
599 | */ | |
600 | public function testIsNotNote() | |
601 | { | |
602 | $this->assertFalse(is_note('')); | |
603 | $this->assertFalse(is_note('nope')); | |
604 | $this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi')); | |
605 | } | |
606 | ||
9ccca401 A |
607 | /** |
608 | * Util function to build an hashtag link. | |
609 | * | |
610 | * @param string $hashtag Hashtag name. | |
fe3713d2 | 611 | * @param string $index Index URL. |
9ccca401 A |
612 | * |
613 | * @return string HTML hashtag link. | |
614 | */ | |
615 | private function getHashtagLink($hashtag, $index = '') | |
616 | { | |
03340c18 | 617 | $hashtagLink = '<a href="' . $index . './add-tag/$1" title="Hashtag $1">#$1</a>'; |
9ccca401 A |
618 | return str_replace('$1', $hashtag, $hashtagLink); |
619 | } | |
1557cefb | 620 | } |