diff options
Diffstat (limited to 'tests/legacy')
-rw-r--r-- | tests/legacy/LegacyControllerTest.php | 101 | ||||
-rw-r--r-- | tests/legacy/LegacyDummyUpdater.php | 74 | ||||
-rw-r--r-- | tests/legacy/LegacyLinkDBTest.php | 655 | ||||
-rw-r--r-- | tests/legacy/LegacyLinkFilterTest.php | 511 | ||||
-rw-r--r-- | tests/legacy/LegacyUpdaterTest.php | 886 |
5 files changed, 2227 insertions, 0 deletions
diff --git a/tests/legacy/LegacyControllerTest.php b/tests/legacy/LegacyControllerTest.php new file mode 100644 index 00000000..1a2549a3 --- /dev/null +++ b/tests/legacy/LegacyControllerTest.php | |||
@@ -0,0 +1,101 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Shaarli\Legacy; | ||
6 | |||
7 | use Shaarli\Front\Controller\Visitor\FrontControllerMockHelper; | ||
8 | use Shaarli\TestCase; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | class LegacyControllerTest extends TestCase | ||
13 | { | ||
14 | use FrontControllerMockHelper; | ||
15 | |||
16 | /** @var LegacyController */ | ||
17 | protected $controller; | ||
18 | |||
19 | public function setUp(): void | ||
20 | { | ||
21 | $this->createContainer(); | ||
22 | |||
23 | $this->controller = new LegacyController($this->container); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * @dataProvider getProcessProvider | ||
28 | */ | ||
29 | public function testProcess(string $legacyRoute, array $queryParameters, string $slimRoute, bool $isLoggedIn): void | ||
30 | { | ||
31 | $request = $this->createMock(Request::class); | ||
32 | $request->method('getQueryParams')->willReturn($queryParameters); | ||
33 | $request | ||
34 | ->method('getParam') | ||
35 | ->willReturnCallback(function (string $key) use ($queryParameters): ?string { | ||
36 | return $queryParameters[$key] ?? null; | ||
37 | }) | ||
38 | ; | ||
39 | $response = new Response(); | ||
40 | |||
41 | $this->container->loginManager->method('isLoggedIn')->willReturn($isLoggedIn); | ||
42 | |||
43 | $result = $this->controller->process($request, $response, $legacyRoute); | ||
44 | |||
45 | static::assertSame('/subfolder' . $slimRoute, $result->getHeader('location')[0]); | ||
46 | } | ||
47 | |||
48 | public function testProcessNotFound(): void | ||
49 | { | ||
50 | $request = $this->createMock(Request::class); | ||
51 | $response = new Response(); | ||
52 | |||
53 | $this->expectException(UnknowLegacyRouteException::class); | ||
54 | |||
55 | $this->controller->process($request, $response, 'nope'); | ||
56 | } | ||
57 | |||
58 | /** | ||
59 | * @return array[] Parameters: | ||
60 | * - string legacyRoute | ||
61 | * - array queryParameters | ||
62 | * - string slimRoute | ||
63 | * - bool isLoggedIn | ||
64 | */ | ||
65 | public function getProcessProvider(): array | ||
66 | { | ||
67 | return [ | ||
68 | ['post', [], '/admin/shaare', true], | ||
69 | ['post', [], '/login?returnurl=/subfolder/admin/shaare', false], | ||
70 | ['post', ['title' => 'test'], '/admin/shaare?title=test', true], | ||
71 | ['post', ['title' => 'test'], '/login?returnurl=/subfolder/admin/shaare?title=test', false], | ||
72 | ['addlink', [], '/admin/add-shaare', true], | ||
73 | ['addlink', [], '/login?returnurl=/subfolder/admin/add-shaare', false], | ||
74 | ['login', [], '/login', true], | ||
75 | ['login', [], '/login', false], | ||
76 | ['logout', [], '/admin/logout', true], | ||
77 | ['logout', [], '/admin/logout', false], | ||
78 | ['picwall', [], '/picture-wall', false], | ||
79 | ['picwall', [], '/picture-wall', true], | ||
80 | ['tagcloud', [], '/tags/cloud', false], | ||
81 | ['tagcloud', [], '/tags/cloud', true], | ||
82 | ['taglist', [], '/tags/list', false], | ||
83 | ['taglist', [], '/tags/list', true], | ||
84 | ['daily', [], '/daily', false], | ||
85 | ['daily', [], '/daily', true], | ||
86 | ['daily', ['day' => '123456789', 'discard' => '1'], '/daily?day=123456789', false], | ||
87 | ['rss', [], '/feed/rss', false], | ||
88 | ['rss', [], '/feed/rss', true], | ||
89 | ['rss', ['search' => 'filter123', 'other' => 'param'], '/feed/rss?search=filter123&other=param', false], | ||
90 | ['atom', [], '/feed/atom', false], | ||
91 | ['atom', [], '/feed/atom', true], | ||
92 | ['atom', ['search' => 'filter123', 'other' => 'param'], '/feed/atom?search=filter123&other=param', false], | ||
93 | ['opensearch', [], '/open-search', false], | ||
94 | ['opensearch', [], '/open-search', true], | ||
95 | ['dailyrss', [], '/daily-rss', false], | ||
96 | ['dailyrss', [], '/daily-rss', true], | ||
97 | ['configure', [], '/login?returnurl=/subfolder/admin/configure', false], | ||
98 | ['configure', [], '/admin/configure', true], | ||
99 | ]; | ||
100 | } | ||
101 | } | ||
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..df2cad62 --- /dev/null +++ b/tests/legacy/LegacyLinkDBTest.php | |||
@@ -0,0 +1,655 @@ | |||
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/Utils.php'; | ||
15 | require_once 'tests/utils/ReferenceLinkDB.php'; | ||
16 | |||
17 | |||
18 | /** | ||
19 | * Unitary tests for LegacyLinkDBTest | ||
20 | */ | ||
21 | class LegacyLinkDBTest extends \Shaarli\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 LegacyLinkDB public LinkDB instance. | ||
33 | */ | ||
34 | protected static $publicLinkDB = null; | ||
35 | |||
36 | /** | ||
37 | * @var LegacyLinkDB 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 bookmarks 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(): void | ||
56 | { | ||
57 | if (file_exists(self::$testDatastore)) { | ||
58 | unlink(self::$testDatastore); | ||
59 | } | ||
60 | |||
61 | self::$refDB = new ReferenceLinkDB(true); | ||
62 | self::$refDB->write(self::$testDatastore); | ||
63 | self::$publicLinkDB = new LegacyLinkDB(self::$testDatastore, false, false); | ||
64 | self::$privateLinkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * Allows to test LinkDB's private methods | ||
69 | * | ||
70 | * @see | ||
71 | * https://sebastian-bergmann.de/archives/881-Testing-Your-Privates.html | ||
72 | * http://stackoverflow.com/a/2798203 | ||
73 | */ | ||
74 | protected static function getMethod($name) | ||
75 | { | ||
76 | $class = new ReflectionClass('Shaarli\Legacy\LegacyLinkDB'); | ||
77 | $method = $class->getMethod($name); | ||
78 | $method->setAccessible(true); | ||
79 | return $method; | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * Instantiate LinkDB objects - logged in user | ||
84 | */ | ||
85 | public function testConstructLoggedIn() | ||
86 | { | ||
87 | new LegacyLinkDB(self::$testDatastore, true, false); | ||
88 | $this->assertFileExists(self::$testDatastore); | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * Instantiate LinkDB objects - logged out or public instance | ||
93 | */ | ||
94 | public function testConstructLoggedOut() | ||
95 | { | ||
96 | new LegacyLinkDB(self::$testDatastore, false, false); | ||
97 | $this->assertFileExists(self::$testDatastore); | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * Attempt to instantiate a LinkDB whereas the datastore is not writable | ||
102 | */ | ||
103 | public function testConstructDatastoreNotWriteable() | ||
104 | { | ||
105 | $this->expectException(\Shaarli\Exceptions\IOException::class); | ||
106 | $this->expectExceptionMessageRegExp('/Error accessing "null"/'); | ||
107 | |||
108 | new LegacyLinkDB('null/store.db', false, false); | ||
109 | } | ||
110 | |||
111 | /** | ||
112 | * The DB doesn't exist, ensure it is created with dummy content | ||
113 | */ | ||
114 | public function testCheckDBNew() | ||
115 | { | ||
116 | $linkDB = new LegacyLinkDB(self::$testDatastore, false, false); | ||
117 | unlink(self::$testDatastore); | ||
118 | $this->assertFileNotExists(self::$testDatastore); | ||
119 | |||
120 | $checkDB = self::getMethod('check'); | ||
121 | $checkDB->invokeArgs($linkDB, array()); | ||
122 | $this->assertFileExists(self::$testDatastore); | ||
123 | |||
124 | // ensure the correct data has been written | ||
125 | $this->assertGreaterThan(0, filesize(self::$testDatastore)); | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * The DB exists, don't do anything | ||
130 | */ | ||
131 | public function testCheckDBLoad() | ||
132 | { | ||
133 | $linkDB = new LegacyLinkDB(self::$testDatastore, false, false); | ||
134 | $datastoreSize = filesize(self::$testDatastore); | ||
135 | $this->assertGreaterThan(0, $datastoreSize); | ||
136 | |||
137 | $checkDB = self::getMethod('check'); | ||
138 | $checkDB->invokeArgs($linkDB, array()); | ||
139 | |||
140 | // ensure the datastore is left unmodified | ||
141 | $this->assertEquals( | ||
142 | $datastoreSize, | ||
143 | filesize(self::$testDatastore) | ||
144 | ); | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * Load an empty DB | ||
149 | */ | ||
150 | public function testReadEmptyDB() | ||
151 | { | ||
152 | file_put_contents(self::$testDatastore, '<?php /* S7QysKquBQA= */ ?>'); | ||
153 | $emptyDB = new LegacyLinkDB(self::$testDatastore, false, false); | ||
154 | $this->assertEquals(0, sizeof($emptyDB)); | ||
155 | $this->assertEquals(0, count($emptyDB)); | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * Load public bookmarks from the DB | ||
160 | */ | ||
161 | public function testReadPublicDB() | ||
162 | { | ||
163 | $this->assertEquals( | ||
164 | self::$refDB->countPublicLinks(), | ||
165 | sizeof(self::$publicLinkDB) | ||
166 | ); | ||
167 | } | ||
168 | |||
169 | /** | ||
170 | * Load public and private bookmarks from the DB | ||
171 | */ | ||
172 | public function testReadPrivateDB() | ||
173 | { | ||
174 | $this->assertEquals( | ||
175 | self::$refDB->countLinks(), | ||
176 | sizeof(self::$privateLinkDB) | ||
177 | ); | ||
178 | } | ||
179 | |||
180 | /** | ||
181 | * Save the bookmarks to the DB | ||
182 | */ | ||
183 | public function testSave() | ||
184 | { | ||
185 | $testDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
186 | $dbSize = sizeof($testDB); | ||
187 | |||
188 | $link = array( | ||
189 | 'id' => 43, | ||
190 | 'title' => 'an additional link', | ||
191 | 'url' => 'http://dum.my', | ||
192 | 'description' => 'One more', | ||
193 | 'private' => 0, | ||
194 | 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, '20150518_190000'), | ||
195 | 'tags' => 'unit test' | ||
196 | ); | ||
197 | $testDB[$link['id']] = $link; | ||
198 | $testDB->save('tests'); | ||
199 | |||
200 | $testDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
201 | $this->assertEquals($dbSize + 1, sizeof($testDB)); | ||
202 | } | ||
203 | |||
204 | /** | ||
205 | * Count existing bookmarks | ||
206 | */ | ||
207 | public function testCount() | ||
208 | { | ||
209 | $this->assertEquals( | ||
210 | self::$refDB->countPublicLinks(), | ||
211 | self::$publicLinkDB->count() | ||
212 | ); | ||
213 | $this->assertEquals( | ||
214 | self::$refDB->countLinks(), | ||
215 | self::$privateLinkDB->count() | ||
216 | ); | ||
217 | } | ||
218 | |||
219 | /** | ||
220 | * Count existing bookmarks - public bookmarks hidden | ||
221 | */ | ||
222 | public function testCountHiddenPublic() | ||
223 | { | ||
224 | $linkDB = new LegacyLinkDB(self::$testDatastore, false, true); | ||
225 | |||
226 | $this->assertEquals( | ||
227 | 0, | ||
228 | $linkDB->count() | ||
229 | ); | ||
230 | $this->assertEquals( | ||
231 | 0, | ||
232 | $linkDB->count() | ||
233 | ); | ||
234 | } | ||
235 | |||
236 | /** | ||
237 | * List the days for which bookmarks have been posted | ||
238 | */ | ||
239 | public function testDays() | ||
240 | { | ||
241 | $this->assertEquals( | ||
242 | array('20100309', '20100310', '20121206', '20121207', '20130614', '20150310'), | ||
243 | self::$publicLinkDB->days() | ||
244 | ); | ||
245 | |||
246 | $this->assertEquals( | ||
247 | array('20100309', '20100310', '20121206', '20121207', '20130614', '20141125', '20150310'), | ||
248 | self::$privateLinkDB->days() | ||
249 | ); | ||
250 | } | ||
251 | |||
252 | /** | ||
253 | * The URL corresponds to an existing entry in the DB | ||
254 | */ | ||
255 | public function testGetKnownLinkFromURL() | ||
256 | { | ||
257 | $link = self::$publicLinkDB->getLinkFromUrl('http://mediagoblin.org/'); | ||
258 | |||
259 | $this->assertNotEquals(false, $link); | ||
260 | $this->assertContainsPolyfill( | ||
261 | 'A free software media publishing platform', | ||
262 | $link['description'] | ||
263 | ); | ||
264 | } | ||
265 | |||
266 | /** | ||
267 | * The URL is not in the DB | ||
268 | */ | ||
269 | public function testGetUnknownLinkFromURL() | ||
270 | { | ||
271 | $this->assertEquals( | ||
272 | false, | ||
273 | self::$publicLinkDB->getLinkFromUrl('http://dev.null') | ||
274 | ); | ||
275 | } | ||
276 | |||
277 | /** | ||
278 | * Lists all tags | ||
279 | */ | ||
280 | public function testAllTags() | ||
281 | { | ||
282 | $this->assertEquals( | ||
283 | array( | ||
284 | 'web' => 3, | ||
285 | 'cartoon' => 2, | ||
286 | 'gnu' => 2, | ||
287 | 'dev' => 1, | ||
288 | 'samba' => 1, | ||
289 | 'media' => 1, | ||
290 | 'software' => 1, | ||
291 | 'stallman' => 1, | ||
292 | 'free' => 1, | ||
293 | '-exclude' => 1, | ||
294 | 'hashtag' => 2, | ||
295 | // The DB contains a link with `sTuff` and another one with `stuff` tag. | ||
296 | // They need to be grouped with the first case found - order by date DESC: `sTuff`. | ||
297 | 'sTuff' => 2, | ||
298 | 'ut' => 1, | ||
299 | ), | ||
300 | self::$publicLinkDB->linksCountPerTag() | ||
301 | ); | ||
302 | |||
303 | $this->assertEquals( | ||
304 | array( | ||
305 | 'web' => 4, | ||
306 | 'cartoon' => 3, | ||
307 | 'gnu' => 2, | ||
308 | 'dev' => 2, | ||
309 | 'samba' => 1, | ||
310 | 'media' => 1, | ||
311 | 'software' => 1, | ||
312 | 'stallman' => 1, | ||
313 | 'free' => 1, | ||
314 | 'html' => 1, | ||
315 | 'w3c' => 1, | ||
316 | 'css' => 1, | ||
317 | 'Mercurial' => 1, | ||
318 | 'sTuff' => 2, | ||
319 | '-exclude' => 1, | ||
320 | '.hidden' => 1, | ||
321 | 'hashtag' => 2, | ||
322 | 'tag1' => 1, | ||
323 | 'tag2' => 1, | ||
324 | 'tag3' => 1, | ||
325 | 'tag4' => 1, | ||
326 | 'ut' => 1, | ||
327 | ), | ||
328 | self::$privateLinkDB->linksCountPerTag() | ||
329 | ); | ||
330 | $this->assertEquals( | ||
331 | array( | ||
332 | 'web' => 4, | ||
333 | 'cartoon' => 2, | ||
334 | 'gnu' => 1, | ||
335 | 'dev' => 1, | ||
336 | 'samba' => 1, | ||
337 | 'media' => 1, | ||
338 | 'html' => 1, | ||
339 | 'w3c' => 1, | ||
340 | 'css' => 1, | ||
341 | 'Mercurial' => 1, | ||
342 | '.hidden' => 1, | ||
343 | 'hashtag' => 1, | ||
344 | ), | ||
345 | self::$privateLinkDB->linksCountPerTag(['web']) | ||
346 | ); | ||
347 | $this->assertEquals( | ||
348 | array( | ||
349 | 'web' => 1, | ||
350 | 'html' => 1, | ||
351 | 'w3c' => 1, | ||
352 | 'css' => 1, | ||
353 | 'Mercurial' => 1, | ||
354 | ), | ||
355 | self::$privateLinkDB->linksCountPerTag(['web'], 'private') | ||
356 | ); | ||
357 | } | ||
358 | |||
359 | /** | ||
360 | * Test filter with string. | ||
361 | */ | ||
362 | public function testFilterString() | ||
363 | { | ||
364 | $tags = 'dev cartoon'; | ||
365 | $request = array('searchtags' => $tags); | ||
366 | $this->assertEquals( | ||
367 | 2, | ||
368 | count(self::$privateLinkDB->filterSearch($request, true, false)) | ||
369 | ); | ||
370 | } | ||
371 | |||
372 | /** | ||
373 | * Test filter with string. | ||
374 | */ | ||
375 | public function testFilterArray() | ||
376 | { | ||
377 | $tags = array('dev', 'cartoon'); | ||
378 | $request = array('searchtags' => $tags); | ||
379 | $this->assertEquals( | ||
380 | 2, | ||
381 | count(self::$privateLinkDB->filterSearch($request, true, false)) | ||
382 | ); | ||
383 | } | ||
384 | |||
385 | /** | ||
386 | * Test hidden tags feature: | ||
387 | * tags starting with a dot '.' are only visible when logged in. | ||
388 | */ | ||
389 | public function testHiddenTags() | ||
390 | { | ||
391 | $tags = '.hidden'; | ||
392 | $request = array('searchtags' => $tags); | ||
393 | $this->assertEquals( | ||
394 | 1, | ||
395 | count(self::$privateLinkDB->filterSearch($request, true, false)) | ||
396 | ); | ||
397 | |||
398 | $this->assertEquals( | ||
399 | 0, | ||
400 | count(self::$publicLinkDB->filterSearch($request, true, false)) | ||
401 | ); | ||
402 | } | ||
403 | |||
404 | /** | ||
405 | * Test filterHash() with a valid smallhash. | ||
406 | */ | ||
407 | public function testFilterHashValid() | ||
408 | { | ||
409 | $request = smallHash('20150310_114651'); | ||
410 | $this->assertEquals( | ||
411 | 1, | ||
412 | count(self::$publicLinkDB->filterHash($request)) | ||
413 | ); | ||
414 | $request = smallHash('20150310_114633' . 8); | ||
415 | $this->assertEquals( | ||
416 | 1, | ||
417 | count(self::$publicLinkDB->filterHash($request)) | ||
418 | ); | ||
419 | } | ||
420 | |||
421 | /** | ||
422 | * Test filterHash() with an invalid smallhash. | ||
423 | */ | ||
424 | public function testFilterHashInValid1() | ||
425 | { | ||
426 | $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); | ||
427 | |||
428 | $request = 'blabla'; | ||
429 | self::$publicLinkDB->filterHash($request); | ||
430 | } | ||
431 | |||
432 | /** | ||
433 | * Test filterHash() with an empty smallhash. | ||
434 | */ | ||
435 | public function testFilterHashInValid() | ||
436 | { | ||
437 | $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); | ||
438 | |||
439 | self::$publicLinkDB->filterHash(''); | ||
440 | } | ||
441 | |||
442 | /** | ||
443 | * Test reorder with asc/desc parameter. | ||
444 | */ | ||
445 | public function testReorderLinksDesc() | ||
446 | { | ||
447 | self::$privateLinkDB->reorder('ASC'); | ||
448 | $stickyIds = [11, 10]; | ||
449 | $standardIds = [42, 4, 9, 1, 0, 7, 6, 8, 41]; | ||
450 | $linkIds = array_merge($stickyIds, $standardIds); | ||
451 | $cpt = 0; | ||
452 | foreach (self::$privateLinkDB as $key => $value) { | ||
453 | $this->assertEquals($linkIds[$cpt++], $key); | ||
454 | } | ||
455 | self::$privateLinkDB->reorder('DESC'); | ||
456 | $linkIds = array_merge(array_reverse($stickyIds), array_reverse($standardIds)); | ||
457 | $cpt = 0; | ||
458 | foreach (self::$privateLinkDB as $key => $value) { | ||
459 | $this->assertEquals($linkIds[$cpt++], $key); | ||
460 | } | ||
461 | } | ||
462 | |||
463 | /** | ||
464 | * Test rename tag with a valid value present in multiple bookmarks | ||
465 | */ | ||
466 | public function testRenameTagMultiple() | ||
467 | { | ||
468 | self::$refDB->write(self::$testDatastore); | ||
469 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
470 | |||
471 | $res = $linkDB->renameTag('cartoon', 'Taz'); | ||
472 | $this->assertEquals(3, count($res)); | ||
473 | $this->assertContainsPolyfill(' Taz ', $linkDB[4]['tags']); | ||
474 | $this->assertContainsPolyfill(' Taz ', $linkDB[1]['tags']); | ||
475 | $this->assertContainsPolyfill(' Taz ', $linkDB[0]['tags']); | ||
476 | } | ||
477 | |||
478 | /** | ||
479 | * Test rename tag with a valid value | ||
480 | */ | ||
481 | public function testRenameTagCaseSensitive() | ||
482 | { | ||
483 | self::$refDB->write(self::$testDatastore); | ||
484 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
485 | |||
486 | $res = $linkDB->renameTag('sTuff', 'Taz'); | ||
487 | $this->assertEquals(1, count($res)); | ||
488 | $this->assertEquals('Taz', $linkDB[41]['tags']); | ||
489 | } | ||
490 | |||
491 | /** | ||
492 | * Test rename tag with invalid values | ||
493 | */ | ||
494 | public function testRenameTagInvalid() | ||
495 | { | ||
496 | $linkDB = new LegacyLinkDB(self::$testDatastore, false, false); | ||
497 | |||
498 | $this->assertFalse($linkDB->renameTag('', 'test')); | ||
499 | $this->assertFalse($linkDB->renameTag('', '')); | ||
500 | // tag non existent | ||
501 | $this->assertEquals([], $linkDB->renameTag('test', '')); | ||
502 | $this->assertEquals([], $linkDB->renameTag('test', 'retest')); | ||
503 | } | ||
504 | |||
505 | /** | ||
506 | * Test delete tag with a valid value | ||
507 | */ | ||
508 | public function testDeleteTag() | ||
509 | { | ||
510 | self::$refDB->write(self::$testDatastore); | ||
511 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
512 | |||
513 | $res = $linkDB->renameTag('cartoon', null); | ||
514 | $this->assertEquals(3, count($res)); | ||
515 | $this->assertNotContainsPolyfill('cartoon', $linkDB[4]['tags']); | ||
516 | } | ||
517 | |||
518 | /** | ||
519 | * Test linksCountPerTag all tags without filter. | ||
520 | * Equal occurrences should be sorted alphabetically. | ||
521 | */ | ||
522 | public function testCountLinkPerTagAllNoFilter() | ||
523 | { | ||
524 | $expected = [ | ||
525 | 'web' => 4, | ||
526 | 'cartoon' => 3, | ||
527 | 'dev' => 2, | ||
528 | 'gnu' => 2, | ||
529 | 'hashtag' => 2, | ||
530 | 'sTuff' => 2, | ||
531 | '-exclude' => 1, | ||
532 | '.hidden' => 1, | ||
533 | 'Mercurial' => 1, | ||
534 | 'css' => 1, | ||
535 | 'free' => 1, | ||
536 | 'html' => 1, | ||
537 | 'media' => 1, | ||
538 | 'samba' => 1, | ||
539 | 'software' => 1, | ||
540 | 'stallman' => 1, | ||
541 | 'tag1' => 1, | ||
542 | 'tag2' => 1, | ||
543 | 'tag3' => 1, | ||
544 | 'tag4' => 1, | ||
545 | 'ut' => 1, | ||
546 | 'w3c' => 1, | ||
547 | ]; | ||
548 | $tags = self::$privateLinkDB->linksCountPerTag(); | ||
549 | |||
550 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
551 | } | ||
552 | |||
553 | /** | ||
554 | * Test linksCountPerTag all tags with filter. | ||
555 | * Equal occurrences should be sorted alphabetically. | ||
556 | */ | ||
557 | public function testCountLinkPerTagAllWithFilter() | ||
558 | { | ||
559 | $expected = [ | ||
560 | 'gnu' => 2, | ||
561 | 'hashtag' => 2, | ||
562 | '-exclude' => 1, | ||
563 | '.hidden' => 1, | ||
564 | 'free' => 1, | ||
565 | 'media' => 1, | ||
566 | 'software' => 1, | ||
567 | 'stallman' => 1, | ||
568 | 'stuff' => 1, | ||
569 | 'web' => 1, | ||
570 | ]; | ||
571 | $tags = self::$privateLinkDB->linksCountPerTag(['gnu']); | ||
572 | |||
573 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
574 | } | ||
575 | |||
576 | /** | ||
577 | * Test linksCountPerTag public tags with filter. | ||
578 | * Equal occurrences should be sorted alphabetically. | ||
579 | */ | ||
580 | public function testCountLinkPerTagPublicWithFilter() | ||
581 | { | ||
582 | $expected = [ | ||
583 | 'gnu' => 2, | ||
584 | 'hashtag' => 2, | ||
585 | '-exclude' => 1, | ||
586 | '.hidden' => 1, | ||
587 | 'free' => 1, | ||
588 | 'media' => 1, | ||
589 | 'software' => 1, | ||
590 | 'stallman' => 1, | ||
591 | 'stuff' => 1, | ||
592 | 'web' => 1, | ||
593 | ]; | ||
594 | $tags = self::$privateLinkDB->linksCountPerTag(['gnu'], 'public'); | ||
595 | |||
596 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
597 | } | ||
598 | |||
599 | /** | ||
600 | * Test linksCountPerTag public tags with filter. | ||
601 | * Equal occurrences should be sorted alphabetically. | ||
602 | */ | ||
603 | public function testCountLinkPerTagPrivateWithFilter() | ||
604 | { | ||
605 | $expected = [ | ||
606 | 'cartoon' => 1, | ||
607 | 'dev' => 1, | ||
608 | 'tag1' => 1, | ||
609 | 'tag2' => 1, | ||
610 | 'tag3' => 1, | ||
611 | 'tag4' => 1, | ||
612 | ]; | ||
613 | $tags = self::$privateLinkDB->linksCountPerTag(['dev'], 'private'); | ||
614 | |||
615 | $this->assertEquals($expected, $tags, var_export($tags, true)); | ||
616 | } | ||
617 | |||
618 | /** | ||
619 | * Make sure that bookmarks with the same timestamp have a consistent order: | ||
620 | * if their creation date is equal, bookmarks are sorted by ID DESC. | ||
621 | */ | ||
622 | public function testConsistentOrder() | ||
623 | { | ||
624 | $nextId = 43; | ||
625 | $creation = DateTime::createFromFormat('Ymd_His', '20190807_130444'); | ||
626 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
627 | for ($i = 0; $i < 4; ++$i) { | ||
628 | $linkDB[$nextId + $i] = [ | ||
629 | 'id' => $nextId + $i, | ||
630 | 'url' => 'http://'. $i, | ||
631 | 'created' => $creation, | ||
632 | 'title' => true, | ||
633 | 'description' => true, | ||
634 | 'tags' => true, | ||
635 | ]; | ||
636 | } | ||
637 | |||
638 | // Check 4 new links 4 times | ||
639 | for ($i = 0; $i < 4; ++$i) { | ||
640 | $linkDB->save('tests'); | ||
641 | $linkDB = new LegacyLinkDB(self::$testDatastore, true, false); | ||
642 | $count = 3; | ||
643 | foreach ($linkDB as $link) { | ||
644 | if ($link['sticky'] === true) { | ||
645 | continue; | ||
646 | } | ||
647 | $this->assertEquals($nextId + $count, $link['id']); | ||
648 | $this->assertEquals('http://'. $count, $link['url']); | ||
649 | if (--$count < 0) { | ||
650 | break; | ||
651 | } | ||
652 | } | ||
653 | } | ||
654 | } | ||
655 | } | ||
diff --git a/tests/legacy/LegacyLinkFilterTest.php b/tests/legacy/LegacyLinkFilterTest.php new file mode 100644 index 00000000..45d7754d --- /dev/null +++ b/tests/legacy/LegacyLinkFilterTest.php | |||
@@ -0,0 +1,511 @@ | |||
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 \Shaarli\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(): void | ||
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 | */ | ||
201 | public function testFilterInvalidDayWithChars() | ||
202 | { | ||
203 | $this->expectException(\Exception::class); | ||
204 | $this->expectExceptionMessageRegExp('/Invalid date format/'); | ||
205 | |||
206 | self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, 'Rainy day, dream away'); | ||
207 | } | ||
208 | |||
209 | /** | ||
210 | * Use an invalid date format | ||
211 | */ | ||
212 | public function testFilterInvalidDayDigits() | ||
213 | { | ||
214 | $this->expectException(\Exception::class); | ||
215 | $this->expectExceptionMessageRegExp('/Invalid date format/'); | ||
216 | |||
217 | self::$linkFilter->filter(LegacyLinkFilter::$FILTER_DAY, '20'); | ||
218 | } | ||
219 | |||
220 | /** | ||
221 | * Retrieve a link entry with its hash | ||
222 | */ | ||
223 | public function testFilterSmallHash() | ||
224 | { | ||
225 | $links = self::$linkFilter->filter(LegacyLinkFilter::$FILTER_HASH, 'IuWvgA'); | ||
226 | |||
227 | $this->assertEquals( | ||
228 | 1, | ||
229 | count($links) | ||
230 | ); | ||
231 | |||
232 | $this->assertEquals( | ||
233 | 'MediaGoblin', | ||
234 | $links[7]['title'] | ||
235 | ); | ||
236 | } | ||
237 | |||
238 | /** | ||
239 | * No link for this hash | ||
240 | */ | ||
241 | public function testFilterUnknownSmallHash() | ||
242 | { | ||
243 | $this->expectException(\Shaarli\Bookmark\Exception\BookmarkNotFoundException::class); | ||
244 | |||
245 | self::$linkFilter->filter(LegacyLinkFilter::$FILTER_HASH, 'Iblaah'); | ||
246 | } | ||
247 | |||
248 | /** | ||
249 | * Full-text search - no result found. | ||
250 | */ | ||
251 | public function testFilterFullTextNoResult() | ||
252 | { | ||
253 | $this->assertEquals( | ||
254 | 0, | ||
255 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'azertyuiop')) | ||
256 | ); | ||
257 | } | ||
258 | |||
259 | /** | ||
260 | * Full-text search - result from a link's URL | ||
261 | */ | ||
262 | public function testFilterFullTextURL() | ||
263 | { | ||
264 | $this->assertEquals( | ||
265 | 2, | ||
266 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'ars.userfriendly.org')) | ||
267 | ); | ||
268 | |||
269 | $this->assertEquals( | ||
270 | 2, | ||
271 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'ars org')) | ||
272 | ); | ||
273 | } | ||
274 | |||
275 | /** | ||
276 | * Full-text search - result from a link's title only | ||
277 | */ | ||
278 | public function testFilterFullTextTitle() | ||
279 | { | ||
280 | // use miscellaneous cases | ||
281 | $this->assertEquals( | ||
282 | 2, | ||
283 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'userfriendly -')) | ||
284 | ); | ||
285 | $this->assertEquals( | ||
286 | 2, | ||
287 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'UserFriendly -')) | ||
288 | ); | ||
289 | $this->assertEquals( | ||
290 | 2, | ||
291 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'uSeRFrIendlY -')) | ||
292 | ); | ||
293 | |||
294 | // use miscellaneous case and offset | ||
295 | $this->assertEquals( | ||
296 | 2, | ||
297 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'RFrIendL')) | ||
298 | ); | ||
299 | } | ||
300 | |||
301 | /** | ||
302 | * Full-text search - result from the link's description only | ||
303 | */ | ||
304 | public function testFilterFullTextDescription() | ||
305 | { | ||
306 | $this->assertEquals( | ||
307 | 1, | ||
308 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'publishing media')) | ||
309 | ); | ||
310 | |||
311 | $this->assertEquals( | ||
312 | 1, | ||
313 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'mercurial w3c')) | ||
314 | ); | ||
315 | |||
316 | $this->assertEquals( | ||
317 | 3, | ||
318 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, '"free software"')) | ||
319 | ); | ||
320 | } | ||
321 | |||
322 | /** | ||
323 | * Full-text search - result from the link's tags only | ||
324 | */ | ||
325 | public function testFilterFullTextTags() | ||
326 | { | ||
327 | $this->assertEquals( | ||
328 | 6, | ||
329 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web')) | ||
330 | ); | ||
331 | |||
332 | $this->assertEquals( | ||
333 | 6, | ||
334 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', 'all')) | ||
335 | ); | ||
336 | |||
337 | $this->assertEquals( | ||
338 | 6, | ||
339 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', 'bla')) | ||
340 | ); | ||
341 | |||
342 | // Private only. | ||
343 | $this->assertEquals( | ||
344 | 1, | ||
345 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', false, 'private')) | ||
346 | ); | ||
347 | |||
348 | // Public only. | ||
349 | $this->assertEquals( | ||
350 | 5, | ||
351 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'web', false, 'public')) | ||
352 | ); | ||
353 | } | ||
354 | |||
355 | /** | ||
356 | * Full-text search - result set from mixed sources | ||
357 | */ | ||
358 | public function testFilterFullTextMixed() | ||
359 | { | ||
360 | $this->assertEquals( | ||
361 | 3, | ||
362 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'free software')) | ||
363 | ); | ||
364 | } | ||
365 | |||
366 | /** | ||
367 | * Full-text search - test exclusion with '-'. | ||
368 | */ | ||
369 | public function testExcludeSearch() | ||
370 | { | ||
371 | $this->assertEquals( | ||
372 | 1, | ||
373 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, 'free -gnu')) | ||
374 | ); | ||
375 | |||
376 | $this->assertEquals( | ||
377 | ReferenceLinkDB::$NB_LINKS_TOTAL - 1, | ||
378 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TEXT, '-revolution')) | ||
379 | ); | ||
380 | } | ||
381 | |||
382 | /** | ||
383 | * Full-text search - test AND, exact terms and exclusion combined, across fields. | ||
384 | */ | ||
385 | public function testMultiSearch() | ||
386 | { | ||
387 | $this->assertEquals( | ||
388 | 2, | ||
389 | count(self::$linkFilter->filter( | ||
390 | LegacyLinkFilter::$FILTER_TEXT, | ||
391 | '"Free Software " stallman "read this" @website stuff' | ||
392 | )) | ||
393 | ); | ||
394 | |||
395 | $this->assertEquals( | ||
396 | 1, | ||
397 | count(self::$linkFilter->filter( | ||
398 | LegacyLinkFilter::$FILTER_TEXT, | ||
399 | '"free software " stallman "read this" -beard @website stuff' | ||
400 | )) | ||
401 | ); | ||
402 | } | ||
403 | |||
404 | /** | ||
405 | * Full-text search - make sure that exact search won't work across fields. | ||
406 | */ | ||
407 | public function testSearchExactTermMultiFieldsKo() | ||
408 | { | ||
409 | $this->assertEquals( | ||
410 | 0, | ||
411 | count(self::$linkFilter->filter( | ||
412 | LegacyLinkFilter::$FILTER_TEXT, | ||
413 | '"designer naming"' | ||
414 | )) | ||
415 | ); | ||
416 | |||
417 | $this->assertEquals( | ||
418 | 0, | ||
419 | count(self::$linkFilter->filter( | ||
420 | LegacyLinkFilter::$FILTER_TEXT, | ||
421 | '"designernaming"' | ||
422 | )) | ||
423 | ); | ||
424 | } | ||
425 | |||
426 | /** | ||
427 | * Tag search with exclusion. | ||
428 | */ | ||
429 | public function testTagFilterWithExclusion() | ||
430 | { | ||
431 | $this->assertEquals( | ||
432 | 1, | ||
433 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, 'gnu -free')) | ||
434 | ); | ||
435 | |||
436 | $this->assertEquals( | ||
437 | ReferenceLinkDB::$NB_LINKS_TOTAL - 1, | ||
438 | count(self::$linkFilter->filter(LegacyLinkFilter::$FILTER_TAG, '-free')) | ||
439 | ); | ||
440 | } | ||
441 | |||
442 | /** | ||
443 | * Test crossed search (terms + tags). | ||
444 | */ | ||
445 | public function testFilterCrossedSearch() | ||
446 | { | ||
447 | $terms = '"Free Software " stallman "read this" @website stuff'; | ||
448 | $tags = 'free'; | ||
449 | $this->assertEquals( | ||
450 | 1, | ||
451 | count(self::$linkFilter->filter( | ||
452 | LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, | ||
453 | array($tags, $terms) | ||
454 | )) | ||
455 | ); | ||
456 | $this->assertEquals( | ||
457 | 2, | ||
458 | count(self::$linkFilter->filter( | ||
459 | LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, | ||
460 | array('', $terms) | ||
461 | )) | ||
462 | ); | ||
463 | $this->assertEquals( | ||
464 | 1, | ||
465 | count(self::$linkFilter->filter( | ||
466 | LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, | ||
467 | array(false, 'PSR-2') | ||
468 | )) | ||
469 | ); | ||
470 | $this->assertEquals( | ||
471 | 1, | ||
472 | count(self::$linkFilter->filter( | ||
473 | LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, | ||
474 | array($tags, '') | ||
475 | )) | ||
476 | ); | ||
477 | $this->assertEquals( | ||
478 | ReferenceLinkDB::$NB_LINKS_TOTAL, | ||
479 | count(self::$linkFilter->filter( | ||
480 | LegacyLinkFilter::$FILTER_TAG | LegacyLinkFilter::$FILTER_TEXT, | ||
481 | '' | ||
482 | )) | ||
483 | ); | ||
484 | } | ||
485 | |||
486 | /** | ||
487 | * Filter bookmarks by #hashtag. | ||
488 | */ | ||
489 | public function testFilterByHashtag() | ||
490 | { | ||
491 | $hashtag = 'hashtag'; | ||
492 | $this->assertEquals( | ||
493 | 3, | ||
494 | count(self::$linkFilter->filter( | ||
495 | LegacyLinkFilter::$FILTER_TAG, | ||
496 | $hashtag | ||
497 | )) | ||
498 | ); | ||
499 | |||
500 | $hashtag = 'private'; | ||
501 | $this->assertEquals( | ||
502 | 1, | ||
503 | count(self::$linkFilter->filter( | ||
504 | LegacyLinkFilter::$FILTER_TAG, | ||
505 | $hashtag, | ||
506 | false, | ||
507 | 'private' | ||
508 | )) | ||
509 | ); | ||
510 | } | ||
511 | } | ||
diff --git a/tests/legacy/LegacyUpdaterTest.php b/tests/legacy/LegacyUpdaterTest.php new file mode 100644 index 00000000..f7391b86 --- /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 \Shaarli\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 | protected function setUp(): void | ||
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 | public function testWriteEmptyUpdatesFile() | ||
85 | { | ||
86 | $this->expectException(\Exception::class); | ||
87 | $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/'); | ||
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 | public function testWriteUpdatesFileNotWritable() | ||
96 | { | ||
97 | $this->expectException(\Exception::class); | ||
98 | $this->expectExceptionMessageRegExp('/Unable to write(.*)/'); | ||
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 | public function testUpdateFailed() | ||
166 | { | ||
167 | $this->expectException(\Exception::class); | ||
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->assertContainsPolyfill('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 | } | ||