1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
<?php
namespace Shaarli\Bookmark;
use Shaarli\Bookmark\Exception\InvalidBookmarkException;
use Shaarli\TestCase;
/**
* Class BookmarkTest
*/
class BookmarkTest extends TestCase
{
/**
* Test fromArray() with a link with full data
*/
public function testFromArrayFull()
{
$data = [
'id' => 1,
'shorturl' => 'abc',
'url' => 'https://domain.tld/oof.html?param=value#anchor',
'title' => 'This is an array link',
'description' => 'HTML desc<br><p>hi!</p>',
'thumbnail' => 'https://domain.tld/pic.png',
'sticky' => true,
'created' => new \DateTime('-1 minute'),
'tags' => ['tag1', 'tag2', 'chair'],
'updated' => new \DateTime(),
'private' => true,
];
$bookmark = (new Bookmark())->fromArray($data);
$this->assertEquals($data['id'], $bookmark->getId());
$this->assertEquals($data['shorturl'], $bookmark->getShortUrl());
$this->assertEquals($data['url'], $bookmark->getUrl());
$this->assertEquals($data['title'], $bookmark->getTitle());
$this->assertEquals($data['description'], $bookmark->getDescription());
$this->assertEquals($data['thumbnail'], $bookmark->getThumbnail());
$this->assertEquals($data['sticky'], $bookmark->isSticky());
$this->assertEquals($data['created'], $bookmark->getCreated());
$this->assertEquals($data['tags'], $bookmark->getTags());
$this->assertEquals('tag1 tag2 chair', $bookmark->getTagsString());
$this->assertEquals($data['updated'], $bookmark->getUpdated());
$this->assertEquals($data['private'], $bookmark->isPrivate());
$this->assertFalse($bookmark->isNote());
}
/**
* Test fromArray() with a link with minimal data.
* Note that I use null values everywhere but this should not happen in the real world.
*/
public function testFromArrayMinimal()
{
$data = [
'id' => null,
'shorturl' => null,
'url' => null,
'title' => null,
'description' => null,
'created' => null,
'tags' => null,
'private' => null,
];
$bookmark = (new Bookmark())->fromArray($data);
$this->assertNull($bookmark->getId());
$this->assertNull($bookmark->getShortUrl());
$this->assertNull($bookmark->getUrl());
$this->assertNull($bookmark->getTitle());
$this->assertEquals('', $bookmark->getDescription());
$this->assertNull($bookmark->getCreated());
$this->assertEquals([], $bookmark->getTags());
$this->assertEquals('', $bookmark->getTagsString());
$this->assertNull($bookmark->getUpdated());
$this->assertFalse($bookmark->getThumbnail());
$this->assertFalse($bookmark->isSticky());
$this->assertFalse($bookmark->isPrivate());
$this->assertTrue($bookmark->isNote());
}
/**
* Test validate() with a valid minimal bookmark
*/
public function testValidateValidFullBookmark()
{
$bookmark = new Bookmark();
$bookmark->setId(2);
$bookmark->setShortUrl('abc');
$bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102'));
$bookmark->setUpdated($dateUp = \DateTime::createFromFormat('Ymd_His', '20190514_210203'));
$bookmark->setUrl($url = 'https://domain.tld/oof.html?param=value#anchor');
$bookmark->setTitle($title = 'This is an array link');
$bookmark->setDescription($desc = 'HTML desc<br><p>hi!</p>');
$bookmark->setTags($tags = ['tag1', 'tag2', 'chair']);
$bookmark->setThumbnail($thumb = 'https://domain.tld/pic.png');
$bookmark->setPrivate(true);
$bookmark->validate();
$this->assertEquals(2, $bookmark->getId());
$this->assertEquals('abc', $bookmark->getShortUrl());
$this->assertEquals($date, $bookmark->getCreated());
$this->assertEquals($dateUp, $bookmark->getUpdated());
$this->assertEquals($url, $bookmark->getUrl());
$this->assertEquals($title, $bookmark->getTitle());
$this->assertEquals($desc, $bookmark->getDescription());
$this->assertEquals($tags, $bookmark->getTags());
$this->assertEquals(implode(' ', $tags), $bookmark->getTagsString());
$this->assertEquals($thumb, $bookmark->getThumbnail());
$this->assertTrue($bookmark->isPrivate());
$this->assertFalse($bookmark->isNote());
}
/**
* Test validate() with a valid minimal bookmark
*/
public function testValidateValidMinimalBookmark()
{
$bookmark = new Bookmark();
$bookmark->setId(1);
$bookmark->setShortUrl('abc');
$bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102'));
$bookmark->validate();
$this->assertEquals(1, $bookmark->getId());
$this->assertEquals('abc', $bookmark->getShortUrl());
$this->assertEquals($date, $bookmark->getCreated());
$this->assertEquals('/shaare/abc', $bookmark->getUrl());
$this->assertEquals('/shaare/abc', $bookmark->getTitle());
$this->assertEquals('', $bookmark->getDescription());
$this->assertEquals([], $bookmark->getTags());
$this->assertEquals('', $bookmark->getTagsString());
$this->assertFalse($bookmark->getThumbnail());
$this->assertFalse($bookmark->isPrivate());
$this->assertTrue($bookmark->isNote());
$this->assertNull($bookmark->getUpdated());
}
/**
* Test validate() with a a bookmark without ID.
*/
public function testValidateNotValidNoId()
{
$bookmark = new Bookmark();
$bookmark->setShortUrl('abc');
$bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102'));
$exception = null;
try {
$bookmark->validate();
} catch (InvalidBookmarkException $e) {
$exception = $e;
}
$this->assertNotNull($exception);
$this->assertContainsPolyfill('- ID: '. PHP_EOL, $exception->getMessage());
}
/**
* Test validate() with a a bookmark without short url.
*/
public function testValidateNotValidNoShortUrl()
{
$bookmark = new Bookmark();
$bookmark->setId(1);
$bookmark->setCreated(\DateTime::createFromFormat('Ymd_His', '20190514_200102'));
$bookmark->setShortUrl(null);
$exception = null;
try {
$bookmark->validate();
} catch (InvalidBookmarkException $e) {
$exception = $e;
}
$this->assertNotNull($exception);
$this->assertContainsPolyfill('- ShortUrl: '. PHP_EOL, $exception->getMessage());
}
/**
* Test validate() with a a bookmark without created datetime.
*/
public function testValidateNotValidNoCreated()
{
$bookmark = new Bookmark();
$bookmark->setId(1);
$bookmark->setShortUrl('abc');
$bookmark->setCreated(null);
$exception = null;
try {
$bookmark->validate();
} catch (InvalidBookmarkException $e) {
$exception = $e;
}
$this->assertNotNull($exception);
$this->assertContainsPolyfill('- Created: '. PHP_EOL, $exception->getMessage());
}
/**
* Test setId() and make sure that default fields are generated.
*/
public function testSetIdEmptyGeneratedFields()
{
$bookmark = new Bookmark();
$bookmark->setId(2);
$this->assertEquals(2, $bookmark->getId());
$this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl());
$this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated());
}
/**
* Test setId() and with generated fields already set.
*/
public function testSetIdSetGeneratedFields()
{
$bookmark = new Bookmark();
$bookmark->setShortUrl('abc');
$bookmark->setCreated($date = \DateTime::createFromFormat('Ymd_His', '20190514_200102'));
$bookmark->setId(2);
$this->assertEquals(2, $bookmark->getId());
$this->assertEquals('abc', $bookmark->getShortUrl());
$this->assertEquals($date, $bookmark->getCreated());
}
/**
* Test setUrl() and make sure it accepts custom protocols
*/
public function testGetUrlWithValidProtocols()
{
$bookmark = new Bookmark();
$bookmark->setUrl($url = 'myprotocol://helloworld', ['myprotocol']);
$this->assertEquals($url, $bookmark->getUrl());
$bookmark->setUrl($url = 'https://helloworld.tld', ['myprotocol']);
$this->assertEquals($url, $bookmark->getUrl());
}
/**
* Test setUrl() and make sure it accepts custom protocols
*/
public function testGetUrlWithNotValidProtocols()
{
$bookmark = new Bookmark();
$bookmark->setUrl('myprotocol://helloworld', []);
$this->assertEquals('http://helloworld', $bookmark->getUrl());
$bookmark->setUrl($url = 'https://helloworld.tld', []);
$this->assertEquals($url, $bookmark->getUrl());
}
/**
* Test setTagsString() with exotic data
*/
public function testSetTagsString()
{
$bookmark = new Bookmark();
$str = 'tag1 tag2 tag3.tag3-2, tag4 , -tag5 ';
$bookmark->setTagsString($str);
$this->assertEquals(
[
'tag1',
'tag2',
'tag3.tag3-2',
'tag4',
'tag5',
],
$bookmark->getTags()
);
}
/**
* Test setTags() with exotic data
*/
public function testSetTags()
{
$bookmark = new Bookmark();
$array = [
'tag1 ',
' tag2',
'tag3.tag3-2,',
', tag4',
', ',
'-tag5 ',
];
$bookmark->setTags($array);
$this->assertEquals(
[
'tag1',
'tag2',
'tag3.tag3-2',
'tag4',
'tag5',
],
$bookmark->getTags()
);
}
/**
* Test renameTag()
*/
public function testRenameTag()
{
$bookmark = new Bookmark();
$bookmark->setTags(['tag1', 'tag2', 'chair']);
$bookmark->renameTag('chair', 'table');
$this->assertEquals(['tag1', 'tag2', 'table'], $bookmark->getTags());
$bookmark->renameTag('tag1', 'tag42');
$this->assertEquals(['tag42', 'tag2', 'table'], $bookmark->getTags());
$bookmark->renameTag('tag42', 'tag43');
$this->assertEquals(['tag43', 'tag2', 'table'], $bookmark->getTags());
$bookmark->renameTag('table', 'desk');
$this->assertEquals(['tag43', 'tag2', 'desk'], $bookmark->getTags());
}
/**
* Test renameTag() with a tag that is not present in the bookmark
*/
public function testRenameTagNotExists()
{
$bookmark = new Bookmark();
$bookmark->setTags(['tag1', 'tag2', 'chair']);
$bookmark->renameTag('nope', 'table');
$this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags());
}
/**
* Test deleteTag()
*/
public function testDeleteTag()
{
$bookmark = new Bookmark();
$bookmark->setTags(['tag1', 'tag2', 'chair']);
$bookmark->deleteTag('chair');
$this->assertEquals(['tag1', 'tag2'], $bookmark->getTags());
$bookmark->deleteTag('tag1');
$this->assertEquals(['tag2'], $bookmark->getTags());
$bookmark->deleteTag('tag2');
$this->assertEquals([], $bookmark->getTags());
}
/**
* Test deleteTag() with a tag that is not present in the bookmark
*/
public function testDeleteTagNotExists()
{
$bookmark = new Bookmark();
$bookmark->setTags(['tag1', 'tag2', 'chair']);
$bookmark->deleteTag('nope');
$this->assertEquals(['tag1', 'tag2', 'chair'], $bookmark->getTags());
}
/**
* Test shouldUpdateThumbnail() with bookmarks needing an update.
*/
public function testShouldUpdateThumbnail(): void
{
$bookmark = (new Bookmark())->setUrl('http://domain.tld/with-image');
static::assertTrue($bookmark->shouldUpdateThumbnail());
$bookmark = (new Bookmark())
->setUrl('http://domain.tld/with-image')
->setThumbnail('unknown file')
;
static::assertTrue($bookmark->shouldUpdateThumbnail());
}
/**
* Test shouldUpdateThumbnail() with bookmarks that should not update.
*/
public function testShouldNotUpdateThumbnail(): void
{
$bookmark = (new Bookmark());
static::assertFalse($bookmark->shouldUpdateThumbnail());
$bookmark = (new Bookmark())
->setUrl('ftp://domain.tld/other-protocol', ['ftp'])
;
static::assertFalse($bookmark->shouldUpdateThumbnail());
$bookmark = (new Bookmark())
->setUrl('http://domain.tld/with-image')
->setThumbnail(__FILE__)
;
static::assertFalse($bookmark->shouldUpdateThumbnail());
$bookmark = (new Bookmark())->setUrl('/shaare/abcdef');
static::assertFalse($bookmark->shouldUpdateThumbnail());
}
}
|