1<?php
2require_once(DOKU_INC.'_test/lib/unittest.php');
3
4require_once(DOKU_INC.'inc/init.php');
5require_once(DOKU_INC.'inc/plugin.php');
6require_once(DOKU_INC.'inc/common.php');
7require_once(DOKU_INC.'lib/plugins/pagemove/admin.php');
8
9class PagemovePageTest extends Doku_UnitTestCase {
10
11    var $movedId = 'parent_ns:current_ns:test_page';
12    var $parentBacklinkingId = 'parent_ns:some_page';
13    var $currentNsBacklinkingId = 'parent_ns:current_ns:some_page';
14    var $otherBacklinkingId = 'level0:level1:other_backlinking_page';
15    var $subNsPage = 'parent_ns:current_ns:sub_ns:some_page';
16
17    // @todo Move page to an ID which already exists
18    // @todo Check backlinks of a sub-namespace page (moving same, up, down, different)
19
20    function setUp() {
21        global $ID;
22        global $INFO;
23
24        $ID = $this->movedId;
25
26        $text = <<<EOT
27[[start|start]]
28[[parallel_page|parallel_page]]
29[[.:|.:]]
30[[..current_ns:|..current_ns:]]
31[[..:current_ns:|..:current_ns:]]
32[[..parallel_ns:|..parallel_ns:]]
33[[..:parallel_ns:|..:parallel_ns:]]
34[[..:..:|..:..:]]
35[[..:..:parent_ns:|..:..:parent_ns:]]
36[[parent_ns:new_page|parent_ns:new_page]]
37[[parent_ns/new_page|parent_ns/new_page]]
38[[/start|/start]]
39EOT;
40        $summary = 'Test';
41        saveWikiText($this->movedId, $text, $summary);
42        $INFO = pageinfo();
43
44        $text = <<<EOT
45[[$this->movedId|$this->movedId]]
46[[:$this->movedId|:$this->movedId]]
47[[.current_ns:test_page|.current_ns:test_page]]
48[[.:current_ns:test_page|.:current_ns:test_page]]
49[[..parent_ns:current_ns:test_page|..parent_ns:current_ns:test_page]]
50[[test_page|test_page]]
51[[new_page|new_page]]
52[[ftp://somewhere.com|ftp://somewhere.com]]
53[[http://somewhere.com|http://somewhere.com]]
54
55[[start|start]]
56[[parallel_page|parallel_page]]
57[[.:|.:]]
58[[..current_ns:|..current_ns:]]
59[[..:current_ns:|..:current_ns:]]
60[[..parallel_ns:|..parallel_ns:]]
61[[..:parallel_ns:|..:parallel_ns:]]
62[[..:..:|..:..:]]
63[[..:..:parent_ns:|..:..:parent_ns:]]
64[[parent_ns:new_page|parent_ns:new_page]]
65[[parent_ns/new_page|parent_ns/new_page]]
66[[/start|/start]]
67EOT;
68        saveWikiText($this->parentBacklinkingId, $text, $summary);
69
70        $text = <<<EOT
71[[$this->movedId|$this->movedId]]
72[[:$this->movedId|:$this->movedId]]
73[[..current_ns:test_page|..current_ns:test_page]]
74[[..:current_ns:test_page|..:current_ns:test_page]]
75[[test_page|test_page]]
76[[.test_page|.test_page]]
77[[.:test_page|.:test_page]]
78[[..test_page|..test_page]]
79[[..:test_page|..:test_page]]
80[[.:..:test_page|.:..:test_page]]
81[[new_page|new_page]]
82[[ftp://somewhere.com|ftp://somewhere.com]]
83[[http://somewhere.com|http://somewhere.com]]
84
85[[start|start]]
86[[parallel_page|parallel_page]]
87[[.:|.:]]
88[[..current_ns:|..current_ns:]]
89[[..:current_ns:|..:current_ns:]]
90[[..parallel_ns:|..parallel_ns:]]
91[[..:parallel_ns:|..:parallel_ns:]]
92[[..:..:|..:..:]]
93[[..:..:parent_ns:|..:..:parent_ns:]]
94[[parent_ns:new_page|parent_ns:new_page]]
95[[parent_ns/new_page|parent_ns/new_page]]
96[[/start|/start]]
97EOT;
98        saveWikiText($this->currentNsBacklinkingId, $text, $summary);
99
100        $text = <<<EOT
101[[$this->movedId|$this->movedId]]
102[[:$this->movedId|:$this->movedId]]
103[[.current_ns:test_page|.current_ns:test_page]]
104[[.:current_ns:test_page|.:current_ns:test_page]]
105[[test_page|test_page]]
106[[new_page|new_page]]
107[[ftp://somewhere.com|ftp://somewhere.com]]
108[[http://somewhere.com|http://somewhere.com]]
109
110[[start|start]]
111[[parallel_page|parallel_page]]
112[[.:|.:]]
113[[..current_ns:|..current_ns:]]
114[[..:current_ns:|..:current_ns:]]
115[[..parallel_ns:|..parallel_ns:]]
116[[..:parallel_ns:|..:parallel_ns:]]
117[[..:..:|..:..:]]
118[[..:..:parent_ns:|..:..:parent_ns:]]
119[[parent_ns:new_page|parent_ns:new_page]]
120[[parent_ns/new_page|parent_ns/new_page]]
121[[/start|/start]]
122EOT;
123        saveWikiText($this->otherBacklinkingId, $text, $summary);
124
125        $text = <<<EOT
126[[$this->movedId|$this->movedId]]
127[[:$this->movedId|:$this->movedId]]
128[[..:..current_ns:test_page|..:..current_ns:test_page]]
129[[..:..:current_ns:test_page|..:..:current_ns:test_page]]
130[[test_page|test_page]]
131[[..:test_page|..:test_page]]
132[[..:test_page|..:test_page]]
133[[.:..:test_page|.:..:test_page]]
134[[new_page|new_page]]
135[[..:new_page|..:new_page]]
136[[ftp://somewhere.com|ftp://somewhere.com]]
137[[http://somewhere.com|http://somewhere.com]]
138
139[[start|start]]
140[[parallel_page|parallel_page]]
141[[.:|.:]]
142[[..current_ns:|..current_ns:]]
143[[..:current_ns:|..:current_ns:]]
144[[..parallel_ns:|..parallel_ns:]]
145[[..:parallel_ns:|..:parallel_ns:]]
146[[..:..:|..:..:]]
147[[..:..:parent_ns:|..:..:parent_ns:]]
148[[parent_ns:new_page|parent_ns:new_page]]
149[[parent_ns/new_page|parent_ns/new_page]]
150[[/start|/start]]
151EOT;
152        saveWikiText($this->subNsPage, $text, $summary);
153
154        $this->pagemove = new admin_plugin_pagemove();
155    }
156
157#	function testPagemove() {
158#		$this->assertEqual(1,1);
159#	}
160
161// 	function test_pm_getforwardlinks() {
162// 		$origLinkAbsLinkArray = $this->pagemove->_pm_getforwardlinks($this->movedId);
163// 	}
164
165	function test_move_page_in_same_ns() {
166	    global $ID;
167
168	    $newPagename = 'new_page';
169
170	    $opts = array();
171	    $opts['page_ns'] = 'page';
172	    $opts['ns']   = getNS($ID);
173        $opts['name'] = noNS($ID);
174        $opts['newns'] = $opts['ns'];
175        $opts['newname'] = $newPagename;
176        $this->movedToId = $opts['newns'].':'.$newPagename;
177	    $this->pagemove->_pm_move_page($opts);
178
179	    $newId = $opts['newns'].':'.$opts['newname'];
180	    $newContent = rawWiki($newId);
181	    $expectedContent = <<<EOT
182[[start|start]]
183[[parallel_page|parallel_page]]
184[[start|.:]]
185[[start|..current_ns:]]
186[[start|..:current_ns:]]
187[[parent_ns:parallel_ns:start|..parallel_ns:]]
188[[parent_ns:parallel_ns:start|..:parallel_ns:]]
189[[..:..:|..:..:]]
190[[parent_ns:start|..:..:parent_ns:]]
191[[parent_ns:new_page|parent_ns:new_page]]
192[[parent_ns:new_page|parent_ns/new_page]]
193[[/start|/start]]
194EOT;
195	    $this->assertEqual($expectedContent, $newContent);
196
197	    $newContent = rawWiki($this->parentBacklinkingId);
198	    $expectedContent = <<<EOT
199[[.:current_ns:new_page|$this->movedId]]
200[[.:current_ns:new_page|:$this->movedId]]
201[[.:current_ns:new_page|.current_ns:test_page]]
202[[.:current_ns:new_page|.:current_ns:test_page]]
203[[.:current_ns:new_page|..parent_ns:current_ns:test_page]]
204[[test_page|test_page]]
205[[new_page|new_page]]
206[[ftp://somewhere.com|ftp://somewhere.com]]
207[[http://somewhere.com|http://somewhere.com]]
208
209[[start|start]]
210[[parallel_page|parallel_page]]
211[[.:|.:]]
212[[..current_ns:|..current_ns:]]
213[[..:current_ns:|..:current_ns:]]
214[[..parallel_ns:|..parallel_ns:]]
215[[..:parallel_ns:|..:parallel_ns:]]
216[[..:..:|..:..:]]
217[[..:..:parent_ns:|..:..:parent_ns:]]
218[[parent_ns:new_page|parent_ns:new_page]]
219[[parent_ns/new_page|parent_ns/new_page]]
220[[/start|/start]]
221EOT;
222	    $this->assertEqual($expectedContent, $newContent);
223
224	    $newContent = rawWiki($this->currentNsBacklinkingId);
225	    $expectedContent = <<<EOT
226[[new_page|$this->movedId]]
227[[new_page|:$this->movedId]]
228[[new_page|..current_ns:test_page]]
229[[new_page|..:current_ns:test_page]]
230[[new_page|test_page]]
231[[new_page|.test_page]]
232[[new_page|.:test_page]]
233[[..test_page|..test_page]]
234[[..:test_page|..:test_page]]
235[[.:..:test_page|.:..:test_page]]
236[[new_page|new_page]]
237[[ftp://somewhere.com|ftp://somewhere.com]]
238[[http://somewhere.com|http://somewhere.com]]
239
240[[start|start]]
241[[parallel_page|parallel_page]]
242[[.:|.:]]
243[[..current_ns:|..current_ns:]]
244[[..:current_ns:|..:current_ns:]]
245[[..parallel_ns:|..parallel_ns:]]
246[[..:parallel_ns:|..:parallel_ns:]]
247[[..:..:|..:..:]]
248[[..:..:parent_ns:|..:..:parent_ns:]]
249[[parent_ns:new_page|parent_ns:new_page]]
250[[parent_ns/new_page|parent_ns/new_page]]
251[[/start|/start]]
252EOT;
253	    $this->assertEqual($expectedContent, $newContent);
254
255	    $newContent = rawWiki($this->otherBacklinkingId);
256	    $expectedContent = <<<EOT
257[[parent_ns:current_ns:new_page|$this->movedId]]
258[[$newId|:$this->movedId]]
259[[.current_ns:test_page|.current_ns:test_page]]
260[[.:current_ns:test_page|.:current_ns:test_page]]
261[[test_page|test_page]]
262[[new_page|new_page]]
263[[ftp://somewhere.com|ftp://somewhere.com]]
264[[http://somewhere.com|http://somewhere.com]]
265
266[[start|start]]
267[[parallel_page|parallel_page]]
268[[.:|.:]]
269[[..current_ns:|..current_ns:]]
270[[..:current_ns:|..:current_ns:]]
271[[..parallel_ns:|..parallel_ns:]]
272[[..:parallel_ns:|..:parallel_ns:]]
273[[..:..:|..:..:]]
274[[..:..:parent_ns:|..:..:parent_ns:]]
275[[parent_ns:new_page|parent_ns:new_page]]
276[[parent_ns/new_page|parent_ns/new_page]]
277[[/start|/start]]
278EOT;
279	    $this->assertEqual($expectedContent, $newContent);
280	}
281
282
283	function test_move_page_to_parallel_ns() {
284	    global $ID;
285
286	    $newPagename = 'new_page';
287
288	    $opts = array();
289	    $opts['page_ns'] = 'page';
290	    $opts['ns']   = getNS($ID);
291	    $opts['name'] = noNS($ID);
292	    $opts['newns'] = 'parent_ns:parallel_ns';
293	    $opts['newname'] = $newPagename;
294	    $this->movedToId = $opts['newns'].':'.$newPagename;
295	    $this->pagemove->_pm_move_page($opts);
296
297	    $newId = $opts['newns'].':'.$opts['newname'];
298	    $newContent = rawWiki($newId);
299	    $expectedContent = <<<EOT
300[[parent_ns:current_ns:start|start]]
301[[parent_ns:current_ns:parallel_page|parallel_page]]
302[[parent_ns:current_ns:start|.:]]
303[[parent_ns:current_ns:start|..current_ns:]]
304[[parent_ns:current_ns:start|..:current_ns:]]
305[[start|..parallel_ns:]]
306[[start|..:parallel_ns:]]
307[[..:..:|..:..:]]
308[[parent_ns:start|..:..:parent_ns:]]
309[[parent_ns:new_page|parent_ns:new_page]]
310[[parent_ns:new_page|parent_ns/new_page]]
311[[/start|/start]]
312EOT;
313	    $this->assertEqual($expectedContent, $newContent);
314
315	    $newContent = rawWiki($this->parentBacklinkingId);
316	    $expectedContent = <<<EOT
317[[.:parallel_ns:new_page|$this->movedId]]
318[[.:parallel_ns:new_page|:$this->movedId]]
319[[.:parallel_ns:new_page|.current_ns:test_page]]
320[[.:parallel_ns:new_page|.:current_ns:test_page]]
321[[.:parallel_ns:new_page|..parent_ns:current_ns:test_page]]
322[[test_page|test_page]]
323[[new_page|new_page]]
324[[ftp://somewhere.com|ftp://somewhere.com]]
325[[http://somewhere.com|http://somewhere.com]]
326
327[[start|start]]
328[[parallel_page|parallel_page]]
329[[.:|.:]]
330[[..current_ns:|..current_ns:]]
331[[..:current_ns:|..:current_ns:]]
332[[..parallel_ns:|..parallel_ns:]]
333[[..:parallel_ns:|..:parallel_ns:]]
334[[..:..:|..:..:]]
335[[..:..:parent_ns:|..:..:parent_ns:]]
336[[parent_ns:new_page|parent_ns:new_page]]
337[[parent_ns/new_page|parent_ns/new_page]]
338[[/start|/start]]
339EOT;
340	    $this->assertEqual($expectedContent, $newContent);
341
342	    $newContent = rawWiki($this->currentNsBacklinkingId);
343	    $expectedContent = <<<EOT
344[[parent_ns:parallel_ns:new_page|$this->movedId]]
345[[$newId|:$this->movedId]]
346[[parent_ns:parallel_ns:new_page|..current_ns:test_page]]
347[[parent_ns:parallel_ns:new_page|..:current_ns:test_page]]
348[[parent_ns:parallel_ns:new_page|test_page]]
349[[parent_ns:parallel_ns:new_page|.test_page]]
350[[parent_ns:parallel_ns:new_page|.:test_page]]
351[[..test_page|..test_page]]
352[[..:test_page|..:test_page]]
353[[.:..:test_page|.:..:test_page]]
354[[new_page|new_page]]
355[[ftp://somewhere.com|ftp://somewhere.com]]
356[[http://somewhere.com|http://somewhere.com]]
357
358[[start|start]]
359[[parallel_page|parallel_page]]
360[[.:|.:]]
361[[..current_ns:|..current_ns:]]
362[[..:current_ns:|..:current_ns:]]
363[[..parallel_ns:|..parallel_ns:]]
364[[..:parallel_ns:|..:parallel_ns:]]
365[[..:..:|..:..:]]
366[[..:..:parent_ns:|..:..:parent_ns:]]
367[[parent_ns:new_page|parent_ns:new_page]]
368[[parent_ns/new_page|parent_ns/new_page]]
369[[/start|/start]]
370EOT;
371	    $this->assertEqual($expectedContent, $newContent);
372
373	    $newContent = rawWiki($this->otherBacklinkingId);
374	    $expectedContent = <<<EOT
375[[parent_ns:parallel_ns:new_page|$this->movedId]]
376[[$newId|:$this->movedId]]
377[[.current_ns:test_page|.current_ns:test_page]]
378[[.:current_ns:test_page|.:current_ns:test_page]]
379[[test_page|test_page]]
380[[new_page|new_page]]
381[[ftp://somewhere.com|ftp://somewhere.com]]
382[[http://somewhere.com|http://somewhere.com]]
383
384[[start|start]]
385[[parallel_page|parallel_page]]
386[[.:|.:]]
387[[..current_ns:|..current_ns:]]
388[[..:current_ns:|..:current_ns:]]
389[[..parallel_ns:|..parallel_ns:]]
390[[..:parallel_ns:|..:parallel_ns:]]
391[[..:..:|..:..:]]
392[[..:..:parent_ns:|..:..:parent_ns:]]
393[[parent_ns:new_page|parent_ns:new_page]]
394[[parent_ns/new_page|parent_ns/new_page]]
395[[/start|/start]]
396EOT;
397	    $this->assertEqual($expectedContent, $newContent);
398	}
399
400
401	function test_move_page_to_parent_ns() {
402	    global $ID;
403
404	    $newPagename = 'new_page';
405
406	    $opts = array();
407	    $opts['page_ns'] = 'page';
408	    $opts['ns']   = getNS($ID);
409	    $opts['name'] = noNS($ID);
410	    $opts['newns'] = 'parent_ns';
411	    $opts['newname'] = $newPagename;
412	    $newId = $opts['newns'].':'.$opts['newname'];
413	    $this->movedToId = $opts['newns'].':'.$newPagename;
414
415	    $this->pagemove->_pm_move_page($opts);
416
417	    $newContent = rawWiki($newId);
418	    $expectedContent = <<<EOT
419[[parent_ns:current_ns:start|start]]
420[[parent_ns:current_ns:parallel_page|parallel_page]]
421[[parent_ns:current_ns:start|.:]]
422[[parent_ns:current_ns:start|..current_ns:]]
423[[parent_ns:current_ns:start|..:current_ns:]]
424[[parent_ns:parallel_ns:start|..parallel_ns:]]
425[[parent_ns:parallel_ns:start|..:parallel_ns:]]
426[[..:..:|..:..:]]
427[[start|..:..:parent_ns:]]
428[[new_page|parent_ns:new_page]]
429[[new_page|parent_ns/new_page]]
430[[/start|/start]]
431EOT;
432	    $this->assertEqual($expectedContent, $newContent);
433
434	    // page is moved to same NS as backlinking page (parent_ns)
435	    $newContent = rawWiki($this->parentBacklinkingId);
436	    $expectedContent = <<<EOT
437[[new_page|$this->movedId]]
438[[new_page|:$this->movedId]]
439[[new_page|.current_ns:test_page]]
440[[new_page|.:current_ns:test_page]]
441[[new_page|..parent_ns:current_ns:test_page]]
442[[test_page|test_page]]
443[[new_page|new_page]]
444[[ftp://somewhere.com|ftp://somewhere.com]]
445[[http://somewhere.com|http://somewhere.com]]
446
447[[start|start]]
448[[parallel_page|parallel_page]]
449[[.:|.:]]
450[[..current_ns:|..current_ns:]]
451[[..:current_ns:|..:current_ns:]]
452[[..parallel_ns:|..parallel_ns:]]
453[[..:parallel_ns:|..:parallel_ns:]]
454[[..:..:|..:..:]]
455[[..:..:parent_ns:|..:..:parent_ns:]]
456[[parent_ns:new_page|parent_ns:new_page]]
457[[parent_ns/new_page|parent_ns/new_page]]
458[[/start|/start]]
459EOT;
460	    $this->assertEqual($expectedContent, $newContent);
461
462	    $newContent = rawWiki($this->currentNsBacklinkingId);
463	    $expectedContent = <<<EOT
464[[parent_ns:new_page|$this->movedId]]
465[[$newId|:$this->movedId]]
466[[parent_ns:new_page|..current_ns:test_page]]
467[[parent_ns:new_page|..:current_ns:test_page]]
468[[parent_ns:new_page|test_page]]
469[[parent_ns:new_page|.test_page]]
470[[parent_ns:new_page|.:test_page]]
471[[..test_page|..test_page]]
472[[..:test_page|..:test_page]]
473[[.:..:test_page|.:..:test_page]]
474[[new_page|new_page]]
475[[ftp://somewhere.com|ftp://somewhere.com]]
476[[http://somewhere.com|http://somewhere.com]]
477
478[[start|start]]
479[[parallel_page|parallel_page]]
480[[.:|.:]]
481[[..current_ns:|..current_ns:]]
482[[..:current_ns:|..:current_ns:]]
483[[..parallel_ns:|..parallel_ns:]]
484[[..:parallel_ns:|..:parallel_ns:]]
485[[..:..:|..:..:]]
486[[..:..:parent_ns:|..:..:parent_ns:]]
487[[parent_ns:new_page|parent_ns:new_page]]
488[[parent_ns/new_page|parent_ns/new_page]]
489[[/start|/start]]
490EOT;
491	    $this->assertEqual($expectedContent, $newContent);
492
493	    $newContent = rawWiki($this->otherBacklinkingId);
494	    $expectedContent = <<<EOT
495[[parent_ns:new_page|$this->movedId]]
496[[$newId|:$this->movedId]]
497[[.current_ns:test_page|.current_ns:test_page]]
498[[.:current_ns:test_page|.:current_ns:test_page]]
499[[test_page|test_page]]
500[[new_page|new_page]]
501[[ftp://somewhere.com|ftp://somewhere.com]]
502[[http://somewhere.com|http://somewhere.com]]
503
504[[start|start]]
505[[parallel_page|parallel_page]]
506[[.:|.:]]
507[[..current_ns:|..current_ns:]]
508[[..:current_ns:|..:current_ns:]]
509[[..parallel_ns:|..parallel_ns:]]
510[[..:parallel_ns:|..:parallel_ns:]]
511[[..:..:|..:..:]]
512[[..:..:parent_ns:|..:..:parent_ns:]]
513[[parent_ns:new_page|parent_ns:new_page]]
514[[parent_ns/new_page|parent_ns/new_page]]
515[[/start|/start]]
516EOT;
517	    $this->assertEqual($expectedContent, $newContent);
518	}
519
520
521	function test_move_ns_in_same_ns() {
522	    global $ID;
523
524	    $newNamespace = 'new_ns';
525
526	    $opts = array();
527	    $opts['page_ns'] = 'ns';
528	    $opts['newns'] = 'parent_ns'.':'.$newNamespace;
529	    $opts['newname'] = $newPagename;
530	    $this->movedToId = $opts['newns'].':'.$newPagename;
531
532	    //$this->pagemove->_pm_move_recursive($opts);
533
534	}
535
536	function tearDown() {
537	    saveWikiText($this->movedId, '', 'removed');
538	    saveWikiText($this->movedToId, '', 'removed');
539	    saveWikiText($this->parentBacklinkingId, '', 'removed');
540	    saveWikiText($this->currentNsBacklinkingId, '', 'removed');
541	    saveWikiText($this->otherBacklinkingId, '', 'removed');
542	    saveWikiText($this->subNsPage, '', 'removed');
543	}
544
545}
546
547?>