3 namespace Shaarli\Bookmark
;
5 use Shaarli\Bookmark\Exception\InvalidBookmarkException
;
11 class BookmarkTest
extends TestCase
14 * Test fromArray() with a link with full data
16 public function testFromArrayFull()
21 'url' => 'https://domain.tld/oof.html?param=value#anchor',
22 'title' => 'This is an array link',
23 'description' => 'HTML desc<br><p>hi!</p>',
24 'thumbnail' => 'https://domain.tld/pic.png',
26 'created' => new \
DateTime('-1 minute'),
27 'tags' => ['tag1', 'tag2', 'chair'],
28 'updated' => new \
DateTime(),
32 $bookmark = (new Bookmark())->fromArray($data);
33 $this->assertEquals($data['id'], $bookmark->getId());
34 $this->assertEquals($data['shorturl'], $bookmark->getShortUrl());
35 $this->assertEquals($data['url'], $bookmark->getUrl());
36 $this->assertEquals($data['title'], $bookmark->getTitle());
37 $this->assertEquals($data['description'], $bookmark->getDescription());
38 $this->assertEquals($data['thumbnail'], $bookmark->getThumbnail());
39 $this->assertEquals($data['sticky'], $bookmark->isSticky());
40 $this->assertEquals($data['created'], $bookmark->getCreated());
41 $this->assertEquals($data['tags'], $bookmark->getTags());
42 $this->assertEquals('tag1 tag2 chair', $bookmark->getTagsString());
43 $this->assertEquals($data['updated'], $bookmark->getUpdated());
44 $this->assertEquals($data['private'], $bookmark->isPrivate());
45 $this->assertFalse($bookmark->isNote());
49 * Test fromArray() with a link with minimal data.
50 * Note that I use null values everywhere but this should not happen in the real world.
52 public function testFromArrayMinimal()
59 'description' => null,
65 $bookmark = (new Bookmark())->fromArray($data);
66 $this->assertNull($bookmark->getId());
67 $this->assertNull($bookmark->getShortUrl());
68 $this->assertNull($bookmark->getUrl());
69 $this->assertNull($bookmark->getTitle());
70 $this->assertEquals('', $bookmark->getDescription());
71 $this->assertNull($bookmark->getCreated());
72 $this->assertEquals([], $bookmark->getTags());
73 $this->assertEquals('', $bookmark->getTagsString());
74 $this->assertNull($bookmark->getUpdated());
75 $this->assertFalse($bookmark->getThumbnail());
76 $this->assertFalse($bookmark->isSticky());
77 $this->assertFalse($bookmark->isPrivate());
78 $this->assertTrue($bookmark->isNote());
82 * Test fromArray() with a link with a custom tags separator
84 public function testFromArrayCustomTagsSeparator()
88 'tags' => ['tag1', 'tag2', 'chair'],
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('@'));
99 * Test validate() with a valid minimal bookmark
101 public function testValidateValidFullBookmark()
103 $bookmark = new Bookmark();
105 $bookmark->setShortUrl('abc');
106 $bookmark->setCreated($date = \DateTime
::createFromFormat('Ymd_His', '20190514_200102'));
107 $bookmark->setUpdated($dateUp = \DateTime
::createFromFormat('Ymd_His', '20190514_210203'));
108 $bookmark->setUrl($url = 'https://domain.tld/oof.html?param=value#anchor');
109 $bookmark->setTitle($title = 'This is an array link');
110 $bookmark->setDescription($desc = 'HTML desc<br><p>hi!</p>');
111 $bookmark->setTags($tags = ['tag1', 'tag2', 'chair']);
112 $bookmark->setThumbnail($thumb = 'https://domain.tld/pic.png');
113 $bookmark->setPrivate(true);
114 $bookmark->validate();
116 $this->assertEquals(2, $bookmark->getId());
117 $this->assertEquals('abc', $bookmark->getShortUrl());
118 $this->assertEquals($date, $bookmark->getCreated());
119 $this->assertEquals($dateUp, $bookmark->getUpdated());
120 $this->assertEquals($url, $bookmark->getUrl());
121 $this->assertEquals($title, $bookmark->getTitle());
122 $this->assertEquals($desc, $bookmark->getDescription());
123 $this->assertEquals($tags, $bookmark->getTags());
124 $this->assertEquals(implode(' ', $tags), $bookmark->getTagsString());
125 $this->assertEquals($thumb, $bookmark->getThumbnail());
126 $this->assertTrue($bookmark->isPrivate());
127 $this->assertFalse($bookmark->isNote());
131 * Test validate() with a valid minimal bookmark
133 public function testValidateValidMinimalBookmark()
135 $bookmark = new Bookmark();
137 $bookmark->setShortUrl('abc');
138 $bookmark->setCreated($date = \DateTime
::createFromFormat('Ymd_His', '20190514_200102'));
139 $bookmark->validate();
141 $this->assertEquals(1, $bookmark->getId());
142 $this->assertEquals('abc', $bookmark->getShortUrl());
143 $this->assertEquals($date, $bookmark->getCreated());
144 $this->assertEquals('/shaare/abc', $bookmark->getUrl());
145 $this->assertEquals('/shaare/abc', $bookmark->getTitle());
146 $this->assertEquals('', $bookmark->getDescription());
147 $this->assertEquals([], $bookmark->getTags());
148 $this->assertEquals('', $bookmark->getTagsString());
149 $this->assertFalse($bookmark->getThumbnail());
150 $this->assertFalse($bookmark->isPrivate());
151 $this->assertTrue($bookmark->isNote());
152 $this->assertNull($bookmark->getUpdated());
156 * Test validate() with a a bookmark without ID.
158 public function testValidateNotValidNoId()
160 $bookmark = new Bookmark();
161 $bookmark->setShortUrl('abc');
162 $bookmark->setCreated(\DateTime
::createFromFormat('Ymd_His', '20190514_200102'));
165 $bookmark->validate();
166 } catch (InvalidBookmarkException
$e) {
169 $this->assertNotNull($exception);
170 $this->assertContainsPolyfill('- ID: '. PHP_EOL
, $exception->getMessage());
174 * Test validate() with a a bookmark without short url.
176 public function testValidateNotValidNoShortUrl()
178 $bookmark = new Bookmark();
180 $bookmark->setCreated(\DateTime
::createFromFormat('Ymd_His', '20190514_200102'));
181 $bookmark->setShortUrl(null);
184 $bookmark->validate();
185 } catch (InvalidBookmarkException
$e) {
188 $this->assertNotNull($exception);
189 $this->assertContainsPolyfill('- ShortUrl: '. PHP_EOL
, $exception->getMessage());
193 * Test validate() with a a bookmark without created datetime.
195 public function testValidateNotValidNoCreated()
197 $bookmark = new Bookmark();
199 $bookmark->setShortUrl('abc');
200 $bookmark->setCreated(null);
203 $bookmark->validate();
204 } catch (InvalidBookmarkException
$e) {
207 $this->assertNotNull($exception);
208 $this->assertContainsPolyfill('- Created: '. PHP_EOL
, $exception->getMessage());
212 * Test setId() and make sure that default fields are generated.
214 public function testSetIdEmptyGeneratedFields()
216 $bookmark = new Bookmark();
219 $this->assertEquals(2, $bookmark->getId());
220 $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
221 $this->assertTrue(new \
DateTime('5 seconds ago') < $bookmark->getCreated());
225 * Test setId() and with generated fields already set.
227 public function testSetIdSetGeneratedFields()
229 $bookmark = new Bookmark();
230 $bookmark->setShortUrl('abc');
231 $bookmark->setCreated($date = \DateTime
::createFromFormat('Ymd_His', '20190514_200102'));
234 $this->assertEquals(2, $bookmark->getId());
235 $this->assertEquals('abc', $bookmark->getShortUrl());
236 $this->assertEquals($date, $bookmark->getCreated());
240 * Test setUrl() and make sure it accepts custom protocols
242 public function testGetUrlWithValidProtocols()
244 $bookmark = new Bookmark();
245 $bookmark->setUrl($url = 'myprotocol://helloworld', ['myprotocol']);
246 $this->assertEquals($url, $bookmark->getUrl());
248 $bookmark->setUrl($url = 'https://helloworld.tld', ['myprotocol']);
249 $this->assertEquals($url, $bookmark->getUrl());
253 * Test setUrl() and make sure it accepts custom protocols
255 public function testGetUrlWithNotValidProtocols()
257 $bookmark = new Bookmark();
258 $bookmark->setUrl('myprotocol://helloworld', []);
259 $this->assertEquals('http://helloworld', $bookmark->getUrl());
261 $bookmark->setUrl($url = 'https://helloworld.tld', []);
262 $this->assertEquals($url, $bookmark->getUrl());
266 * Test setTagsString() with exotic data
268 public function testSetTagsString()
270 $bookmark = new Bookmark();
272 $str = 'tag1 tag2 tag3.tag3-2 tag4 -tag5 ';
273 $bookmark->setTagsString($str);
287 * Test setTags() with exotic data
289 public function testSetTags()
291 $bookmark = new Bookmark();
301 $bookmark->setTags($array);
317 public function testRenameTag()
319 $bookmark = new Bookmark();
320 $bookmark->setTags(['tag1', 'tag2', 'chair']);
321 $bookmark->renameTag('chair', 'table');
322 $this->assertEquals(['tag1', 'tag2', 'table'], $bookmark->getTags());
323 $bookmark->renameTag('tag1', 'tag42');
324 $this->assertEquals(['tag42', 'tag2', 'table'], $bookmark->getTags());
325 $bookmark->renameTag('tag42', 'tag43');
326 $this->assertEquals(['tag43', 'tag2', 'table'], $bookmark->getTags());
327 $bookmark->renameTag('table', 'desk');
328 $this->assertEquals(['tag43', 'tag2', 'desk'], $bookmark->getTags());
332 * Test renameTag() with a tag that is not present in the bookmark
334 public function testRenameTagNotExists()
336 $bookmark = new Bookmark();
337 $bookmark->setTags(['tag1', 'tag2', 'chair']);
338 $bookmark->renameTag('nope', 'table');
339 $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags());
345 public function testDeleteTag()
347 $bookmark = new Bookmark();
348 $bookmark->setTags(['tag1', 'tag2', 'chair']);
349 $bookmark->deleteTag('chair');
350 $this->assertEquals(['tag1', 'tag2'], $bookmark->getTags());
351 $bookmark->deleteTag('tag1');
352 $this->assertEquals(['tag2'], $bookmark->getTags());
353 $bookmark->deleteTag('tag2');
354 $this->assertEquals([], $bookmark->getTags());
358 * Test deleteTag() with a tag that is not present in the bookmark
360 public function testDeleteTagNotExists()
362 $bookmark = new Bookmark();
363 $bookmark->setTags(['tag1', 'tag2', 'chair']);
364 $bookmark->deleteTag('nope');
365 $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags());
369 * Test shouldUpdateThumbnail() with bookmarks needing an update.
371 public function testShouldUpdateThumbnail(): void
373 $bookmark = (new Bookmark())->setUrl('http://domain.tld/with-image');
375 static::assertTrue($bookmark->shouldUpdateThumbnail());
377 $bookmark = (new Bookmark())
378 ->setUrl('http://domain.tld/with-image')
379 ->setThumbnail('unknown file')
382 static::assertTrue($bookmark->shouldUpdateThumbnail());
386 * Test shouldUpdateThumbnail() with bookmarks that should not update.
388 public function testShouldNotUpdateThumbnail(): void
390 $bookmark = (new Bookmark());
392 static::assertFalse($bookmark->shouldUpdateThumbnail());
394 $bookmark = (new Bookmark())
395 ->setUrl('ftp://domain.tld/other-protocol', ['ftp'])
398 static::assertFalse($bookmark->shouldUpdateThumbnail());
400 $bookmark = (new Bookmark())
401 ->setUrl('http://domain.tld/with-image')
402 ->setThumbnail(__FILE__
)
405 static::assertFalse($bookmark->shouldUpdateThumbnail());
407 $bookmark = (new Bookmark())->setUrl('/shaare/abcdef');
409 static::assertFalse($bookmark->shouldUpdateThumbnail());