1<?php
2/**
3 * Integration Tests for the 404manager plugin through Dokuwiki Request
4 *
5 * @group plugin_404manager
6 * @group plugins
7 *
8 */
9require_once(__DIR__ . '/constant_parameters.php');
10require_once(__DIR__ . '/../action.php');
11
12class manager_plugin_404manager_test extends DokuWikiTest
13{
14
15    // Needed otherwise the plugin is not enabled
16    protected $pluginsEnabled = array('404manager', 'sqlite');
17
18
19    /**
20     * A data provider to create parametrized test
21     * @return array
22     */
23    public function providerDataStoreTypeData()
24    {
25        return array(
26            array(null),
27            array(admin_plugin_404manager::DATA_STORE_TYPE_CONF_FILE),
28            array(admin_plugin_404manager::DATA_STORE_TYPE_SQLITE)
29        );
30    }
31
32
33    /**
34     * Test a redirect to an external Web Site
35     *
36     * @dataProvider providerDataStoreTypeData
37     * @param $dataStoreType
38     * @throws Exception
39     */
40    public function test_externalRedirect($dataStoreType)
41    {
42
43        $redirectManager = admin_plugin_404manager::get()
44            ->setDataStoreType($dataStoreType);
45
46        if ($redirectManager->isRedirectionPresent(constant_parameters::$PAGE_REDIRECTED_TO_EXTERNAL_WEBSITE)) {
47            $redirectManager->deleteRedirection(constant_parameters::$PAGE_REDIRECTED_TO_EXTERNAL_WEBSITE);
48        }
49
50        $externalURL = 'http://gerardnico.com';
51        $redirectManager->addRedirection(constant_parameters::$PAGE_REDIRECTED_TO_EXTERNAL_WEBSITE, $externalURL);
52
53        // Read only otherwise you are redirected to the Edit Mode
54        global $AUTH_ACL;
55        $aclReadOnlyFile = constant_parameters::$DIR_RESOURCES . '/acl.auth.read_only.php';
56        $AUTH_ACL = file($aclReadOnlyFile);
57
58        $request = new TestRequest();
59        $request->get(array('id' => constant_parameters::$PAGE_REDIRECTED_TO_EXTERNAL_WEBSITE), '/doku.php');
60        $response = $request->execute();
61
62        $locationHeader = $response->getHeader("Location");
63
64        $this->assertEquals("Location: " . $externalURL, $locationHeader, "The page was redirected");
65
66    }
67
68    /**
69     * Test a redirect to the search engine
70     * @dataProvider providerDataStoreTypeData
71     * @param $dataStoreType
72     */
73    public function test_internalRedirectToSearchEngine($dataStoreType)
74    {
75
76        admin_plugin_404manager::get()->setDataStoreType($dataStoreType);
77
78        global $conf;
79        $conf ['plugin'][constant_parameters::$PLUGIN_BASE]['ActionReaderFirst'] = action_plugin_404manager::GO_TO_SEARCH_ENGINE;
80
81        global $AUTH_ACL;
82        $aclReadOnlyFile = constant_parameters::$DIR_RESOURCES . '/acl.auth.read_only.php';
83        $AUTH_ACL = file($aclReadOnlyFile);
84
85        $request = new TestRequest();
86        $request->get(array('id' => constant_parameters::$PAGE_DOES_NOT_EXIST_NO_REDIRECTION_ID), '/doku.php');
87        $response = $request->execute();
88
89
90        $locationHeader = $response->getHeader("Location");
91        $components = parse_url($locationHeader);
92        parse_str($components['query'], $queryKeys);
93        /** @noinspection PhpUndefinedMethodInspection */
94        $this->assertEquals($queryKeys['do'], 'search', "The page was redirected to the search page");
95        $this->assertEquals($queryKeys['id'], constant_parameters::$PAGE_DOES_NOT_EXIST_NO_REDIRECTION_ID, "The Id of the source page is the asked page");
96        $this->assertNotNull($queryKeys['q'], "The query must be not null");
97        $this->assertEquals($queryKeys[action_plugin_404manager::QUERY_STRING_ORIGIN_PAGE], constant_parameters::$PAGE_DOES_NOT_EXIST_NO_REDIRECTION_ID, "The 404 id must be present");
98        $this->assertEquals($queryKeys[action_plugin_404manager::QUERY_STRING_REDIR_TYPE], action_plugin_404manager::REDIRECT_SEARCH_ENGINE, "The redirect type is known");
99
100
101    }
102
103
104    /**
105     * Test a redirect to an internal page that does not exist
106     * Where a actionReaderFirst is search
107     * @dataProvider providerDataStoreTypeData
108     * @param $dataStoreType
109     */
110    public function test_goToSearchEngineBasic($dataStoreType)
111    {
112
113        $conf ['plugin'][constant_parameters::$PLUGIN_BASE]['ActionReaderFirst'] = action_plugin_404manager::GO_TO_SEARCH_ENGINE;
114
115        $redirectManager = admin_plugin_404manager::get()->setDataStoreType($dataStoreType);
116        if ($redirectManager->isRedirectionPresent(constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE)) {
117            $redirectManager->deleteRedirection(constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE);
118        }
119        $redirectManager->addRedirection(constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE, constant_parameters::$PAGE_DOES_NOT_EXIST_ID);
120
121        // Read only otherwise, you go in edit mode
122        global $AUTH_ACL;
123        $aclReadOnlyFile = constant_parameters::$DIR_RESOURCES . '/acl.auth.read_only.php';
124        $AUTH_ACL = file($aclReadOnlyFile);
125
126        $request = new TestRequest();
127        $request->get(array('id' => constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE), '/doku.php');
128        $response = $request->execute();
129
130
131        $locationHeader = $response->getHeader("Location");
132        $components = parse_url($locationHeader);
133        parse_str($components['query'], $queryKeys);
134        $this->assertEquals($queryKeys['do'], 'search', "The page was redirected to the search page");
135        $this->assertEquals($queryKeys['id'], constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE, "The Id of the source page is the asked page");
136        $this->assertNotNull($queryKeys['q'], "The query must be not null");
137        $this->assertEquals($queryKeys[action_plugin_404manager::QUERY_STRING_ORIGIN_PAGE], constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE, "The 404 id must be present");
138        $this->assertEquals($queryKeys[action_plugin_404manager::QUERY_STRING_REDIR_TYPE], action_plugin_404manager::REDIRECT_SEARCH_ENGINE, "The redirect type is known");
139
140
141    }
142
143    /**
144     * Test a redirect to an internal page that exist
145     *
146     * @dataProvider providerDataStoreTypeData
147     * @param $dataStoreType
148     */
149    public function test_internalRedirectToExistingPage($dataStoreType)
150    {
151
152
153        $redirectManager = admin_plugin_404manager::get()->setDataStoreType($dataStoreType);
154        if ($redirectManager->isRedirectionPresent(constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE)) {
155            $redirectManager->deleteRedirection(constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE);
156        }
157        $redirectManager->addRedirection(constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE, constant_parameters::$EXPLICIT_REDIRECT_PAGE_TARGET);
158
159        // Create the target Page
160        saveWikiText(constant_parameters::$EXPLICIT_REDIRECT_PAGE_TARGET, 'EXPLICIT_REDIRECT_PAGE_TARGET', 'Test initialization');
161
162        $conf ['plugin'][constant_parameters::$PLUGIN_BASE]['ActionReaderFirst'] = action_plugin_404manager::GO_TO_SEARCH_ENGINE;
163
164        // Read only otherwise, you go in edit mode
165        global $AUTH_ACL;
166        $aclReadOnlyFile = constant_parameters::$DIR_RESOURCES . '/acl.auth.read_only.php';
167        $AUTH_ACL = file($aclReadOnlyFile);
168
169
170        $request = new TestRequest();
171        $request->get(array('id' => constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE), '/doku.php');
172        $response = $request->execute();
173
174
175        $locationHeader = $response->getHeader("Location");
176        $components = parse_url($locationHeader);
177        parse_str($components['query'], $queryKeys);
178        $this->assertNull($queryKeys['do'], "The page is only shown");
179
180        $this->assertEquals(constant_parameters::$EXPLICIT_REDIRECT_PAGE_TARGET, $queryKeys['id'], "The Id of the page is the target page");
181
182        $this->assertEquals(constant_parameters::$EXPLICIT_REDIRECT_PAGE_SOURCE, $queryKeys[action_plugin_404manager::QUERY_STRING_ORIGIN_PAGE], "The 404 id must be present");
183        $this->assertEquals(action_plugin_404manager::REDIRECT_TARGET_PAGE_FROM_DATASTORE, $queryKeys[action_plugin_404manager::QUERY_STRING_REDIR_TYPE], "The redirect type is known");
184
185
186    }
187
188    /**
189     * Test a redirect to an internal page that was chosen through BestNamePage
190     * with a relocation in the same branch
191     * @dataProvider providerDataStoreTypeData
192     * @param $dataStoreType
193     */
194    public function test_internalRedirectToBestNamePageSameBranch($dataStoreType)
195    {
196
197        $redirectManager = admin_plugin_404manager::get()->setDataStoreType($dataStoreType);
198        if ($redirectManager->isRedirectionPresent(constant_parameters::$REDIRECT_BEST_PAGE_NAME_SOURCE)) {
199            $redirectManager->deleteRedirection(constant_parameters::$REDIRECT_BEST_PAGE_NAME_SOURCE);
200        }
201
202        // Create the target Page
203        saveWikiText(constant_parameters::$REDIRECT_BEST_PAGE_NAME_TARGET_SAME_BRANCH, 'REDIRECT Best Page Name Same Branch', 'Test initialization');
204        // Add the page to the index, otherwise, it will not be find by the ft_lookup
205        idx_addPage(constant_parameters::$REDIRECT_BEST_PAGE_NAME_TARGET_SAME_BRANCH);
206
207
208        // Read only otherwise, you go in edit mode
209        global $AUTH_ACL;
210        $aclReadOnlyFile = constant_parameters::$DIR_RESOURCES . '/acl.auth.read_only.php';
211        $AUTH_ACL = file($aclReadOnlyFile);
212
213        global $conf;
214        $conf ['plugin'][constant_parameters::$PLUGIN_BASE]['ActionReaderFirst'] = action_plugin_404manager::GO_TO_BEST_PAGE_NAME;
215        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForSamePageName'] = 4;
216        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForStartPage'] = 3;
217        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForSameNamespace'] = 5;
218
219        $request = new TestRequest();
220        $request->get(array('id' => constant_parameters::$REDIRECT_BEST_PAGE_NAME_SOURCE), '/doku.php');
221        $response = $request->execute();
222
223
224        $locationHeader = $response->getHeader("Location");
225        $components = parse_url($locationHeader);
226        parse_str($components['query'], $queryKeys);
227        $this->assertNull($queryKeys['do'], "The page has no action than show");
228        $this->assertEquals(constant_parameters::$REDIRECT_BEST_PAGE_NAME_TARGET_SAME_BRANCH, $queryKeys['id'], "The Id of the source page is the asked page");
229        $this->assertEquals(constant_parameters::$REDIRECT_BEST_PAGE_NAME_SOURCE, $queryKeys[action_plugin_404manager::QUERY_STRING_ORIGIN_PAGE], "The 404 id must be present");
230        $this->assertEquals(action_plugin_404manager::REDIRECT_SOURCE_BEST_PAGE_NAME, $queryKeys[action_plugin_404manager::QUERY_STRING_REDIR_TYPE], "The redirect type is known");
231
232
233    }
234
235    /**
236     * Test a redirect to an internal page that was chosen through BestNamePage
237     * with a relocation to the same branch (the minimum target Id length)
238     * even if there is another page with the same name in an other branch
239     * @dataProvider providerDataStoreTypeData
240     * @param $dataStoreType
241     */
242    public function test_internalRedirectToBestNamePageOtherBranch($dataStoreType)
243    {
244
245
246        $redirectManager = admin_plugin_404manager::get()->setDataStoreType($dataStoreType);
247        if ($redirectManager->isRedirectionPresent(constant_parameters::$REDIRECT_BEST_PAGE_NAME_SOURCE)) {
248            $redirectManager->deleteRedirection(constant_parameters::$REDIRECT_BEST_PAGE_NAME_SOURCE);
249        }
250
251
252        // Create the target Pages and add the pages to the index, otherwise, they will not be find by the ft_lookup
253        saveWikiText(constant_parameters::$REDIRECT_BEST_PAGE_NAME_TARGET_SAME_BRANCH, 'REDIRECT Best Page Name Same Branch', 'Test initialization');
254        idx_addPage(constant_parameters::$REDIRECT_BEST_PAGE_NAME_TARGET_SAME_BRANCH);
255        saveWikiText(constant_parameters::$REDIRECT_BEST_PAGE_NAME_TARGET_OTHER_BRANCH, 'REDIRECT Best Page Name Other Branch', 'Test initialization');
256        idx_addPage(constant_parameters::$REDIRECT_BEST_PAGE_NAME_TARGET_OTHER_BRANCH);
257
258
259        // Read only otherwise, you go in edit mode
260        global $AUTH_ACL;
261        $aclReadOnlyFile = constant_parameters::$DIR_RESOURCES . '/acl.auth.read_only.php';
262        $AUTH_ACL = file($aclReadOnlyFile);
263
264        global $conf;
265        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['ActionReaderFirst'] = action_plugin_404manager::GO_TO_BEST_PAGE_NAME;
266        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForSamePageName'] = 4;
267        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForStartPage'] = 3;
268        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForSameNamespace'] = 5;
269
270        $request = new TestRequest();
271        $request->get(array('id' => constant_parameters::$REDIRECT_BEST_PAGE_NAME_SOURCE), '/doku.php');
272        $response = $request->execute();
273
274
275        $locationHeader = $response->getHeader("Location");
276        $components = parse_url($locationHeader);
277        parse_str($components['query'], $queryKeys);
278
279        $this->assertNull($queryKeys['do'], "The is only shown");
280        $this->assertEquals(constant_parameters::$REDIRECT_BEST_PAGE_NAME_TARGET_SAME_BRANCH, $queryKeys['id'], "The Id of the source page is the asked page");
281        $this->assertEquals(constant_parameters::$REDIRECT_BEST_PAGE_NAME_SOURCE, $queryKeys[action_plugin_404manager::QUERY_STRING_ORIGIN_PAGE], "The 404 id must be present");
282        $this->assertEquals(action_plugin_404manager::REDIRECT_SOURCE_BEST_PAGE_NAME, $queryKeys[action_plugin_404manager::QUERY_STRING_REDIR_TYPE], "The redirect type is known");
283
284
285    }
286
287    /**
288     * Test a redirect to a namespace start page (that begins with start)
289     * It must happens when a page exists within another namespace that is completely not related to the old one.
290     * @dataProvider providerDataStoreTypeData
291     * @param $dataStoreType
292     */
293    public function test_internalRedirectToNamespaceStartPage($dataStoreType)
294    {
295
296
297        $redirectManager = admin_plugin_404manager::get()->setDataStoreType($dataStoreType);
298        if ($redirectManager->isRedirectionPresent(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_SOURCE)) {
299            $redirectManager->deleteRedirection(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_SOURCE);
300        }
301
302        // Create the target Pages and add the pages to the index, otherwise, they will not be find by the ft_lookup
303        saveWikiText(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_BAD_TARGET, 'Page with the same name', 'but without any common name (namespace) in the path');
304        idx_addPage(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_BAD_TARGET);
305        saveWikiText(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_GOOD_TARGET, 'The start page of the 404 page namespace', 'Test initialization');
306        idx_addPage(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_GOOD_TARGET);
307
308        // Read only otherwise, you go in edit mode
309        global $AUTH_ACL;
310        $aclReadOnlyFile = constant_parameters::$DIR_RESOURCES . '/acl.auth.read_only.php';
311        $AUTH_ACL = file($aclReadOnlyFile);
312
313        global $conf;
314        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['ActionReaderFirst'] = action_plugin_404manager::GO_TO_BEST_NAMESPACE;
315        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForSamePageName'] = 4;
316        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForStartPage'] = 3;
317        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForSameNamespace'] = 5;
318        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WordsSeparator'] = ':';
319
320        $request = new TestRequest();
321        $request->get(array('id' => constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_SOURCE), '/doku.php');
322        $response = $request->execute();
323
324
325        $locationHeader = $response->getHeader("Location");
326        $components = parse_url($locationHeader);
327        parse_str($components['query'], $queryKeys);
328
329        $this->assertNull($queryKeys['do'], "The page was only shown");
330
331        // $REDIRECT_TO_NAMESPACE_START_PAGE_BAD_TARGET got a score of 9 (The base namespace 5 + same page name 4)
332        // $REDIRECT_TO_NAMESPACE_START_PAGE_GOOD_TARGET got a score of 13 (The base namespace 5 + the same namspace 5 + start page 3)
333        $this->assertEquals(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_GOOD_TARGET, $queryKeys['id'], "The Id of the source page is the asked page");
334        $this->assertNotEquals(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_BAD_TARGET, $queryKeys['id'], "The Id of the source page is the asked page");
335
336        // 404 Params
337        $this->assertEquals(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_SOURCE, $queryKeys[action_plugin_404manager::QUERY_STRING_ORIGIN_PAGE], "The 404 id must be present");
338        $this->assertEquals(action_plugin_404manager::REDIRECT_SOURCE_BEST_NAMESPACE, $queryKeys[action_plugin_404manager::QUERY_STRING_REDIR_TYPE], "The redirect type is known");
339
340
341    }
342
343    /**
344     * Test a redirect to a namespace start page (ie the start page has the name of its parent, not start as in the conf['start'] parameters )
345     * It must happens when a page exists within another namespace that is completely not related to the old one.
346     *
347     * @dataProvider providerDataStoreTypeData
348     * @param $dataStoreType
349     * @throws Exception
350     */
351    public function test_internalRedirectToNamespaceStartPageWithParentName($dataStoreType)
352    {
353
354
355        $redirectManager = admin_plugin_404manager::get()->setDataStoreType($dataStoreType);
356        if ($redirectManager->isRedirectionPresent(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_PARENT_SOURCE)) {
357            $redirectManager->deleteRedirection(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_PARENT_SOURCE);
358        }
359
360
361        // Create the target Pages and add the pages to the index, otherwise, they will not be find by the ft_lookup
362        saveWikiText(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_PARENT_BAD_TARGET, 'Page with the same name', 'but without any common name (namespace) in the path');
363        idx_addPage(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_PARENT_BAD_TARGET);
364        saveWikiText(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_PARENT_GOOD_TARGET, 'The start page that has the same name that it\'s parent', 'Test initialization');
365        idx_addPage(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_PARENT_GOOD_TARGET);
366
367        // Read only otherwise, you go in edit mode
368        global $AUTH_ACL;
369        $aclReadOnlyFile = constant_parameters::$DIR_RESOURCES . '/acl.auth.read_only.php';
370        $AUTH_ACL = file($aclReadOnlyFile);
371
372        global $conf;
373        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['ActionReaderFirst'] = action_plugin_404manager::GO_TO_BEST_PAGE_NAME;
374        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForSamePageName'] = 4;
375        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForStartPage'] = 3;
376        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WeightFactorForSameNamespace'] = 5;
377        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['WordsSeparator'] = ':';
378        $conf['plugin'][constant_parameters::$PLUGIN_BASE]['ShowPageNameIsNotUnique'] = 1;
379
380        $request = new TestRequest();
381        $request->get(array('id' => constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_PARENT_SOURCE), '/doku.php');
382
383        $response = $request->execute();
384
385        $locationHeader = $response->getHeader("Location");
386        $components = parse_url($locationHeader);
387        parse_str($components['query'], $queryKeys);
388        $this->assertNull($queryKeys['do'], "The page is only shown");
389
390        // 404manager:ns_branch2:redirect_to_namespace_start_page = score 9
391        // 404manager:ns_branch3:ns_branch3
392        // $REDIRECT_TO_NAMESPACE_START_PAGE_BAD_TARGET got a score of 9 (The base namespace 5 + same page name 4)
393        // $REDIRECT_TO_NAMESPACE_START_PAGE_GOOD_TARGET got a score of 13 (The base namespace 5 + the same namespace 5 + start page 3)
394        $this->assertEquals(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_PARENT_GOOD_TARGET, $queryKeys['id'], "The Id is the target page");
395        $this->assertNotEquals(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_PARENT_BAD_TARGET, $queryKeys['id'], "The Id is not the source page");
396
397        $this->assertEquals(constant_parameters::$REDIRECT_TO_NAMESPACE_START_PAGE_PARENT_SOURCE, $queryKeys[action_plugin_404manager::QUERY_STRING_ORIGIN_PAGE], "The 404 id must be present");
398        $this->assertEquals(action_plugin_404manager::REDIRECT_SOURCE_BEST_PAGE_NAME, $queryKeys[action_plugin_404manager::QUERY_STRING_REDIR_TYPE], "The redirect type is known");
399
400
401    }
402
403    /**
404     * Test basic redirections operations
405     *
406     * @dataProvider providerDataStoreTypeData
407     * @param $dataStoreType
408     */
409    public function testRedirectionsOperations($dataStoreType)
410    {
411        $targetPage = 'testRedirectionsOperations:test';
412        saveWikiText($targetPage, 'Test ', 'but without any common name (namespace) in the path');
413        idx_addPage($targetPage);
414        /** @var admin_plugin_404manager $redirectManager */
415        $redirectManager = admin_plugin_404manager::get()
416            ->setDataStoreType($dataStoreType);
417
418
419        $redirectManager->deleteAllRedirections();
420        $count = $redirectManager->countRedirections();
421        $this->assertEquals(0, $count, "The number of redirection is zero");
422        $sourcePageId = "source";
423        $redirectManager->addRedirection($sourcePageId, $targetPage);
424        $count = $redirectManager->countRedirections();
425        $this->assertEquals(1, $count, "The number of redirection is one");
426        $bool = $redirectManager->isRedirectionPresent($sourcePageId);
427        $this->assertEquals(true, $bool, "The redirection is present");
428
429
430    }
431
432
433    /**
434     * Test the migration of a data store from file to Sqlite
435     */
436    public function testMigrateDataStore()
437    {
438
439        $targetPage = 'testMigrateDataStore:test';
440        saveWikiText($targetPage, 'Test ', 'test summary');
441        idx_addPage($targetPage);
442
443        // Cleaning
444        /** @var admin_plugin_404manager $redirectManager */
445        $redirectManager = admin_plugin_404manager::get()
446            ->setDataStoreType(admin_plugin_404manager::DATA_STORE_TYPE_SQLITE);
447        $redirectManager->deleteAllRedirections();
448        $filenameMigrated = admin_plugin_404manager::DATA_STORE_CONF_FILE_PATH . '.migrated';
449        if (file_exists($filenameMigrated)){
450            unlink($filenameMigrated);
451        }
452
453        // Create a conf file
454        $redirectManager->setDataStoreType(admin_plugin_404manager::DATA_STORE_TYPE_CONF_FILE);
455        $redirectManager->deleteAllRedirections();
456        $sourcePageIdValidated = "doesNotExistValidateRedirections";
457        $redirectManager->addRedirection($sourcePageIdValidated, $targetPage);
458        $redirectManager->validateRedirection($sourcePageIdValidated);
459        $sourcePageIdNotValidated = "doesNotExistNotValidateRedirections";
460        $redirectManager->addRedirection($sourcePageIdNotValidated, $targetPage);
461
462        $count = $redirectManager->countRedirections();
463        $this->assertEquals(2, $count, "The number of redirection is 2 in the conf file");
464
465        $this->assertEquals(true, file_exists(admin_plugin_404manager::DATA_STORE_CONF_FILE_PATH), "The file was created");
466
467        // Settings the store will trigger the migration
468        $redirectManager->setDataStoreType(admin_plugin_404manager::DATA_STORE_TYPE_SQLITE);
469
470        $count = $redirectManager->countRedirections();
471        $this->assertEquals(1, $count, "The number of redirection is 1");
472
473        $this->assertEquals(false, file_exists(admin_plugin_404manager::DATA_STORE_CONF_FILE_PATH), "The file does not exist anymore");
474        $this->assertEquals(true, file_exists($filenameMigrated), "The file migrated exist");
475
476
477
478    }
479
480
481}
482