1<?php
2
3// must be run within Dokuwiki
4if (!defined('DOKU_INC')) die();
5
6/**
7 * Test cases for namespace move functionality of the move plugin
8 *
9 * @group plugin_move
10 * @group plugins
11 */
12class plugin_move_namespace_move_test extends DokuWikiTest {
13
14    public function setUp() {
15        $this->pluginsEnabled[] = 'move';
16        parent::setUp();
17    }
18
19    /**
20     * @coversNothing
21     */
22    public function tearDown() {
23        /** @var helper_plugin_move_plan $plan  */
24        $plan = plugin_load('helper', 'move_plan');
25        $plan->abort();
26
27        io_rmdir(DOKU_TMP_DATA."pages/newns",true);
28        io_rmdir(DOKU_TMP_DATA."media/newns",true);
29        io_rmdir(DOKU_TMP_DATA."meta/newns",true);
30
31        parent::tearDown();
32    }
33
34    /**
35     * This is an integration test, which checks the correct working of an entire namespace move.
36     * Hence it is not an unittest, hence it @coversNothing
37     *
38     * @group slow
39     */
40    public function test_move_wiki_namespace() {
41        global $AUTH_ACL;
42
43        $AUTH_ACL[] = "wiki:*\t@ALL\t16";
44
45        idx_addPage('wiki:dokuwiki');
46        idx_addPage('wiki:syntax');
47
48        /** @var helper_plugin_move_plan $plan  */
49        $plan = plugin_load('helper', 'move_plan');
50
51        $this->assertFalse($plan->inProgress());
52
53        $plan->addPageNamespaceMove('wiki', 'foo');
54        $plan->addMediaNamespaceMove('wiki', 'foo');
55
56        $plan->commit();
57
58        $this->assertSame(1, $plan->nextStep(),'pages');
59        $this->assertSame(1, $plan->nextStep(),'media');
60        $this->assertSame(1, $plan->nextStep(),'missing');
61        $this->assertSame(1, $plan->nextStep(),'namespace');
62        $this->assertSame(1, $plan->nextStep(),'autorewrite');
63        $this->assertSame(0, $plan->nextStep(),'done');
64
65        $this->assertFileExists(wikiFN('foo:dokuwiki'));
66        $this->assertFileNotExists(wikiFN('wiki:syntax'));
67        $this->assertFileExists(mediaFN('foo:dokuwiki-128.png'));
68    }
69
70    /**
71     * This is an integration test, which checks the correct working of an entire namespace move.
72     * Hence it is not an unittest, hence it @coversNothing
73     *
74     * @group slow
75     */
76    public function test_move_missing() {
77        saveWikiText('oldspace:page', '[[missing]]', 'setup');
78        idx_addPage('oldspace:page');
79
80        /** @var helper_plugin_move_plan $plan  */
81        $plan = plugin_load('helper', 'move_plan');
82
83        $this->assertFalse($plan->inProgress());
84
85        $plan->addPageNamespaceMove('oldspace', 'newspace');
86
87        $plan->commit();
88
89        $this->assertSame(1, $plan->nextStep(),'page');
90        $this->assertSame(1, $plan->nextStep(),'missing');
91        $this->assertSame(1, $plan->nextStep(),'namespace');
92        $this->assertSame(1, $plan->nextStep(),'autorewrite');
93        $this->assertSame(0, $plan->nextStep(),'done');
94
95        $this->assertFileExists(wikiFN('newspace:page'));
96        $this->assertFileNotExists(wikiFN('oldspace:page'));
97
98        $this->assertEquals('[[missing]]', rawWiki('newspace:page'));
99    }
100
101    /**
102     * @covers helper_plugin_move_plan::findAffectedPages
103     * @uses Doku_Indexer
104     */
105    public function test_move_affected() {
106        saveWikiText('oldaffectedspace:page', '[[missing]]', 'setup');
107        idx_addPage('oldaffectedspace:page');
108        /** @var helper_plugin_move_plan $plan  */
109        $plan = plugin_load('helper', 'move_plan');
110
111        $this->assertFalse($plan->inProgress());
112
113        $plan->addPageNamespaceMove('oldaffectedspace', 'newaffectedspace');
114
115        $plan->commit();
116
117        $affected_file = file(TMP_DIR . '/data/meta/__move_affected');
118        $this->assertSame('newaffectedspace:page',trim($affected_file[0]));
119    }
120
121    /**
122     * This is an integration test, which checks the correct working of an entire namespace move.
123     * Hence it is not an unittest, hence it @coversNothing
124     *
125     * @group slow
126     */
127    function test_move_large_ns(){
128
129        $this->markTestSkipped(
130                'This test randomly fails with the page "testns:start" being moved, but "start" not being rewritten in the request.'
131        );
132
133        global $conf;
134
135        $test = '[[testns:start]] [[testns:test_page17]]';
136        $summary = 'testsetup';
137
138
139        saveWikiText(':start', $test, $summary);
140        idx_addPage(':start');
141        saveWikiText('testns:start', $test, $summary);
142        idx_addPage('testns:start');
143        saveWikiText('testns:test_page1', $test, $summary);
144        idx_addPage('testns:test_page1');
145        saveWikiText('testns:test_page2', $test, $summary);
146        idx_addPage('testns:test_page2');
147        saveWikiText('testns:test_page3', $test, $summary);
148        idx_addPage('testns:test_page3');
149        saveWikiText('testns:test_page4', $test, $summary);
150        idx_addPage('testns:test_page4');
151        saveWikiText('testns:test_page5', $test, $summary);
152        idx_addPage('testns:test_page5');
153        saveWikiText('testns:test_page6', $test, $summary);
154        idx_addPage('testns:test_page6');
155        saveWikiText('testns:test_page7', $test, $summary);
156        idx_addPage('testns:test_page7');
157        saveWikiText('testns:test_page8', $test, $summary);
158        idx_addPage('testns:test_page8');
159        saveWikiText('testns:test_page9', $test, $summary);
160        idx_addPage('testns:test_page9');
161        saveWikiText('testns:test_page10', $test, $summary);
162        idx_addPage('testns:test_page10');
163        saveWikiText('testns:test_page11', $test, $summary);
164        idx_addPage('testns:test_page11');
165        saveWikiText('testns:test_page12', $test, $summary);
166        idx_addPage('testns:test_page12');
167        saveWikiText('testns:test_page13', $test, $summary);
168        idx_addPage('testns:test_page13');
169        saveWikiText('testns:test_page14', $test, $summary);
170        idx_addPage('testns:test_page14');
171        saveWikiText('testns:test_page15', $test, $summary);
172        idx_addPage('testns:test_page15');
173        saveWikiText('testns:test_page16', $test, $summary);
174        idx_addPage('testns:test_page16');
175        saveWikiText('testns:test_page17', $test, $summary);
176        idx_addPage('testns:test_page17');
177        saveWikiText('testns:test_page18', $test, $summary);
178        idx_addPage('testns:test_page18');
179        saveWikiText('testns:test_page19', $test, $summary);
180        idx_addPage('testns:test_page19');
181
182        $conf['plugin']['move']['autorewrite'] = 0;
183
184        /** @var helper_plugin_move_plan $plan  */
185        $plan = plugin_load('helper', 'move_plan');
186
187        $this->assertFalse($plan->inProgress());
188
189        $plan->addPageNamespaceMove('testns', 'foo:testns');
190
191        $plan->commit();
192        global $conf;
193        $lockfile = $conf['lockdir'] . 'move.lock';
194
195        $this->assertSame(10, $plan->nextStep(),"After processing first chunk of pages, 10 steps should be left");
196
197        $request = new TestRequest();
198        $response = $request->get();
199        $actual_response = $response->getContent();
200        //clean away clutter
201        $actual_response = substr($actual_response,strpos($actual_response,"<!-- wikipage start -->") + 23);
202        $actual_response = substr($actual_response,strpos($actual_response, 'doku.php'));
203        $actual_response = substr($actual_response,0,strpos($actual_response,"<!-- wikipage stop -->"));
204        $actual_response = trim($actual_response);
205        $actual_response = rtrim($actual_response,"</p>");
206        $actual_response = trim($actual_response);
207
208        $expected_response = 'doku.php?id=foo:testns:start" class="wikilink1" title="foo:testns:start">testns</a> <a href="/./doku.php?id=testns:test_page17" class="wikilink1" title="testns:test_page17">test_page17</a>';
209        $this->assertSame($expected_response,$actual_response); // todo: this assert fails occaisionally, but not reproduciably. It then has the following oputput: <a href="/./doku.php?id=testns:start" class="wikilink2" title="testns:start" rel="nofollow">testns</a> <a href="/./doku.php?id=testns:test_page17" class="wikilink1" title="testns:test_page17">test_page17</a>
210
211        $expected_file_contents = '[[testns:start]] [[testns:test_page17]]';
212        $start_file = file(TMP_DIR . '/data/pages/start.txt');
213        $actual_file_contents = $start_file[0];
214        $this->assertSame($expected_file_contents,$actual_file_contents);
215
216        /** @var helper_plugin_move_rewrite $rewrite */
217        $rewrite = plugin_load('helper', 'move_rewrite');
218        $expected_move_meta = array('origin'=> 'testns:start', 'pages' => array(array('testns:start','foo:testns:start')),'media' => array());
219        $actual_move_media = $rewrite->getMoveMeta('foo:testns:start');
220        $this->assertSame($expected_move_meta,$actual_move_media);
221        $this->assertFileExists($lockfile);
222
223    }
224
225    /**
226     * This is an integration test, which checks the correct working of an entire namespace move.
227     * Hence it is not an unittest, hence it @coversNothing
228     *
229     * @group slow
230     */
231    public function test_move_small_namespace_pages() {
232        global $AUTH_ACL;
233
234        $AUTH_ACL[] = "oldns:*\t@ALL\t16";
235        $AUTH_ACL[] = "newns:*\t@ALL\t16";
236
237        saveWikiText('start', '[[oldns:start]] [[oldns:page]] [[oldns:missing]]', 'setup');
238        idx_addPage('start');
239        saveWikiText('oldns:start', '[[oldns:start]] [[oldns:page]] [[oldns:missing]] [[missing]] [[page]]', 'setup');
240        idx_addPage('oldns:start');
241        saveWikiText('oldns:page', '[[oldns:start]] [[oldns:page]] [[oldns:missing]] [[missing]] [[start]]', 'setup');
242        idx_addPage('oldns:page');
243
244
245        /** @var helper_plugin_move_plan $plan  */
246        $plan = plugin_load('helper', 'move_plan');
247
248        $this->assertFalse($plan->inProgress());
249
250        $plan->addPageNamespaceMove('oldns', 'newns');
251
252        $plan->commit();
253
254        $this->assertSame(1, $plan->nextStep(), 'pages');
255        $this->assertSame(1, $plan->nextStep(), 'missing');
256        $this->assertSame(1, $plan->nextStep(), 'namespace');
257        $this->assertSame(1, $plan->nextStep(), 'autorewrite');
258        $this->assertSame(0, $plan->nextStep(), 'done');
259
260        $this->assertFileExists(wikiFN('newns:start'));
261        $this->assertFileExists(wikiFN('newns:page'));
262        $this->assertFileNotExists(wikiFN('oldns:start'));
263        $this->assertFileNotExists(wikiFN('oldns:page'));
264
265        $this->assertSame('[[newns:start]] [[newns:page]] [[newns:missing]] [[missing]] [[page]]',rawWiki('newns:start'));
266        $this->assertSame('[[newns:start]] [[newns:page]] [[newns:missing]] [[missing]] [[start]]',rawWiki('newns:page'));
267        $this->assertSame('[[newns:start]] [[newns:page]] [[newns:missing]]',rawWiki('start'));
268    }
269
270    /**
271     * This is an integration test, which checks the correct working of an entire namespace move.
272     * Hence it is not an unittest, hence it @coversNothing
273     *
274     * @group slow
275     */
276    public function test_move_small_namespace_media() {
277        global $AUTH_ACL;
278
279        $AUTH_ACL[] = "oldns:*\t@ALL\t16";
280        $AUTH_ACL[] = "newns:*\t@ALL\t16";
281
282        $filepath = DOKU_TMP_DATA.'media/oldns/oldnsimage.png';
283        io_makeFileDir($filepath);
284        io_saveFile($filepath,'');
285        saveWikiText('start', '{{oldns:oldnsimage.png}} {{oldns:oldnsimage_missing.png}} {{image_missing.png}}', 'setup');
286        idx_addPage('start');
287
288        /** @var helper_plugin_move_plan $plan  */
289        $plan = plugin_load('helper', 'move_plan');
290
291        $this->assertFalse($plan->inProgress());
292
293        $plan->addMediaNamespaceMove('oldns', 'newns');
294
295        $plan->commit();
296
297        $this->assertSame(1, $plan->nextStep(), 'media');
298        $this->assertSame(1, $plan->nextStep(), 'missing_media');
299        $this->assertSame(1, $plan->nextStep(), 'autorewrite');
300        $this->assertSame(0, $plan->nextStep(), 'done');
301
302        $this->assertFileExists(mediaFN('newns:oldnsimage.png'));
303        $this->assertFileNotExists(mediaFN('oldns:oldnsimage.png'));
304
305        $this->assertSame('{{newns:oldnsimage.png}} {{newns:oldnsimage_missing.png}} {{image_missing.png}}',rawWiki('start'));
306    }
307
308    /**
309     * This is an integration test, which checks the correct working of an entire namespace move.
310     * Hence it is not an unittest, hence it @coversNothing
311     *
312     * @group slow
313     */
314    public function test_move_small_namespace_media_affected() {
315        global $AUTH_ACL;
316
317        $AUTH_ACL[] = "oldns:*\t@ALL\t16";
318        $AUTH_ACL[] = "newns:*\t@ALL\t16";
319
320        $filepath = DOKU_TMP_DATA.'media/oldns/oldnsimage.png';
321        io_makeFileDir($filepath);
322        io_saveFile($filepath,'');
323        saveWikiText('oldns:start', '{{:oldns:oldnsimage.png}} {{oldns:oldnsimage_missing.png}} {{oldnsimage_missing.png}} {{oldnsimage.png}}', 'setup');
324        idx_addPage('oldns:start');
325
326        /** @var helper_plugin_move_plan $plan  */
327        $plan = plugin_load('helper', 'move_plan');
328
329        $this->assertFalse($plan->inProgress());
330
331        $plan->addMediaNamespaceMove('oldns', 'newns');
332
333        $plan->commit();
334
335        $this->assertSame(1, $plan->nextStep(), 'media');
336        $this->assertSame(1, $plan->nextStep(), 'missing_media');
337        $this->assertSame(1, $plan->nextStep(), 'autorewrite');
338        $this->assertSame(0, $plan->nextStep(), 'done');
339
340        $this->assertFileExists(mediaFN('newns:oldnsimage.png'));
341        $this->assertFileNotExists(mediaFN('oldns:oldnsimage.png'));
342
343        $this->assertSame('{{newns:oldnsimage.png}} {{newns:oldnsimage_missing.png}} {{newns:oldnsimage_missing.png}} {{newns:oldnsimage.png}}',rawWiki('oldns:start'));
344    }
345
346    /**
347     * This is an integration test, which checks the correct working of an entire namespace move.
348     * Hence it is not an unittest, hence it @coversNothing
349     *
350     * @group slow
351     */
352    public function test_move_small_namespace_combi() {
353        global $AUTH_ACL;
354
355        $AUTH_ACL[] = "oldns:*\t@ALL\t16";
356        $AUTH_ACL[] = "newns:*\t@ALL\t16";
357
358        $filepath = DOKU_TMP_DATA.'media/oldns/oldnsimage.png';
359        io_makeFileDir($filepath);
360        io_saveFile($filepath,'');
361        saveWikiText('start', "[[oldns:start]] [[oldns:page]] [[oldns:missing]]\n{{oldns:oldnsimage.png}} {{oldns:oldnsimage_missing.png}} {{oldnsimage_missing.png}}", 'setup');
362        idx_addPage('start');
363        saveWikiText('oldns:start', '[[oldns:start]] [[oldns:page]] [[oldns:missing]] [[missing]] [[page]]', 'setup');
364        idx_addPage('oldns:start');
365        saveWikiText('oldns:page', '[[oldns:start]] [[oldns:page]] [[oldns:missing]] [[missing]] [[start]]', 'setup');
366        idx_addPage('oldns:page');
367
368        /** @var helper_plugin_move_plan $plan  */
369        $plan = plugin_load('helper', 'move_plan');
370
371        $this->assertFalse($plan->inProgress());
372
373        $plan->addMediaNamespaceMove('oldns', 'newns');
374        $plan->addPageNamespaceMove('oldns', 'newns');
375
376        $plan->commit();
377
378        $this->assertSame(1, $plan->nextStep(), 'pages');
379        $this->assertSame(1, $plan->nextStep(), 'media');
380        $this->assertSame(1, $plan->nextStep(), 'missing');
381        $this->assertSame(1, $plan->nextStep(), 'missing_media');
382        $this->assertSame(1, $plan->nextStep(), 'namespaces');
383        $this->assertSame(1, $plan->nextStep(), 'autorewrite');
384        $this->assertSame(0, $plan->nextStep(), 'done');
385
386        $this->assertFileExists(mediaFN('newns:oldnsimage.png'));
387        $this->assertFileNotExists(mediaFN('oldns:oldnsimage.png'));
388
389        $this->assertSame("[[newns:start]] [[newns:page]] [[newns:missing]]\n{{newns:oldnsimage.png}} {{newns:oldnsimage_missing.png}} {{oldnsimage_missing.png}}",rawWiki('start'));
390    }
391
392    /**
393     * This is an integration test, which checks the correct working of an entire namespace move.
394     * Hence it is not an unittest, hence it @coversNothing
395     *
396     * @group slow
397     */
398    public function test_move_small_namespace_subscription_ns() {
399        global $AUTH_ACL;
400
401        $AUTH_ACL[] = "subns:*\t@ALL\t16";
402        $AUTH_ACL[] = "newns:*\t@ALL\t16";
403
404        saveWikiText('subns:start', 'Lorem Ipsum', 'setup');
405        idx_addPage('subns:start');
406
407        $oldfilepath = DOKU_TMP_DATA.'meta/subns/.mlist';
408        $subscription = 'doe every 1427984341';
409        io_makeFileDir($oldfilepath);
410        io_saveFile($oldfilepath,$subscription);
411        $newfilepath = DOKU_TMP_DATA.'meta/newns/.mlist';
412
413        /** @var helper_plugin_move_plan $plan  */
414        $plan = plugin_load('helper', 'move_plan');
415
416        $this->assertFalse($plan->inProgress());
417
418        $plan->addPageNamespaceMove('subns', 'newns');
419
420        $plan->commit();
421
422        $this->assertSame(1, $plan->nextStep(), 'pages');
423        $this->assertSame(1, $plan->nextStep(), 'namespace');
424        $this->assertSame(0, $plan->nextStep(), 'done');
425
426        $this->assertFileExists(wikiFN('newns:start'));
427        $this->assertFileExists($newfilepath);
428        $this->assertFileNotExists(wikiFN('subns:start'));
429        $this->assertFileNotExists($oldfilepath);
430
431        $this->assertSame($subscription,file_get_contents($newfilepath));
432
433    }
434
435    /**
436     * This is an integration test, which checks the correct working of an entire namespace move.
437     * Hence it is not an unittest, hence it @coversNothing
438     *
439     * @group slow
440     */
441    public function test_move_small_namespace_subscription_page() {
442        global $AUTH_ACL;
443
444        $AUTH_ACL[] = "subns:*\t@ALL\t16";
445        $AUTH_ACL[] = "newns:*\t@ALL\t16";
446
447        saveWikiText('subns:start', 'Lorem Ipsum', 'setup');
448        idx_addPage('subns:start');
449
450        $oldfilepath = DOKU_TMP_DATA.'meta/subns/start.mlist';
451        $subscription = 'doe every 1427984341';
452        io_makeFileDir($oldfilepath);
453        io_saveFile($oldfilepath,$subscription);
454        $newfilepath = DOKU_TMP_DATA.'meta/newns/start.mlist';
455
456        /** @var helper_plugin_move_plan $plan  */
457        $plan = plugin_load('helper', 'move_plan');
458
459        $this->assertFalse($plan->inProgress());
460
461        $plan->addPageNamespaceMove('subns', 'newns');
462
463        $plan->commit();
464
465        $this->assertSame(1, $plan->nextStep(), 'pages');
466        $this->assertSame(1, $plan->nextStep(), 'namespace');
467        $this->assertSame(0, $plan->nextStep(), 'done');
468
469        $this->assertFileExists(wikiFN('newns:start'));
470        $this->assertFileExists($newfilepath);
471        $this->assertFileNotExists(wikiFN('subns:start'));
472        $this->assertFileNotExists($oldfilepath);
473
474        $this->assertSame($subscription,file_get_contents($newfilepath));
475
476    }
477
478}
479