]>
Commit | Line | Data |
---|---|---|
e26e2060 A |
1 | <?php |
2 | ||
3 | namespace Shaarli\Bookmark; | |
4 | ||
5 | use PHPUnit\Framework\TestCase; | |
6 | use Shaarli\Bookmark\Exception\InvalidBookmarkException; | |
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 | ||
81 | /** | |
82 | * Test validate() with a valid minimal bookmark | |
83 | */ | |
84 | public function testValidateValidFullBookmark() | |
85 | { | |
86 | $bookmark = new Bookmark(); | |
87 | $bookmark->setId(2); | |
88 | $bookmark->setShortUrl('abc'); | |
89 | $bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
90 | $bookmark->setUpdated($dateUp = \DateTime::createFromFormat('Ymd_His', '20190514_210203')); | |
91 | $bookmark->setUrl($url = 'https://domain.tld/oof.html?param=value#anchor'); | |
92 | $bookmark->setTitle($title = 'This is an array link'); | |
93 | $bookmark->setDescription($desc = 'HTML desc<br><p>hi!</p>'); | |
94 | $bookmark->setTags($tags = ['tag1', 'tag2', 'chair']); | |
95 | $bookmark->setThumbnail($thumb = 'https://domain.tld/pic.png'); | |
96 | $bookmark->setPrivate(true); | |
97 | $bookmark->validate(); | |
98 | ||
99 | $this->assertEquals(2, $bookmark->getId()); | |
100 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
101 | $this->assertEquals($date, $bookmark->getCreated()); | |
102 | $this->assertEquals($dateUp, $bookmark->getUpdated()); | |
103 | $this->assertEquals($url, $bookmark->getUrl()); | |
104 | $this->assertEquals($title, $bookmark->getTitle()); | |
105 | $this->assertEquals($desc, $bookmark->getDescription()); | |
106 | $this->assertEquals($tags, $bookmark->getTags()); | |
107 | $this->assertEquals(implode(' ', $tags), $bookmark->getTagsString()); | |
108 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | |
109 | $this->assertTrue($bookmark->isPrivate()); | |
110 | $this->assertFalse($bookmark->isNote()); | |
111 | } | |
112 | ||
113 | /** | |
114 | * Test validate() with a valid minimal bookmark | |
115 | */ | |
116 | public function testValidateValidMinimalBookmark() | |
117 | { | |
118 | $bookmark = new Bookmark(); | |
119 | $bookmark->setId(1); | |
120 | $bookmark->setShortUrl('abc'); | |
121 | $bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
122 | $bookmark->validate(); | |
123 | ||
124 | $this->assertEquals(1, $bookmark->getId()); | |
125 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
126 | $this->assertEquals($date, $bookmark->getCreated()); | |
301c7ab1 A |
127 | $this->assertEquals('/shaare/abc', $bookmark->getUrl()); |
128 | $this->assertEquals('/shaare/abc', $bookmark->getTitle()); | |
e26e2060 A |
129 | $this->assertEquals('', $bookmark->getDescription()); |
130 | $this->assertEquals([], $bookmark->getTags()); | |
131 | $this->assertEquals('', $bookmark->getTagsString()); | |
132 | $this->assertFalse($bookmark->getThumbnail()); | |
133 | $this->assertFalse($bookmark->isPrivate()); | |
134 | $this->assertTrue($bookmark->isNote()); | |
135 | $this->assertNull($bookmark->getUpdated()); | |
136 | } | |
137 | ||
138 | /** | |
139 | * Test validate() with a a bookmark without ID. | |
140 | */ | |
141 | public function testValidateNotValidNoId() | |
142 | { | |
143 | $bookmark = new Bookmark(); | |
144 | $bookmark->setShortUrl('abc'); | |
145 | $bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
146 | $exception = null; | |
147 | try { | |
148 | $bookmark->validate(); | |
149 | } catch (InvalidBookmarkException $e) { | |
150 | $exception = $e; | |
151 | } | |
152 | $this->assertNotNull($exception); | |
153 | $this->assertContains('- ID: '. PHP_EOL, $exception->getMessage()); | |
154 | } | |
155 | ||
156 | /** | |
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->assertContains('- ID: str'. PHP_EOL, $exception->getMessage()); | |
173 | } | |
174 | ||
175 | /** | |
176 | * Test validate() with a a bookmark without short url. | |
177 | */ | |
178 | public function testValidateNotValidNoShortUrl() | |
179 | { | |
180 | $bookmark = new Bookmark(); | |
181 | $bookmark->setId(1); | |
182 | $bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
183 | $bookmark->setShortUrl(null); | |
184 | $exception = null; | |
185 | try { | |
186 | $bookmark->validate(); | |
187 | } catch (InvalidBookmarkException $e) { | |
188 | $exception = $e; | |
189 | } | |
190 | $this->assertNotNull($exception); | |
191 | $this->assertContains('- ShortUrl: '. PHP_EOL, $exception->getMessage()); | |
192 | } | |
193 | ||
194 | /** | |
195 | * Test validate() with a a bookmark without created datetime. | |
196 | */ | |
197 | public function testValidateNotValidNoCreated() | |
198 | { | |
199 | $bookmark = new Bookmark(); | |
200 | $bookmark->setId(1); | |
201 | $bookmark->setShortUrl('abc'); | |
202 | $bookmark->setCreated(null); | |
203 | $exception = null; | |
204 | try { | |
205 | $bookmark->validate(); | |
206 | } catch (InvalidBookmarkException $e) { | |
207 | $exception = $e; | |
208 | } | |
209 | $this->assertNotNull($exception); | |
210 | $this->assertContains('- Created: '. PHP_EOL, $exception->getMessage()); | |
211 | } | |
212 | ||
213 | /** | |
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->assertContains('- Created: Not a DateTime object'. PHP_EOL, $exception->getMessage()); | |
230 | } | |
231 | ||
232 | /** | |
233 | * Test setId() and make sure that default fields are generated. | |
234 | */ | |
235 | public function testSetIdEmptyGeneratedFields() | |
236 | { | |
237 | $bookmark = new Bookmark(); | |
238 | $bookmark->setId(2); | |
239 | ||
240 | $this->assertEquals(2, $bookmark->getId()); | |
241 | $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); | |
242 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); | |
243 | } | |
244 | ||
245 | /** | |
246 | * Test setId() and with generated fields already set. | |
247 | */ | |
248 | public function testSetIdSetGeneratedFields() | |
249 | { | |
250 | $bookmark = new Bookmark(); | |
251 | $bookmark->setShortUrl('abc'); | |
252 | $bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
253 | $bookmark->setId(2); | |
254 | ||
255 | $this->assertEquals(2, $bookmark->getId()); | |
256 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
257 | $this->assertEquals($date, $bookmark->getCreated()); | |
258 | } | |
259 | ||
260 | /** | |
261 | * Test setUrl() and make sure it accepts custom protocols | |
262 | */ | |
263 | public function testGetUrlWithValidProtocols() | |
264 | { | |
265 | $bookmark = new Bookmark(); | |
266 | $bookmark->setUrl($url = 'myprotocol://helloworld', ['myprotocol']); | |
267 | $this->assertEquals($url, $bookmark->getUrl()); | |
268 | ||
269 | $bookmark->setUrl($url = 'https://helloworld.tld', ['myprotocol']); | |
270 | $this->assertEquals($url, $bookmark->getUrl()); | |
271 | } | |
272 | ||
273 | /** | |
274 | * Test setUrl() and make sure it accepts custom protocols | |
275 | */ | |
276 | public function testGetUrlWithNotValidProtocols() | |
277 | { | |
278 | $bookmark = new Bookmark(); | |
279 | $bookmark->setUrl('myprotocol://helloworld', []); | |
280 | $this->assertEquals('http://helloworld', $bookmark->getUrl()); | |
281 | ||
282 | $bookmark->setUrl($url = 'https://helloworld.tld', []); | |
283 | $this->assertEquals($url, $bookmark->getUrl()); | |
284 | } | |
285 | ||
286 | /** | |
287 | * Test setTagsString() with exotic data | |
288 | */ | |
289 | public function testSetTagsString() | |
290 | { | |
291 | $bookmark = new Bookmark(); | |
292 | ||
293 | $str = 'tag1 tag2 tag3.tag3-2, tag4 , -tag5 '; | |
294 | $bookmark->setTagsString($str); | |
295 | $this->assertEquals( | |
296 | [ | |
297 | 'tag1', | |
298 | 'tag2', | |
299 | 'tag3.tag3-2', | |
300 | 'tag4', | |
301 | 'tag5', | |
302 | ], | |
303 | $bookmark->getTags() | |
304 | ); | |
305 | } | |
306 | ||
307 | /** | |
308 | * Test setTags() with exotic data | |
309 | */ | |
310 | public function testSetTags() | |
311 | { | |
312 | $bookmark = new Bookmark(); | |
313 | ||
314 | $array = [ | |
315 | 'tag1 ', | |
316 | ' tag2', | |
317 | 'tag3.tag3-2,', | |
318 | ', tag4', | |
319 | ', ', | |
320 | '-tag5 ', | |
321 | ]; | |
322 | $bookmark->setTags($array); | |
323 | $this->assertEquals( | |
324 | [ | |
325 | 'tag1', | |
326 | 'tag2', | |
327 | 'tag3.tag3-2', | |
328 | 'tag4', | |
329 | 'tag5', | |
330 | ], | |
331 | $bookmark->getTags() | |
332 | ); | |
333 | } | |
334 | ||
335 | /** | |
336 | * Test renameTag() | |
337 | */ | |
338 | public function testRenameTag() | |
339 | { | |
340 | $bookmark = new Bookmark(); | |
341 | $bookmark->setTags(['tag1', 'tag2', 'chair']); | |
342 | $bookmark->renameTag('chair', 'table'); | |
343 | $this->assertEquals(['tag1', 'tag2', 'table'], $bookmark->getTags()); | |
344 | $bookmark->renameTag('tag1', 'tag42'); | |
345 | $this->assertEquals(['tag42', 'tag2', 'table'], $bookmark->getTags()); | |
346 | $bookmark->renameTag('tag42', 'tag43'); | |
347 | $this->assertEquals(['tag43', 'tag2', 'table'], $bookmark->getTags()); | |
348 | $bookmark->renameTag('table', 'desk'); | |
349 | $this->assertEquals(['tag43', 'tag2', 'desk'], $bookmark->getTags()); | |
350 | } | |
351 | ||
352 | /** | |
353 | * Test renameTag() with a tag that is not present in the bookmark | |
354 | */ | |
355 | public function testRenameTagNotExists() | |
356 | { | |
357 | $bookmark = new Bookmark(); | |
358 | $bookmark->setTags(['tag1', 'tag2', 'chair']); | |
359 | $bookmark->renameTag('nope', 'table'); | |
360 | $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags()); | |
361 | } | |
362 | ||
363 | /** | |
364 | * Test deleteTag() | |
365 | */ | |
366 | public function testDeleteTag() | |
367 | { | |
368 | $bookmark = new Bookmark(); | |
369 | $bookmark->setTags(['tag1', 'tag2', 'chair']); | |
370 | $bookmark->deleteTag('chair'); | |
371 | $this->assertEquals(['tag1', 'tag2'], $bookmark->getTags()); | |
372 | $bookmark->deleteTag('tag1'); | |
373 | $this->assertEquals(['tag2'], $bookmark->getTags()); | |
374 | $bookmark->deleteTag('tag2'); | |
375 | $this->assertEquals([], $bookmark->getTags()); | |
376 | } | |
377 | ||
378 | /** | |
379 | * Test deleteTag() with a tag that is not present in the bookmark | |
380 | */ | |
381 | public function testDeleteTagNotExists() | |
382 | { | |
383 | $bookmark = new Bookmark(); | |
384 | $bookmark->setTags(['tag1', 'tag2', 'chair']); | |
385 | $bookmark->deleteTag('nope'); | |
386 | $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags()); | |
387 | } | |
388 | } |