diff options
Diffstat (limited to 'tests/legacy')
-rw-r--r-- | tests/legacy/LegacyDummyUpdater.php | 74 | ||||
-rw-r--r-- | tests/legacy/LegacyLinkDBTest.php | 656 | ||||
-rw-r--r-- | tests/legacy/LegacyLinkFilterTest.php | 509 | ||||
-rw-r--r-- | tests/legacy/LegacyUpdaterTest.php | 886 |
4 files changed, 2125 insertions, 0 deletions
diff --git a/tests/legacy/LegacyDummyUpdater.php b/tests/legacy/LegacyDummyUpdater.php new file mode 100644 index 00000000..10e0a5b7 --- /dev/null +++ b/tests/legacy/LegacyDummyUpdater.php | |||
@@ -0,0 +1,74 @@ | |||
1 | <?php | ||
2 | namespace Shaarli\Updater; | ||
3 | |||
4 | use Exception; | ||
5 | use ReflectionClass; | ||
6 | use ReflectionMethod; | ||
7 | use Shaarli\Config\ConfigManager; | ||
8 | use Shaarli\Legacy\LegacyLinkDB; | ||
9 | use Shaarli\Legacy\LegacyUpdater; | ||
10 | |||
11 | /** | ||
12 | * Class LegacyDummyUpdater. | ||
13 | * Extends updater to add update method designed for unit tests. | ||
14 | */ | ||
15 | class LegacyDummyUpdater extends LegacyUpdater | ||
16 | { | ||
17 | /** | ||
18 | * Object constructor. | ||
19 | * | ||
20 | * @param array $doneUpdates Updates which are already done. | ||
21 | * @param LegacyLinkDB $linkDB LinkDB instance. | ||
22 | * @param ConfigManager $conf Configuration Manager instance. | ||
23 | * @param boolean $isLoggedIn True if the user is logged in. | ||
24 | */ | ||
25 | public function __construct($doneUpdates, $linkDB, $conf, $isLoggedIn) | ||
26 | { | ||
27 | parent::__construct($doneUpdates, $linkDB, $conf, $isLoggedIn); | ||
28 | |||
29 | // Retrieve all update methods. | ||
30 | // For unit test, only retrieve final methods, | ||
31 | $class = new ReflectionClass($this); | ||
32 | $this->methods = $class->getMethods(ReflectionMethod::IS_FINAL); | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * Update method 1. | ||
37 | * | ||
38 | * @return bool true. | ||
39 | */ | ||
40 | final private function updateMethodDummy1() | ||
41 | { | ||
42 | return true; | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * Update method 2. | ||
47 | * | ||
48 | * @return bool true. | ||
49 | */ | ||
50 | final private function updateMethodDummy2() | ||
51 | { | ||
52 | return true; | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * Update method 3. | ||
57 | * | ||
58 | * @return bool true. | ||
59 | */ | ||
60 | final private function updateMethodDummy3() | ||
61 | { | ||
62 | return true; | ||
63 | } | ||
64 | |||
65 | /** | ||
66 | * Update method 4, raise an exception. | ||
67 | * | ||
68 | * @throws Exception error. | ||
69 | */ | ||
70 | final private function updateMethodException() | ||
71 | { | ||
72 | throw new Exception('whatever'); | ||
73 | } | ||
74 | } | ||
diff --git a/tests/legacy/LegacyLinkDBTest.php b/tests/legacy/LegacyLinkDBTest.php new file mode 100644 index 00000000..17b2b0e6 --- /dev/null +++ b/tests/legacy/LegacyLinkDBTest.php | |||
@@ -0,0 +1,656 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Link datastore tests | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Legacy; | ||
7 | |||
8 | use DateTime; | ||
9 | use ReferenceLinkDB; | ||
10 | use ReflectionClass; | ||
11 | use Shaarli; | ||
12 | use Shaarli\Bookmark\Bookmark; | ||
13 | |||
14 | require_once 'application/feed/Cache.php'; | ||
15 | require_once 'application/Utils.php'; | ||
16 | require_once 'tests/utils/ReferenceLinkDB.php'; | ||
17 | |||
18 | |||
19 | /** | ||
20 | * Unitary tests for LegacyLinkDBTest | ||
21 | */ | ||
22 | class LegacyLinkDBTest extends \PHPUnit\Framework\TestCase | ||
23 | { | ||
24 | // datastore to test write operations | ||
25 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
26 | |||
27 | /** | ||
28 | * @var ReferenceLinkDB instance. | ||
29 | */ | ||
30 | protected static $refDB = null; | ||
31 | |||
32 | /** | ||
33 | * @var LegacyLinkDB public LinkDB instance. | ||
34 | */ | ||
35 | protected static $publicLinkDB = null; | ||
36 | |||
37 | /** | ||
38 | * @var LegacyLinkDB private LinkDB instance. | ||
39 | */ | ||
40 | protected static $privateLinkDB = null; | ||
41 | |||
42 | /** | ||
43 | * Instantiates public and private LinkDBs with test data | ||
44 | * | ||
45 | * The reference datastore contains public and private bookmarks that | ||
46 | * will be used to test LinkDB's methods: | ||
47 | * - access filtering (public/private), | ||
48 | * - link searches: | ||
49 | * - by day, | ||
50 | * - by tag, | ||
51 | * - by text, | ||
52 | * - etc. | ||
53 | * | ||
54 | * Resets test data for each test | ||
55 | */ | ||
56 | protected function setUp() | ||
57 | { | ||
58 | if (file_exists(self::$testDatastore)) { | ||
59 | unlink(self::$testDatastore); | ||
60 | } | ||
61 | |||
62 | self::$refDB = new ReferenceLinkDB(true); | ||
63 | self::$refDB->write(self::$testDatastore); | ||
64 | self::$publicLinkDB = new LegacyLinkDB(self::$testDatastore, false, false); | ||
65 | self::$privateLinkDB = new LegacyLinkDB(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\Legacy\LegacyLinkDB'); | ||
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 LegacyLinkDB(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 LegacyLinkDB(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 LegacyLinkDB('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 LegacyLinkDB(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 LegacyLinkDB(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 LegacyLinkDB(self::$testDatastore, false, false); | ||
155 | $this->assertEquals(0, sizeof($emptyDB)); | ||
156 | $this->assertEquals(0, count($emptyDB)); | ||
157 | } | ||
158 | |||
159 | /** | ||
160 | * Load public bookmarks 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 bookmarks 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 bookmarks to the DB | ||
183 | */ | ||
184 | public function testSave() | ||
185 | { | ||
186 | $testDB = new LegacyLinkDB(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(Bookmark::LINK_DATE_FORMAT, '20150518_190000'), | ||
196 | 'tags' => 'unit test' | ||
197 | ); | ||
198 | $testDB[$link['id']] = $link; | ||
199 | $testDB->save('tests'); | ||
200 | |||
201 | $testDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
202 | $this->assertEquals($dbSize + 1, sizeof($testDB)); | ||
203 | } | ||
204 | |||
205 | /** | ||
206 | * Count existing bookmarks | ||
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 bookmarks - public bookmarks hidden | ||
222 | */ | ||
223 | public function testCountHiddenPublic() | ||
224 | { | ||
225 | $linkDB = new LegacyLinkDB(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 bookmarks 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\BookmarkNotFoundException | ||
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\BookmarkNotFoundException | ||
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 bookmarks | ||
466 | */ | ||
467 | public function testRenameTagMultiple() | ||
468 | { | ||
469 | self::$refDB->write(self::$testDatastore); | ||
470 | $linkDB = new LegacyLinkDB(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 LegacyLinkDB(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 LegacyLinkDB(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 LegacyLinkDB(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 LegacyLinkDB(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 LegacyLinkDB(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/legacy/LegacyLinkFilterTest.php b/tests/legacy/LegacyLinkFilterTest.php new file mode 100644 index 00000000..ba9ec529 --- /dev/null +++ b/tests/legacy/LegacyLinkFilterTest.php | |||
@@ -0,0 +1,509 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Bookmark; | ||
4 | |||
5 | use Exception; | ||
6 | use ReferenceLinkDB; | ||
7 | use Shaarli\Legacy\LegacyLinkDB; | ||
8 | use Shaarli\Legacy\LegacyLinkFilter; | ||
9 | |||
10 | /** | ||
11 | * Class LegacyLinkFilterTest. | ||
12 | */ | ||
13 | class LegacyLinkFilterTest extends \PHPUnit\Framework\TestCase | ||
14 | { | ||
15 | /** | ||
16 | * @var string Test datastore path. | ||
17 | */ | ||
18 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
19 | /** | ||
20 | * @var BookmarkFilter instance. | ||
21 | */ | ||
22 | protected static $linkFilter; | ||
23 | |||
24 | /** | ||
25 | * @var ReferenceLinkDB instance | ||
26 | */ | ||
27 | protected static $refDB; | ||
28 | |||
29 | /** | ||
30 | * @var LegacyLinkDB instance | ||
31 | */ | ||
32 | protected static $linkDB; | ||
33 | |||
34 | /** | ||
35 | * Instantiate linkFilter with ReferenceLinkDB data. | ||
36 | */ | ||
37 | public static function setUpBeforeClass() | ||
38 | { | ||
39 | self::$refDB = new ReferenceLinkDB(true); | ||
40 | self::$refDB->write(self::$testDatastore); | ||
41 | self::$linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
42 | self::$linkFilter = new LegacyLinkFilter(self::$linkDB); | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * Blank filter. | ||
47 | */ | ||
48 | public function testFilter() | ||
49 | { | ||
50 | $this->assertEquals( | ||
51 | self::$refDB->countLinks(), | ||
52 | count(self::$linkFilter->filter('', '')) | ||
53 | ); | ||
54 | |||
55 | $this->assertEquals( | ||
56 | self::$refDB->countLinks(), | ||
57 | count(self::$linkFilter->filter('', '', 'all')) | ||
58 | ); | ||
59 | |||
60 | $this->assertEquals( | ||
61 | self::$refDB->countLinks(), | ||
62 | count(self::$linkFilter->filter('', '', 'randomstr')) | ||
63 | ); | ||
64 | |||
65 | // Private only. | ||
66 | $this->assertEquals( | ||
67 | self::$refDB->countPrivateLinks(), | ||
68 | count(self::$linkFilter->filter('', '', false, 'private')) | ||
69 | ); | ||
70 | |||
71 | // Public only. | ||
72 | $this->assertEquals( | ||
73 | self::$refDB->countPublicLinks(), | ||
74 | count(self::$linkFilter->filter('', '', false, 'public')) | ||
75 | ); | ||
76 | |||
77 | $this->assertEquals( | ||
78 | ReferenceLinkDB::$NB_LINKS_TOTAL, | ||
79 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, '')) | ||
80 | ); | ||
81 | |||
82 | $this->assertEquals( | ||
83 | self::$refDB->countUntaggedLinks(), | ||
84 | count( | ||
85 | self::$linkFilter->filter( | ||
86 | LegacyLinkFilter::$FILTER_TAG, | ||
87 | /*$request=*/ | ||
88 | '', | ||
89 | /*$casesensitive=*/ | ||
90 | false, | ||
91 | /*$visibility=*/ | ||
92 | 'all', | ||
93 | /*$untaggedonly=*/ | ||
94 | true | ||
95 | ) | ||
96 | ) | ||
97 | ); | ||
98 | |||
99 | $this->assertEquals( | ||
100 | ReferenceLinkDB::$NB_LINKS_TOTAL, | ||
101 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, '')) | ||
102 | ); | ||
103 | } | ||
104 | |||
105 | /** | ||
106 | * Filter bookmarks using a tag | ||
107 | */ | ||
108 | public function testFilterOneTag() | ||
109 | { | ||
110 | $this->assertEquals( | ||
111 | 4, | ||
112 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false)) | ||
113 | ); | ||
114 | |||
115 | $this->assertEquals( | ||
116 | 4, | ||
117 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'all')) | ||
118 | ); | ||
119 | |||
120 | $this->assertEquals( | ||
121 | 4, | ||
122 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'default-blabla')) | ||
123 | ); | ||
124 | |||
125 | // Private only. | ||
126 | $this->assertEquals( | ||
127 | 1, | ||
128 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'private')) | ||
129 | ); | ||
130 | |||
131 | // Public only. | ||
132 | $this->assertEquals( | ||
133 | 3, | ||
134 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'web', false, 'public')) | ||
135 | ); | ||
136 | } | ||
137 | |||
138 | /** | ||
139 | * Filter bookmarks using a tag - case-sensitive | ||
140 | */ | ||
141 | public function testFilterCaseSensitiveTag() | ||
142 | { | ||
143 | $this->assertEquals( | ||
144 | 0, | ||
145 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'mercurial', true)) | ||
146 | ); | ||
147 | |||
148 | $this->assertEquals( | ||
149 | 1, | ||
150 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'Mercurial', true)) | ||
151 | ); | ||
152 | } | ||
153 | |||
154 | /** | ||
155 | * Filter bookmarks using a tag combination | ||
156 | */ | ||
157 | public function testFilterMultipleTags() | ||
158 | { | ||
159 | $this->assertEquals( | ||
160 | 2, | ||
161 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'dev cartoon', false)) | ||
162 | ); | ||
163 | } | ||
164 | |||
165 | /** | ||
166 | * Filter bookmarks using a non-existent tag | ||
167 | */ | ||
168 | public function testFilterUnknownTag() | ||
169 | { | ||
170 | $this->assertEquals( | ||
171 | 0, | ||
172 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'null', false)) | ||
173 | ); | ||
174 | } | ||
175 | |||
176 | /** | ||
177 | * Return bookmarks for a given day | ||
178 | */ | ||
179 | public function testFilterDay() | ||
180 | { | ||
181 | $this->assertEquals( | ||
182 | 4, | ||
183 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '20121206')) | ||
184 | ); | ||
185 | } | ||
186 | |||
187 | /** | ||
188 | * 404 - day not found | ||
189 | */ | ||
190 | public function testFilterUnknownDay() | ||
191 | { | ||
192 | $this->assertEquals( | ||
193 | 0, | ||
194 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '19700101')) | ||
195 | ); | ||
196 | } | ||
197 | |||
198 | /** | ||
199 | * Use an invalid date format | ||
200 | * @expectedException Exception | ||
201 | * @expectedExceptionMessageRegExp /Invalid date format/ | ||
202 | */ | ||
203 | public function testFilterInvalidDayWithChars() | ||
204 | { | ||
205 | self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, 'Rainy day, dream away'); | ||
206 | } | ||
207 | |||
208 | /** | ||
209 | * Use an invalid date format | ||
210 | * @expectedException Exception | ||
211 | * @expectedExceptionMessageRegExp /Invalid date format/ | ||
212 | */ | ||
213 | public function testFilterInvalidDayDigits() | ||
214 | { | ||
215 | self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '20'); | ||
216 | } | ||
217 | |||
218 | /** | ||
219 | * Retrieve a link entry with its hash | ||
220 | */ | ||
221 | public function testFilterSmallHash() | ||
222 | { | ||
223 | $links = self::$linkFilter->filter(LegacyLinkFilter::$FILTER_HASH, 'IuWvgA'); | ||
224 | |||
225 | $this->assertEquals( | ||
226 | 1, | ||
227 | count($links) | ||
228 | ); | ||
229 | |||
230 | $this->assertEquals( | ||
231 | 'MediaGoblin', | ||
232 | $links[7]['title'] | ||
233 | ); | ||
234 | } | ||
235 | |||
236 | /** | ||
237 | * No link for this hash | ||
238 | * | ||
239 | * @expectedException \Shaarli\Bookmark\Exception\BookmarkNotFoundException | ||
240 | */ | ||
241 | public function testFilterUnknownSmallHash() | ||
242 | { | ||
243 | self::$linkFilter->filter(LegacyLinkFilter::$FILTER_HASH, 'Iblaah'); | ||
244 | } | ||
245 | |||
246 | /** | ||
247 | * Full-text search - no result found. | ||
248 | */ | ||
249 | public function testFilterFullTextNoResult() | ||
250 | { | ||
251 | $this->assertEquals( | ||
252 | 0, | ||
253 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'azertyuiop')) | ||
254 | ); | ||
255 | } | ||
256 | |||
257 | /** | ||
258 | * Full-text search - result from a link's URL | ||
259 | */ | ||
260 | public function testFilterFullTextURL() | ||
261 | { | ||
262 | $this->assertEquals( | ||
263 | 2, | ||
264 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'ars.userfriendly.org')) | ||
265 | ); | ||
266 | |||
267 | $this->assertEquals( | ||
268 | 2, | ||
269 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'ars org')) | ||
270 | ); | ||
271 | } | ||
272 | |||
273 | /** | ||
274 | * Full-text search - result from a link's title only | ||
275 | */ | ||
276 | public function testFilterFullTextTitle() | ||
277 | { | ||
278 | // use miscellaneous cases | ||
279 | $this->assertEquals( | ||
280 | 2, | ||
281 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'userfriendly -')) | ||
282 | ); | ||
283 | $this->assertEquals( | ||
284 | 2, | ||
285 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'UserFriendly -')) | ||
286 | ); | ||
287 | $this->assertEquals( | ||
288 | 2, | ||
289 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'uSeRFrIendlY -')) | ||
290 | ); | ||
291 | |||
292 | // use miscellaneous case and offset | ||
293 | $this->assertEquals( | ||
294 | 2, | ||
295 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'RFrIendL')) | ||
296 | ); | ||
297 | } | ||
298 | |||
299 | /** | ||
300 | * Full-text search - result from the link's description only | ||
301 | */ | ||
302 | public function testFilterFullTextDescription() | ||
303 | { | ||
304 | $this->assertEquals( | ||
305 | 1, | ||
306 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'publishing media')) | ||
307 | ); | ||
308 | |||
309 | $this->assertEquals( | ||
310 | 1, | ||
311 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'mercurial w3c')) | ||
312 | ); | ||
313 | |||
314 | $this->assertEquals( | ||
315 | 3, | ||
316 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, '"free software"')) | ||
317 | ); | ||
318 | } | ||
319 | |||
320 | /** | ||
321 | * Full-text search - result from the link's tags only | ||
322 | */ | ||
323 | public function testFilterFullTextTags() | ||
324 | { | ||
325 | $this->assertEquals( | ||
326 | 6, | ||
327 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web')) | ||
328 | ); | ||
329 | |||
330 | $this->assertEquals( | ||
331 | 6, | ||
332 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', 'all')) | ||
333 | ); | ||
334 | |||
335 | $this->assertEquals( | ||
336 | 6, | ||
337 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', 'bla')) | ||
338 | ); | ||
339 | |||
340 | // Private only. | ||
341 | $this->assertEquals( | ||
342 | 1, | ||
343 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', false, 'private')) | ||
344 | ); | ||
345 | |||
346 | // Public only. | ||
347 | $this->assertEquals( | ||
348 | 5, | ||
349 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', false, 'public')) | ||
350 | ); | ||
351 | } | ||
352 | |||
353 | /** | ||
354 | * Full-text search - result set from mixed sources | ||
355 | */ | ||
356 | public function testFilterFullTextMixed() | ||
357 | { | ||
358 | $this->assertEquals( | ||
359 | 3, | ||
360 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'free software')) | ||
361 | ); | ||
362 | } | ||
363 | |||
364 | /** | ||
365 | * Full-text search - test exclusion with '-'. | ||
366 | */ | ||
367 | public function testExcludeSearch() | ||
368 | { | ||
369 | $this->assertEquals( | ||
370 | 1, | ||
371 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'free -gnu')) | ||
372 | ); | ||
373 | |||
374 | $this->assertEquals( | ||
375 | ReferenceLinkDB::$NB_LINKS_TOTAL - 1, | ||
376 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, '-revolution')) | ||
377 | ); | ||
378 | } | ||
379 | |||
380 | /** | ||
381 | * Full-text search - test AND, exact terms and exclusion combined, across fields. | ||
382 | */ | ||
383 | public function testMultiSearch() | ||
384 | { | ||
385 | $this->assertEquals( | ||
386 | 2, | ||
387 | count(self::$linkFilter->filter( | ||
388 | LegacyLinkFilter::$FILTER_TEXT, | ||
389 | '"Free Software " stallman "read this" @website stuff' | ||
390 | )) | ||
391 | ); | ||
392 | |||
393 | $this->assertEquals( | ||
394 | 1, | ||
395 | count(self::$linkFilter->filter( | ||
396 | LegacyLinkFilter::$FILTER_TEXT, | ||
397 | '"free software " stallman "read this" -beard @website stuff' | ||
398 | )) | ||
399 | ); | ||
400 | } | ||
401 | |||
402 | /** | ||
403 | * Full-text search - make sure that exact search won't work across fields. | ||
404 | */ | ||
405 | public function testSearchExactTermMultiFieldsKo() | ||
406 | { | ||
407 | $this->assertEquals( | ||
408 | 0, | ||
409 | count(self::$linkFilter->filter( | ||
410 | LegacyLinkFilter::$FILTER_TEXT, | ||
411 | '"designer naming"' | ||
412 | )) | ||
413 | ); | ||
414 | |||
415 | $this->assertEquals( | ||
416 | 0, | ||
417 | count(self::$linkFilter->filter( | ||
418 | LegacyLinkFilter::$FILTER_TEXT, | ||
419 | '"designernaming"' | ||
420 | )) | ||
421 | ); | ||
422 | } | ||
423 | |||
424 | /** | ||
425 | * Tag search with exclusion. | ||
426 | */ | ||
427 | public function testTagFilterWithExclusion() | ||
428 | { | ||
429 | $this->assertEquals( | ||
430 | 1, | ||
431 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'gnu -free')) | ||
432 | ); | ||
433 | |||
434 | $this->assertEquals( | ||
435 | ReferenceLinkDB::$NB_LINKS_TOTAL - 1, | ||
436 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, '-free')) | ||
437 | ); | ||
438 | } | ||
439 | |||
440 | /** | ||
441 | * Test crossed search (terms + tags). | ||
442 | */ | ||
443 | public function testFilterCrossedSearch() | ||
444 | { | ||
445 | $terms = '"Free Software " stallman "read this" @website stuff'; | ||
446 | $tags = 'free'; | ||
447 | $this->assertEquals( | ||
448 | 1, | ||
449 | count(self::$linkFilter->filter( | ||
450 | LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, | ||
451 | array($tags, $terms) | ||
452 | )) | ||
453 | ); | ||
454 | $this->assertEquals( | ||
455 | 2, | ||
456 | count(self::$linkFilter->filter( | ||
457 | LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, | ||
458 | array('', $terms) | ||
459 | )) | ||
460 | ); | ||
461 | $this->assertEquals( | ||
462 | 1, | ||
463 | count(self::$linkFilter->filter( | ||
464 | LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, | ||
465 | array(false, 'PSR-2') | ||
466 | )) | ||
467 | ); | ||
468 | $this->assertEquals( | ||
469 | 1, | ||
470 | count(self::$linkFilter->filter( | ||
471 | LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, | ||
472 | array($tags, '') | ||
473 | )) | ||
474 | ); | ||
475 | $this->assertEquals( | ||
476 | ReferenceLinkDB::$NB_LINKS_TOTAL, | ||
477 | count(self::$linkFilter->filter( | ||
478 | LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, | ||
479 | '' | ||
480 | )) | ||
481 | ); | ||
482 | } | ||
483 | |||
484 | /** | ||
485 | * Filter bookmarks by #hashtag. | ||
486 | */ | ||
487 | public function testFilterByHashtag() | ||
488 | { | ||
489 | $hashtag = 'hashtag'; | ||
490 | $this->assertEquals( | ||
491 | 3, | ||
492 | count(self::$linkFilter->filter( | ||
493 | LegacyLinkFilter::$FILTER_TAG, | ||
494 | $hashtag | ||
495 | )) | ||
496 | ); | ||
497 | |||
498 | $hashtag = 'private'; | ||
499 | $this->assertEquals( | ||
500 | 1, | ||
501 | count(self::$linkFilter->filter( | ||
502 | LegacyLinkFilter::$FILTER_TAG, | ||
503 | $hashtag, | ||
504 | false, | ||
505 | 'private' | ||
506 | )) | ||
507 | ); | ||
508 | } | ||
509 | } | ||
diff --git a/tests/legacy/LegacyUpdaterTest.php b/tests/legacy/LegacyUpdaterTest.php new file mode 100644 index 00000000..7c429811 --- /dev/null +++ b/tests/legacy/LegacyUpdaterTest.php | |||
@@ -0,0 +1,886 @@ | |||
1 | <?php | ||
2 | namespace Shaarli\Updater; | ||
3 | |||
4 | use DateTime; | ||
5 | use Exception; | ||
6 | use Shaarli\Bookmark\Bookmark; | ||
7 | use Shaarli\Config\ConfigJson; | ||
8 | use Shaarli\Config\ConfigManager; | ||
9 | use Shaarli\Config\ConfigPhp; | ||
10 | use Shaarli\Legacy\LegacyLinkDB; | ||
11 | use Shaarli\Legacy\LegacyUpdater; | ||
12 | use Shaarli\Thumbnailer; | ||
13 | |||
14 | require_once 'application/updater/UpdaterUtils.php'; | ||
15 | require_once 'tests/updater/DummyUpdater.php'; | ||
16 | require_once 'tests/utils/ReferenceLinkDB.php'; | ||
17 | require_once 'inc/rain.tpl.class.php'; | ||
18 | |||
19 | /** | ||
20 | * Class UpdaterTest. | ||
21 | * Runs unit tests against the updater class. | ||
22 | */ | ||
23 | class LegacyUpdaterTest extends \PHPUnit\Framework\TestCase | ||
24 | { | ||
25 | /** | ||
26 | * @var string Path to test datastore. | ||
27 | */ | ||
28 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
29 | |||
30 | /** | ||
31 | * @var string Config file path (without extension). | ||
32 | */ | ||
33 | protected static $configFile = 'sandbox/config'; | ||
34 | |||
35 | /** | ||
36 | * @var ConfigManager | ||
37 | */ | ||
38 | protected $conf; | ||
39 | |||
40 | /** | ||
41 | * Executed before each test. | ||
42 | */ | ||
43 | public function setUp() | ||
44 | { | ||
45 | copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); | ||
46 | $this->conf = new ConfigManager(self::$configFile); | ||
47 | } | ||
48 | |||
49 | /** | ||
50 | * Test UpdaterUtils::read_updates_file with an empty/missing file. | ||
51 | */ | ||
52 | public function testReadEmptyUpdatesFile() | ||
53 | { | ||
54 | $this->assertEquals(array(), UpdaterUtils::read_updates_file('')); | ||
55 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | ||
56 | touch($updatesFile); | ||
57 | $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile)); | ||
58 | unlink($updatesFile); | ||
59 | } | ||
60 | |||
61 | /** | ||
62 | * Test read/write updates file. | ||
63 | */ | ||
64 | public function testReadWriteUpdatesFile() | ||
65 | { | ||
66 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | ||
67 | $updatesMethods = array('m1', 'm2', 'm3'); | ||
68 | |||
69 | UpdaterUtils::write_updates_file($updatesFile, $updatesMethods); | ||
70 | $readMethods = UpdaterUtils::read_updates_file($updatesFile); | ||
71 | $this->assertEquals($readMethods, $updatesMethods); | ||
72 | |||
73 | // Update | ||
74 | $updatesMethods[] = 'm4'; | ||
75 | UpdaterUtils::write_updates_file($updatesFile, $updatesMethods); | ||
76 | $readMethods = UpdaterUtils::read_updates_file($updatesFile); | ||
77 | $this->assertEquals($readMethods, $updatesMethods); | ||
78 | unlink($updatesFile); | ||
79 | } | ||
80 | |||
81 | /** | ||
82 | * Test errors in UpdaterUtils::write_updates_file(): empty updates file. | ||
83 | * | ||
84 | * @expectedException Exception | ||
85 | * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ | ||
86 | */ | ||
87 | public function testWriteEmptyUpdatesFile() | ||
88 | { | ||
89 | UpdaterUtils::write_updates_file('', array('test')); | ||
90 | } | ||
91 | |||
92 | /** | ||
93 | * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. | ||
94 | * | ||
95 | * @expectedException Exception | ||
96 | * @expectedExceptionMessageRegExp /Unable to write(.*)/ | ||
97 | */ | ||
98 | public function testWriteUpdatesFileNotWritable() | ||
99 | { | ||
100 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | ||
101 | touch($updatesFile); | ||
102 | chmod($updatesFile, 0444); | ||
103 | try { | ||
104 | @UpdaterUtils::write_updates_file($updatesFile, array('test')); | ||
105 | } catch (Exception $e) { | ||
106 | unlink($updatesFile); | ||
107 | throw $e; | ||
108 | } | ||
109 | } | ||
110 | |||
111 | /** | ||
112 | * Test the update() method, with no update to run. | ||
113 | * 1. Everything already run. | ||
114 | * 2. User is logged out. | ||
115 | */ | ||
116 | public function testNoUpdates() | ||
117 | { | ||
118 | $updates = array( | ||
119 | 'updateMethodDummy1', | ||
120 | 'updateMethodDummy2', | ||
121 | 'updateMethodDummy3', | ||
122 | 'updateMethodException', | ||
123 | ); | ||
124 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | ||
125 | $this->assertEquals(array(), $updater->update()); | ||
126 | |||
127 | $updater = new DummyUpdater(array(), array(), $this->conf, false); | ||
128 | $this->assertEquals(array(), $updater->update()); | ||
129 | } | ||
130 | |||
131 | /** | ||
132 | * Test the update() method, with all updates to run (except the failing one). | ||
133 | */ | ||
134 | public function testUpdatesFirstTime() | ||
135 | { | ||
136 | $updates = array('updateMethodException',); | ||
137 | $expectedUpdates = array( | ||
138 | 'updateMethodDummy1', | ||
139 | 'updateMethodDummy2', | ||
140 | 'updateMethodDummy3', | ||
141 | ); | ||
142 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | ||
143 | $this->assertEquals($expectedUpdates, $updater->update()); | ||
144 | } | ||
145 | |||
146 | /** | ||
147 | * Test the update() method, only one update to run. | ||
148 | */ | ||
149 | public function testOneUpdate() | ||
150 | { | ||
151 | $updates = array( | ||
152 | 'updateMethodDummy1', | ||
153 | 'updateMethodDummy3', | ||
154 | 'updateMethodException', | ||
155 | ); | ||
156 | $expectedUpdate = array('updateMethodDummy2'); | ||
157 | |||
158 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | ||
159 | $this->assertEquals($expectedUpdate, $updater->update()); | ||
160 | } | ||
161 | |||
162 | /** | ||
163 | * Test Update failed. | ||
164 | * | ||
165 | * @expectedException \Exception | ||
166 | */ | ||
167 | public function testUpdateFailed() | ||
168 | { | ||
169 | $updates = array( | ||
170 | 'updateMethodDummy1', | ||
171 | 'updateMethodDummy2', | ||
172 | 'updateMethodDummy3', | ||
173 | ); | ||
174 | |||
175 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | ||
176 | $updater->update(); | ||
177 | } | ||
178 | |||
179 | /** | ||
180 | * Test update mergeDeprecatedConfig: | ||
181 | * 1. init a config file. | ||
182 | * 2. init a options.php file with update value. | ||
183 | * 3. merge. | ||
184 | * 4. check updated value in config file. | ||
185 | */ | ||
186 | public function testUpdateMergeDeprecatedConfig() | ||
187 | { | ||
188 | $this->conf->setConfigFile('tests/utils/config/configPhp'); | ||
189 | $this->conf->reset(); | ||
190 | |||
191 | $optionsFile = 'tests/updater/options.php'; | ||
192 | $options = '<?php | ||
193 | $GLOBALS[\'privateLinkByDefault\'] = true;'; | ||
194 | file_put_contents($optionsFile, $options); | ||
195 | |||
196 | // tmp config file. | ||
197 | $this->conf->setConfigFile('tests/updater/config'); | ||
198 | |||
199 | // merge configs | ||
200 | $updater = new LegacyUpdater(array(), array(), $this->conf, true); | ||
201 | // This writes a new config file in tests/updater/config.php | ||
202 | $updater->updateMethodMergeDeprecatedConfigFile(); | ||
203 | |||
204 | // make sure updated field is changed | ||
205 | $this->conf->reload(); | ||
206 | $this->assertTrue($this->conf->get('privacy.default_private_links')); | ||
207 | $this->assertFalse(is_file($optionsFile)); | ||
208 | // Delete the generated file. | ||
209 | unlink($this->conf->getConfigFileExt()); | ||
210 | } | ||
211 | |||
212 | /** | ||
213 | * Test mergeDeprecatedConfig in without options file. | ||
214 | */ | ||
215 | public function testMergeDeprecatedConfigNoFile() | ||
216 | { | ||
217 | $updater = new LegacyUpdater(array(), array(), $this->conf, true); | ||
218 | $updater->updateMethodMergeDeprecatedConfigFile(); | ||
219 | |||
220 | $this->assertEquals('root', $this->conf->get('credentials.login')); | ||
221 | } | ||
222 | |||
223 | /** | ||
224 | * Test renameDashTags update method. | ||
225 | */ | ||
226 | public function testRenameDashTags() | ||
227 | { | ||
228 | $refDB = new \ReferenceLinkDB(true); | ||
229 | $refDB->write(self::$testDatastore); | ||
230 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
231 | |||
232 | $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); | ||
233 | $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true); | ||
234 | $updater->updateMethodRenameDashTags(); | ||
235 | $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); | ||
236 | } | ||
237 | |||
238 | /** | ||
239 | * Convert old PHP config file to JSON config. | ||
240 | */ | ||
241 | public function testConfigToJson() | ||
242 | { | ||
243 | $configFile = 'tests/utils/config/configPhp'; | ||
244 | $this->conf->setConfigFile($configFile); | ||
245 | $this->conf->reset(); | ||
246 | |||
247 | // The ConfigIO is initialized with ConfigPhp. | ||
248 | $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp); | ||
249 | |||
250 | $updater = new LegacyUpdater(array(), array(), $this->conf, false); | ||
251 | $done = $updater->updateMethodConfigToJson(); | ||
252 | $this->assertTrue($done); | ||
253 | |||
254 | // The ConfigIO has been updated to ConfigJson. | ||
255 | $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson); | ||
256 | $this->assertTrue(file_exists($this->conf->getConfigFileExt())); | ||
257 | |||
258 | // Check JSON config data. | ||
259 | $this->conf->reload(); | ||
260 | $this->assertEquals('root', $this->conf->get('credentials.login')); | ||
261 | $this->assertEquals('lala', $this->conf->get('redirector.url')); | ||
262 | $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore')); | ||
263 | $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION')); | ||
264 | |||
265 | rename($configFile . '.save.php', $configFile . '.php'); | ||
266 | unlink($this->conf->getConfigFileExt()); | ||
267 | } | ||
268 | |||
269 | /** | ||
270 | * Launch config conversion update with an existing JSON file => nothing to do. | ||
271 | */ | ||
272 | public function testConfigToJsonNothingToDo() | ||
273 | { | ||
274 | $filetime = filemtime($this->conf->getConfigFileExt()); | ||
275 | $updater = new LegacyUpdater(array(), array(), $this->conf, false); | ||
276 | $done = $updater->updateMethodConfigToJson(); | ||
277 | $this->assertTrue($done); | ||
278 | $expected = filemtime($this->conf->getConfigFileExt()); | ||
279 | $this->assertEquals($expected, $filetime); | ||
280 | } | ||
281 | |||
282 | /** | ||
283 | * Test escapeUnescapedConfig with valid data. | ||
284 | */ | ||
285 | public function testEscapeConfig() | ||
286 | { | ||
287 | $sandbox = 'sandbox/config'; | ||
288 | copy(self::$configFile . '.json.php', $sandbox . '.json.php'); | ||
289 | $this->conf = new ConfigManager($sandbox); | ||
290 | $title = '<script>alert("title");</script>'; | ||
291 | $headerLink = '<script>alert("header_link");</script>'; | ||
292 | $this->conf->set('general.title', $title); | ||
293 | $this->conf->set('general.header_link', $headerLink); | ||
294 | $updater = new LegacyUpdater(array(), array(), $this->conf, true); | ||
295 | $done = $updater->updateMethodEscapeUnescapedConfig(); | ||
296 | $this->assertTrue($done); | ||
297 | $this->conf->reload(); | ||
298 | $this->assertEquals(escape($title), $this->conf->get('general.title')); | ||
299 | $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link')); | ||
300 | unlink($sandbox . '.json.php'); | ||
301 | } | ||
302 | |||
303 | /** | ||
304 | * Test updateMethodApiSettings(): create default settings for the API (enabled + secret). | ||
305 | */ | ||
306 | public function testUpdateApiSettings() | ||
307 | { | ||
308 | $confFile = 'sandbox/config'; | ||
309 | copy(self::$configFile .'.json.php', $confFile .'.json.php'); | ||
310 | $conf = new ConfigManager($confFile); | ||
311 | $updater = new LegacyUpdater(array(), array(), $conf, true); | ||
312 | |||
313 | $this->assertFalse($conf->exists('api.enabled')); | ||
314 | $this->assertFalse($conf->exists('api.secret')); | ||
315 | $updater->updateMethodApiSettings(); | ||
316 | $conf->reload(); | ||
317 | $this->assertTrue($conf->get('api.enabled')); | ||
318 | $this->assertTrue($conf->exists('api.secret')); | ||
319 | unlink($confFile .'.json.php'); | ||
320 | } | ||
321 | |||
322 | /** | ||
323 | * Test updateMethodApiSettings(): already set, do nothing. | ||
324 | */ | ||
325 | public function testUpdateApiSettingsNothingToDo() | ||
326 | { | ||
327 | $confFile = 'sandbox/config'; | ||
328 | copy(self::$configFile .'.json.php', $confFile .'.json.php'); | ||
329 | $conf = new ConfigManager($confFile); | ||
330 | $conf->set('api.enabled', false); | ||
331 | $conf->set('api.secret', ''); | ||
332 | $updater = new LegacyUpdater(array(), array(), $conf, true); | ||
333 | $updater->updateMethodApiSettings(); | ||
334 | $this->assertFalse($conf->get('api.enabled')); | ||
335 | $this->assertEmpty($conf->get('api.secret')); | ||
336 | unlink($confFile .'.json.php'); | ||
337 | } | ||
338 | |||
339 | /** | ||
340 | * Test updateMethodDatastoreIds(). | ||
341 | */ | ||
342 | public function testDatastoreIds() | ||
343 | { | ||
344 | $links = array( | ||
345 | '20121206_182539' => array( | ||
346 | 'linkdate' => '20121206_182539', | ||
347 | 'title' => 'Geek and Poke', | ||
348 | 'url' => 'http://geek-and-poke.com/', | ||
349 | 'description' => 'desc', | ||
350 | 'tags' => 'dev cartoon tag1 tag2 tag3 tag4 ', | ||
351 | 'updated' => '20121206_190301', | ||
352 | 'private' => false, | ||
353 | ), | ||
354 | '20121206_172539' => array( | ||
355 | 'linkdate' => '20121206_172539', | ||
356 | 'title' => 'UserFriendly - Samba', | ||
357 | 'url' => 'http://ars.userfriendly.org/cartoons/?id=20010306', | ||
358 | 'description' => '', | ||
359 | 'tags' => 'samba cartoon web', | ||
360 | 'private' => false, | ||
361 | ), | ||
362 | '20121206_142300' => array( | ||
363 | 'linkdate' => '20121206_142300', | ||
364 | 'title' => 'UserFriendly - Web Designer', | ||
365 | 'url' => 'http://ars.userfriendly.org/cartoons/?id=20121206', | ||
366 | 'description' => 'Naming conventions... #private', | ||
367 | 'tags' => 'samba cartoon web', | ||
368 | 'private' => true, | ||
369 | ), | ||
370 | ); | ||
371 | $refDB = new \ReferenceLinkDB(true); | ||
372 | $refDB->setLinks($links); | ||
373 | $refDB->write(self::$testDatastore); | ||
374 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
375 | |||
376 | $checksum = hash_file('sha1', self::$testDatastore); | ||
377 | |||
378 | $this->conf->set('resource.data_dir', 'sandbox'); | ||
379 | $this->conf->set('resource.datastore', self::$testDatastore); | ||
380 | |||
381 | $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true); | ||
382 | $this->assertTrue($updater->updateMethodDatastoreIds()); | ||
383 | |||
384 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
385 | |||
386 | $backupFiles = glob($this->conf->get('resource.data_dir') . '/datastore.'. date('YmdH') .'*.php'); | ||
387 | $backup = null; | ||
388 | foreach ($backupFiles as $backupFile) { | ||
389 | if (strpos($backupFile, '_1') === false) { | ||
390 | $backup = $backupFile; | ||
391 | } | ||
392 | } | ||
393 | $this->assertNotNull($backup); | ||
394 | $this->assertFileExists($backup); | ||
395 | $this->assertEquals($checksum, hash_file('sha1', $backup)); | ||
396 | unlink($backup); | ||
397 | |||
398 | $this->assertEquals(3, count($linkDB)); | ||
399 | $this->assertTrue(isset($linkDB[0])); | ||
400 | $this->assertFalse(isset($linkDB[0]['linkdate'])); | ||
401 | $this->assertEquals(0, $linkDB[0]['id']); | ||
402 | $this->assertEquals('UserFriendly - Web Designer', $linkDB[0]['title']); | ||
403 | $this->assertEquals('http://ars.userfriendly.org/cartoons/?id=20121206', $linkDB[0]['url']); | ||
404 | $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']); | ||
405 | $this->assertEquals('samba cartoon web', $linkDB[0]['tags']); | ||
406 | $this->assertTrue($linkDB[0]['private']); | ||
407 | $this->assertEquals( | ||
408 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_142300'), | ||
409 | $linkDB[0]['created'] | ||
410 | ); | ||
411 | |||
412 | $this->assertTrue(isset($linkDB[1])); | ||
413 | $this->assertFalse(isset($linkDB[1]['linkdate'])); | ||
414 | $this->assertEquals(1, $linkDB[1]['id']); | ||
415 | $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']); | ||
416 | $this->assertEquals( | ||
417 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_172539'), | ||
418 | $linkDB[1]['created'] | ||
419 | ); | ||
420 | |||
421 | $this->assertTrue(isset($linkDB[2])); | ||
422 | $this->assertFalse(isset($linkDB[2]['linkdate'])); | ||
423 | $this->assertEquals(2, $linkDB[2]['id']); | ||
424 | $this->assertEquals('Geek and Poke', $linkDB[2]['title']); | ||
425 | $this->assertEquals( | ||
426 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_182539'), | ||
427 | $linkDB[2]['created'] | ||
428 | ); | ||
429 | $this->assertEquals( | ||
430 | DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20121206_190301'), | ||
431 | $linkDB[2]['updated'] | ||
432 | ); | ||
433 | } | ||
434 | |||
435 | /** | ||
436 | * Test updateMethodDatastoreIds() with the update already applied: nothing to do. | ||
437 | */ | ||
438 | public function testDatastoreIdsNothingToDo() | ||
439 | { | ||
440 | $refDB = new \ReferenceLinkDB(true); | ||
441 | $refDB->write(self::$testDatastore); | ||
442 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
443 | |||
444 | $this->conf->set('resource.data_dir', 'sandbox'); | ||
445 | $this->conf->set('resource.datastore', self::$testDatastore); | ||
446 | |||
447 | $checksum = hash_file('sha1', self::$testDatastore); | ||
448 | $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true); | ||
449 | $this->assertTrue($updater->updateMethodDatastoreIds()); | ||
450 | $this->assertEquals($checksum, hash_file('sha1', self::$testDatastore)); | ||
451 | } | ||
452 | |||
453 | /** | ||
454 | * Test defaultTheme update with default settings: nothing to do. | ||
455 | */ | ||
456 | public function testDefaultThemeWithDefaultSettings() | ||
457 | { | ||
458 | $sandbox = 'sandbox/config'; | ||
459 | copy(self::$configFile . '.json.php', $sandbox . '.json.php'); | ||
460 | $this->conf = new ConfigManager($sandbox); | ||
461 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
462 | $this->assertTrue($updater->updateMethodDefaultTheme()); | ||
463 | |||
464 | $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl')); | ||
465 | $this->assertEquals('default', $this->conf->get('resource.theme')); | ||
466 | $this->conf = new ConfigManager($sandbox); | ||
467 | $this->assertEquals('tpl/', $this->conf->get('resource.raintpl_tpl')); | ||
468 | $this->assertEquals('default', $this->conf->get('resource.theme')); | ||
469 | unlink($sandbox . '.json.php'); | ||
470 | } | ||
471 | |||
472 | /** | ||
473 | * Test defaultTheme update with a custom theme in a subfolder | ||
474 | */ | ||
475 | public function testDefaultThemeWithCustomTheme() | ||
476 | { | ||
477 | $theme = 'iamanartist'; | ||
478 | $sandbox = 'sandbox/config'; | ||
479 | copy(self::$configFile . '.json.php', $sandbox . '.json.php'); | ||
480 | $this->conf = new ConfigManager($sandbox); | ||
481 | mkdir('sandbox/'. $theme); | ||
482 | touch('sandbox/'. $theme .'/linklist.html'); | ||
483 | $this->conf->set('resource.raintpl_tpl', 'sandbox/'. $theme .'/'); | ||
484 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
485 | $this->assertTrue($updater->updateMethodDefaultTheme()); | ||
486 | |||
487 | $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl')); | ||
488 | $this->assertEquals($theme, $this->conf->get('resource.theme')); | ||
489 | $this->conf = new ConfigManager($sandbox); | ||
490 | $this->assertEquals('sandbox', $this->conf->get('resource.raintpl_tpl')); | ||
491 | $this->assertEquals($theme, $this->conf->get('resource.theme')); | ||
492 | unlink($sandbox . '.json.php'); | ||
493 | unlink('sandbox/'. $theme .'/linklist.html'); | ||
494 | rmdir('sandbox/'. $theme); | ||
495 | } | ||
496 | |||
497 | /** | ||
498 | * Test updateMethodEscapeMarkdown with markdown plugin enabled | ||
499 | * => setting markdown_escape set to false. | ||
500 | */ | ||
501 | public function testEscapeMarkdownSettingToFalse() | ||
502 | { | ||
503 | $sandboxConf = 'sandbox/config'; | ||
504 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
505 | $this->conf = new ConfigManager($sandboxConf); | ||
506 | |||
507 | $this->conf->set('general.enabled_plugins', ['markdown']); | ||
508 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
509 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | ||
510 | $this->assertFalse($this->conf->get('security.markdown_escape')); | ||
511 | |||
512 | // reload from file | ||
513 | $this->conf = new ConfigManager($sandboxConf); | ||
514 | $this->assertFalse($this->conf->get('security.markdown_escape')); | ||
515 | } | ||
516 | |||
517 | |||
518 | /** | ||
519 | * Test updateMethodEscapeMarkdown with markdown plugin disabled | ||
520 | * => setting markdown_escape set to true. | ||
521 | */ | ||
522 | public function testEscapeMarkdownSettingToTrue() | ||
523 | { | ||
524 | $sandboxConf = 'sandbox/config'; | ||
525 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
526 | $this->conf = new ConfigManager($sandboxConf); | ||
527 | |||
528 | $this->conf->set('general.enabled_plugins', []); | ||
529 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
530 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | ||
531 | $this->assertTrue($this->conf->get('security.markdown_escape')); | ||
532 | |||
533 | // reload from file | ||
534 | $this->conf = new ConfigManager($sandboxConf); | ||
535 | $this->assertTrue($this->conf->get('security.markdown_escape')); | ||
536 | } | ||
537 | |||
538 | /** | ||
539 | * Test updateMethodEscapeMarkdown with nothing to do (setting already enabled) | ||
540 | */ | ||
541 | public function testEscapeMarkdownSettingNothingToDoEnabled() | ||
542 | { | ||
543 | $sandboxConf = 'sandbox/config'; | ||
544 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
545 | $this->conf = new ConfigManager($sandboxConf); | ||
546 | $this->conf->set('security.markdown_escape', true); | ||
547 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
548 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | ||
549 | $this->assertTrue($this->conf->get('security.markdown_escape')); | ||
550 | } | ||
551 | |||
552 | /** | ||
553 | * Test updateMethodEscapeMarkdown with nothing to do (setting already disabled) | ||
554 | */ | ||
555 | public function testEscapeMarkdownSettingNothingToDoDisabled() | ||
556 | { | ||
557 | $this->conf->set('security.markdown_escape', false); | ||
558 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
559 | $this->assertTrue($updater->updateMethodEscapeMarkdown()); | ||
560 | $this->assertFalse($this->conf->get('security.markdown_escape')); | ||
561 | } | ||
562 | |||
563 | /** | ||
564 | * Test updateMethodPiwikUrl with valid data | ||
565 | */ | ||
566 | public function testUpdatePiwikUrlValid() | ||
567 | { | ||
568 | $sandboxConf = 'sandbox/config'; | ||
569 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
570 | $this->conf = new ConfigManager($sandboxConf); | ||
571 | $url = 'mypiwik.tld'; | ||
572 | $this->conf->set('plugins.PIWIK_URL', $url); | ||
573 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
574 | $this->assertTrue($updater->updateMethodPiwikUrl()); | ||
575 | $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL')); | ||
576 | |||
577 | // reload from file | ||
578 | $this->conf = new ConfigManager($sandboxConf); | ||
579 | $this->assertEquals('http://'. $url, $this->conf->get('plugins.PIWIK_URL')); | ||
580 | } | ||
581 | |||
582 | /** | ||
583 | * Test updateMethodPiwikUrl without setting | ||
584 | */ | ||
585 | public function testUpdatePiwikUrlEmpty() | ||
586 | { | ||
587 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
588 | $this->assertTrue($updater->updateMethodPiwikUrl()); | ||
589 | $this->assertEmpty($this->conf->get('plugins.PIWIK_URL')); | ||
590 | } | ||
591 | |||
592 | /** | ||
593 | * Test updateMethodPiwikUrl: valid URL, nothing to do | ||
594 | */ | ||
595 | public function testUpdatePiwikUrlNothingToDo() | ||
596 | { | ||
597 | $url = 'https://mypiwik.tld'; | ||
598 | $this->conf->set('plugins.PIWIK_URL', $url); | ||
599 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
600 | $this->assertTrue($updater->updateMethodPiwikUrl()); | ||
601 | $this->assertEquals($url, $this->conf->get('plugins.PIWIK_URL')); | ||
602 | } | ||
603 | |||
604 | /** | ||
605 | * Test updateMethodAtomDefault with show_atom set to false | ||
606 | * => update to true. | ||
607 | */ | ||
608 | public function testUpdateMethodAtomDefault() | ||
609 | { | ||
610 | $sandboxConf = 'sandbox/config'; | ||
611 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
612 | $this->conf = new ConfigManager($sandboxConf); | ||
613 | $this->conf->set('feed.show_atom', false); | ||
614 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
615 | $this->assertTrue($updater->updateMethodAtomDefault()); | ||
616 | $this->assertTrue($this->conf->get('feed.show_atom')); | ||
617 | // reload from file | ||
618 | $this->conf = new ConfigManager($sandboxConf); | ||
619 | $this->assertTrue($this->conf->get('feed.show_atom')); | ||
620 | } | ||
621 | /** | ||
622 | * Test updateMethodAtomDefault with show_atom not set. | ||
623 | * => nothing to do | ||
624 | */ | ||
625 | public function testUpdateMethodAtomDefaultNoExist() | ||
626 | { | ||
627 | $sandboxConf = 'sandbox/config'; | ||
628 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
629 | $this->conf = new ConfigManager($sandboxConf); | ||
630 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
631 | $this->assertTrue($updater->updateMethodAtomDefault()); | ||
632 | $this->assertTrue($this->conf->get('feed.show_atom')); | ||
633 | } | ||
634 | /** | ||
635 | * Test updateMethodAtomDefault with show_atom set to true. | ||
636 | * => nothing to do | ||
637 | */ | ||
638 | public function testUpdateMethodAtomDefaultAlreadyTrue() | ||
639 | { | ||
640 | $sandboxConf = 'sandbox/config'; | ||
641 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
642 | $this->conf = new ConfigManager($sandboxConf); | ||
643 | $this->conf->set('feed.show_atom', true); | ||
644 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
645 | $this->assertTrue($updater->updateMethodAtomDefault()); | ||
646 | $this->assertTrue($this->conf->get('feed.show_atom')); | ||
647 | } | ||
648 | |||
649 | /** | ||
650 | * Test updateMethodDownloadSizeAndTimeoutConf, it should be set if none is already defined. | ||
651 | */ | ||
652 | public function testUpdateMethodDownloadSizeAndTimeoutConf() | ||
653 | { | ||
654 | $sandboxConf = 'sandbox/config'; | ||
655 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
656 | $this->conf = new ConfigManager($sandboxConf); | ||
657 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
658 | $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); | ||
659 | $this->assertEquals(4194304, $this->conf->get('general.download_max_size')); | ||
660 | $this->assertEquals(30, $this->conf->get('general.download_timeout')); | ||
661 | |||
662 | $this->conf = new ConfigManager($sandboxConf); | ||
663 | $this->assertEquals(4194304, $this->conf->get('general.download_max_size')); | ||
664 | $this->assertEquals(30, $this->conf->get('general.download_timeout')); | ||
665 | } | ||
666 | |||
667 | /** | ||
668 | * Test updateMethodDownloadSizeAndTimeoutConf, it shouldn't be set if it is already defined. | ||
669 | */ | ||
670 | public function testUpdateMethodDownloadSizeAndTimeoutConfIgnore() | ||
671 | { | ||
672 | $sandboxConf = 'sandbox/config'; | ||
673 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
674 | $this->conf = new ConfigManager($sandboxConf); | ||
675 | $this->conf->set('general.download_max_size', 38); | ||
676 | $this->conf->set('general.download_timeout', 70); | ||
677 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
678 | $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); | ||
679 | $this->assertEquals(38, $this->conf->get('general.download_max_size')); | ||
680 | $this->assertEquals(70, $this->conf->get('general.download_timeout')); | ||
681 | } | ||
682 | |||
683 | /** | ||
684 | * Test updateMethodDownloadSizeAndTimeoutConf, only the maz size should be set here. | ||
685 | */ | ||
686 | public function testUpdateMethodDownloadSizeAndTimeoutConfOnlySize() | ||
687 | { | ||
688 | $sandboxConf = 'sandbox/config'; | ||
689 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
690 | $this->conf = new ConfigManager($sandboxConf); | ||
691 | $this->conf->set('general.download_max_size', 38); | ||
692 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
693 | $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); | ||
694 | $this->assertEquals(38, $this->conf->get('general.download_max_size')); | ||
695 | $this->assertEquals(30, $this->conf->get('general.download_timeout')); | ||
696 | } | ||
697 | |||
698 | /** | ||
699 | * Test updateMethodDownloadSizeAndTimeoutConf, only the time out should be set here. | ||
700 | */ | ||
701 | public function testUpdateMethodDownloadSizeAndTimeoutConfOnlyTimeout() | ||
702 | { | ||
703 | $sandboxConf = 'sandbox/config'; | ||
704 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
705 | $this->conf = new ConfigManager($sandboxConf); | ||
706 | $this->conf->set('general.download_timeout', 3); | ||
707 | $updater = new LegacyUpdater([], [], $this->conf, true); | ||
708 | $this->assertTrue($updater->updateMethodDownloadSizeAndTimeoutConf()); | ||
709 | $this->assertEquals(4194304, $this->conf->get('general.download_max_size')); | ||
710 | $this->assertEquals(3, $this->conf->get('general.download_timeout')); | ||
711 | } | ||
712 | |||
713 | /** | ||
714 | * Test updateMethodWebThumbnailer with thumbnails enabled. | ||
715 | */ | ||
716 | public function testUpdateMethodWebThumbnailerEnabled() | ||
717 | { | ||
718 | $this->conf->remove('thumbnails'); | ||
719 | $this->conf->set('thumbnail.enable_thumbnails', true); | ||
720 | $updater = new LegacyUpdater([], [], $this->conf, true, $_SESSION); | ||
721 | $this->assertTrue($updater->updateMethodWebThumbnailer()); | ||
722 | $this->assertFalse($this->conf->exists('thumbnail')); | ||
723 | $this->assertEquals(\Shaarli\Thumbnailer::MODE_ALL, $this->conf->get('thumbnails.mode')); | ||
724 | $this->assertEquals(125, $this->conf->get('thumbnails.width')); | ||
725 | $this->assertEquals(90, $this->conf->get('thumbnails.height')); | ||
726 | $this->assertContains('You have enabled or changed thumbnails', $_SESSION['warnings'][0]); | ||
727 | } | ||
728 | |||
729 | /** | ||
730 | * Test updateMethodWebThumbnailer with thumbnails disabled. | ||
731 | */ | ||
732 | public function testUpdateMethodWebThumbnailerDisabled() | ||
733 | { | ||
734 | if (isset($_SESSION['warnings'])) { | ||
735 | unset($_SESSION['warnings']); | ||
736 | } | ||
737 | |||
738 | $this->conf->remove('thumbnails'); | ||
739 | $this->conf->set('thumbnail.enable_thumbnails', false); | ||
740 | $updater = new LegacyUpdater([], [], $this->conf, true, $_SESSION); | ||
741 | $this->assertTrue($updater->updateMethodWebThumbnailer()); | ||
742 | $this->assertFalse($this->conf->exists('thumbnail')); | ||
743 | $this->assertEquals(Thumbnailer::MODE_NONE, $this->conf->get('thumbnails.mode')); | ||
744 | $this->assertEquals(125, $this->conf->get('thumbnails.width')); | ||
745 | $this->assertEquals(90, $this->conf->get('thumbnails.height')); | ||
746 | $this->assertTrue(empty($_SESSION['warnings'])); | ||
747 | } | ||
748 | |||
749 | /** | ||
750 | * Test updateMethodWebThumbnailer with thumbnails disabled. | ||
751 | */ | ||
752 | public function testUpdateMethodWebThumbnailerNothingToDo() | ||
753 | { | ||
754 | if (isset($_SESSION['warnings'])) { | ||
755 | unset($_SESSION['warnings']); | ||
756 | } | ||
757 | |||
758 | $updater = new LegacyUpdater([], [], $this->conf, true, $_SESSION); | ||
759 | $this->assertTrue($updater->updateMethodWebThumbnailer()); | ||
760 | $this->assertFalse($this->conf->exists('thumbnail')); | ||
761 | $this->assertEquals(Thumbnailer::MODE_COMMON, $this->conf->get('thumbnails.mode')); | ||
762 | $this->assertEquals(90, $this->conf->get('thumbnails.width')); | ||
763 | $this->assertEquals(53, $this->conf->get('thumbnails.height')); | ||
764 | $this->assertTrue(empty($_SESSION['warnings'])); | ||
765 | } | ||
766 | |||
767 | /** | ||
768 | * Test updateMethodSetSticky(). | ||
769 | */ | ||
770 | public function testUpdateStickyValid() | ||
771 | { | ||
772 | $blank = [ | ||
773 | 'id' => 1, | ||
774 | 'url' => 'z', | ||
775 | 'title' => '', | ||
776 | 'description' => '', | ||
777 | 'tags' => '', | ||
778 | 'created' => new DateTime(), | ||
779 | ]; | ||
780 | $links = [ | ||
781 | 1 => ['id' => 1] + $blank, | ||
782 | 2 => ['id' => 2] + $blank, | ||
783 | ]; | ||
784 | $refDB = new \ReferenceLinkDB(true); | ||
785 | $refDB->setLinks($links); | ||
786 | $refDB->write(self::$testDatastore); | ||
787 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
788 | |||
789 | $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true); | ||
790 | $this->assertTrue($updater->updateMethodSetSticky()); | ||
791 | |||
792 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
793 | foreach ($linkDB as $link) { | ||
794 | $this->assertFalse($link['sticky']); | ||
795 | } | ||
796 | } | ||
797 | |||
798 | /** | ||
799 | * Test updateMethodSetSticky(). | ||
800 | */ | ||
801 | public function testUpdateStickyNothingToDo() | ||
802 | { | ||
803 | $blank = [ | ||
804 | 'id' => 1, | ||
805 | 'url' => 'z', | ||
806 | 'title' => '', | ||
807 | 'description' => '', | ||
808 | 'tags' => '', | ||
809 | 'created' => new DateTime(), | ||
810 | ]; | ||
811 | $links = [ | ||
812 | 1 => ['id' => 1, 'sticky' => true] + $blank, | ||
813 | 2 => ['id' => 2] + $blank, | ||
814 | ]; | ||
815 | $refDB = new \ReferenceLinkDB(true); | ||
816 | $refDB->setLinks($links); | ||
817 | $refDB->write(self::$testDatastore); | ||
818 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
819 | |||
820 | $updater = new LegacyUpdater(array(), $linkDB, $this->conf, true); | ||
821 | $this->assertTrue($updater->updateMethodSetSticky()); | ||
822 | |||
823 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
824 | $this->assertTrue($linkDB[1]['sticky']); | ||
825 | } | ||
826 | |||
827 | /** | ||
828 | * Test updateMethodRemoveRedirector(). | ||
829 | */ | ||
830 | public function testUpdateRemoveRedirector() | ||
831 | { | ||
832 | $sandboxConf = 'sandbox/config'; | ||
833 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
834 | $this->conf = new ConfigManager($sandboxConf); | ||
835 | $updater = new LegacyUpdater([], null, $this->conf, true); | ||
836 | $this->assertTrue($updater->updateMethodRemoveRedirector()); | ||
837 | $this->assertFalse($this->conf->exists('redirector')); | ||
838 | $this->conf = new ConfigManager($sandboxConf); | ||
839 | $this->assertFalse($this->conf->exists('redirector')); | ||
840 | } | ||
841 | |||
842 | /** | ||
843 | * Test updateMethodFormatterSetting() | ||
844 | */ | ||
845 | public function testUpdateMethodFormatterSettingDefault() | ||
846 | { | ||
847 | $sandboxConf = 'sandbox/config'; | ||
848 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
849 | $this->conf = new ConfigManager($sandboxConf); | ||
850 | $this->conf->set('formatter', 'default'); | ||
851 | $updater = new LegacyUpdater([], null, $this->conf, true); | ||
852 | $enabledPlugins = $this->conf->get('general.enabled_plugins'); | ||
853 | $this->assertFalse(in_array('markdown', $enabledPlugins)); | ||
854 | $this->assertTrue($updater->updateMethodFormatterSetting()); | ||
855 | $this->assertEquals('default', $this->conf->get('formatter')); | ||
856 | $this->assertEquals($enabledPlugins, $this->conf->get('general.enabled_plugins')); | ||
857 | |||
858 | $this->conf = new ConfigManager($sandboxConf); | ||
859 | $this->assertEquals('default', $this->conf->get('formatter')); | ||
860 | $this->assertEquals($enabledPlugins, $this->conf->get('general.enabled_plugins')); | ||
861 | } | ||
862 | |||
863 | /** | ||
864 | * Test updateMethodFormatterSetting() | ||
865 | */ | ||
866 | public function testUpdateMethodFormatterSettingMarkdown() | ||
867 | { | ||
868 | $sandboxConf = 'sandbox/config'; | ||
869 | copy(self::$configFile . '.json.php', $sandboxConf . '.json.php'); | ||
870 | $this->conf = new ConfigManager($sandboxConf); | ||
871 | $this->conf->set('formatter', 'default'); | ||
872 | $updater = new LegacyUpdater([], null, $this->conf, true); | ||
873 | $enabledPlugins = $this->conf->get('general.enabled_plugins'); | ||
874 | $enabledPlugins[] = 'markdown'; | ||
875 | $this->conf->set('general.enabled_plugins', $enabledPlugins); | ||
876 | |||
877 | $this->assertTrue(in_array('markdown', $this->conf->get('general.enabled_plugins'))); | ||
878 | $this->assertTrue($updater->updateMethodFormatterSetting()); | ||
879 | $this->assertEquals('markdown', $this->conf->get('formatter')); | ||
880 | $this->assertFalse(in_array('markdown', $this->conf->get('general.enabled_plugins'))); | ||
881 | |||
882 | $this->conf = new ConfigManager($sandboxConf); | ||
883 | $this->assertEquals('markdown', $this->conf->get('formatter')); | ||
884 | $this->assertFalse(in_array('markdown', $this->conf->get('general.enabled_plugins'))); | ||
885 | } | ||
886 | } | ||