]>
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 | ||
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); | |
a5a9cf23 | 153 | $this->assertContainsPolyfill('- ID: '. PHP_EOL, $exception->getMessage()); |
e26e2060 A |
154 | } |
155 | ||
e26e2060 A |
156 | /** |
157 | * Test validate() with a a bookmark without short url. | |
158 | */ | |
159 | public function testValidateNotValidNoShortUrl() | |
160 | { | |
161 | $bookmark = new Bookmark(); | |
162 | $bookmark->setId(1); | |
163 | $bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
164 | $bookmark->setShortUrl(null); | |
165 | $exception = null; | |
166 | try { | |
167 | $bookmark->validate(); | |
168 | } catch (InvalidBookmarkException $e) { | |
169 | $exception = $e; | |
170 | } | |
171 | $this->assertNotNull($exception); | |
a5a9cf23 | 172 | $this->assertContainsPolyfill('- ShortUrl: '. PHP_EOL, $exception->getMessage()); |
e26e2060 A |
173 | } |
174 | ||
175 | /** | |
176 | * Test validate() with a a bookmark without created datetime. | |
177 | */ | |
178 | public function testValidateNotValidNoCreated() | |
179 | { | |
180 | $bookmark = new Bookmark(); | |
181 | $bookmark->setId(1); | |
182 | $bookmark->setShortUrl('abc'); | |
183 | $bookmark->setCreated(null); | |
184 | $exception = null; | |
185 | try { | |
186 | $bookmark->validate(); | |
187 | } catch (InvalidBookmarkException $e) { | |
188 | $exception = $e; | |
189 | } | |
190 | $this->assertNotNull($exception); | |
a5a9cf23 | 191 | $this->assertContainsPolyfill('- Created: '. PHP_EOL, $exception->getMessage()); |
e26e2060 A |
192 | } |
193 | ||
e26e2060 A |
194 | /** |
195 | * Test setId() and make sure that default fields are generated. | |
196 | */ | |
197 | public function testSetIdEmptyGeneratedFields() | |
198 | { | |
199 | $bookmark = new Bookmark(); | |
200 | $bookmark->setId(2); | |
201 | ||
202 | $this->assertEquals(2, $bookmark->getId()); | |
203 | $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); | |
204 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); | |
205 | } | |
206 | ||
207 | /** | |
208 | * Test setId() and with generated fields already set. | |
209 | */ | |
210 | public function testSetIdSetGeneratedFields() | |
211 | { | |
212 | $bookmark = new Bookmark(); | |
213 | $bookmark->setShortUrl('abc'); | |
214 | $bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102')); | |
215 | $bookmark->setId(2); | |
216 | ||
217 | $this->assertEquals(2, $bookmark->getId()); | |
218 | $this->assertEquals('abc', $bookmark->getShortUrl()); | |
219 | $this->assertEquals($date, $bookmark->getCreated()); | |
220 | } | |
221 | ||
222 | /** | |
223 | * Test setUrl() and make sure it accepts custom protocols | |
224 | */ | |
225 | public function testGetUrlWithValidProtocols() | |
226 | { | |
227 | $bookmark = new Bookmark(); | |
228 | $bookmark->setUrl($url = 'myprotocol://helloworld', ['myprotocol']); | |
229 | $this->assertEquals($url, $bookmark->getUrl()); | |
230 | ||
231 | $bookmark->setUrl($url = 'https://helloworld.tld', ['myprotocol']); | |
232 | $this->assertEquals($url, $bookmark->getUrl()); | |
233 | } | |
234 | ||
235 | /** | |
236 | * Test setUrl() and make sure it accepts custom protocols | |
237 | */ | |
238 | public function testGetUrlWithNotValidProtocols() | |
239 | { | |
240 | $bookmark = new Bookmark(); | |
241 | $bookmark->setUrl('myprotocol://helloworld', []); | |
242 | $this->assertEquals('http://helloworld', $bookmark->getUrl()); | |
243 | ||
244 | $bookmark->setUrl($url = 'https://helloworld.tld', []); | |
245 | $this->assertEquals($url, $bookmark->getUrl()); | |
246 | } | |
247 | ||
248 | /** | |
249 | * Test setTagsString() with exotic data | |
250 | */ | |
251 | public function testSetTagsString() | |
252 | { | |
253 | $bookmark = new Bookmark(); | |
254 | ||
255 | $str = 'tag1 tag2 tag3.tag3-2, tag4 , -tag5 '; | |
256 | $bookmark->setTagsString($str); | |
257 | $this->assertEquals( | |
258 | [ | |
259 | 'tag1', | |
260 | 'tag2', | |
261 | 'tag3.tag3-2', | |
262 | 'tag4', | |
263 | 'tag5', | |
264 | ], | |
265 | $bookmark->getTags() | |
266 | ); | |
267 | } | |
268 | ||
269 | /** | |
270 | * Test setTags() with exotic data | |
271 | */ | |
272 | public function testSetTags() | |
273 | { | |
274 | $bookmark = new Bookmark(); | |
275 | ||
276 | $array = [ | |
277 | 'tag1 ', | |
278 | ' tag2', | |
279 | 'tag3.tag3-2,', | |
280 | ', tag4', | |
281 | ', ', | |
282 | '-tag5 ', | |
283 | ]; | |
284 | $bookmark->setTags($array); | |
285 | $this->assertEquals( | |
286 | [ | |
287 | 'tag1', | |
288 | 'tag2', | |
289 | 'tag3.tag3-2', | |
290 | 'tag4', | |
291 | 'tag5', | |
292 | ], | |
293 | $bookmark->getTags() | |
294 | ); | |
295 | } | |
296 | ||
297 | /** | |
298 | * Test renameTag() | |
299 | */ | |
300 | public function testRenameTag() | |
301 | { | |
302 | $bookmark = new Bookmark(); | |
303 | $bookmark->setTags(['tag1', 'tag2', 'chair']); | |
304 | $bookmark->renameTag('chair', 'table'); | |
305 | $this->assertEquals(['tag1', 'tag2', 'table'], $bookmark->getTags()); | |
306 | $bookmark->renameTag('tag1', 'tag42'); | |
307 | $this->assertEquals(['tag42', 'tag2', 'table'], $bookmark->getTags()); | |
308 | $bookmark->renameTag('tag42', 'tag43'); | |
309 | $this->assertEquals(['tag43', 'tag2', 'table'], $bookmark->getTags()); | |
310 | $bookmark->renameTag('table', 'desk'); | |
311 | $this->assertEquals(['tag43', 'tag2', 'desk'], $bookmark->getTags()); | |
312 | } | |
313 | ||
314 | /** | |
315 | * Test renameTag() with a tag that is not present in the bookmark | |
316 | */ | |
317 | public function testRenameTagNotExists() | |
318 | { | |
319 | $bookmark = new Bookmark(); | |
320 | $bookmark->setTags(['tag1', 'tag2', 'chair']); | |
321 | $bookmark->renameTag('nope', 'table'); | |
322 | $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags()); | |
323 | } | |
324 | ||
325 | /** | |
326 | * Test deleteTag() | |
327 | */ | |
328 | public function testDeleteTag() | |
329 | { | |
330 | $bookmark = new Bookmark(); | |
331 | $bookmark->setTags(['tag1', 'tag2', 'chair']); | |
332 | $bookmark->deleteTag('chair'); | |
333 | $this->assertEquals(['tag1', 'tag2'], $bookmark->getTags()); | |
334 | $bookmark->deleteTag('tag1'); | |
335 | $this->assertEquals(['tag2'], $bookmark->getTags()); | |
336 | $bookmark->deleteTag('tag2'); | |
337 | $this->assertEquals([], $bookmark->getTags()); | |
338 | } | |
339 | ||
340 | /** | |
341 | * Test deleteTag() with a tag that is not present in the bookmark | |
342 | */ | |
343 | public function testDeleteTagNotExists() | |
344 | { | |
345 | $bookmark = new Bookmark(); | |
346 | $bookmark->setTags(['tag1', 'tag2', 'chair']); | |
347 | $bookmark->deleteTag('nope'); | |
348 | $this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags()); | |
349 | } | |
350 | } |