aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/bookmark/BookmarkTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/bookmark/BookmarkTest.php')
-rw-r--r--tests/bookmark/BookmarkTest.php107
1 files changed, 65 insertions, 42 deletions
diff --git a/tests/bookmark/BookmarkTest.php b/tests/bookmark/BookmarkTest.php
index afec2440..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()
@@ -154,25 +171,6 @@ class BookmarkTest extends TestCase
154 } 171 }
155 172
156 /** 173 /**
157 * Test validate() with a a bookmark with a non integer ID.
158 */
159 public function testValidateNotValidStringId()
160 {
161 $bookmark = new Bookmark();
162 $bookmark->setId('str');
163 $bookmark->setShortUrl('abc');
164 $bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102'));
165 $exception = null;
166 try {
167 $bookmark->validate();
168 } catch (InvalidBookmarkException $e) {
169 $exception = $e;
170 }
171 $this->assertNotNull($exception);
172 $this->assertContainsPolyfill('- ID: str'. PHP_EOL, $exception->getMessage());
173 }
174
175 /**
176 * Test validate() with a a bookmark without short url. 174 * Test validate() with a a bookmark without short url.
177 */ 175 */
178 public function testValidateNotValidNoShortUrl() 176 public function testValidateNotValidNoShortUrl()
@@ -211,25 +209,6 @@ class BookmarkTest extends TestCase
211 } 209 }
212 210
213 /** 211 /**
214 * Test validate() with a a bookmark with a bad created datetime.
215 */
216 public function testValidateNotValidBadCreated()
217 {
218 $bookmark = new Bookmark();
219 $bookmark->setId(1);
220 $bookmark->setShortUrl('abc');
221 $bookmark->setCreated('hi!');
222 $exception = null;
223 try {
224 $bookmark->validate();
225 } catch (InvalidBookmarkException $e) {
226 $exception = $e;
227 }
228 $this->assertNotNull($exception);
229 $this->assertContainsPolyfill('- Created: Not a DateTime object'. PHP_EOL, $exception->getMessage());
230 }
231
232 /**
233 * Test setId() and make sure that default fields are generated. 212 * Test setId() and make sure that default fields are generated.
234 */ 213 */
235 public function testSetIdEmptyGeneratedFields() 214 public function testSetIdEmptyGeneratedFields()
@@ -290,7 +269,7 @@ class BookmarkTest extends TestCase
290 { 269 {
291 $bookmark = new Bookmark(); 270 $bookmark = new Bookmark();
292 271
293 $str = 'tag1 tag2 tag3.tag3-2, tag4 , -tag5 '; 272 $str = 'tag1 tag2 tag3.tag3-2 tag4 -tag5 ';
294 $bookmark->setTagsString($str); 273 $bookmark->setTagsString($str);
295 $this->assertEquals( 274 $this->assertEquals(
296 [ 275 [
@@ -314,9 +293,9 @@ class BookmarkTest extends TestCase
314 $array = [ 293 $array = [
315 'tag1 ', 294 'tag1 ',
316 ' tag2', 295 ' tag2',
317 'tag3.tag3-2,', 296 'tag3.tag3-2',
318 ', tag4', 297 ' tag4',
319 ', ', 298 ' ',
320 '-tag5 ', 299 '-tag5 ',
321 ]; 300 ];
322 $bookmark->setTags($array); 301 $bookmark->setTags($array);
@@ -385,4 +364,48 @@ class BookmarkTest extends TestCase
385 $bookmark->deleteTag('nope'); 364 $bookmark->deleteTag('nope');
386 $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags()); 365 $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags());
387 } 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 }
388} 411}