]>
Commit | Line | Data |
---|---|---|
e26e2060 A |
1 | <?php |
2 | ||
3 | namespace Shaarli\Bookmark; | |
4 | ||
e26e2060 | 5 | use Shaarli\Bookmark\Exception\InvalidBookmarkException; |
a5a9cf23 | 6 | use Shaarli\TestCase; |
e26e2060 A |
7 | |
8 | /** | |
9 | * Class BookmarkTest | |
10 | */ | |
11 | class BookmarkTest extends TestCase | |
12 | { | |
13 | /** | |
14 | * Test fromArray() with a link with full data | |
15 | */ | |
16 | public function testFromArrayFull() | |
17 | { | |
18 | $data = [ | |
19 | 'id' => 1, | |
20 | 'shorturl' => 'abc', | |
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', | |
25 | 'sticky' => true, | |
26 | 'created' => new \DateTime('-1 minute'), | |
27 | 'tags' => ['tag1', 'tag2', 'chair'], | |
28 | 'updated' => new \DateTime(), | |
29 | 'private' => true, | |
30 | ]; | |
31 | ||
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()); | |
46 | } | |
47 | ||
48 | /** | |
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. | |
51 | */ | |
52 | public function testFromArrayMinimal() | |
53 | { | |
54 | $data = [ | |
55 | 'id' => null, | |
56 | 'shorturl' => null, | |
57 | 'url' => null, | |
58 | 'title' => null, | |
59 | 'description' => null, | |
60 | 'created' => null, | |
61 | 'tags' => null, | |
62 | 'private' => null, | |
63 | ]; | |
64 | ||
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()); | |
79 | } | |
80 | ||
b3bd8c3e A |
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 | ||
e26e2060 A |
98 | /** |
99 | * Test validate() with a valid minimal bookmark | |
100 | */ | |
101 | public function testValidateValidFullBookmark() | |
102 | { | |
103 | $bookmark = new Bookmark(); | |
104 | $bookmark->setId(2); | |
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(); | |
115 | ||
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()); | |
128 | } | |
129 | ||
130 | /** | |
131 | * Test validate() with a valid minimal bookmark | |
132 | */ | |
133 | public function testValidateValidMinimalBookmark() | |
134 | { | |
135 | $bookmark = new Bookmark(); | |
136 | $bookmark->setId(1); | |
137 | $bookmark->setShortUrl('abc'); | |
138 | $bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
139 | $bookmark->validate(); | |
140 | ||
141 | $this->assertEquals(1, $bookmark->getId()); | |
142 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
143 | $this->assertEquals($date, $bookmark->getCreated()); | |
301c7ab1 A |
144 | $this->assertEquals('/shaare/abc', $bookmark->getUrl()); |
145 | $this->assertEquals('/shaare/abc', $bookmark->getTitle()); | |
e26e2060 A |
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()); | |
153 | } | |
154 | ||
155 | /** | |
156 | * Test validate() with a a bookmark without ID. | |
157 | */ | |
158 | public function testValidateNotValidNoId() | |
159 | { | |
160 | $bookmark = new Bookmark(); | |
161 | $bookmark->setShortUrl('abc'); | |
162 | $bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
163 | $exception = null; | |
164 | try { | |
165 | $bookmark->validate(); | |
166 | } catch (InvalidBookmarkException $e) { | |
167 | $exception = $e; | |
168 | } | |
169 | $this->assertNotNull($exception); | |
a5a9cf23 | 170 | $this->assertContainsPolyfill('- ID: '. PHP_EOL, $exception->getMessage()); |
e26e2060 A |
171 | } |
172 | ||
e26e2060 A |
173 | /** |
174 | * Test validate() with a a bookmark without short url. | |
175 | */ | |
176 | public function testValidateNotValidNoShortUrl() | |
177 | { | |
178 | $bookmark = new Bookmark(); | |
179 | $bookmark->setId(1); | |
180 | $bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
181 | $bookmark->setShortUrl(null); | |
182 | $exception = null; | |
183 | try { | |
184 | $bookmark->validate(); | |
185 | } catch (InvalidBookmarkException $e) { | |
186 | $exception = $e; | |
187 | } | |
188 | $this->assertNotNull($exception); | |
a5a9cf23 | 189 | $this->assertContainsPolyfill('- ShortUrl: '. PHP_EOL, $exception->getMessage()); |
e26e2060 A |
190 | } |
191 | ||
192 | /** | |
193 | * Test validate() with a a bookmark without created datetime. | |
194 | */ | |
195 | public function testValidateNotValidNoCreated() | |
196 | { | |
197 | $bookmark = new Bookmark(); | |
198 | $bookmark->setId(1); | |
199 | $bookmark->setShortUrl('abc'); | |
200 | $bookmark->setCreated(null); | |
201 | $exception = null; | |
202 | try { | |
203 | $bookmark->validate(); | |
204 | } catch (InvalidBookmarkException $e) { | |
205 | $exception = $e; | |
206 | } | |
207 | $this->assertNotNull($exception); | |
a5a9cf23 | 208 | $this->assertContainsPolyfill('- Created: '. PHP_EOL, $exception->getMessage()); |
e26e2060 A |
209 | } |
210 | ||
e26e2060 A |
211 | /** |
212 | * Test setId() and make sure that default fields are generated. | |
213 | */ | |
214 | public function testSetIdEmptyGeneratedFields() | |
215 | { | |
216 | $bookmark = new Bookmark(); | |
217 | $bookmark->setId(2); | |
218 | ||
219 | $this->assertEquals(2, $bookmark->getId()); | |
220 | $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); | |
221 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); | |
222 | } | |
223 | ||
224 | /** | |
225 | * Test setId() and with generated fields already set. | |
226 | */ | |
227 | public function testSetIdSetGeneratedFields() | |
228 | { | |
229 | $bookmark = new Bookmark(); | |
230 | $bookmark->setShortUrl('abc'); | |
231 | $bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
232 | $bookmark->setId(2); | |
233 | ||
234 | $this->assertEquals(2, $bookmark->getId()); | |
235 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
236 | $this->assertEquals($date, $bookmark->getCreated()); | |
237 | } | |
238 | ||
239 | /** | |
240 | * Test setUrl() and make sure it accepts custom protocols | |
241 | */ | |
242 | public function testGetUrlWithValidProtocols() | |
243 | { | |
244 | $bookmark = new Bookmark(); | |
245 | $bookmark->setUrl($url = 'myprotocol://helloworld', ['myprotocol']); | |
246 | $this->assertEquals($url, $bookmark->getUrl()); | |
247 | ||
248 | $bookmark->setUrl($url = 'https://helloworld.tld', ['myprotocol']); | |
249 | $this->assertEquals($url, $bookmark->getUrl()); | |
250 | } | |
251 | ||
252 | /** | |
253 | * Test setUrl() and make sure it accepts custom protocols | |
254 | */ | |
255 | public function testGetUrlWithNotValidProtocols() | |
256 | { | |
257 | $bookmark = new Bookmark(); | |
258 | $bookmark->setUrl('myprotocol://helloworld', []); | |
259 | $this->assertEquals('http://helloworld', $bookmark->getUrl()); | |
260 | ||
261 | $bookmark->setUrl($url = 'https://helloworld.tld', []); | |
262 | $this->assertEquals($url, $bookmark->getUrl()); | |
263 | } | |
264 | ||
265 | /** | |
266 | * Test setTagsString() with exotic data | |
267 | */ | |
268 | public function testSetTagsString() | |
269 | { | |
270 | $bookmark = new Bookmark(); | |
271 | ||
b3bd8c3e | 272 | $str = 'tag1 tag2 tag3.tag3-2 tag4 -tag5 '; |
e26e2060 A |
273 | $bookmark->setTagsString($str); |
274 | $this->assertEquals( | |
275 | [ | |
276 | 'tag1', | |
277 | 'tag2', | |
278 | 'tag3.tag3-2', | |
279 | 'tag4', | |
280 | 'tag5', | |
281 | ], | |
282 | $bookmark->getTags() | |
283 | ); | |
284 | } | |
285 | ||
286 | /** | |
287 | * Test setTags() with exotic data | |
288 | */ | |
289 | public function testSetTags() | |
290 | { | |
291 | $bookmark = new Bookmark(); | |
292 | ||
293 | $array = [ | |
294 | 'tag1 ', | |
295 | ' tag2', | |
b3bd8c3e A |
296 | 'tag3.tag3-2', |
297 | ' tag4', | |
298 | ' ', | |
e26e2060 A |
299 | '-tag5 ', |
300 | ]; | |
301 | $bookmark->setTags($array); | |
302 | $this->assertEquals( | |
303 | [ | |
304 | 'tag1', | |
305 | 'tag2', | |
306 | 'tag3.tag3-2', | |
307 | 'tag4', | |
308 | 'tag5', | |
309 | ], | |
310 | $bookmark->getTags() | |
311 | ); | |
312 | } | |
313 | ||
314 | /** | |
315 | * Test renameTag() | |
316 | */ | |
317 | public function testRenameTag() | |
318 | { | |
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()); | |
329 | } | |
330 | ||
331 | /** | |
332 | * Test renameTag() with a tag that is not present in the bookmark | |
333 | */ | |
334 | public function testRenameTagNotExists() | |
335 | { | |
336 | $bookmark = new Bookmark(); | |
337 | $bookmark->setTags(['tag1', 'tag2', 'chair']); | |
338 | $bookmark->renameTag('nope', 'table'); | |
339 | $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags()); | |
340 | } | |
341 | ||
342 | /** | |
343 | * Test deleteTag() | |
344 | */ | |
345 | public function testDeleteTag() | |
346 | { | |
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()); | |
355 | } | |
356 | ||
357 | /** | |
358 | * Test deleteTag() with a tag that is not present in the bookmark | |
359 | */ | |
360 | public function testDeleteTagNotExists() | |
361 | { | |
362 | $bookmark = new Bookmark(); | |
363 | $bookmark->setTags(['tag1', 'tag2', 'chair']); | |
364 | $bookmark->deleteTag('nope'); | |
365 | $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags()); | |
366 | } | |
21e72da9 A |
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 | } | |
e26e2060 | 411 | } |