diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-01-18 10:01:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-18 10:01:06 +0100 |
commit | 3fb29fdda04ca86e04422d49b86cf646d53c4f9d (patch) | |
tree | adf8512f93f5559ba87d0c9931969ae4ebea7133 /tests/bookmark | |
parent | 796c4c57d085ae4589b53dfe8369ae9ba30ffdaf (diff) | |
parent | e26e2060f5470ce8bf4c5973284bae07b8af170a (diff) | |
download | Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.gz Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.tar.zst Shaarli-3fb29fdda04ca86e04422d49b86cf646d53c4f9d.zip |
Store bookmarks as PHP objects and add a service layer to retriā¦ (#1307)
Store bookmarks as PHP objects and add a service layer to retrieve them
Diffstat (limited to 'tests/bookmark')
-rw-r--r-- | tests/bookmark/BookmarkArrayTest.php | 239 | ||||
-rw-r--r-- | tests/bookmark/BookmarkFileServiceTest.php | 1042 | ||||
-rw-r--r-- | tests/bookmark/BookmarkFilterTest.php (renamed from tests/bookmark/LinkFilterTest.php) | 137 | ||||
-rw-r--r-- | tests/bookmark/BookmarkInitializerTest.php | 120 | ||||
-rw-r--r-- | tests/bookmark/BookmarkTest.php | 388 | ||||
-rw-r--r-- | tests/bookmark/LinkDBTest.php | 656 | ||||
-rw-r--r-- | tests/bookmark/LinkUtilsTest.php | 9 |
7 files changed, 1861 insertions, 730 deletions
diff --git a/tests/bookmark/BookmarkArrayTest.php b/tests/bookmark/BookmarkArrayTest.php new file mode 100644 index 00000000..0f8f04c5 --- /dev/null +++ b/tests/bookmark/BookmarkArrayTest.php | |||
@@ -0,0 +1,239 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Bookmark; | ||
4 | |||
5 | use PHPUnit\Framework\TestCase; | ||
6 | use Shaarli\Bookmark\Exception\InvalidBookmarkException; | ||
7 | use Shaarli\Config\ConfigManager; | ||
8 | use Shaarli\History; | ||
9 | |||
10 | /** | ||
11 | * Class BookmarkArrayTest | ||
12 | */ | ||
13 | class BookmarkArrayTest extends TestCase | ||
14 | { | ||
15 | /** | ||
16 | * Test the constructor and make sure that the instance is properly initialized | ||
17 | */ | ||
18 | public function testArrayConstructorEmpty() | ||
19 | { | ||
20 | $array = new BookmarkArray(); | ||
21 | $this->assertTrue(is_iterable($array)); | ||
22 | $this->assertEmpty($array); | ||
23 | } | ||
24 | |||
25 | /** | ||
26 | * Test adding entries to the array, specifying the key offset or not. | ||
27 | */ | ||
28 | public function testArrayAccessAddEntries() | ||
29 | { | ||
30 | $array = new BookmarkArray(); | ||
31 | $bookmark = new Bookmark(); | ||
32 | $bookmark->setId(11)->validate(); | ||
33 | $array[] = $bookmark; | ||
34 | $this->assertCount(1, $array); | ||
35 | $this->assertTrue(isset($array[11])); | ||
36 | $this->assertNull($array[0]); | ||
37 | $this->assertEquals($bookmark, $array[11]); | ||
38 | |||
39 | $bookmark = new Bookmark(); | ||
40 | $bookmark->setId(14)->validate(); | ||
41 | $array[14] = $bookmark; | ||
42 | $this->assertCount(2, $array); | ||
43 | $this->assertTrue(isset($array[14])); | ||
44 | $this->assertNull($array[0]); | ||
45 | $this->assertEquals($bookmark, $array[14]); | ||
46 | } | ||
47 | |||
48 | /** | ||
49 | * Test adding a bad entry: wrong type | ||
50 | * | ||
51 | * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException | ||
52 | */ | ||
53 | public function testArrayAccessAddBadEntryInstance() | ||
54 | { | ||
55 | $array = new BookmarkArray(); | ||
56 | $array[] = 'nope'; | ||
57 | } | ||
58 | |||
59 | /** | ||
60 | * Test adding a bad entry: no id | ||
61 | * | ||
62 | * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException | ||
63 | */ | ||
64 | public function testArrayAccessAddBadEntryNoId() | ||
65 | { | ||
66 | $array = new BookmarkArray(); | ||
67 | $bookmark = new Bookmark(); | ||
68 | $array[] = $bookmark; | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * Test adding a bad entry: no url | ||
73 | * | ||
74 | * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException | ||
75 | */ | ||
76 | public function testArrayAccessAddBadEntryNoUrl() | ||
77 | { | ||
78 | $array = new BookmarkArray(); | ||
79 | $bookmark = (new Bookmark())->setId(11); | ||
80 | $array[] = $bookmark; | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * Test adding a bad entry: invalid offset | ||
85 | * | ||
86 | * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException | ||
87 | */ | ||
88 | public function testArrayAccessAddBadEntryOffset() | ||
89 | { | ||
90 | $array = new BookmarkArray(); | ||
91 | $bookmark = (new Bookmark())->setId(11); | ||
92 | $bookmark->validate(); | ||
93 | $array['nope'] = $bookmark; | ||
94 | } | ||
95 | |||
96 | /** | ||
97 | * Test adding a bad entry: invalid ID type | ||
98 | * | ||
99 | * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException | ||
100 | */ | ||
101 | public function testArrayAccessAddBadEntryIdType() | ||
102 | { | ||
103 | $array = new BookmarkArray(); | ||
104 | $bookmark = (new Bookmark())->setId('nope'); | ||
105 | $bookmark->validate(); | ||
106 | $array[] = $bookmark; | ||
107 | } | ||
108 | |||
109 | /** | ||
110 | * Test adding a bad entry: ID/offset not consistent | ||
111 | * | ||
112 | * @expectedException Shaarli\Bookmark\Exception\InvalidBookmarkException | ||
113 | */ | ||
114 | public function testArrayAccessAddBadEntryIdOffset() | ||
115 | { | ||
116 | $array = new BookmarkArray(); | ||
117 | $bookmark = (new Bookmark())->setId(11); | ||
118 | $bookmark->validate(); | ||
119 | $array[14] = $bookmark; | ||
120 | } | ||
121 | |||
122 | /** | ||
123 | * Test update entries through array access. | ||
124 | */ | ||
125 | public function testArrayAccessUpdateEntries() | ||
126 | { | ||
127 | $array = new BookmarkArray(); | ||
128 | $bookmark = new Bookmark(); | ||
129 | $bookmark->setId(11)->validate(); | ||
130 | $bookmark->setTitle('old'); | ||
131 | $array[] = $bookmark; | ||
132 | $bookmark = new Bookmark(); | ||
133 | $bookmark->setId(11)->validate(); | ||
134 | $bookmark->setTitle('test'); | ||
135 | $array[] = $bookmark; | ||
136 | $this->assertCount(1, $array); | ||
137 | $this->assertEquals('test', $array[11]->getTitle()); | ||
138 | |||
139 | $bookmark = new Bookmark(); | ||
140 | $bookmark->setId(11)->validate(); | ||
141 | $bookmark->setTitle('test2'); | ||
142 | $array[11] = $bookmark; | ||
143 | $this->assertCount(1, $array); | ||
144 | $this->assertEquals('test2', $array[11]->getTitle()); | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * Test delete entries through array access. | ||
149 | */ | ||
150 | public function testArrayAccessDeleteEntries() | ||
151 | { | ||
152 | $array = new BookmarkArray(); | ||
153 | $bookmark11 = new Bookmark(); | ||
154 | $bookmark11->setId(11)->validate(); | ||
155 | $array[] = $bookmark11; | ||
156 | $bookmark14 = new Bookmark(); | ||
157 | $bookmark14->setId(14)->validate(); | ||
158 | $array[] = $bookmark14; | ||
159 | $bookmark23 = new Bookmark(); | ||
160 | $bookmark23->setId(23)->validate(); | ||
161 | $array[] = $bookmark23; | ||
162 | $bookmark0 = new Bookmark(); | ||
163 | $bookmark0->setId(0)->validate(); | ||
164 | $array[] = $bookmark0; | ||
165 | $this->assertCount(4, $array); | ||
166 | |||
167 | unset($array[14]); | ||
168 | $this->assertCount(3, $array); | ||
169 | $this->assertEquals($bookmark11, $array[11]); | ||
170 | $this->assertEquals($bookmark23, $array[23]); | ||
171 | $this->assertEquals($bookmark0, $array[0]); | ||
172 | |||
173 | unset($array[23]); | ||
174 | $this->assertCount(2, $array); | ||
175 | $this->assertEquals($bookmark11, $array[11]); | ||
176 | $this->assertEquals($bookmark0, $array[0]); | ||
177 | |||
178 | unset($array[11]); | ||
179 | $this->assertCount(1, $array); | ||
180 | $this->assertEquals($bookmark0, $array[0]); | ||
181 | |||
182 | unset($array[0]); | ||
183 | $this->assertCount(0, $array); | ||
184 | } | ||
185 | |||
186 | /** | ||
187 | * Test iterating through array access. | ||
188 | */ | ||
189 | public function testArrayAccessIterate() | ||
190 | { | ||
191 | $array = new BookmarkArray(); | ||
192 | $bookmark11 = new Bookmark(); | ||
193 | $bookmark11->setId(11)->validate(); | ||
194 | $array[] = $bookmark11; | ||
195 | $bookmark14 = new Bookmark(); | ||
196 | $bookmark14->setId(14)->validate(); | ||
197 | $array[] = $bookmark14; | ||
198 | $bookmark23 = new Bookmark(); | ||
199 | $bookmark23->setId(23)->validate(); | ||
200 | $array[] = $bookmark23; | ||
201 | $this->assertCount(3, $array); | ||
202 | |||
203 | foreach ($array as $id => $bookmark) { | ||
204 | $this->assertEquals(${'bookmark'. $id}, $bookmark); | ||
205 | } | ||
206 | } | ||
207 | |||
208 | /** | ||
209 | * Test reordering the array. | ||
210 | */ | ||
211 | public function testReorder() | ||
212 | { | ||
213 | $refDB = new \ReferenceLinkDB(); | ||
214 | $refDB->write('sandbox/datastore.php'); | ||
215 | |||
216 | |||
217 | $bookmarks = $refDB->getLinks(); | ||
218 | $bookmarks->reorder('ASC'); | ||
219 | $this->assertInstanceOf(BookmarkArray::class, $bookmarks); | ||
220 | |||
221 | $stickyIds = [11, 10]; | ||
222 | $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41]; | ||
223 | $linkIds = array_merge($stickyIds, $standardIds); | ||
224 | $cpt = 0; | ||
225 | foreach ($bookmarks as $key => $value) { | ||
226 | $this->assertEquals($linkIds[$cpt++], $key); | ||
227 | } | ||
228 | |||
229 | $bookmarks = $refDB->getLinks(); | ||
230 | $bookmarks->reorder('DESC'); | ||
231 | $this->assertInstanceOf(BookmarkArray::class, $bookmarks); | ||
232 | |||
233 | $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds)); | ||
234 | $cpt = 0; | ||
235 | foreach ($bookmarks as $key => $value) { | ||
236 | $this->assertEquals($linkIds[$cpt++], $key); | ||
237 | } | ||
238 | } | ||
239 | } | ||
diff --git a/tests/bookmark/BookmarkFileServiceTest.php b/tests/bookmark/BookmarkFileServiceTest.php new file mode 100644 index 00000000..1b438a7f --- /dev/null +++ b/tests/bookmark/BookmarkFileServiceTest.php | |||
@@ -0,0 +1,1042 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Link datastore tests | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Bookmark; | ||
7 | |||
8 | use DateTime; | ||
9 | use PHPUnit\Framework\TestCase; | ||
10 | use ReferenceLinkDB; | ||
11 | use ReflectionClass; | ||
12 | use Shaarli; | ||
13 | use Shaarli\Bookmark\Exception\BookmarkNotFoundException; | ||
14 | use Shaarli\Config\ConfigManager; | ||
15 | use Shaarli\History; | ||
16 | |||
17 | /** | ||
18 | * Unitary tests for LegacyLinkDBTest | ||
19 | */ | ||
20 | class BookmarkFileServiceTest extends TestCase | ||
21 | { | ||
22 | // datastore to test write operations | ||
23 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
24 | |||
25 | protected static $testConf = 'sandbox/config'; | ||
26 | |||
27 | protected static $testUpdates = 'sandbox/updates.txt'; | ||
28 | |||
29 | /** | ||
30 | * @var ConfigManager instance. | ||
31 | */ | ||
32 | protected $conf; | ||
33 | |||
34 | /** | ||
35 | * @var History instance. | ||
36 | */ | ||
37 | protected $history; | ||
38 | |||
39 | /** | ||
40 | * @var ReferenceLinkDB instance. | ||
41 | */ | ||
42 | protected $refDB = null; | ||
43 | |||
44 | /** | ||
45 | * @var BookmarkFileService public LinkDB instance. | ||
46 | */ | ||
47 | protected $publicLinkDB = null; | ||
48 | |||
49 | /** | ||
50 | * @var BookmarkFileService private LinkDB instance. | ||
51 | */ | ||
52 | protected $privateLinkDB = null; | ||
53 | |||
54 | /** | ||
55 | * Instantiates public and private LinkDBs with test data | ||
56 | * | ||
57 | * The reference datastore contains public and private bookmarks that | ||
58 | * will be used to test LinkDB's methods: | ||
59 | * - access filtering (public/private), | ||
60 | * - link searches: | ||
61 | * - by day, | ||
62 | * - by tag, | ||
63 | * - by text, | ||
64 | * - etc. | ||
65 | * | ||
66 | * Resets test data for each test | ||
67 | */ | ||
68 | protected function setUp() | ||
69 | { | ||
70 | if (file_exists(self::$testDatastore)) { | ||
71 | unlink(self::$testDatastore); | ||
72 | } | ||
73 | |||
74 | if (file_exists(self::$testConf .'.json.php')) { | ||
75 | unlink(self::$testConf .'.json.php'); | ||
76 | } | ||
77 | |||
78 | if (file_exists(self::$testUpdates)) { | ||
79 | unlink(self::$testUpdates); | ||
80 | } | ||
81 | |||
82 | copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php'); | ||
83 | $this->conf = new ConfigManager(self::$testConf); | ||
84 | $this->conf->set('resource.datastore', self::$testDatastore); | ||
85 | $this->conf->set('resource.updates', self::$testUpdates); | ||
86 | $this->refDB = new \ReferenceLinkDB(); | ||
87 | $this->refDB->write(self::$testDatastore); | ||
88 | $this->history = new History('sandbox/history.php'); | ||
89 | $this->publicLinkDB = new BookmarkFileService($this->conf, $this->history, false); | ||
90 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
91 | } | ||
92 | |||
93 | /** | ||
94 | * Test migrate() method with a legacy datastore. | ||
95 | */ | ||
96 | public function testDatabaseMigration() | ||
97 | { | ||
98 | if (!defined('SHAARLI_VERSION')) { | ||
99 | define('SHAARLI_VERSION', 'dev'); | ||
100 | } | ||
101 | |||
102 | $this->refDB = new \ReferenceLinkDB(true); | ||
103 | $this->refDB->write(self::$testDatastore); | ||
104 | $db = self::getMethod('migrate'); | ||
105 | $db->invokeArgs($this->privateLinkDB, []); | ||
106 | |||
107 | $db = new \FakeBookmarkService($this->conf, $this->history, true); | ||
108 | $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); | ||
109 | $this->assertEquals($this->refDB->countLinks(), $db->count()); | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * Test get() method for a defined and saved bookmark | ||
114 | */ | ||
115 | public function testGetDefinedSaved() | ||
116 | { | ||
117 | $bookmark = $this->privateLinkDB->get(42); | ||
118 | $this->assertEquals(42, $bookmark->getId()); | ||
119 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | ||
120 | } | ||
121 | |||
122 | /** | ||
123 | * Test get() method for a defined and not saved bookmark | ||
124 | */ | ||
125 | public function testGetDefinedNotSaved() | ||
126 | { | ||
127 | $bookmark = new Bookmark(); | ||
128 | $this->privateLinkDB->add($bookmark); | ||
129 | $createdBookmark = $this->privateLinkDB->get(43); | ||
130 | $this->assertEquals(43, $createdBookmark->getId()); | ||
131 | $this->assertEmpty($createdBookmark->getDescription()); | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Test get() method for an undefined bookmark | ||
136 | * | ||
137 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | ||
138 | */ | ||
139 | public function testGetUndefined() | ||
140 | { | ||
141 | $this->privateLinkDB->get(666); | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * Test add() method for a bookmark fully built | ||
146 | */ | ||
147 | public function testAddFull() | ||
148 | { | ||
149 | $bookmark = new Bookmark(); | ||
150 | $bookmark->setUrl($url = 'https://domain.tld/index.php'); | ||
151 | $bookmark->setShortUrl('abc'); | ||
152 | $bookmark->setTitle($title = 'This a brand new bookmark'); | ||
153 | $bookmark->setDescription($desc = 'It should be created and written'); | ||
154 | $bookmark->setTags($tags = ['tag1', 'tagssss']); | ||
155 | $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png'); | ||
156 | $bookmark->setPrivate(true); | ||
157 | $bookmark->setSticky(true); | ||
158 | $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354')); | ||
159 | $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354')); | ||
160 | |||
161 | $this->privateLinkDB->add($bookmark); | ||
162 | $bookmark = $this->privateLinkDB->get(43); | ||
163 | $this->assertEquals(43, $bookmark->getId()); | ||
164 | $this->assertEquals($url, $bookmark->getUrl()); | ||
165 | $this->assertEquals('abc', $bookmark->getShortUrl()); | ||
166 | $this->assertEquals($title, $bookmark->getTitle()); | ||
167 | $this->assertEquals($desc, $bookmark->getDescription()); | ||
168 | $this->assertEquals($tags, $bookmark->getTags()); | ||
169 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | ||
170 | $this->assertTrue($bookmark->isPrivate()); | ||
171 | $this->assertTrue($bookmark->isSticky()); | ||
172 | $this->assertEquals($created, $bookmark->getCreated()); | ||
173 | $this->assertEquals($updated, $bookmark->getUpdated()); | ||
174 | |||
175 | // reload from file | ||
176 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
177 | |||
178 | $bookmark = $this->privateLinkDB->get(43); | ||
179 | $this->assertEquals(43, $bookmark->getId()); | ||
180 | $this->assertEquals($url, $bookmark->getUrl()); | ||
181 | $this->assertEquals('abc', $bookmark->getShortUrl()); | ||
182 | $this->assertEquals($title, $bookmark->getTitle()); | ||
183 | $this->assertEquals($desc, $bookmark->getDescription()); | ||
184 | $this->assertEquals($tags, $bookmark->getTags()); | ||
185 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | ||
186 | $this->assertTrue($bookmark->isPrivate()); | ||
187 | $this->assertTrue($bookmark->isSticky()); | ||
188 | $this->assertEquals($created, $bookmark->getCreated()); | ||
189 | $this->assertEquals($updated, $bookmark->getUpdated()); | ||
190 | } | ||
191 | |||
192 | /** | ||
193 | * Test add() method for a bookmark without any field set | ||
194 | */ | ||
195 | public function testAddMinimal() | ||
196 | { | ||
197 | $bookmark = new Bookmark(); | ||
198 | $this->privateLinkDB->add($bookmark); | ||
199 | |||
200 | $bookmark = $this->privateLinkDB->get(43); | ||
201 | $this->assertEquals(43, $bookmark->getId()); | ||
202 | $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); | ||
203 | $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); | ||
204 | $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); | ||
205 | $this->assertEmpty($bookmark->getDescription()); | ||
206 | $this->assertEmpty($bookmark->getTags()); | ||
207 | $this->assertEmpty($bookmark->getThumbnail()); | ||
208 | $this->assertFalse($bookmark->isPrivate()); | ||
209 | $this->assertFalse($bookmark->isSticky()); | ||
210 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); | ||
211 | $this->assertNull($bookmark->getUpdated()); | ||
212 | |||
213 | // reload from file | ||
214 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
215 | |||
216 | $bookmark = $this->privateLinkDB->get(43); | ||
217 | $this->assertEquals(43, $bookmark->getId()); | ||
218 | $this->assertRegExp('/\?[\w\-]{6}/', $bookmark->getUrl()); | ||
219 | $this->assertRegExp('/[\w\-]{6}/', $bookmark->getShortUrl()); | ||
220 | $this->assertEquals($bookmark->getUrl(), $bookmark->getTitle()); | ||
221 | $this->assertEmpty($bookmark->getDescription()); | ||
222 | $this->assertEmpty($bookmark->getTags()); | ||
223 | $this->assertEmpty($bookmark->getThumbnail()); | ||
224 | $this->assertFalse($bookmark->isPrivate()); | ||
225 | $this->assertFalse($bookmark->isSticky()); | ||
226 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getCreated()); | ||
227 | $this->assertNull($bookmark->getUpdated()); | ||
228 | } | ||
229 | |||
230 | /** | ||
231 | * Test add() method for a bookmark without any field set and without writing the data store | ||
232 | * | ||
233 | * @expectedExceptionMessage Shaarli\Bookmark\Exception\BookmarkNotFoundException | ||
234 | */ | ||
235 | public function testAddMinimalNoWrite() | ||
236 | { | ||
237 | $bookmark = new Bookmark(); | ||
238 | $this->privateLinkDB->add($bookmark); | ||
239 | |||
240 | $bookmark = $this->privateLinkDB->get(43); | ||
241 | $this->assertEquals(43, $bookmark->getId()); | ||
242 | |||
243 | // reload from file | ||
244 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
245 | |||
246 | $this->privateLinkDB->get(43); | ||
247 | } | ||
248 | |||
249 | /** | ||
250 | * Test add() method while logged out | ||
251 | * | ||
252 | * @expectedException \Exception | ||
253 | * @expectedExceptionMessage You're not authorized to alter the datastore | ||
254 | */ | ||
255 | public function testAddLoggedOut() | ||
256 | { | ||
257 | $this->publicLinkDB->add(new Bookmark()); | ||
258 | } | ||
259 | |||
260 | /** | ||
261 | * Test add() method with an entry which is not a bookmark instance | ||
262 | * | ||
263 | * @expectedException \Exception | ||
264 | * @expectedExceptionMessage Provided data is invalid | ||
265 | */ | ||
266 | public function testAddNotABookmark() | ||
267 | { | ||
268 | $this->privateLinkDB->add(['title' => 'hi!']); | ||
269 | } | ||
270 | |||
271 | /** | ||
272 | * Test add() method with a Bookmark already containing an ID | ||
273 | * | ||
274 | * @expectedException \Exception | ||
275 | * @expectedExceptionMessage This bookmarks already exists | ||
276 | */ | ||
277 | public function testAddWithId() | ||
278 | { | ||
279 | $bookmark = new Bookmark(); | ||
280 | $bookmark->setId(43); | ||
281 | $this->privateLinkDB->add($bookmark); | ||
282 | } | ||
283 | |||
284 | /** | ||
285 | * Test set() method for a bookmark fully built | ||
286 | */ | ||
287 | public function testSetFull() | ||
288 | { | ||
289 | $bookmark = $this->privateLinkDB->get(42); | ||
290 | $bookmark->setUrl($url = 'https://domain.tld/index.php'); | ||
291 | $bookmark->setShortUrl('abc'); | ||
292 | $bookmark->setTitle($title = 'This a brand new bookmark'); | ||
293 | $bookmark->setDescription($desc = 'It should be created and written'); | ||
294 | $bookmark->setTags($tags = ['tag1', 'tagssss']); | ||
295 | $bookmark->setThumbnail($thumb = 'http://thumb.tld/dle.png'); | ||
296 | $bookmark->setPrivate(true); | ||
297 | $bookmark->setSticky(true); | ||
298 | $bookmark->setCreated($created = DateTime::createFromFormat('Ymd_His', '20190518_140354')); | ||
299 | $bookmark->setUpdated($updated = DateTime::createFromFormat('Ymd_His', '20190518_150354')); | ||
300 | |||
301 | $this->privateLinkDB->set($bookmark); | ||
302 | $bookmark = $this->privateLinkDB->get(42); | ||
303 | $this->assertEquals(42, $bookmark->getId()); | ||
304 | $this->assertEquals($url, $bookmark->getUrl()); | ||
305 | $this->assertEquals('abc', $bookmark->getShortUrl()); | ||
306 | $this->assertEquals($title, $bookmark->getTitle()); | ||
307 | $this->assertEquals($desc, $bookmark->getDescription()); | ||
308 | $this->assertEquals($tags, $bookmark->getTags()); | ||
309 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | ||
310 | $this->assertTrue($bookmark->isPrivate()); | ||
311 | $this->assertTrue($bookmark->isSticky()); | ||
312 | $this->assertEquals($created, $bookmark->getCreated()); | ||
313 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | ||
314 | |||
315 | // reload from file | ||
316 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
317 | |||
318 | $bookmark = $this->privateLinkDB->get(42); | ||
319 | $this->assertEquals(42, $bookmark->getId()); | ||
320 | $this->assertEquals($url, $bookmark->getUrl()); | ||
321 | $this->assertEquals('abc', $bookmark->getShortUrl()); | ||
322 | $this->assertEquals($title, $bookmark->getTitle()); | ||
323 | $this->assertEquals($desc, $bookmark->getDescription()); | ||
324 | $this->assertEquals($tags, $bookmark->getTags()); | ||
325 | $this->assertEquals($thumb, $bookmark->getThumbnail()); | ||
326 | $this->assertTrue($bookmark->isPrivate()); | ||
327 | $this->assertTrue($bookmark->isSticky()); | ||
328 | $this->assertEquals($created, $bookmark->getCreated()); | ||
329 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | ||
330 | } | ||
331 | |||
332 | /** | ||
333 | * Test set() method for a bookmark without any field set | ||
334 | */ | ||
335 | public function testSetMinimal() | ||
336 | { | ||
337 | $bookmark = $this->privateLinkDB->get(42); | ||
338 | $this->privateLinkDB->set($bookmark); | ||
339 | |||
340 | $bookmark = $this->privateLinkDB->get(42); | ||
341 | $this->assertEquals(42, $bookmark->getId()); | ||
342 | $this->assertEquals('?WDWyig', $bookmark->getUrl()); | ||
343 | $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); | ||
344 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | ||
345 | $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); | ||
346 | $this->assertEquals(['ut'], $bookmark->getTags()); | ||
347 | $this->assertFalse($bookmark->getThumbnail()); | ||
348 | $this->assertFalse($bookmark->isPrivate()); | ||
349 | $this->assertFalse($bookmark->isSticky()); | ||
350 | $this->assertEquals( | ||
351 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'), | ||
352 | $bookmark->getCreated() | ||
353 | ); | ||
354 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | ||
355 | |||
356 | // reload from file | ||
357 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
358 | |||
359 | $bookmark = $this->privateLinkDB->get(42); | ||
360 | $this->assertEquals(42, $bookmark->getId()); | ||
361 | $this->assertEquals('?WDWyig', $bookmark->getUrl()); | ||
362 | $this->assertEquals('1eYJ1Q', $bookmark->getShortUrl()); | ||
363 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | ||
364 | $this->assertEquals('Used to test bookmarks reordering.', $bookmark->getDescription()); | ||
365 | $this->assertEquals(['ut'], $bookmark->getTags()); | ||
366 | $this->assertFalse($bookmark->getThumbnail()); | ||
367 | $this->assertFalse($bookmark->isPrivate()); | ||
368 | $this->assertFalse($bookmark->isSticky()); | ||
369 | $this->assertEquals( | ||
370 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20100310_101010'), | ||
371 | $bookmark->getCreated() | ||
372 | ); | ||
373 | $this->assertTrue(new \DateTime('5 seconds ago') < $bookmark->getUpdated()); | ||
374 | } | ||
375 | |||
376 | /** | ||
377 | * Test set() method for a bookmark without any field set and without writing the data store | ||
378 | */ | ||
379 | public function testSetMinimalNoWrite() | ||
380 | { | ||
381 | $bookmark = $this->privateLinkDB->get(42); | ||
382 | $bookmark->setTitle($title = 'hi!'); | ||
383 | $this->privateLinkDB->set($bookmark, false); | ||
384 | |||
385 | $bookmark = $this->privateLinkDB->get(42); | ||
386 | $this->assertEquals(42, $bookmark->getId()); | ||
387 | $this->assertEquals($title, $bookmark->getTitle()); | ||
388 | |||
389 | // reload from file | ||
390 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
391 | |||
392 | $bookmark = $this->privateLinkDB->get(42); | ||
393 | $this->assertEquals(42, $bookmark->getId()); | ||
394 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | ||
395 | } | ||
396 | |||
397 | /** | ||
398 | * Test set() method while logged out | ||
399 | * | ||
400 | * @expectedException \Exception | ||
401 | * @expectedExceptionMessage You're not authorized to alter the datastore | ||
402 | */ | ||
403 | public function testSetLoggedOut() | ||
404 | { | ||
405 | $this->publicLinkDB->set(new Bookmark()); | ||
406 | } | ||
407 | |||
408 | /** | ||
409 | * Test set() method with an entry which is not a bookmark instance | ||
410 | * | ||
411 | * @expectedException \Exception | ||
412 | * @expectedExceptionMessage Provided data is invalid | ||
413 | */ | ||
414 | public function testSetNotABookmark() | ||
415 | { | ||
416 | $this->privateLinkDB->set(['title' => 'hi!']); | ||
417 | } | ||
418 | |||
419 | /** | ||
420 | * Test set() method with a Bookmark without an ID defined. | ||
421 | * | ||
422 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | ||
423 | */ | ||
424 | public function testSetWithoutId() | ||
425 | { | ||
426 | $bookmark = new Bookmark(); | ||
427 | $this->privateLinkDB->set($bookmark); | ||
428 | } | ||
429 | |||
430 | /** | ||
431 | * Test set() method with a Bookmark with an unknow ID | ||
432 | * | ||
433 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | ||
434 | */ | ||
435 | public function testSetWithUnknownId() | ||
436 | { | ||
437 | $bookmark = new Bookmark(); | ||
438 | $bookmark->setId(666); | ||
439 | $this->privateLinkDB->set($bookmark); | ||
440 | } | ||
441 | |||
442 | /** | ||
443 | * Test addOrSet() method with a new ID | ||
444 | */ | ||
445 | public function testAddOrSetNew() | ||
446 | { | ||
447 | $bookmark = new Bookmark(); | ||
448 | $this->privateLinkDB->addOrSet($bookmark); | ||
449 | |||
450 | $bookmark = $this->privateLinkDB->get(43); | ||
451 | $this->assertEquals(43, $bookmark->getId()); | ||
452 | |||
453 | // reload from file | ||
454 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
455 | |||
456 | $bookmark = $this->privateLinkDB->get(43); | ||
457 | $this->assertEquals(43, $bookmark->getId()); | ||
458 | } | ||
459 | |||
460 | /** | ||
461 | * Test addOrSet() method with an existing ID | ||
462 | */ | ||
463 | public function testAddOrSetExisting() | ||
464 | { | ||
465 | $bookmark = $this->privateLinkDB->get(42); | ||
466 | $bookmark->setTitle($title = 'hi!'); | ||
467 | $this->privateLinkDB->addOrSet($bookmark); | ||
468 | |||
469 | $bookmark = $this->privateLinkDB->get(42); | ||
470 | $this->assertEquals(42, $bookmark->getId()); | ||
471 | $this->assertEquals($title, $bookmark->getTitle()); | ||
472 | |||
473 | // reload from file | ||
474 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
475 | |||
476 | $bookmark = $this->privateLinkDB->get(42); | ||
477 | $this->assertEquals(42, $bookmark->getId()); | ||
478 | $this->assertEquals($title, $bookmark->getTitle()); | ||
479 | } | ||
480 | |||
481 | /** | ||
482 | * Test addOrSet() method while logged out | ||
483 | * | ||
484 | * @expectedException \Exception | ||
485 | * @expectedExceptionMessage You're not authorized to alter the datastore | ||
486 | */ | ||
487 | public function testAddOrSetLoggedOut() | ||
488 | { | ||
489 | $this->publicLinkDB->addOrSet(new Bookmark()); | ||
490 | } | ||
491 | |||
492 | /** | ||
493 | * Test addOrSet() method with an entry which is not a bookmark instance | ||
494 | * | ||
495 | * @expectedException \Exception | ||
496 | * @expectedExceptionMessage Provided data is invalid | ||
497 | */ | ||
498 | public function testAddOrSetNotABookmark() | ||
499 | { | ||
500 | $this->privateLinkDB->addOrSet(['title' => 'hi!']); | ||
501 | } | ||
502 | |||
503 | /** | ||
504 | * Test addOrSet() method for a bookmark without any field set and without writing the data store | ||
505 | */ | ||
506 | public function testAddOrSetMinimalNoWrite() | ||
507 | { | ||
508 | $bookmark = $this->privateLinkDB->get(42); | ||
509 | $bookmark->setTitle($title = 'hi!'); | ||
510 | $this->privateLinkDB->addOrSet($bookmark, false); | ||
511 | |||
512 | $bookmark = $this->privateLinkDB->get(42); | ||
513 | $this->assertEquals(42, $bookmark->getId()); | ||
514 | $this->assertEquals($title, $bookmark->getTitle()); | ||
515 | |||
516 | // reload from file | ||
517 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
518 | |||
519 | $bookmark = $this->privateLinkDB->get(42); | ||
520 | $this->assertEquals(42, $bookmark->getId()); | ||
521 | $this->assertEquals('Note: I have a big ID but an old date', $bookmark->getTitle()); | ||
522 | } | ||
523 | |||
524 | /** | ||
525 | * Test remove() method with an existing Bookmark | ||
526 | * | ||
527 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | ||
528 | */ | ||
529 | public function testRemoveExisting() | ||
530 | { | ||
531 | $bookmark = $this->privateLinkDB->get(42); | ||
532 | $this->privateLinkDB->remove($bookmark); | ||
533 | |||
534 | $exception = null; | ||
535 | try { | ||
536 | $this->privateLinkDB->get(42); | ||
537 | } catch (BookmarkNotFoundException $e) { | ||
538 | $exception = $e; | ||
539 | } | ||
540 | $this->assertInstanceOf(BookmarkNotFoundException::class, $exception); | ||
541 | |||
542 | // reload from file | ||
543 | $this->privateLinkDB = new BookmarkFileService($this->conf, $this->history, true); | ||
544 | |||
545 | $this->privateLinkDB->get(42); | ||
546 | } | ||
547 | |||
548 | /** | ||
549 | * Test remove() method while logged out | ||
550 | * | ||
551 | * @expectedException \Exception | ||
552 | * @expectedExceptionMessage You're not authorized to alter the datastore | ||
553 | */ | ||
554 | public function testRemoveLoggedOut() | ||
555 | { | ||
556 | $bookmark = $this->privateLinkDB->get(42); | ||
557 | $this->publicLinkDB->remove($bookmark); | ||
558 | } | ||
559 | |||
560 | /** | ||
561 | * Test remove() method with an entry which is not a bookmark instance | ||
562 | * | ||
563 | * @expectedException \Exception | ||
564 | * @expectedExceptionMessage Provided data is invalid | ||
565 | */ | ||
566 | public function testRemoveNotABookmark() | ||
567 | { | ||
568 | $this->privateLinkDB->remove(['title' => 'hi!']); | ||
569 | } | ||
570 | |||
571 | /** | ||
572 | * Test remove() method with a Bookmark with an unknown ID | ||
573 | * | ||
574 | * @expectedException Shaarli\Bookmark\Exception\BookmarkNotFoundException | ||
575 | */ | ||
576 | public function testRemoveWithUnknownId() | ||
577 | { | ||
578 | $bookmark = new Bookmark(); | ||
579 | $bookmark->setId(666); | ||
580 | $this->privateLinkDB->remove($bookmark); | ||
581 | } | ||
582 | |||
583 | /** | ||
584 | * Test exists() method | ||
585 | */ | ||
586 | public function testExists() | ||
587 | { | ||
588 | $this->assertTrue($this->privateLinkDB->exists(42)); // public | ||
589 | $this->assertTrue($this->privateLinkDB->exists(6)); // private | ||
590 | |||
591 | $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$ALL)); | ||
592 | $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$ALL)); | ||
593 | |||
594 | $this->assertTrue($this->privateLinkDB->exists(42, BookmarkFilter::$PUBLIC)); | ||
595 | $this->assertFalse($this->privateLinkDB->exists(6, BookmarkFilter::$PUBLIC)); | ||
596 | |||
597 | $this->assertFalse($this->privateLinkDB->exists(42, BookmarkFilter::$PRIVATE)); | ||
598 | $this->assertTrue($this->privateLinkDB->exists(6, BookmarkFilter::$PRIVATE)); | ||
599 | |||
600 | $this->assertTrue($this->publicLinkDB->exists(42)); | ||
601 | $this->assertFalse($this->publicLinkDB->exists(6)); | ||
602 | |||
603 | $this->assertTrue($this->publicLinkDB->exists(42, BookmarkFilter::$PUBLIC)); | ||
604 | $this->assertFalse($this->publicLinkDB->exists(6, BookmarkFilter::$PUBLIC)); | ||
605 | |||
606 | $this->assertFalse($this->publicLinkDB->exists(42, BookmarkFilter::$PRIVATE)); | ||
607 | $this->assertTrue($this->publicLinkDB->exists(6, BookmarkFilter::$PRIVATE)); | ||
608 | } | ||
609 | |||
610 | /** | ||
611 | * Test initialize() method | ||
612 | */ | ||
613 | public function testInitialize() | ||
614 | { | ||
615 | $dbSize = $this->privateLinkDB->count(); | ||
616 | $this->privateLinkDB->initialize(); | ||
617 | $this->assertEquals($dbSize + 2, $this->privateLinkDB->count()); | ||
618 | $this->assertEquals( | ||
619 | 'My secret stuff... - Pastebin.com', | ||
620 | $this->privateLinkDB->get(43)->getTitle() | ||
621 | ); | ||
622 | $this->assertEquals( | ||
623 | 'The personal, minimalist, super-fast, database free, bookmarking service', | ||
624 | $this->privateLinkDB->get(44)->getTitle() | ||
625 | ); | ||
626 | } | ||
627 | |||
628 | /* | ||
629 | * The following tests have been taken from the legacy LinkDB test and adapted | ||
630 | * to make sure that nothing have been broken in the migration process. | ||
631 | * They mostly cover search/filters. Some of them might be redundant with the previous ones. | ||
632 | */ | ||
633 | |||
634 | /** | ||
635 | * Attempt to instantiate a LinkDB whereas the datastore is not writable | ||
636 | * | ||
637 | * @expectedException Shaarli\Bookmark\Exception\NotWritableDataStoreException | ||
638 | * @expectedExceptionMessageRegExp #Couldn't load data from the data store file "null".*# | ||
639 | */ | ||
640 | public function testConstructDatastoreNotWriteable() | ||
641 | { | ||
642 | $conf = new ConfigManager('tests/utils/config/configJson'); | ||
643 | $conf->set('resource.datastore', 'null/store.db'); | ||
644 | new BookmarkFileService($conf, $this->history, true); | ||
645 | } | ||
646 | |||
647 | /** | ||
648 | * The DB doesn't exist, ensure it is created with an empty datastore | ||
649 | */ | ||
650 | public function testCheckDBNewLoggedIn() | ||
651 | { | ||
652 | unlink(self::$testDatastore); | ||
653 | $this->assertFileNotExists(self::$testDatastore); | ||
654 | new BookmarkFileService($this->conf, $this->history, true); | ||
655 | $this->assertFileExists(self::$testDatastore); | ||
656 | |||
657 | // ensure the correct data has been written | ||
658 | $this->assertGreaterThan(0, filesize(self::$testDatastore)); | ||
659 | } | ||
660 | |||
661 | /** | ||
662 | * The DB doesn't exist, but not logged in, ensure it initialized, but the file is not written | ||
663 | */ | ||
664 | public function testCheckDBNewLoggedOut() | ||
665 | { | ||
666 | unlink(self::$testDatastore); | ||
667 | $this->assertFileNotExists(self::$testDatastore); | ||
668 | $db = new \FakeBookmarkService($this->conf, $this->history, false); | ||
669 | $this->assertFileNotExists(self::$testDatastore); | ||
670 | $this->assertInstanceOf(BookmarkArray::class, $db->getBookmarks()); | ||
671 | $this->assertCount(0, $db->getBookmarks()); | ||
672 | } | ||
673 | |||
674 | /** | ||
675 | * Load public bookmarks from the DB | ||
676 | */ | ||
677 | public function testReadPublicDB() | ||
678 | { | ||
679 | $this->assertEquals( | ||
680 | $this->refDB->countPublicLinks(), | ||
681 | $this->publicLinkDB->count() | ||
682 | ); | ||
683 | } | ||
684 | |||
685 | /** | ||
686 | * Load public and private bookmarks from the DB | ||
687 | */ | ||
688 | public function testReadPrivateDB() | ||
689 | { | ||
690 | $this->assertEquals( | ||
691 | $this->refDB->countLinks(), | ||
692 | $this->privateLinkDB->count() | ||
693 | ); | ||
694 | } | ||
695 | |||
696 | /** | ||
697 | * Save the bookmarks to the DB | ||
698 | */ | ||
699 | public function testSave() | ||
700 | { | ||
701 | $testDB = new BookmarkFileService($this->conf, $this->history, true); | ||
702 | $dbSize = $testDB->count(); | ||
703 | |||
704 | $bookmark = new Bookmark(); | ||
705 | $testDB->add($bookmark); | ||
706 | |||
707 | $testDB = new BookmarkFileService($this->conf, $this->history, true); | ||
708 | $this->assertEquals($dbSize + 1, $testDB->count()); | ||
709 | } | ||
710 | |||
711 | /** | ||
712 | * Count existing bookmarks - public bookmarks hidden | ||
713 | */ | ||
714 | public function testCountHiddenPublic() | ||
715 | { | ||
716 | $this->conf->set('privacy.hide_public_links', true); | ||
717 | $linkDB = new BookmarkFileService($this->conf, $this->history, false); | ||
718 | |||
719 | $this->assertEquals(0, $linkDB->count()); | ||
720 | } | ||
721 | |||
722 | /** | ||
723 | * List the days for which bookmarks have been posted | ||
724 | */ | ||
725 | public function testDays() | ||
726 | { | ||
727 | $this->assertEquals( | ||
728 | ['20100309', '20100310', '20121206', '20121207', '20130614', '20150310'], | ||
729 | $this->publicLinkDB->days() | ||
730 | ); | ||
731 | |||
732 | $this->assertEquals( | ||
733 | ['20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'], | ||
734 | $this->privateLinkDB->days() | ||
735 | ); | ||
736 | } | ||
737 | |||
738 | /** | ||
739 | * The URL corresponds to an existing entry in the DB | ||
740 | */ | ||
741 | public function testGetKnownLinkFromURL() | ||
742 | { | ||
743 | $link = $this->publicLinkDB->findByUrl('http://mediagoblin.org/'); | ||
744 | |||
745 | $this->assertNotEquals(false, $link); | ||
746 | $this->assertContains( | ||
747 | 'A free software media publishing platform', | ||
748 | $link->getDescription() | ||
749 | ); | ||
750 | } | ||
751 | |||
752 | /** | ||
753 | * The URL is not in the DB | ||
754 | */ | ||
755 | public function testGetUnknownLinkFromURL() | ||
756 | { | ||
757 | $this->assertEquals( | ||
758 | false, | ||
759 | $this->publicLinkDB->findByUrl('http://dev.null') | ||
760 | ); | ||
761 | } | ||
762 | |||
763 | /** | ||
764 | * Lists all tags | ||
765 | */ | ||
766 | public function testAllTags() | ||
767 | { | ||
768 | $this->assertEquals( | ||
769 | [ | ||
770 | 'web' => 3, | ||
771 | 'cartoon' => 2, | ||
772 | 'gnu' => 2, | ||
773 | 'dev' => 1, | ||
774 | 'samba' => 1, | ||
775 | 'media' => 1, | ||
776 | 'software' => 1, | ||
777 | 'stallman' => 1, | ||
778 | 'free' => 1, | ||
779 | '-exclude' => 1, | ||
780 | 'hashtag' => 2, | ||
781 | // The DB contains a link with `sTuff` and another one with `stuff` tag. | ||
782 | // They need to be grouped with the first case found - order by date DESC: `sTuff`. | ||
783 | 'sTuff' => 2, | ||
784 | 'ut' => 1, | ||
785 | ], | ||
786 | $this->publicLinkDB->bookmarksCountPerTag() | ||
787 | ); | ||
788 | |||
789 | $this->assertEquals( | ||
790 | [ | ||
791 | 'web' => 4, | ||
792 | 'cartoon' => 3, | ||
793 | 'gnu' => 2, | ||
794 | 'dev' => 2, | ||
795 | 'samba' => 1, | ||
796 | 'media' => 1, | ||
797 | 'software' => 1, | ||
798 | 'stallman' => 1, | ||
799 | 'free' => 1, | ||
800 | 'html' => 1, | ||
801 | 'w3c' => 1, | ||
802 | 'css' => 1, | ||
803 | 'Mercurial' => 1, | ||
804 | 'sTuff' => 2, | ||
805 | '-exclude' => 1, | ||
806 | '.hidden' => 1, | ||
807 | 'hashtag' => 2, | ||
808 | 'tag1' => 1, | ||
809 | 'tag2' => 1, | ||
810 | 'tag3' => 1, | ||
811 | 'tag4' => 1, | ||
812 | 'ut' => 1, | ||
813 | ], | ||
814 | $this->privateLinkDB->bookmarksCountPerTag() | ||
815 | ); | ||
816 | $this->assertEquals( | ||
817 | [ | ||
818 | 'web' => 4, | ||
819 | 'cartoon' => 2, | ||
820 | 'gnu' => 1, | ||
821 | 'dev' => 1, | ||
822 | 'samba' => 1, | ||
823 | 'media' => 1, | ||
824 | 'html' => 1, | ||
825 | 'w3c' => 1, | ||
826 | 'css' => 1, | ||
827 | 'Mercurial' => 1, | ||
828 | '.hidden' => 1, | ||
829 | 'hashtag' => 1, | ||
830 | ], | ||
831 | $this->privateLinkDB->bookmarksCountPerTag(['web']) | ||
832 | ); | ||
833 | $this->assertEquals( | ||
834 | [ | ||
835 | 'web' => 1, | ||
836 | 'html' => 1, | ||
837 | 'w3c' => 1, | ||
838 | 'css' => 1, | ||
839 | 'Mercurial' => 1, | ||
840 | ], | ||
841 | $this->privateLinkDB->bookmarksCountPerTag(['web'], 'private') | ||
842 | ); | ||
843 | } | ||
844 | |||
845 | /** | ||
846 | * Test filter with string. | ||
847 | */ | ||
848 | public function testFilterString() | ||
849 | { | ||
850 | $tags = 'dev cartoon'; | ||
851 | $request = ['searchtags' => $tags]; | ||
852 | $this->assertEquals( | ||
853 | 2, | ||
854 | count($this->privateLinkDB->search($request, null, true)) | ||
855 | ); | ||
856 | } | ||
857 | |||
858 | /** | ||
859 | * Test filter with array. | ||
860 | */ | ||
861 | public function testFilterArray() | ||
862 | { | ||
863 | $tags = ['dev', 'cartoon']; | ||
864 | $request = ['searchtags' => $tags]; | ||
865 | $this->assertEquals( | ||
866 | 2, | ||
867 | count($this->privateLinkDB->search($request, null, true)) | ||
868 | ); | ||
869 | } | ||
870 | |||
871 | /** | ||
872 | * Test hidden tags feature: | ||
873 | * tags starting with a dot '.' are only visible when logged in. | ||
874 | */ | ||
875 | public function testHiddenTags() | ||
876 | { | ||
877 | $tags = '.hidden'; | ||
878 | $request = ['searchtags' => $tags]; | ||
879 | $this->assertEquals( | ||
880 | 1, | ||
881 | count($this->privateLinkDB->search($request, 'all', true)) | ||
882 | ); | ||
883 | |||
884 | $this->assertEquals( | ||
885 | 0, | ||
886 | count($this->publicLinkDB->search($request, 'public', true)) | ||
887 | ); | ||
888 | } | ||
889 | |||
890 | /** | ||
891 | * Test filterHash() with a valid smallhash. | ||
892 | */ | ||
893 | public function testFilterHashValid() | ||
894 | { | ||
895 | $request = smallHash('20150310_114651'); | ||
896 | $this->assertEquals( | ||
897 | 1, | ||
898 | count($this->publicLinkDB->findByHash($request)) | ||
899 | ); | ||
900 | $request = smallHash('20150310_114633' . 8); | ||
901 | $this->assertEquals( | ||
902 | 1, | ||
903 | count($this->publicLinkDB->findByHash($request)) | ||
904 | ); | ||
905 | } | ||
906 | |||
907 | /** | ||
908 | * Test filterHash() with an invalid smallhash. | ||
909 | * | ||
910 | * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException | ||
911 | */ | ||
912 | public function testFilterHashInValid1() | ||
913 | { | ||
914 | $request = 'blabla'; | ||
915 | $this->publicLinkDB->findByHash($request); | ||
916 | } | ||
917 | |||
918 | /** | ||
919 | * Test filterHash() with an empty smallhash. | ||
920 | * | ||
921 | * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException | ||
922 | */ | ||
923 | public function testFilterHashInValid() | ||
924 | { | ||
925 | $this->publicLinkDB->findByHash(''); | ||
926 | } | ||
927 | |||
928 | /** | ||
929 | * Test linksCountPerTag all tags without filter. | ||
930 | * Equal occurrences should be sorted alphabetically. | ||
931 | */ | ||
932 | public function testCountLinkPerTagAllNoFilter() | ||
933 | { | ||
934 | $expected = [ | ||
935 | 'web' => 4, | ||
936 | 'cartoon' => 3, | ||
937 | 'dev' => 2, | ||
938 | 'gnu' => 2, | ||
939 | 'hashtag' => 2, | ||
940 | 'sTuff' => 2, | ||
941 | '-exclude' => 1, | ||
942 | '.hidden' => 1, | ||
943 | 'Mercurial' => 1, | ||
944 | 'css' => 1, | ||
945 | 'free' => 1, | ||
946 | 'html' => 1, | ||
947 | 'media' => 1, | ||
948 | 'samba' => 1, | ||
949 | 'software' => 1, | ||
950 | 'stallman' => 1, | ||
951 | 'tag1' => 1, | ||
952 | 'tag2' => 1, | ||
953 | 'tag3' => 1, | ||
954 | 'tag4' => 1, | ||
955 | 'ut' => 1, | ||
956 | 'w3c' => 1, | ||
957 | ]; | ||
958 | $tags = $this->privateLinkDB->bookmarksCountPerTag(); | ||
959 | |||
960 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
961 | } | ||
962 | |||
963 | /** | ||
964 | * Test linksCountPerTag all tags with filter. | ||
965 | * Equal occurrences should be sorted alphabetically. | ||
966 | */ | ||
967 | public function testCountLinkPerTagAllWithFilter() | ||
968 | { | ||
969 | $expected = [ | ||
970 | 'gnu' => 2, | ||
971 | 'hashtag' => 2, | ||
972 | '-exclude' => 1, | ||
973 | '.hidden' => 1, | ||
974 | 'free' => 1, | ||
975 | 'media' => 1, | ||
976 | 'software' => 1, | ||
977 | 'stallman' => 1, | ||
978 | 'stuff' => 1, | ||
979 | 'web' => 1, | ||
980 | ]; | ||
981 | $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu']); | ||
982 | |||
983 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
984 | } | ||
985 | |||
986 | /** | ||
987 | * Test linksCountPerTag public tags with filter. | ||
988 | * Equal occurrences should be sorted alphabetically. | ||
989 | */ | ||
990 | public function testCountLinkPerTagPublicWithFilter() | ||
991 | { | ||
992 | $expected = [ | ||
993 | 'gnu' => 2, | ||
994 | 'hashtag' => 2, | ||
995 | '-exclude' => 1, | ||
996 | '.hidden' => 1, | ||
997 | 'free' => 1, | ||
998 | 'media' => 1, | ||
999 | 'software' => 1, | ||
1000 | 'stallman' => 1, | ||
1001 | 'stuff' => 1, | ||
1002 | 'web' => 1, | ||
1003 | ]; | ||
1004 | $tags = $this->privateLinkDB->bookmarksCountPerTag(['gnu'], 'public'); | ||
1005 | |||
1006 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
1007 | } | ||
1008 | |||
1009 | /** | ||
1010 | * Test linksCountPerTag public tags with filter. | ||
1011 | * Equal occurrences should be sorted alphabetically. | ||
1012 | */ | ||
1013 | public function testCountLinkPerTagPrivateWithFilter() | ||
1014 | { | ||
1015 | $expected = [ | ||
1016 | 'cartoon' => 1, | ||
1017 | 'dev' => 1, | ||
1018 | 'tag1' => 1, | ||
1019 | 'tag2' => 1, | ||
1020 | 'tag3' => 1, | ||
1021 | 'tag4' => 1, | ||
1022 | ]; | ||
1023 | $tags = $this->privateLinkDB->bookmarksCountPerTag(['dev'], 'private'); | ||
1024 | |||
1025 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
1026 | } | ||
1027 | |||
1028 | /** | ||
1029 | * Allows to test LinkDB's private methods | ||
1030 | * | ||
1031 | * @see | ||
1032 | * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html | ||
1033 | * http://stackoverflow.com/a/2798203 | ||
1034 | */ | ||
1035 | protected static function getMethod($name) | ||
1036 | { | ||
1037 | $class = new ReflectionClass('Shaarli\Bookmark\BookmarkFileService'); | ||
1038 | $method = $class->getMethod($name); | ||
1039 | $method->setAccessible(true); | ||
1040 | return $method; | ||
1041 | } | ||
1042 | } | ||
diff --git a/tests/bookmark/LinkFilterTest.php b/tests/bookmark/BookmarkFilterTest.php index 808f8122..d4c71cb9 100644 --- a/tests/bookmark/LinkFilterTest.php +++ b/tests/bookmark/BookmarkFilterTest.php | |||
@@ -3,19 +3,23 @@ | |||
3 | namespace Shaarli\Bookmark; | 3 | namespace Shaarli\Bookmark; |
4 | 4 | ||
5 | use Exception; | 5 | use Exception; |
6 | use PHPUnit\Framework\TestCase; | ||
6 | use ReferenceLinkDB; | 7 | use ReferenceLinkDB; |
8 | use Shaarli\Config\ConfigManager; | ||
9 | use Shaarli\Formatter\FormatterFactory; | ||
10 | use Shaarli\History; | ||
7 | 11 | ||
8 | /** | 12 | /** |
9 | * Class LinkFilterTest. | 13 | * Class BookmarkFilterTest. |
10 | */ | 14 | */ |
11 | class LinkFilterTest extends \PHPUnit\Framework\TestCase | 15 | class BookmarkFilterTest extends TestCase |
12 | { | 16 | { |
13 | /** | 17 | /** |
14 | * @var string Test datastore path. | 18 | * @var string Test datastore path. |
15 | */ | 19 | */ |
16 | protected static $testDatastore = 'sandbox/datastore.php'; | 20 | protected static $testDatastore = 'sandbox/datastore.php'; |
17 | /** | 21 | /** |
18 | * @var LinkFilter instance. | 22 | * @var BookmarkFilter instance. |
19 | */ | 23 | */ |
20 | protected static $linkFilter; | 24 | protected static $linkFilter; |
21 | 25 | ||
@@ -25,19 +29,22 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
25 | protected static $refDB; | 29 | protected static $refDB; |
26 | 30 | ||
27 | /** | 31 | /** |
28 | * @var LinkDB instance | 32 | * @var BookmarkFileService instance |
29 | */ | 33 | */ |
30 | protected static $linkDB; | 34 | protected static $bookmarkService; |
31 | 35 | ||
32 | /** | 36 | /** |
33 | * Instantiate linkFilter with ReferenceLinkDB data. | 37 | * Instantiate linkFilter with ReferenceLinkDB data. |
34 | */ | 38 | */ |
35 | public static function setUpBeforeClass() | 39 | public static function setUpBeforeClass() |
36 | { | 40 | { |
37 | self::$refDB = new ReferenceLinkDB(); | 41 | $conf = new ConfigManager('tests/utils/config/configJson'); |
42 | $conf->set('resource.datastore', self::$testDatastore); | ||
43 | self::$refDB = new \ReferenceLinkDB(); | ||
38 | self::$refDB->write(self::$testDatastore); | 44 | self::$refDB->write(self::$testDatastore); |
39 | self::$linkDB = new LinkDB(self::$testDatastore, true, false); | 45 | $history = new History('sandbox/history.php'); |
40 | self::$linkFilter = new LinkFilter(self::$linkDB); | 46 | self::$bookmarkService = new \FakeBookmarkService($conf, $history, true); |
47 | self::$linkFilter = new BookmarkFilter(self::$bookmarkService->getBookmarks()); | ||
41 | } | 48 | } |
42 | 49 | ||
43 | /** | 50 | /** |
@@ -74,14 +81,14 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
74 | 81 | ||
75 | $this->assertEquals( | 82 | $this->assertEquals( |
76 | ReferenceLinkDB::$NB_LINKS_TOTAL, | 83 | ReferenceLinkDB::$NB_LINKS_TOTAL, |
77 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, '')) | 84 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, '')) |
78 | ); | 85 | ); |
79 | 86 | ||
80 | $this->assertEquals( | 87 | $this->assertEquals( |
81 | self::$refDB->countUntaggedLinks(), | 88 | self::$refDB->countUntaggedLinks(), |
82 | count( | 89 | count( |
83 | self::$linkFilter->filter( | 90 | self::$linkFilter->filter( |
84 | LinkFilter::$FILTER_TAG, | 91 | BookmarkFilter::$FILTER_TAG, |
85 | /*$request=*/ | 92 | /*$request=*/ |
86 | '', | 93 | '', |
87 | /*$casesensitive=*/ | 94 | /*$casesensitive=*/ |
@@ -96,89 +103,89 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
96 | 103 | ||
97 | $this->assertEquals( | 104 | $this->assertEquals( |
98 | ReferenceLinkDB::$NB_LINKS_TOTAL, | 105 | ReferenceLinkDB::$NB_LINKS_TOTAL, |
99 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '')) | 106 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, '')) |
100 | ); | 107 | ); |
101 | } | 108 | } |
102 | 109 | ||
103 | /** | 110 | /** |
104 | * Filter links using a tag | 111 | * Filter bookmarks using a tag |
105 | */ | 112 | */ |
106 | public function testFilterOneTag() | 113 | public function testFilterOneTag() |
107 | { | 114 | { |
108 | $this->assertEquals( | 115 | $this->assertEquals( |
109 | 4, | 116 | 4, |
110 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false)) | 117 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false)) |
111 | ); | 118 | ); |
112 | 119 | ||
113 | $this->assertEquals( | 120 | $this->assertEquals( |
114 | 4, | 121 | 4, |
115 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'all')) | 122 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'all')) |
116 | ); | 123 | ); |
117 | 124 | ||
118 | $this->assertEquals( | 125 | $this->assertEquals( |
119 | 4, | 126 | 4, |
120 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'default-blabla')) | 127 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'default-blabla')) |
121 | ); | 128 | ); |
122 | 129 | ||
123 | // Private only. | 130 | // Private only. |
124 | $this->assertEquals( | 131 | $this->assertEquals( |
125 | 1, | 132 | 1, |
126 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'private')) | 133 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'private')) |
127 | ); | 134 | ); |
128 | 135 | ||
129 | // Public only. | 136 | // Public only. |
130 | $this->assertEquals( | 137 | $this->assertEquals( |
131 | 3, | 138 | 3, |
132 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'web', false, 'public')) | 139 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'web', false, 'public')) |
133 | ); | 140 | ); |
134 | } | 141 | } |
135 | 142 | ||
136 | /** | 143 | /** |
137 | * Filter links using a tag - case-sensitive | 144 | * Filter bookmarks using a tag - case-sensitive |
138 | */ | 145 | */ |
139 | public function testFilterCaseSensitiveTag() | 146 | public function testFilterCaseSensitiveTag() |
140 | { | 147 | { |
141 | $this->assertEquals( | 148 | $this->assertEquals( |
142 | 0, | 149 | 0, |
143 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'mercurial', true)) | 150 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'mercurial', true)) |
144 | ); | 151 | ); |
145 | 152 | ||
146 | $this->assertEquals( | 153 | $this->assertEquals( |
147 | 1, | 154 | 1, |
148 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'Mercurial', true)) | 155 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'Mercurial', true)) |
149 | ); | 156 | ); |
150 | } | 157 | } |
151 | 158 | ||
152 | /** | 159 | /** |
153 | * Filter links using a tag combination | 160 | * Filter bookmarks using a tag combination |
154 | */ | 161 | */ |
155 | public function testFilterMultipleTags() | 162 | public function testFilterMultipleTags() |
156 | { | 163 | { |
157 | $this->assertEquals( | 164 | $this->assertEquals( |
158 | 2, | 165 | 2, |
159 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'dev cartoon', false)) | 166 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'dev cartoon', false)) |
160 | ); | 167 | ); |
161 | } | 168 | } |
162 | 169 | ||
163 | /** | 170 | /** |
164 | * Filter links using a non-existent tag | 171 | * Filter bookmarks using a non-existent tag |
165 | */ | 172 | */ |
166 | public function testFilterUnknownTag() | 173 | public function testFilterUnknownTag() |
167 | { | 174 | { |
168 | $this->assertEquals( | 175 | $this->assertEquals( |
169 | 0, | 176 | 0, |
170 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'null', false)) | 177 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'null', false)) |
171 | ); | 178 | ); |
172 | } | 179 | } |
173 | 180 | ||
174 | /** | 181 | /** |
175 | * Return links for a given day | 182 | * Return bookmarks for a given day |
176 | */ | 183 | */ |
177 | public function testFilterDay() | 184 | public function testFilterDay() |
178 | { | 185 | { |
179 | $this->assertEquals( | 186 | $this->assertEquals( |
180 | 4, | 187 | 4, |
181 | count(self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '20121206')) | 188 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20121206')) |
182 | ); | 189 | ); |
183 | } | 190 | } |
184 | 191 | ||
@@ -189,7 +196,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
189 | { | 196 | { |
190 | $this->assertEquals( | 197 | $this->assertEquals( |
191 | 0, | 198 | 0, |
192 | count(self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '19700101')) | 199 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '19700101')) |
193 | ); | 200 | ); |
194 | } | 201 | } |
195 | 202 | ||
@@ -200,7 +207,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
200 | */ | 207 | */ |
201 | public function testFilterInvalidDayWithChars() | 208 | public function testFilterInvalidDayWithChars() |
202 | { | 209 | { |
203 | self::$linkFilter->filter(LinkFilter::$FILTER_DAY, 'Rainy day, dream away'); | 210 | self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, 'Rainy day, dream away'); |
204 | } | 211 | } |
205 | 212 | ||
206 | /** | 213 | /** |
@@ -210,7 +217,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
210 | */ | 217 | */ |
211 | public function testFilterInvalidDayDigits() | 218 | public function testFilterInvalidDayDigits() |
212 | { | 219 | { |
213 | self::$linkFilter->filter(LinkFilter::$FILTER_DAY, '20'); | 220 | self::$linkFilter->filter(BookmarkFilter::$FILTER_DAY, '20'); |
214 | } | 221 | } |
215 | 222 | ||
216 | /** | 223 | /** |
@@ -218,7 +225,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
218 | */ | 225 | */ |
219 | public function testFilterSmallHash() | 226 | public function testFilterSmallHash() |
220 | { | 227 | { |
221 | $links = self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'IuWvgA'); | 228 | $links = self::$linkFilter->filter(BookmarkFilter::$FILTER_HASH, 'IuWvgA'); |
222 | 229 | ||
223 | $this->assertEquals( | 230 | $this->assertEquals( |
224 | 1, | 231 | 1, |
@@ -227,18 +234,18 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
227 | 234 | ||
228 | $this->assertEquals( | 235 | $this->assertEquals( |
229 | 'MediaGoblin', | 236 | 'MediaGoblin', |
230 | $links[7]['title'] | 237 | $links[7]->getTitle() |
231 | ); | 238 | ); |
232 | } | 239 | } |
233 | 240 | ||
234 | /** | 241 | /** |
235 | * No link for this hash | 242 | * No link for this hash |
236 | * | 243 | * |
237 | * @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException | 244 | * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException |
238 | */ | 245 | */ |
239 | public function testFilterUnknownSmallHash() | 246 | public function testFilterUnknownSmallHash() |
240 | { | 247 | { |
241 | self::$linkFilter->filter(LinkFilter::$FILTER_HASH, 'Iblaah'); | 248 | self::$linkFilter->filter(BookmarkFilter::$FILTER_HASH, 'Iblaah'); |
242 | } | 249 | } |
243 | 250 | ||
244 | /** | 251 | /** |
@@ -248,7 +255,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
248 | { | 255 | { |
249 | $this->assertEquals( | 256 | $this->assertEquals( |
250 | 0, | 257 | 0, |
251 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'azertyuiop')) | 258 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'azertyuiop')) |
252 | ); | 259 | ); |
253 | } | 260 | } |
254 | 261 | ||
@@ -259,12 +266,12 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
259 | { | 266 | { |
260 | $this->assertEquals( | 267 | $this->assertEquals( |
261 | 2, | 268 | 2, |
262 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'ars.userfriendly.org')) | 269 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'ars.userfriendly.org')) |
263 | ); | 270 | ); |
264 | 271 | ||
265 | $this->assertEquals( | 272 | $this->assertEquals( |
266 | 2, | 273 | 2, |
267 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'ars org')) | 274 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'ars org')) |
268 | ); | 275 | ); |
269 | } | 276 | } |
270 | 277 | ||
@@ -276,21 +283,21 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
276 | // use miscellaneous cases | 283 | // use miscellaneous cases |
277 | $this->assertEquals( | 284 | $this->assertEquals( |
278 | 2, | 285 | 2, |
279 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'userfriendly -')) | 286 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'userfriendly -')) |
280 | ); | 287 | ); |
281 | $this->assertEquals( | 288 | $this->assertEquals( |
282 | 2, | 289 | 2, |
283 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'UserFriendly -')) | 290 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'UserFriendly -')) |
284 | ); | 291 | ); |
285 | $this->assertEquals( | 292 | $this->assertEquals( |
286 | 2, | 293 | 2, |
287 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'uSeRFrIendlY -')) | 294 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'uSeRFrIendlY -')) |
288 | ); | 295 | ); |
289 | 296 | ||
290 | // use miscellaneous case and offset | 297 | // use miscellaneous case and offset |
291 | $this->assertEquals( | 298 | $this->assertEquals( |
292 | 2, | 299 | 2, |
293 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'RFrIendL')) | 300 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'RFrIendL')) |
294 | ); | 301 | ); |
295 | } | 302 | } |
296 | 303 | ||
@@ -301,17 +308,17 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
301 | { | 308 | { |
302 | $this->assertEquals( | 309 | $this->assertEquals( |
303 | 1, | 310 | 1, |
304 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'publishing media')) | 311 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'publishing media')) |
305 | ); | 312 | ); |
306 | 313 | ||
307 | $this->assertEquals( | 314 | $this->assertEquals( |
308 | 1, | 315 | 1, |
309 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'mercurial w3c')) | 316 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'mercurial w3c')) |
310 | ); | 317 | ); |
311 | 318 | ||
312 | $this->assertEquals( | 319 | $this->assertEquals( |
313 | 3, | 320 | 3, |
314 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '"free software"')) | 321 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, '"free software"')) |
315 | ); | 322 | ); |
316 | } | 323 | } |
317 | 324 | ||
@@ -322,29 +329,29 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
322 | { | 329 | { |
323 | $this->assertEquals( | 330 | $this->assertEquals( |
324 | 6, | 331 | 6, |
325 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web')) | 332 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web')) |
326 | ); | 333 | ); |
327 | 334 | ||
328 | $this->assertEquals( | 335 | $this->assertEquals( |
329 | 6, | 336 | 6, |
330 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', 'all')) | 337 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', 'all')) |
331 | ); | 338 | ); |
332 | 339 | ||
333 | $this->assertEquals( | 340 | $this->assertEquals( |
334 | 6, | 341 | 6, |
335 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', 'bla')) | 342 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', 'bla')) |
336 | ); | 343 | ); |
337 | 344 | ||
338 | // Private only. | 345 | // Private only. |
339 | $this->assertEquals( | 346 | $this->assertEquals( |
340 | 1, | 347 | 1, |
341 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', false, 'private')) | 348 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', false, 'private')) |
342 | ); | 349 | ); |
343 | 350 | ||
344 | // Public only. | 351 | // Public only. |
345 | $this->assertEquals( | 352 | $this->assertEquals( |
346 | 5, | 353 | 5, |
347 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'web', false, 'public')) | 354 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'web', false, 'public')) |
348 | ); | 355 | ); |
349 | } | 356 | } |
350 | 357 | ||
@@ -355,7 +362,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
355 | { | 362 | { |
356 | $this->assertEquals( | 363 | $this->assertEquals( |
357 | 3, | 364 | 3, |
358 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'free software')) | 365 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'free software')) |
359 | ); | 366 | ); |
360 | } | 367 | } |
361 | 368 | ||
@@ -366,12 +373,12 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
366 | { | 373 | { |
367 | $this->assertEquals( | 374 | $this->assertEquals( |
368 | 1, | 375 | 1, |
369 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'free -gnu')) | 376 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, 'free -gnu')) |
370 | ); | 377 | ); |
371 | 378 | ||
372 | $this->assertEquals( | 379 | $this->assertEquals( |
373 | ReferenceLinkDB::$NB_LINKS_TOTAL - 1, | 380 | ReferenceLinkDB::$NB_LINKS_TOTAL - 1, |
374 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, '-revolution')) | 381 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TEXT, '-revolution')) |
375 | ); | 382 | ); |
376 | } | 383 | } |
377 | 384 | ||
@@ -383,7 +390,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
383 | $this->assertEquals( | 390 | $this->assertEquals( |
384 | 2, | 391 | 2, |
385 | count(self::$linkFilter->filter( | 392 | count(self::$linkFilter->filter( |
386 | LinkFilter::$FILTER_TEXT, | 393 | BookmarkFilter::$FILTER_TEXT, |
387 | '"Free Software " stallman "read this" @website stuff' | 394 | '"Free Software " stallman "read this" @website stuff' |
388 | )) | 395 | )) |
389 | ); | 396 | ); |
@@ -391,7 +398,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
391 | $this->assertEquals( | 398 | $this->assertEquals( |
392 | 1, | 399 | 1, |
393 | count(self::$linkFilter->filter( | 400 | count(self::$linkFilter->filter( |
394 | LinkFilter::$FILTER_TEXT, | 401 | BookmarkFilter::$FILTER_TEXT, |
395 | '"free software " stallman "read this" -beard @website stuff' | 402 | '"free software " stallman "read this" -beard @website stuff' |
396 | )) | 403 | )) |
397 | ); | 404 | ); |
@@ -405,7 +412,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
405 | $this->assertEquals( | 412 | $this->assertEquals( |
406 | 0, | 413 | 0, |
407 | count(self::$linkFilter->filter( | 414 | count(self::$linkFilter->filter( |
408 | LinkFilter::$FILTER_TEXT, | 415 | BookmarkFilter::$FILTER_TEXT, |
409 | '"designer naming"' | 416 | '"designer naming"' |
410 | )) | 417 | )) |
411 | ); | 418 | ); |
@@ -413,7 +420,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
413 | $this->assertEquals( | 420 | $this->assertEquals( |
414 | 0, | 421 | 0, |
415 | count(self::$linkFilter->filter( | 422 | count(self::$linkFilter->filter( |
416 | LinkFilter::$FILTER_TEXT, | 423 | BookmarkFilter::$FILTER_TEXT, |
417 | '"designernaming"' | 424 | '"designernaming"' |
418 | )) | 425 | )) |
419 | ); | 426 | ); |
@@ -426,12 +433,12 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
426 | { | 433 | { |
427 | $this->assertEquals( | 434 | $this->assertEquals( |
428 | 1, | 435 | 1, |
429 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'gnu -free')) | 436 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, 'gnu -free')) |
430 | ); | 437 | ); |
431 | 438 | ||
432 | $this->assertEquals( | 439 | $this->assertEquals( |
433 | ReferenceLinkDB::$NB_LINKS_TOTAL - 1, | 440 | ReferenceLinkDB::$NB_LINKS_TOTAL - 1, |
434 | count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, '-free')) | 441 | count(self::$linkFilter->filter(BookmarkFilter::$FILTER_TAG, '-free')) |
435 | ); | 442 | ); |
436 | } | 443 | } |
437 | 444 | ||
@@ -445,42 +452,42 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
445 | $this->assertEquals( | 452 | $this->assertEquals( |
446 | 1, | 453 | 1, |
447 | count(self::$linkFilter->filter( | 454 | count(self::$linkFilter->filter( |
448 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, | 455 | BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT, |
449 | array($tags, $terms) | 456 | array($tags, $terms) |
450 | )) | 457 | )) |
451 | ); | 458 | ); |
452 | $this->assertEquals( | 459 | $this->assertEquals( |
453 | 2, | 460 | 2, |
454 | count(self::$linkFilter->filter( | 461 | count(self::$linkFilter->filter( |
455 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, | 462 | BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT, |
456 | array('', $terms) | 463 | array('', $terms) |
457 | )) | 464 | )) |
458 | ); | 465 | ); |
459 | $this->assertEquals( | 466 | $this->assertEquals( |
460 | 1, | 467 | 1, |
461 | count(self::$linkFilter->filter( | 468 | count(self::$linkFilter->filter( |
462 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, | 469 | BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT, |
463 | array(false, 'PSR-2') | 470 | array(false, 'PSR-2') |
464 | )) | 471 | )) |
465 | ); | 472 | ); |
466 | $this->assertEquals( | 473 | $this->assertEquals( |
467 | 1, | 474 | 1, |
468 | count(self::$linkFilter->filter( | 475 | count(self::$linkFilter->filter( |
469 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, | 476 | BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT, |
470 | array($tags, '') | 477 | array($tags, '') |
471 | )) | 478 | )) |
472 | ); | 479 | ); |
473 | $this->assertEquals( | 480 | $this->assertEquals( |
474 | ReferenceLinkDB::$NB_LINKS_TOTAL, | 481 | ReferenceLinkDB::$NB_LINKS_TOTAL, |
475 | count(self::$linkFilter->filter( | 482 | count(self::$linkFilter->filter( |
476 | LinkFilter::$FILTER_TAG | LinkFilter::$FILTER_TEXT, | 483 | BookmarkFilter::$FILTER_TAG | BookmarkFilter::$FILTER_TEXT, |
477 | '' | 484 | '' |
478 | )) | 485 | )) |
479 | ); | 486 | ); |
480 | } | 487 | } |
481 | 488 | ||
482 | /** | 489 | /** |
483 | * Filter links by #hashtag. | 490 | * Filter bookmarks by #hashtag. |
484 | */ | 491 | */ |
485 | public function testFilterByHashtag() | 492 | public function testFilterByHashtag() |
486 | { | 493 | { |
@@ -488,7 +495,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
488 | $this->assertEquals( | 495 | $this->assertEquals( |
489 | 3, | 496 | 3, |
490 | count(self::$linkFilter->filter( | 497 | count(self::$linkFilter->filter( |
491 | LinkFilter::$FILTER_TAG, | 498 | BookmarkFilter::$FILTER_TAG, |
492 | $hashtag | 499 | $hashtag |
493 | )) | 500 | )) |
494 | ); | 501 | ); |
@@ -497,7 +504,7 @@ class LinkFilterTest extends \PHPUnit\Framework\TestCase | |||
497 | $this->assertEquals( | 504 | $this->assertEquals( |
498 | 1, | 505 | 1, |
499 | count(self::$linkFilter->filter( | 506 | count(self::$linkFilter->filter( |
500 | LinkFilter::$FILTER_TAG, | 507 | BookmarkFilter::$FILTER_TAG, |
501 | $hashtag, | 508 | $hashtag, |
502 | false, | 509 | false, |
503 | 'private' | 510 | 'private' |
diff --git a/tests/bookmark/BookmarkInitializerTest.php b/tests/bookmark/BookmarkInitializerTest.php new file mode 100644 index 00000000..d23eb069 --- /dev/null +++ b/tests/bookmark/BookmarkInitializerTest.php | |||
@@ -0,0 +1,120 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Bookmark; | ||
4 | |||
5 | use PHPUnit\Framework\TestCase; | ||
6 | use ReferenceLinkDB; | ||
7 | use Shaarli\Config\ConfigManager; | ||
8 | use Shaarli\History; | ||
9 | |||
10 | /** | ||
11 | * Class BookmarkInitializerTest | ||
12 | * @package Shaarli\Bookmark | ||
13 | */ | ||
14 | class BookmarkInitializerTest extends TestCase | ||
15 | { | ||
16 | /** @var string Path of test data store */ | ||
17 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
18 | |||
19 | /** @var string Path of test config file */ | ||
20 | protected static $testConf = 'sandbox/config'; | ||
21 | |||
22 | /** | ||
23 | * @var ConfigManager instance. | ||
24 | */ | ||
25 | protected $conf; | ||
26 | |||
27 | /** | ||
28 | * @var History instance. | ||
29 | */ | ||
30 | protected $history; | ||
31 | |||
32 | /** @var BookmarkServiceInterface instance */ | ||
33 | protected $bookmarkService; | ||
34 | |||
35 | /** @var BookmarkInitializer instance */ | ||
36 | protected $initializer; | ||
37 | |||
38 | /** | ||
39 | * Initialize an empty BookmarkFileService | ||
40 | */ | ||
41 | public function setUp() | ||
42 | { | ||
43 | if (file_exists(self::$testDatastore)) { | ||
44 | unlink(self::$testDatastore); | ||
45 | } | ||
46 | |||
47 | copy('tests/utils/config/configJson.json.php', self::$testConf .'.json.php'); | ||
48 | $this->conf = new ConfigManager(self::$testConf); | ||
49 | $this->conf->set('resource.datastore', self::$testDatastore); | ||
50 | $this->history = new History('sandbox/history.php'); | ||
51 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | ||
52 | |||
53 | $this->initializer = new BookmarkInitializer($this->bookmarkService); | ||
54 | } | ||
55 | |||
56 | /** | ||
57 | * Test initialize() with an empty data store. | ||
58 | */ | ||
59 | public function testInitializeEmptyDataStore() | ||
60 | { | ||
61 | $refDB = new \ReferenceLinkDB(); | ||
62 | $refDB->write(self::$testDatastore); | ||
63 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | ||
64 | $this->initializer = new BookmarkInitializer($this->bookmarkService); | ||
65 | |||
66 | $this->initializer->initialize(); | ||
67 | |||
68 | $this->assertEquals($refDB->countLinks() + 2, $this->bookmarkService->count()); | ||
69 | $bookmark = $this->bookmarkService->get(43); | ||
70 | $this->assertEquals(43, $bookmark->getId()); | ||
71 | $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle()); | ||
72 | $this->assertTrue($bookmark->isPrivate()); | ||
73 | |||
74 | $bookmark = $this->bookmarkService->get(44); | ||
75 | $this->assertEquals(44, $bookmark->getId()); | ||
76 | $this->assertEquals( | ||
77 | 'The personal, minimalist, super-fast, database free, bookmarking service', | ||
78 | $bookmark->getTitle() | ||
79 | ); | ||
80 | $this->assertFalse($bookmark->isPrivate()); | ||
81 | |||
82 | // Reload from file | ||
83 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->history, true); | ||
84 | $this->assertEquals($refDB->countLinks() + 2, $this->bookmarkService->count()); | ||
85 | $bookmark = $this->bookmarkService->get(43); | ||
86 | $this->assertEquals(43, $bookmark->getId()); | ||
87 | $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle()); | ||
88 | $this->assertTrue($bookmark->isPrivate()); | ||
89 | |||
90 | $bookmark = $this->bookmarkService->get(44); | ||
91 | $this->assertEquals(44, $bookmark->getId()); | ||
92 | $this->assertEquals( | ||
93 | 'The personal, minimalist, super-fast, database free, bookmarking service', | ||
94 | $bookmark->getTitle() | ||
95 | ); | ||
96 | $this->assertFalse($bookmark->isPrivate()); | ||
97 | } | ||
98 | |||
99 | /** | ||
100 | * Test initialize() with a data store containing bookmarks. | ||
101 | */ | ||
102 | public function testInitializeNotEmptyDataStore() | ||
103 | { | ||
104 | $this->initializer->initialize(); | ||
105 | |||
106 | $this->assertEquals(2, $this->bookmarkService->count()); | ||
107 | $bookmark = $this->bookmarkService->get(0); | ||
108 | $this->assertEquals(0, $bookmark->getId()); | ||
109 | $this->assertEquals('My secret stuff... - Pastebin.com', $bookmark->getTitle()); | ||
110 | $this->assertTrue($bookmark->isPrivate()); | ||
111 | |||
112 | $bookmark = $this->bookmarkService->get(1); | ||
113 | $this->assertEquals(1, $bookmark->getId()); | ||
114 | $this->assertEquals( | ||
115 | 'The personal, minimalist, super-fast, database free, bookmarking service', | ||
116 | $bookmark->getTitle() | ||
117 | ); | ||
118 | $this->assertFalse($bookmark->isPrivate()); | ||
119 | } | ||
120 | } | ||
diff --git a/tests/bookmark/BookmarkTest.php b/tests/bookmark/BookmarkTest.php new file mode 100644 index 00000000..9a3bbbfc --- /dev/null +++ b/tests/bookmark/BookmarkTest.php | |||
@@ -0,0 +1,388 @@ | |||
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()); | ||
127 | $this->assertEquals('?abc', $bookmark->getUrl()); | ||
128 | $this->assertEquals('?abc', $bookmark->getTitle()); | ||
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 | } | ||
diff --git a/tests/bookmark/LinkDBTest.php b/tests/bookmark/LinkDBTest.php deleted file mode 100644 index ffe03cc5..00000000 --- a/tests/bookmark/LinkDBTest.php +++ /dev/null | |||
@@ -1,656 +0,0 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Link datastore tests | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Bookmark; | ||
7 | |||
8 | use DateTime; | ||
9 | use ReferenceLinkDB; | ||
10 | use ReflectionClass; | ||
11 | use Shaarli; | ||
12 | |||
13 | require_once 'application/feed/Cache.php'; | ||
14 | require_once 'application/Utils.php'; | ||
15 | require_once 'tests/utils/ReferenceLinkDB.php'; | ||
16 | |||
17 | |||
18 | /** | ||
19 | * Unitary tests for LinkDB | ||
20 | */ | ||
21 | class LinkDBTest extends \PHPUnit\Framework\TestCase | ||
22 | { | ||
23 | // datastore to test write operations | ||
24 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
25 | |||
26 | /** | ||
27 | * @var ReferenceLinkDB instance. | ||
28 | */ | ||
29 | protected static $refDB = null; | ||
30 | |||
31 | /** | ||
32 | * @var LinkDB public LinkDB instance. | ||
33 | */ | ||
34 | protected static $publicLinkDB = null; | ||
35 | |||
36 | /** | ||
37 | * @var LinkDB private LinkDB instance. | ||
38 | */ | ||
39 | protected static $privateLinkDB = null; | ||
40 | |||
41 | /** | ||
42 | * Instantiates public and private LinkDBs with test data | ||
43 | * | ||
44 | * The reference datastore contains public and private links that | ||
45 | * will be used to test LinkDB's methods: | ||
46 | * - access filtering (public/private), | ||
47 | * - link searches: | ||
48 | * - by day, | ||
49 | * - by tag, | ||
50 | * - by text, | ||
51 | * - etc. | ||
52 | * | ||
53 | * Resets test data for each test | ||
54 | */ | ||
55 | protected function setUp() | ||
56 | { | ||
57 | if (file_exists(self::$testDatastore)) { | ||
58 | unlink(self::$testDatastore); | ||
59 | } | ||
60 | |||
61 | self::$refDB = new ReferenceLinkDB(); | ||
62 | self::$refDB->write(self::$testDatastore); | ||
63 | |||
64 | self::$publicLinkDB = new LinkDB(self::$testDatastore, false, false); | ||
65 | self::$privateLinkDB = new LinkDB(self::$testDatastore, true, false); | ||
66 | } | ||
67 | |||
68 | /** | ||
69 | * Allows to test LinkDB's private methods | ||
70 | * | ||
71 | * @see | ||
72 | * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html | ||
73 | * http://stackoverflow.com/a/2798203 | ||
74 | */ | ||
75 | protected static function getMethod($name) | ||
76 | { | ||
77 | $class = new ReflectionClass('Shaarli\Bookmark\LinkDB'); | ||
78 | $method = $class->getMethod($name); | ||
79 | $method->setAccessible(true); | ||
80 | return $method; | ||
81 | } | ||
82 | |||
83 | /** | ||
84 | * Instantiate LinkDB objects - logged in user | ||
85 | */ | ||
86 | public function testConstructLoggedIn() | ||
87 | { | ||
88 | new LinkDB(self::$testDatastore, true, false); | ||
89 | $this->assertFileExists(self::$testDatastore); | ||
90 | } | ||
91 | |||
92 | /** | ||
93 | * Instantiate LinkDB objects - logged out or public instance | ||
94 | */ | ||
95 | public function testConstructLoggedOut() | ||
96 | { | ||
97 | new LinkDB(self::$testDatastore, false, false); | ||
98 | $this->assertFileExists(self::$testDatastore); | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * Attempt to instantiate a LinkDB whereas the datastore is not writable | ||
103 | * | ||
104 | * @expectedException Shaarli\Exceptions\IOException | ||
105 | * @expectedExceptionMessageRegExp /Error accessing "null"/ | ||
106 | */ | ||
107 | public function testConstructDatastoreNotWriteable() | ||
108 | { | ||
109 | new LinkDB('null/store.db', false, false); | ||
110 | } | ||
111 | |||
112 | /** | ||
113 | * The DB doesn't exist, ensure it is created with dummy content | ||
114 | */ | ||
115 | public function testCheckDBNew() | ||
116 | { | ||
117 | $linkDB = new LinkDB(self::$testDatastore, false, false); | ||
118 | unlink(self::$testDatastore); | ||
119 | $this->assertFileNotExists(self::$testDatastore); | ||
120 | |||
121 | $checkDB = self::getMethod('check'); | ||
122 | $checkDB->invokeArgs($linkDB, array()); | ||
123 | $this->assertFileExists(self::$testDatastore); | ||
124 | |||
125 | // ensure the correct data has been written | ||
126 | $this->assertGreaterThan(0, filesize(self::$testDatastore)); | ||
127 | } | ||
128 | |||
129 | /** | ||
130 | * The DB exists, don't do anything | ||
131 | */ | ||
132 | public function testCheckDBLoad() | ||
133 | { | ||
134 | $linkDB = new LinkDB(self::$testDatastore, false, false); | ||
135 | $datastoreSize = filesize(self::$testDatastore); | ||
136 | $this->assertGreaterThan(0, $datastoreSize); | ||
137 | |||
138 | $checkDB = self::getMethod('check'); | ||
139 | $checkDB->invokeArgs($linkDB, array()); | ||
140 | |||
141 | // ensure the datastore is left unmodified | ||
142 | $this->assertEquals( | ||
143 | $datastoreSize, | ||
144 | filesize(self::$testDatastore) | ||
145 | ); | ||
146 | } | ||
147 | |||
148 | /** | ||
149 | * Load an empty DB | ||
150 | */ | ||
151 | public function testReadEmptyDB() | ||
152 | { | ||
153 | file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>'); | ||
154 | $emptyDB = new LinkDB(self::$testDatastore, false, false); | ||
155 | $this->assertEquals(0, sizeof($emptyDB)); | ||
156 | $this->assertEquals(0, count($emptyDB)); | ||
157 | } | ||
158 | |||
159 | /** | ||
160 | * Load public links from the DB | ||
161 | */ | ||
162 | public function testReadPublicDB() | ||
163 | { | ||
164 | $this->assertEquals( | ||
165 | self::$refDB->countPublicLinks(), | ||
166 | sizeof(self::$publicLinkDB) | ||
167 | ); | ||
168 | } | ||
169 | |||
170 | /** | ||
171 | * Load public and private links from the DB | ||
172 | */ | ||
173 | public function testReadPrivateDB() | ||
174 | { | ||
175 | $this->assertEquals( | ||
176 | self::$refDB->countLinks(), | ||
177 | sizeof(self::$privateLinkDB) | ||
178 | ); | ||
179 | } | ||
180 | |||
181 | /** | ||
182 | * Save the links to the DB | ||
183 | */ | ||
184 | public function testSave() | ||
185 | { | ||
186 | $testDB = new LinkDB(self::$testDatastore, true, false); | ||
187 | $dbSize = sizeof($testDB); | ||
188 | |||
189 | $link = array( | ||
190 | 'id' => 43, | ||
191 | 'title' => 'an additional link', | ||
192 | 'url' => 'http://dum.my', | ||
193 | 'description' => 'One more', | ||
194 | 'private' => 0, | ||
195 | 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150518_190000'), | ||
196 | 'tags' => 'unit test' | ||
197 | ); | ||
198 | $testDB[$link['id']] = $link; | ||
199 | $testDB->save('tests'); | ||
200 | |||
201 | $testDB = new LinkDB(self::$testDatastore, true, false); | ||
202 | $this->assertEquals($dbSize + 1, sizeof($testDB)); | ||
203 | } | ||
204 | |||
205 | /** | ||
206 | * Count existing links | ||
207 | */ | ||
208 | public function testCount() | ||
209 | { | ||
210 | $this->assertEquals( | ||
211 | self::$refDB->countPublicLinks(), | ||
212 | self::$publicLinkDB->count() | ||
213 | ); | ||
214 | $this->assertEquals( | ||
215 | self::$refDB->countLinks(), | ||
216 | self::$privateLinkDB->count() | ||
217 | ); | ||
218 | } | ||
219 | |||
220 | /** | ||
221 | * Count existing links - public links hidden | ||
222 | */ | ||
223 | public function testCountHiddenPublic() | ||
224 | { | ||
225 | $linkDB = new LinkDB(self::$testDatastore, false, true); | ||
226 | |||
227 | $this->assertEquals( | ||
228 | 0, | ||
229 | $linkDB->count() | ||
230 | ); | ||
231 | $this->assertEquals( | ||
232 | 0, | ||
233 | $linkDB->count() | ||
234 | ); | ||
235 | } | ||
236 | |||
237 | /** | ||
238 | * List the days for which links have been posted | ||
239 | */ | ||
240 | public function testDays() | ||
241 | { | ||
242 | $this->assertEquals( | ||
243 | array('20100309', '20100310', '20121206', '20121207', '20130614', '20150310'), | ||
244 | self::$publicLinkDB->days() | ||
245 | ); | ||
246 | |||
247 | $this->assertEquals( | ||
248 | array('20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'), | ||
249 | self::$privateLinkDB->days() | ||
250 | ); | ||
251 | } | ||
252 | |||
253 | /** | ||
254 | * The URL corresponds to an existing entry in the DB | ||
255 | */ | ||
256 | public function testGetKnownLinkFromURL() | ||
257 | { | ||
258 | $link = self::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/'); | ||
259 | |||
260 | $this->assertNotEquals(false, $link); | ||
261 | $this->assertContains( | ||
262 | 'A free software media publishing platform', | ||
263 | $link['description'] | ||
264 | ); | ||
265 | } | ||
266 | |||
267 | /** | ||
268 | * The URL is not in the DB | ||
269 | */ | ||
270 | public function testGetUnknownLinkFromURL() | ||
271 | { | ||
272 | $this->assertEquals( | ||
273 | false, | ||
274 | self::$publicLinkDB->getLinkFromUrl('http://dev.null') | ||
275 | ); | ||
276 | } | ||
277 | |||
278 | /** | ||
279 | * Lists all tags | ||
280 | */ | ||
281 | public function testAllTags() | ||
282 | { | ||
283 | $this->assertEquals( | ||
284 | array( | ||
285 | 'web' => 3, | ||
286 | 'cartoon' => 2, | ||
287 | 'gnu' => 2, | ||
288 | 'dev' => 1, | ||
289 | 'samba' => 1, | ||
290 | 'media' => 1, | ||
291 | 'software' => 1, | ||
292 | 'stallman' => 1, | ||
293 | 'free' => 1, | ||
294 | '-exclude' => 1, | ||
295 | 'hashtag' => 2, | ||
296 | // The DB contains a link with `sTuff` and another one with `stuff` tag. | ||
297 | // They need to be grouped with the first case found - order by date DESC: `sTuff`. | ||
298 | 'sTuff' => 2, | ||
299 | 'ut' => 1, | ||
300 | ), | ||
301 | self::$publicLinkDB->linksCountPerTag() | ||
302 | ); | ||
303 | |||
304 | $this->assertEquals( | ||
305 | array( | ||
306 | 'web' => 4, | ||
307 | 'cartoon' => 3, | ||
308 | 'gnu' => 2, | ||
309 | 'dev' => 2, | ||
310 | 'samba' => 1, | ||
311 | 'media' => 1, | ||
312 | 'software' => 1, | ||
313 | 'stallman' => 1, | ||
314 | 'free' => 1, | ||
315 | 'html' => 1, | ||
316 | 'w3c' => 1, | ||
317 | 'css' => 1, | ||
318 | 'Mercurial' => 1, | ||
319 | 'sTuff' => 2, | ||
320 | '-exclude' => 1, | ||
321 | '.hidden' => 1, | ||
322 | 'hashtag' => 2, | ||
323 | 'tag1' => 1, | ||
324 | 'tag2' => 1, | ||
325 | 'tag3' => 1, | ||
326 | 'tag4' => 1, | ||
327 | 'ut' => 1, | ||
328 | ), | ||
329 | self::$privateLinkDB->linksCountPerTag() | ||
330 | ); | ||
331 | $this->assertEquals( | ||
332 | array( | ||
333 | 'web' => 4, | ||
334 | 'cartoon' => 2, | ||
335 | 'gnu' => 1, | ||
336 | 'dev' => 1, | ||
337 | 'samba' => 1, | ||
338 | 'media' => 1, | ||
339 | 'html' => 1, | ||
340 | 'w3c' => 1, | ||
341 | 'css' => 1, | ||
342 | 'Mercurial' => 1, | ||
343 | '.hidden' => 1, | ||
344 | 'hashtag' => 1, | ||
345 | ), | ||
346 | self::$privateLinkDB->linksCountPerTag(['web']) | ||
347 | ); | ||
348 | $this->assertEquals( | ||
349 | array( | ||
350 | 'web' => 1, | ||
351 | 'html' => 1, | ||
352 | 'w3c' => 1, | ||
353 | 'css' => 1, | ||
354 | 'Mercurial' => 1, | ||
355 | ), | ||
356 | self::$privateLinkDB->linksCountPerTag(['web'], 'private') | ||
357 | ); | ||
358 | } | ||
359 | |||
360 | /** | ||
361 | * Test filter with string. | ||
362 | */ | ||
363 | public function testFilterString() | ||
364 | { | ||
365 | $tags = 'dev cartoon'; | ||
366 | $request = array('searchtags' => $tags); | ||
367 | $this->assertEquals( | ||
368 | 2, | ||
369 | count(self::$privateLinkDB->filterSearch($request, true, false)) | ||
370 | ); | ||
371 | } | ||
372 | |||
373 | /** | ||
374 | * Test filter with string. | ||
375 | */ | ||
376 | public function testFilterArray() | ||
377 | { | ||
378 | $tags = array('dev', 'cartoon'); | ||
379 | $request = array('searchtags' => $tags); | ||
380 | $this->assertEquals( | ||
381 | 2, | ||
382 | count(self::$privateLinkDB->filterSearch($request, true, false)) | ||
383 | ); | ||
384 | } | ||
385 | |||
386 | /** | ||
387 | * Test hidden tags feature: | ||
388 | * tags starting with a dot '.' are only visible when logged in. | ||
389 | */ | ||
390 | public function testHiddenTags() | ||
391 | { | ||
392 | $tags = '.hidden'; | ||
393 | $request = array('searchtags' => $tags); | ||
394 | $this->assertEquals( | ||
395 | 1, | ||
396 | count(self::$privateLinkDB->filterSearch($request, true, false)) | ||
397 | ); | ||
398 | |||
399 | $this->assertEquals( | ||
400 | 0, | ||
401 | count(self::$publicLinkDB->filterSearch($request, true, false)) | ||
402 | ); | ||
403 | } | ||
404 | |||
405 | /** | ||
406 | * Test filterHash() with a valid smallhash. | ||
407 | */ | ||
408 | public function testFilterHashValid() | ||
409 | { | ||
410 | $request = smallHash('20150310_114651'); | ||
411 | $this->assertEquals( | ||
412 | 1, | ||
413 | count(self::$publicLinkDB->filterHash($request)) | ||
414 | ); | ||
415 | $request = smallHash('20150310_114633' . 8); | ||
416 | $this->assertEquals( | ||
417 | 1, | ||
418 | count(self::$publicLinkDB->filterHash($request)) | ||
419 | ); | ||
420 | } | ||
421 | |||
422 | /** | ||
423 | * Test filterHash() with an invalid smallhash. | ||
424 | * | ||
425 | * @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException | ||
426 | */ | ||
427 | public function testFilterHashInValid1() | ||
428 | { | ||
429 | $request = 'blabla'; | ||
430 | self::$publicLinkDB->filterHash($request); | ||
431 | } | ||
432 | |||
433 | /** | ||
434 | * Test filterHash() with an empty smallhash. | ||
435 | * | ||
436 | * @expectedException \Shaarli\Bookmark\Exception\LinkNotFoundException | ||
437 | */ | ||
438 | public function testFilterHashInValid() | ||
439 | { | ||
440 | self::$publicLinkDB->filterHash(''); | ||
441 | } | ||
442 | |||
443 | /** | ||
444 | * Test reorder with asc/desc parameter. | ||
445 | */ | ||
446 | public function testReorderLinksDesc() | ||
447 | { | ||
448 | self::$privateLinkDB->reorder('ASC'); | ||
449 | $stickyIds = [11, 10]; | ||
450 | $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41]; | ||
451 | $linkIds = array_merge($stickyIds, $standardIds); | ||
452 | $cpt = 0; | ||
453 | foreach (self::$privateLinkDB as $key => $value) { | ||
454 | $this->assertEquals($linkIds[$cpt++], $key); | ||
455 | } | ||
456 | self::$privateLinkDB->reorder('DESC'); | ||
457 | $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds)); | ||
458 | $cpt = 0; | ||
459 | foreach (self::$privateLinkDB as $key => $value) { | ||
460 | $this->assertEquals($linkIds[$cpt++], $key); | ||
461 | } | ||
462 | } | ||
463 | |||
464 | /** | ||
465 | * Test rename tag with a valid value present in multiple links | ||
466 | */ | ||
467 | public function testRenameTagMultiple() | ||
468 | { | ||
469 | self::$refDB->write(self::$testDatastore); | ||
470 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
471 | |||
472 | $res = $linkDB->renameTag('cartoon', 'Taz'); | ||
473 | $this->assertEquals(3, count($res)); | ||
474 | $this->assertContains(' Taz ', $linkDB[4]['tags']); | ||
475 | $this->assertContains(' Taz ', $linkDB[1]['tags']); | ||
476 | $this->assertContains(' Taz ', $linkDB[0]['tags']); | ||
477 | } | ||
478 | |||
479 | /** | ||
480 | * Test rename tag with a valid value | ||
481 | */ | ||
482 | public function testRenameTagCaseSensitive() | ||
483 | { | ||
484 | self::$refDB->write(self::$testDatastore); | ||
485 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
486 | |||
487 | $res = $linkDB->renameTag('sTuff', 'Taz'); | ||
488 | $this->assertEquals(1, count($res)); | ||
489 | $this->assertEquals('Taz', $linkDB[41]['tags']); | ||
490 | } | ||
491 | |||
492 | /** | ||
493 | * Test rename tag with invalid values | ||
494 | */ | ||
495 | public function testRenameTagInvalid() | ||
496 | { | ||
497 | $linkDB = new LinkDB(self::$testDatastore, false, false); | ||
498 | |||
499 | $this->assertFalse($linkDB->renameTag('', 'test')); | ||
500 | $this->assertFalse($linkDB->renameTag('', '')); | ||
501 | // tag non existent | ||
502 | $this->assertEquals([], $linkDB->renameTag('test', '')); | ||
503 | $this->assertEquals([], $linkDB->renameTag('test', 'retest')); | ||
504 | } | ||
505 | |||
506 | /** | ||
507 | * Test delete tag with a valid value | ||
508 | */ | ||
509 | public function testDeleteTag() | ||
510 | { | ||
511 | self::$refDB->write(self::$testDatastore); | ||
512 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
513 | |||
514 | $res = $linkDB->renameTag('cartoon', null); | ||
515 | $this->assertEquals(3, count($res)); | ||
516 | $this->assertNotContains('cartoon', $linkDB[4]['tags']); | ||
517 | } | ||
518 | |||
519 | /** | ||
520 | * Test linksCountPerTag all tags without filter. | ||
521 | * Equal occurrences should be sorted alphabetically. | ||
522 | */ | ||
523 | public function testCountLinkPerTagAllNoFilter() | ||
524 | { | ||
525 | $expected = [ | ||
526 | 'web' => 4, | ||
527 | 'cartoon' => 3, | ||
528 | 'dev' => 2, | ||
529 | 'gnu' => 2, | ||
530 | 'hashtag' => 2, | ||
531 | 'sTuff' => 2, | ||
532 | '-exclude' => 1, | ||
533 | '.hidden' => 1, | ||
534 | 'Mercurial' => 1, | ||
535 | 'css' => 1, | ||
536 | 'free' => 1, | ||
537 | 'html' => 1, | ||
538 | 'media' => 1, | ||
539 | 'samba' => 1, | ||
540 | 'software' => 1, | ||
541 | 'stallman' => 1, | ||
542 | 'tag1' => 1, | ||
543 | 'tag2' => 1, | ||
544 | 'tag3' => 1, | ||
545 | 'tag4' => 1, | ||
546 | 'ut' => 1, | ||
547 | 'w3c' => 1, | ||
548 | ]; | ||
549 | $tags = self::$privateLinkDB->linksCountPerTag(); | ||
550 | |||
551 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
552 | } | ||
553 | |||
554 | /** | ||
555 | * Test linksCountPerTag all tags with filter. | ||
556 | * Equal occurrences should be sorted alphabetically. | ||
557 | */ | ||
558 | public function testCountLinkPerTagAllWithFilter() | ||
559 | { | ||
560 | $expected = [ | ||
561 | 'gnu' => 2, | ||
562 | 'hashtag' => 2, | ||
563 | '-exclude' => 1, | ||
564 | '.hidden' => 1, | ||
565 | 'free' => 1, | ||
566 | 'media' => 1, | ||
567 | 'software' => 1, | ||
568 | 'stallman' => 1, | ||
569 | 'stuff' => 1, | ||
570 | 'web' => 1, | ||
571 | ]; | ||
572 | $tags = self::$privateLinkDB->linksCountPerTag(['gnu']); | ||
573 | |||
574 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
575 | } | ||
576 | |||
577 | /** | ||
578 | * Test linksCountPerTag public tags with filter. | ||
579 | * Equal occurrences should be sorted alphabetically. | ||
580 | */ | ||
581 | public function testCountLinkPerTagPublicWithFilter() | ||
582 | { | ||
583 | $expected = [ | ||
584 | 'gnu' => 2, | ||
585 | 'hashtag' => 2, | ||
586 | '-exclude' => 1, | ||
587 | '.hidden' => 1, | ||
588 | 'free' => 1, | ||
589 | 'media' => 1, | ||
590 | 'software' => 1, | ||
591 | 'stallman' => 1, | ||
592 | 'stuff' => 1, | ||
593 | 'web' => 1, | ||
594 | ]; | ||
595 | $tags = self::$privateLinkDB->linksCountPerTag(['gnu'], 'public'); | ||
596 | |||
597 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
598 | } | ||
599 | |||
600 | /** | ||
601 | * Test linksCountPerTag public tags with filter. | ||
602 | * Equal occurrences should be sorted alphabetically. | ||
603 | */ | ||
604 | public function testCountLinkPerTagPrivateWithFilter() | ||
605 | { | ||
606 | $expected = [ | ||
607 | 'cartoon' => 1, | ||
608 | 'dev' => 1, | ||
609 | 'tag1' => 1, | ||
610 | 'tag2' => 1, | ||
611 | 'tag3' => 1, | ||
612 | 'tag4' => 1, | ||
613 | ]; | ||
614 | $tags = self::$privateLinkDB->linksCountPerTag(['dev'], 'private'); | ||
615 | |||
616 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
617 | } | ||
618 | |||
619 | /** | ||
620 | * Make sure that bookmarks with the same timestamp have a consistent order: | ||
621 | * if their creation date is equal, bookmarks are sorted by ID DESC. | ||
622 | */ | ||
623 | public function testConsistentOrder() | ||
624 | { | ||
625 | $nextId = 43; | ||
626 | $creation = DateTime::createFromFormat('Ymd_His', '20190807_130444'); | ||
627 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
628 | for ($i = 0; $i < 4; ++$i) { | ||
629 | $linkDB[$nextId + $i] = [ | ||
630 | 'id' => $nextId + $i, | ||
631 | 'url' => 'http://'. $i, | ||
632 | 'created' => $creation, | ||
633 | 'title' => true, | ||
634 | 'description' => true, | ||
635 | 'tags' => true, | ||
636 | ]; | ||
637 | } | ||
638 | |||
639 | // Check 4 new links 4 times | ||
640 | for ($i = 0; $i < 4; ++$i) { | ||
641 | $linkDB->save('tests'); | ||
642 | $linkDB = new LinkDB(self::$testDatastore, true, false); | ||
643 | $count = 3; | ||
644 | foreach ($linkDB as $link) { | ||
645 | if ($link['sticky'] === true) { | ||
646 | continue; | ||
647 | } | ||
648 | $this->assertEquals($nextId + $count, $link['id']); | ||
649 | $this->assertEquals('http://'. $count, $link['url']); | ||
650 | if (--$count < 0) { | ||
651 | break; | ||
652 | } | ||
653 | } | ||
654 | } | ||
655 | } | ||
656 | } | ||
diff --git a/tests/bookmark/LinkUtilsTest.php b/tests/bookmark/LinkUtilsTest.php index 78cb8f2a..591976f2 100644 --- a/tests/bookmark/LinkUtilsTest.php +++ b/tests/bookmark/LinkUtilsTest.php | |||
@@ -389,15 +389,6 @@ class LinkUtilsTest extends TestCase | |||
389 | } | 389 | } |
390 | 390 | ||
391 | /** | 391 | /** |
392 | * Test count_private. | ||
393 | */ | ||
394 | public function testCountPrivateLinks() | ||
395 | { | ||
396 | $refDB = new ReferenceLinkDB(); | ||
397 | $this->assertEquals($refDB->countPrivateLinks(), count_private($refDB->getLinks())); | ||
398 | } | ||
399 | |||
400 | /** | ||
401 | * Test text2clickable. | 392 | * Test text2clickable. |
402 | */ | 393 | */ |
403 | public function testText2clickable() | 394 | public function testText2clickable() |