diff options
author | yude <yudesleepy@gmail.com> | 2021-01-04 18:51:10 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-04 18:51:10 +0900 |
commit | e6754f2154a79abd8e5e64bd923f6984aa9ad44b (patch) | |
tree | f074119530bb59ef155938ea367f719f1e4b70f1 /tests/bookmark/LinkUtilsTest.php | |
parent | 5256b4287021342a9f8868967b2a77e481314331 (diff) | |
parent | ed4ee8f0297941ac83300389b7de6a293312d20e (diff) | |
download | Shaarli-e6754f2154a79abd8e5e64bd923f6984aa9ad44b.tar.gz Shaarli-e6754f2154a79abd8e5e64bd923f6984aa9ad44b.tar.zst Shaarli-e6754f2154a79abd8e5e64bd923f6984aa9ad44b.zip |
Merge pull request #2 from shaarli/master
Merge fork source
Diffstat (limited to 'tests/bookmark/LinkUtilsTest.php')
-rw-r--r-- | tests/bookmark/LinkUtilsTest.php | 367 |
1 files changed, 257 insertions, 110 deletions
diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php index 29941c8c..46a7f1fe 100644 --- a/tests/bookmark/LinkUtilsTest.php +++ b/tests/bookmark/LinkUtilsTest.php | |||
@@ -169,6 +169,36 @@ class LinkUtilsTest extends TestCase | |||
169 | } | 169 | } |
170 | 170 | ||
171 | /** | 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 | |||
201 | /** | ||
172 | * Test html_extract_tag() when the tag <meta name= is not found. | 202 | * Test html_extract_tag() when the tag <meta name= is not found. |
173 | */ | 203 | */ |
174 | public function testHtmlExtractNonExistentNameTag() | 204 | public function testHtmlExtractNonExistentNameTag() |
@@ -215,61 +245,104 @@ class LinkUtilsTest extends TestCase | |||
215 | $this->assertFalse(html_extract_tag('description', $html)); | 245 | $this->assertFalse(html_extract_tag('description', $html)); |
216 | } | 246 | } |
217 | 247 | ||
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 | |||
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 | |||
218 | /** | 279 | /** |
219 | * Test the download callback with valid value | 280 | * Test the download callback with valid value |
220 | */ | 281 | */ |
221 | public function testCurlDownloadCallbackOk() | 282 | public function testCurlDownloadCallbackOk(): void |
222 | { | 283 | { |
284 | $charset = 'utf-8'; | ||
223 | $callback = get_curl_download_callback( | 285 | $callback = get_curl_download_callback( |
224 | $charset, | 286 | $charset, |
225 | $title, | 287 | $title, |
226 | $desc, | 288 | $desc, |
227 | $keywords, | 289 | $keywords, |
228 | false, | 290 | false, |
229 | 'ut_curl_getinfo_ok' | 291 | ' ' |
230 | ); | 292 | ); |
293 | |||
231 | $data = [ | 294 | $data = [ |
232 | 'HTTP/1.1 200 OK', | 295 | 'th=device-width">' |
233 | 'Server: GitHub.com', | ||
234 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
235 | 'Content-Type: text/html; charset=utf-8', | ||
236 | 'Status: 200 OK', | ||
237 | 'end' => 'th=device-width">' | ||
238 | . '<title>Refactoring · GitHub</title>' | 296 | . '<title>Refactoring · GitHub</title>' |
239 | . '<link rel="search" type="application/opensea', | 297 | . '<link rel="search" type="application/opensea', |
240 | '<title>ignored</title>' | 298 | '<title>ignored</title>' |
241 | . '<meta name="description" content="desc" />' | 299 | . '<meta name="description" content="desc" />' |
242 | . '<meta name="keywords" content="key1,key2" />', | 300 | . '<meta name="keywords" content="key1,key2" />', |
243 | ]; | 301 | ]; |
244 | foreach ($data as $key => $line) { | 302 | |
245 | $ignore = null; | 303 | foreach ($data as $chunk) { |
246 | $expected = $key !== 'end' ? strlen($line) : false; | 304 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
247 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
248 | if ($expected === false) { | ||
249 | break; | ||
250 | } | ||
251 | } | 305 | } |
252 | $this->assertEquals('utf-8', $charset); | 306 | |
253 | $this->assertEquals('Refactoring · GitHub', $title); | 307 | static::assertSame('utf-8', $charset); |
254 | $this->assertEmpty($desc); | 308 | static::assertSame('Refactoring · GitHub', $title); |
255 | $this->assertEmpty($keywords); | 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); | ||
256 | } | 328 | } |
257 | 329 | ||
258 | /** | 330 | /** |
259 | * Test the download callback with valid values and no charset | 331 | * Test the download callback with valid values and no charset |
260 | */ | 332 | */ |
261 | public function testCurlDownloadCallbackOkNoCharset() | 333 | public function testCurlDownloadCallbackOkNoCharset(): void |
262 | { | 334 | { |
335 | $charset = null; | ||
263 | $callback = get_curl_download_callback( | 336 | $callback = get_curl_download_callback( |
264 | $charset, | 337 | $charset, |
265 | $title, | 338 | $title, |
266 | $desc, | 339 | $desc, |
267 | $keywords, | 340 | $keywords, |
268 | false, | 341 | false, |
269 | 'ut_curl_getinfo_no_charset' | 342 | ' ' |
270 | ); | 343 | ); |
344 | |||
271 | $data = [ | 345 | $data = [ |
272 | 'HTTP/1.1 200 OK', | ||
273 | 'end' => 'th=device-width">' | 346 | 'end' => 'th=device-width">' |
274 | . '<title>Refactoring · GitHub</title>' | 347 | . '<title>Refactoring · GitHub</title>' |
275 | . '<link rel="search" type="application/opensea', | 348 | . '<link rel="search" type="application/opensea', |
@@ -277,10 +350,11 @@ class LinkUtilsTest extends TestCase | |||
277 | . '<meta name="description" content="desc" />' | 350 | . '<meta name="description" content="desc" />' |
278 | . '<meta name="keywords" content="key1,key2" />', | 351 | . '<meta name="keywords" content="key1,key2" />', |
279 | ]; | 352 | ]; |
280 | foreach ($data as $key => $line) { | 353 | |
281 | $ignore = null; | 354 | foreach ($data as $chunk) { |
282 | $this->assertEquals(strlen($line), $callback($ignore, $line)); | 355 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
283 | } | 356 | } |
357 | |||
284 | $this->assertEmpty($charset); | 358 | $this->assertEmpty($charset); |
285 | $this->assertEquals('Refactoring · GitHub', $title); | 359 | $this->assertEquals('Refactoring · GitHub', $title); |
286 | $this->assertEmpty($desc); | 360 | $this->assertEmpty($desc); |
@@ -290,18 +364,19 @@ class LinkUtilsTest extends TestCase | |||
290 | /** | 364 | /** |
291 | * Test the download callback with valid values and no charset | 365 | * Test the download callback with valid values and no charset |
292 | */ | 366 | */ |
293 | public function testCurlDownloadCallbackOkHtmlCharset() | 367 | public function testCurlDownloadCallbackOkHtmlCharset(): void |
294 | { | 368 | { |
369 | $charset = null; | ||
295 | $callback = get_curl_download_callback( | 370 | $callback = get_curl_download_callback( |
296 | $charset, | 371 | $charset, |
297 | $title, | 372 | $title, |
298 | $desc, | 373 | $desc, |
299 | $keywords, | 374 | $keywords, |
300 | false, | 375 | false, |
301 | 'ut_curl_getinfo_no_charset' | 376 | ' ' |
302 | ); | 377 | ); |
378 | |||
303 | $data = [ | 379 | $data = [ |
304 | 'HTTP/1.1 200 OK', | ||
305 | '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', | 380 | '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', |
306 | 'end' => 'th=device-width">' | 381 | 'end' => 'th=device-width">' |
307 | . '<title>Refactoring · GitHub</title>' | 382 | . '<title>Refactoring · GitHub</title>' |
@@ -310,14 +385,10 @@ class LinkUtilsTest extends TestCase | |||
310 | . '<meta name="description" content="desc" />' | 385 | . '<meta name="description" content="desc" />' |
311 | . '<meta name="keywords" content="key1,key2" />', | 386 | . '<meta name="keywords" content="key1,key2" />', |
312 | ]; | 387 | ]; |
313 | foreach ($data as $key => $line) { | 388 | foreach ($data as $chunk) { |
314 | $ignore = null; | 389 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
315 | $expected = $key !== 'end' ? strlen($line) : false; | ||
316 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
317 | if ($expected === false) { | ||
318 | break; | ||
319 | } | ||
320 | } | 390 | } |
391 | |||
321 | $this->assertEquals('utf-8', $charset); | 392 | $this->assertEquals('utf-8', $charset); |
322 | $this->assertEquals('Refactoring · GitHub', $title); | 393 | $this->assertEquals('Refactoring · GitHub', $title); |
323 | $this->assertEmpty($desc); | 394 | $this->assertEmpty($desc); |
@@ -327,25 +398,27 @@ class LinkUtilsTest extends TestCase | |||
327 | /** | 398 | /** |
328 | * Test the download callback with valid values and no title | 399 | * Test the download callback with valid values and no title |
329 | */ | 400 | */ |
330 | public function testCurlDownloadCallbackOkNoTitle() | 401 | public function testCurlDownloadCallbackOkNoTitle(): void |
331 | { | 402 | { |
403 | $charset = 'utf-8'; | ||
332 | $callback = get_curl_download_callback( | 404 | $callback = get_curl_download_callback( |
333 | $charset, | 405 | $charset, |
334 | $title, | 406 | $title, |
335 | $desc, | 407 | $desc, |
336 | $keywords, | 408 | $keywords, |
337 | false, | 409 | false, |
338 | 'ut_curl_getinfo_ok' | 410 | ' ' |
339 | ); | 411 | ); |
412 | |||
340 | $data = [ | 413 | $data = [ |
341 | 'HTTP/1.1 200 OK', | ||
342 | 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea', | 414 | 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea', |
343 | 'ignored', | 415 | 'ignored', |
344 | ]; | 416 | ]; |
345 | foreach ($data as $key => $line) { | 417 | |
346 | $ignore = null; | 418 | foreach ($data as $chunk) { |
347 | $this->assertEquals(strlen($line), $callback($ignore, $line)); | 419 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
348 | } | 420 | } |
421 | |||
349 | $this->assertEquals('utf-8', $charset); | 422 | $this->assertEquals('utf-8', $charset); |
350 | $this->assertEmpty($title); | 423 | $this->assertEmpty($title); |
351 | $this->assertEmpty($desc); | 424 | $this->assertEmpty($desc); |
@@ -353,81 +426,56 @@ class LinkUtilsTest extends TestCase | |||
353 | } | 426 | } |
354 | 427 | ||
355 | /** | 428 | /** |
356 | * Test the download callback with an invalid content type. | 429 | * Test the header callback with an invalid content type. |
357 | */ | 430 | */ |
358 | public function testCurlDownloadCallbackInvalidContentType() | 431 | public function testCurlHeaderCallbackInvalidContentType(): void |
359 | { | 432 | { |
360 | $callback = get_curl_download_callback( | 433 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_ct_ko'); |
361 | $charset, | 434 | $data = [ |
362 | $title, | 435 | 'HTTP/1.1 200 OK', |
363 | $desc, | 436 | ]; |
364 | $keywords, | 437 | |
365 | false, | 438 | static::assertFalse($callback(null, $data[0])); |
366 | 'ut_curl_getinfo_ct_ko' | 439 | static::assertNull($charset); |
367 | ); | ||
368 | $ignore = null; | ||
369 | $this->assertFalse($callback($ignore, '')); | ||
370 | $this->assertEmpty($charset); | ||
371 | $this->assertEmpty($title); | ||
372 | } | 440 | } |
373 | 441 | ||
374 | /** | 442 | /** |
375 | * Test the download callback with an invalid response code. | 443 | * Test the header callback with an invalid response code. |
376 | */ | 444 | */ |
377 | public function testCurlDownloadCallbackInvalidResponseCode() | 445 | public function testCurlHeaderCallbackInvalidResponseCode(): void |
378 | { | 446 | { |
379 | $callback = $callback = get_curl_download_callback( | 447 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rc_ko'); |
380 | $charset, | 448 | |
381 | $title, | 449 | static::assertFalse($callback(null, '')); |
382 | $desc, | 450 | static::assertNull($charset); |
383 | $keywords, | ||
384 | false, | ||
385 | 'ut_curl_getinfo_rc_ko' | ||
386 | ); | ||
387 | $ignore = null; | ||
388 | $this->assertFalse($callback($ignore, '')); | ||
389 | $this->assertEmpty($charset); | ||
390 | $this->assertEmpty($title); | ||
391 | } | 451 | } |
392 | 452 | ||
393 | /** | 453 | /** |
394 | * Test the download callback with an invalid content type and response code. | 454 | * Test the header callback with an invalid content type and response code. |
395 | */ | 455 | */ |
396 | public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode() | 456 | public function testCurlHeaderCallbackInvalidContentTypeAndResponseCode(): void |
397 | { | 457 | { |
398 | $callback = $callback = get_curl_download_callback( | 458 | $callback = get_curl_header_callback($charset, 'ut_curl_getinfo_rs_ct_ko'); |
399 | $charset, | 459 | |
400 | $title, | 460 | static::assertFalse($callback(null, '')); |
401 | $desc, | 461 | static::assertNull($charset); |
402 | $keywords, | ||
403 | false, | ||
404 | 'ut_curl_getinfo_rs_ct_ko' | ||
405 | ); | ||
406 | $ignore = null; | ||
407 | $this->assertFalse($callback($ignore, '')); | ||
408 | $this->assertEmpty($charset); | ||
409 | $this->assertEmpty($title); | ||
410 | } | 462 | } |
411 | 463 | ||
412 | /** | 464 | /** |
413 | * Test the download callback with valid value, and retrieve_description option enabled. | 465 | * Test the download callback with valid value, and retrieve_description option enabled. |
414 | */ | 466 | */ |
415 | public function testCurlDownloadCallbackOkWithDesc() | 467 | public function testCurlDownloadCallbackOkWithDesc(): void |
416 | { | 468 | { |
469 | $charset = 'utf-8'; | ||
417 | $callback = get_curl_download_callback( | 470 | $callback = get_curl_download_callback( |
418 | $charset, | 471 | $charset, |
419 | $title, | 472 | $title, |
420 | $desc, | 473 | $desc, |
421 | $keywords, | 474 | $keywords, |
422 | true, | 475 | true, |
423 | 'ut_curl_getinfo_ok' | 476 | ' ' |
424 | ); | 477 | ); |
425 | $data = [ | 478 | $data = [ |
426 | 'HTTP/1.1 200 OK', | ||
427 | 'Server: GitHub.com', | ||
428 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
429 | 'Content-Type: text/html; charset=utf-8', | ||
430 | 'Status: 200 OK', | ||
431 | 'th=device-width">' | 479 | 'th=device-width">' |
432 | . '<title>Refactoring · GitHub</title>' | 480 | . '<title>Refactoring · GitHub</title>' |
433 | . '<link rel="search" type="application/opensea', | 481 | . '<link rel="search" type="application/opensea', |
@@ -435,14 +483,11 @@ class LinkUtilsTest extends TestCase | |||
435 | . '<meta name="description" content="link desc" />' | 483 | . '<meta name="description" content="link desc" />' |
436 | . '<meta name="keywords" content="key1,key2" />', | 484 | . '<meta name="keywords" content="key1,key2" />', |
437 | ]; | 485 | ]; |
438 | foreach ($data as $key => $line) { | 486 | |
439 | $ignore = null; | 487 | foreach ($data as $chunk) { |
440 | $expected = $key !== 'end' ? strlen($line) : false; | 488 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
441 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
442 | if ($expected === false) { | ||
443 | break; | ||
444 | } | ||
445 | } | 489 | } |
490 | |||
446 | $this->assertEquals('utf-8', $charset); | 491 | $this->assertEquals('utf-8', $charset); |
447 | $this->assertEquals('Refactoring · GitHub', $title); | 492 | $this->assertEquals('Refactoring · GitHub', $title); |
448 | $this->assertEquals('link desc', $desc); | 493 | $this->assertEquals('link desc', $desc); |
@@ -453,8 +498,9 @@ class LinkUtilsTest extends TestCase | |||
453 | * Test the download callback with valid value, and retrieve_description option enabled, | 498 | * Test the download callback with valid value, and retrieve_description option enabled, |
454 | * but no desc or keyword defined in the page. | 499 | * but no desc or keyword defined in the page. |
455 | */ | 500 | */ |
456 | public function testCurlDownloadCallbackOkWithDescNotFound() | 501 | public function testCurlDownloadCallbackOkWithDescNotFound(): void |
457 | { | 502 | { |
503 | $charset = 'utf-8'; | ||
458 | $callback = get_curl_download_callback( | 504 | $callback = get_curl_download_callback( |
459 | $charset, | 505 | $charset, |
460 | $title, | 506 | $title, |
@@ -464,24 +510,16 @@ class LinkUtilsTest extends TestCase | |||
464 | 'ut_curl_getinfo_ok' | 510 | 'ut_curl_getinfo_ok' |
465 | ); | 511 | ); |
466 | $data = [ | 512 | $data = [ |
467 | 'HTTP/1.1 200 OK', | ||
468 | 'Server: GitHub.com', | ||
469 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
470 | 'Content-Type: text/html; charset=utf-8', | ||
471 | 'Status: 200 OK', | ||
472 | 'th=device-width">' | 513 | 'th=device-width">' |
473 | . '<title>Refactoring · GitHub</title>' | 514 | . '<title>Refactoring · GitHub</title>' |
474 | . '<link rel="search" type="application/opensea', | 515 | . '<link rel="search" type="application/opensea', |
475 | 'end' => '<title>ignored</title>', | 516 | 'end' => '<title>ignored</title>', |
476 | ]; | 517 | ]; |
477 | foreach ($data as $key => $line) { | 518 | |
478 | $ignore = null; | 519 | foreach ($data as $chunk) { |
479 | $expected = $key !== 'end' ? strlen($line) : false; | 520 | static::assertSame(strlen($chunk), $callback(null, $chunk)); |
480 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
481 | if ($expected === false) { | ||
482 | break; | ||
483 | } | ||
484 | } | 521 | } |
522 | |||
485 | $this->assertEquals('utf-8', $charset); | 523 | $this->assertEquals('utf-8', $charset); |
486 | $this->assertEquals('Refactoring · GitHub', $title); | 524 | $this->assertEquals('Refactoring · GitHub', $title); |
487 | $this->assertEmpty($desc); | 525 | $this->assertEmpty($desc); |
@@ -582,6 +620,115 @@ class LinkUtilsTest extends TestCase | |||
582 | } | 620 | } |
583 | 621 | ||
584 | /** | 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 | |||
731 | /** | ||
585 | * Util function to build an hashtag link. | 732 | * Util function to build an hashtag link. |
586 | * | 733 | * |
587 | * @param string $hashtag Hashtag name. | 734 | * @param string $hashtag Hashtag name. |