aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/bookmark
diff options
context:
space:
mode:
Diffstat (limited to 'tests/bookmark')
-rw-r--r--tests/bookmark/BookmarkFileServiceTest.php155
-rw-r--r--tests/bookmark/BookmarkFilterTest.php2
-rw-r--r--tests/bookmark/BookmarkTest.php69
-rw-r--r--tests/bookmark/LinkUtilsTest.php367
4 files changed, 444 insertions, 149 deletions
diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php
index daafd250..f619aff3 100644
--- a/tests/bookmark/BookmarkFileServiceTest.php
+++ b/tests/bookmark/BookmarkFileServiceTest.php
@@ -686,22 +686,6 @@ class BookmarkFileServiceTest extends TestCase
686 } 686 }
687 687
688 /** 688 /**
689 * List the days for which bookmarks have been posted
690 */
691 public function testDays()
692 {
693 $this->assertSame(
694 ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'],
695 $this->publicLinkDB->days()
696 );
697
698 $this->assertSame(
699 ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'],
700 $this->privateLinkDB->days()
701 );
702 }
703
704 /**
705 * The URL corresponds to an existing entry in the DB 689 * The URL corresponds to an existing entry in the DB
706 */ 690 */
707 public function testGetKnownLinkFromURL() 691 public function testGetKnownLinkFromURL()
@@ -898,6 +882,37 @@ class BookmarkFileServiceTest extends TestCase
898 } 882 }
899 883
900 /** 884 /**
885 * Test filterHash() on a private bookmark while logged out.
886 */
887 public function testFilterHashPrivateWhileLoggedOut()
888 {
889 $this->expectException(BookmarkNotFoundException::class);
890 $this->expectExceptionMessage('The link you are trying to reach does not exist or has been deleted');
891
892 $hash = smallHash('20141125_084734' . 6);
893
894 $this->publicLinkDB->findByHash($hash);
895 }
896
897 /**
898 * Test filterHash() with private key.
899 */
900 public function testFilterHashWithPrivateKey()
901 {
902 $hash = smallHash('20141125_084734' . 6);
903 $privateKey = 'this is usually auto generated';
904
905 $bookmark = $this->privateLinkDB->findByHash($hash);
906 $bookmark->addAdditionalContentEntry('private_key', $privateKey);
907 $this->privateLinkDB->save();
908
909 $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, false);
910 $bookmark = $this->privateLinkDB->findByHash($hash, $privateKey);
911
912 static::assertSame(6, $bookmark->getId());
913 }
914
915 /**
901 * Test linksCountPerTag all tags without filter. 916 * Test linksCountPerTag all tags without filter.
902 * Equal occurrences should be sorted alphabetically. 917 * Equal occurrences should be sorted alphabetically.
903 */ 918 */
@@ -1043,33 +1058,105 @@ class BookmarkFileServiceTest extends TestCase
1043 } 1058 }
1044 1059
1045 /** 1060 /**
1046 * Test filterDay while logged in 1061 * Test find by dates in the middle of the datastore (sorted by dates) with a single bookmark as a result.
1047 */ 1062 */
1048 public function testFilterDayLoggedIn(): void 1063 public function testFilterByDateMidTimePeriodSingleBookmark(): void
1049 { 1064 {
1050 $bookmarks = $this->privateLinkDB->filterDay('20121206'); 1065 $bookmarks = $this->privateLinkDB->findByDate(
1051 $expectedIds = [4, 9, 1, 0]; 1066 DateTime::createFromFormat('Ymd_His', '20121206_150000'),
1067 DateTime::createFromFormat('Ymd_His', '20121206_160000'),
1068 $before,
1069 $after
1070 );
1052 1071
1053 static::assertCount(4, $bookmarks); 1072 static::assertCount(1, $bookmarks);
1054 foreach ($bookmarks as $bookmark) { 1073
1055 $i = ($i ?? -1) + 1; 1074 static::assertSame(9, $bookmarks[0]->getId());
1056 static::assertSame($expectedIds[$i], $bookmark->getId()); 1075 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_142300'), $before);
1057 } 1076 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_172539'), $after);
1058 } 1077 }
1059 1078
1060 /** 1079 /**
1061 * Test filterDay while logged out 1080 * Test find by dates in the middle of the datastore (sorted by dates) with a multiple bookmarks as a result.
1062 */ 1081 */
1063 public function testFilterDayLoggedOut(): void 1082 public function testFilterByDateMidTimePeriodMultipleBookmarks(): void
1064 { 1083 {
1065 $bookmarks = $this->publicLinkDB->filterDay('20121206'); 1084 $bookmarks = $this->privateLinkDB->findByDate(
1066 $expectedIds = [4, 9, 1]; 1085 DateTime::createFromFormat('Ymd_His', '20121206_150000'),
1086 DateTime::createFromFormat('Ymd_His', '20121206_180000'),
1087 $before,
1088 $after
1089 );
1067 1090
1068 static::assertCount(3, $bookmarks); 1091 static::assertCount(2, $bookmarks);
1069 foreach ($bookmarks as $bookmark) { 1092
1070 $i = ($i ?? -1) + 1; 1093 static::assertSame(1, $bookmarks[0]->getId());
1071 static::assertSame($expectedIds[$i], $bookmark->getId()); 1094 static::assertSame(9, $bookmarks[1]->getId());
1072 } 1095 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_142300'), $before);
1096 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_182539'), $after);
1097 }
1098
1099 /**
1100 * Test find by dates at the end of the datastore (sorted by dates).
1101 */
1102 public function testFilterByDateLastTimePeriod(): void
1103 {
1104 $after = new DateTime();
1105 $bookmarks = $this->privateLinkDB->findByDate(
1106 DateTime::createFromFormat('Ymd_His', '20150310_114640'),
1107 DateTime::createFromFormat('Ymd_His', '20450101_010101'),
1108 $before,
1109 $after
1110 );
1111
1112 static::assertCount(1, $bookmarks);
1113
1114 static::assertSame(41, $bookmarks[0]->getId());
1115 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114633'), $before);
1116 static::assertNull($after);
1117 }
1118
1119 /**
1120 * Test find by dates at the beginning of the datastore (sorted by dates).
1121 */
1122 public function testFilterByDateFirstTimePeriod(): void
1123 {
1124 $before = new DateTime();
1125 $bookmarks = $this->privateLinkDB->findByDate(
1126 DateTime::createFromFormat('Ymd_His', '20000101_101010'),
1127 DateTime::createFromFormat('Ymd_His', '20100309_110000'),
1128 $before,
1129 $after
1130 );
1131
1132 static::assertCount(1, $bookmarks);
1133
1134 static::assertSame(11, $bookmarks[0]->getId());
1135 static::assertNull($before);
1136 static::assertEquals(DateTime::createFromFormat('Ymd_His', '20100310_101010'), $after);
1137 }
1138
1139 /**
1140 * Test getLatest with a sticky bookmark: it should be ignored and return the latest by creation date instead.
1141 */
1142 public function testGetLatestWithSticky(): void
1143 {
1144 $bookmark = $this->publicLinkDB->getLatest();
1145
1146 static::assertSame(41, $bookmark->getId());
1147 }
1148
1149 /**
1150 * Test getLatest with a sticky bookmark: it should be ignored and return the latest by creation date instead.
1151 */
1152 public function testGetLatestEmptyDatastore(): void
1153 {
1154 unlink($this->conf->get('resource.datastore'));
1155 $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, $this->mutex, false);
1156
1157 $bookmark = $this->publicLinkDB->getLatest();
1158
1159 static::assertNull($bookmark);
1073 } 1160 }
1074 1161
1075 /** 1162 /**
diff --git a/tests/bookmark/BookmarkFilterTest.php b/tests/bookmark/BookmarkFilterTest.php
index 574d8e3f..835674f2 100644
--- a/tests/bookmark/BookmarkFilterTest.php
+++ b/tests/bookmark/BookmarkFilterTest.php
@@ -44,7 +44,7 @@ class BookmarkFilterTest extends TestCase
44 self::$refDB->write(self::$testDatastore); 44 self::$refDB->write(self::$testDatastore);
45 $history = new History('sandbox/history.php'); 45 $history = new History('sandbox/history.php');
46 self::$bookmarkService = new \FakeBookmarkService($conf, $history, $mutex, true); 46 self::$bookmarkService = new \FakeBookmarkService($conf, $history, $mutex, true);
47 self::$linkFilter = new BookmarkFilter(self::$bookmarkService->getBookmarks()); 47 self::$linkFilter = new BookmarkFilter(self::$bookmarkService->getBookmarks(), $conf);
48 } 48 }
49 49
50 /** 50 /**
diff --git a/tests/bookmark/BookmarkTest.php b/tests/bookmark/BookmarkTest.php
index 4c7ae4c0..cb91b26b 100644
--- a/tests/bookmark/BookmarkTest.php
+++ b/tests/bookmark/BookmarkTest.php
@@ -79,6 +79,23 @@ class BookmarkTest extends TestCase
79 } 79 }
80 80
81 /** 81 /**
82 * Test fromArray() with a link with a custom tags separator
83 */
84 public function testFromArrayCustomTagsSeparator()
85 {
86 $data = [
87 'id' => 1,
88 'tags' => ['tag1', 'tag2', 'chair'],
89 ];
90
91 $bookmark = (new Bookmark())->fromArray($data, '@');
92 $this->assertEquals($data['id'], $bookmark->getId());
93 $this->assertEquals($data['tags'], $bookmark->getTags());
94 $this->assertEquals('tag1@tag2@chair', $bookmark->getTagsString('@'));
95 }
96
97
98 /**
82 * Test validate() with a valid minimal bookmark 99 * Test validate() with a valid minimal bookmark
83 */ 100 */
84 public function testValidateValidFullBookmark() 101 public function testValidateValidFullBookmark()
@@ -252,7 +269,7 @@ class BookmarkTest extends TestCase
252 { 269 {
253 $bookmark = new Bookmark(); 270 $bookmark = new Bookmark();
254 271
255 $str = 'tag1 tag2 tag3.tag3-2, tag4 , -tag5 '; 272 $str = 'tag1 tag2 tag3.tag3-2 tag4 -tag5 ';
256 $bookmark->setTagsString($str); 273 $bookmark->setTagsString($str);
257 $this->assertEquals( 274 $this->assertEquals(
258 [ 275 [
@@ -276,9 +293,9 @@ class BookmarkTest extends TestCase
276 $array = [ 293 $array = [
277 'tag1 ', 294 'tag1 ',
278 ' tag2', 295 ' tag2',
279 'tag3.tag3-2,', 296 'tag3.tag3-2',
280 ', tag4', 297 ' tag4',
281 ', ', 298 ' ',
282 '-tag5 ', 299 '-tag5 ',
283 ]; 300 ];
284 $bookmark->setTags($array); 301 $bookmark->setTags($array);
@@ -347,4 +364,48 @@ class BookmarkTest extends TestCase
347 $bookmark->deleteTag('nope'); 364 $bookmark->deleteTag('nope');
348 $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags()); 365 $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags());
349 } 366 }
367
368 /**
369 * Test shouldUpdateThumbnail() with bookmarks needing an update.
370 */
371 public function testShouldUpdateThumbnail(): void
372 {
373 $bookmark = (new Bookmark())->setUrl('http://domain.tld/with-image');
374
375 static::assertTrue($bookmark->shouldUpdateThumbnail());
376
377 $bookmark = (new Bookmark())
378 ->setUrl('http://domain.tld/with-image')
379 ->setThumbnail('unknown file')
380 ;
381
382 static::assertTrue($bookmark->shouldUpdateThumbnail());
383 }
384
385 /**
386 * Test shouldUpdateThumbnail() with bookmarks that should not update.
387 */
388 public function testShouldNotUpdateThumbnail(): void
389 {
390 $bookmark = (new Bookmark());
391
392 static::assertFalse($bookmark->shouldUpdateThumbnail());
393
394 $bookmark = (new Bookmark())
395 ->setUrl('ftp://domain.tld/other-protocol', ['ftp'])
396 ;
397
398 static::assertFalse($bookmark->shouldUpdateThumbnail());
399
400 $bookmark = (new Bookmark())
401 ->setUrl('http://domain.tld/with-image')
402 ->setThumbnail(__FILE__)
403 ;
404
405 static::assertFalse($bookmark->shouldUpdateThumbnail());
406
407 $bookmark = (new Bookmark())->setUrl('/shaare/abcdef');
408
409 static::assertFalse($bookmark->shouldUpdateThumbnail());
410 }
350} 411}
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.