1<?php
2
3/**
4 * mock class to access the helper_plugin_move_plan::stepThroughDocuments function in tests
5 */
6class helper_plugin_move_plan_mock extends helper_plugin_move_plan {
7
8    public $moveLog = array();
9
10    public function __construct() {
11        parent::__construct();
12        $this->MoveOperator  = new helper_plugin_move_op_mock;
13    }
14
15    public function stepThroughDocumentsCall($type = parent::TYPE_PAGES, $skip = false) {
16        return $this->stepThroughDocuments($type, $skip);
17    }
18
19    public function getMoveOperator() {
20        return $this->MoveOperator;
21    }
22
23    public function setMoveOperator($newMoveOPerator) {
24        $this->MoveOperator = $newMoveOPerator;
25    }
26
27    public function build_log_line($type, $from, $to, $success) {
28        $logEntry = array($type,$from,$to,$success);
29        array_push($this->moveLog,$logEntry);
30        return parent::build_log_line($type, $from, $to, $success);
31    }
32
33
34
35}
36
37class helper_plugin_move_op_mock extends helper_plugin_move_op {
38
39    public $movedPages = array();
40    public $fail = false;
41
42    public function movePage($src, $dst) {
43        if ($this->fail !== false && count($this->movedPages) == $this->fail) {
44            $this->fail=false;
45            return false;
46        }
47        $moveOperation = array($src => $dst);
48        array_push($this->movedPages,$moveOperation);
49        return true;
50    }
51}
52
53
54
55
56/**
57 * Test cases for helper_plugin_move_plan::stepThroughDocuments function of the move plugin
58 *
59 * @group plugin_move
60 * @group plugin_move_unittests
61 * @group plugins
62 * @group unittests
63 */
64class plugin_move_stepThroughDocuments_test extends DokuWikiTest {
65
66    public function setUp() {
67        parent::setUp();
68        $opts_file = dirname(DOKU_CONF) . '/data/meta/__move_opts';
69        if(file_exists($opts_file)){
70            unlink($opts_file);
71        }
72
73        $file = "oldns:page01\tnewns:page01\n"
74            . "oldns:page02\tnewns:page02\n"
75            . "oldns:page03\tnewns:page03\n"
76            . "oldns:page04\tnewns:page04\n"
77            . "oldns:page05\tnewns:page05\n"
78            . "oldns:page06\tnewns:page06\n"
79            . "oldns:page07\tnewns:page07\n"
80            . "oldns:page08\tnewns:page08\n"
81            . "oldns:page09\tnewns:page09\n"
82            . "oldns:page10\tnewns:page10\n"
83            . "oldns:page11\tnewns:page11\n"
84            . "oldns:page12\tnewns:page12\n"
85            . "oldns:page13\tnewns:page13\n"
86            . "oldns:page14\tnewns:page14\n"
87            . "oldns:page15\tnewns:page15\n"
88            . "oldns:page16\tnewns:page16\n"
89            . "oldns:page17\tnewns:page17\n"
90            . "oldns:page18\tnewns:page18";
91        $file_path = dirname(DOKU_CONF) . '/data/meta/__move_pagelist';
92        io_saveFile($file_path,$file);
93    }
94
95
96    /**
97     * @covers helper_plugin_move_plan::stepThroughDocuments
98     */
99    public function test_stepThroughPages() {
100
101        $file_path = dirname(DOKU_CONF) . '/data/meta/__move_pagelist';
102        $mock = new helper_plugin_move_plan_mock();
103        $actual_return = $mock->stepThroughDocumentsCall();
104        $actual_file = file_get_contents($file_path);
105        $expected_file = "oldns:page01\tnewns:page01\n"
106            . "oldns:page02\tnewns:page02\n"
107            . "oldns:page03\tnewns:page03\n"
108            . "oldns:page04\tnewns:page04\n"
109            . "oldns:page05\tnewns:page05\n"
110            . "oldns:page06\tnewns:page06\n"
111            . "oldns:page07\tnewns:page07\n"
112            . "oldns:page08\tnewns:page08";
113
114        $expected_pages_run = -10;
115        $this->assertSame($expected_pages_run,$actual_return,"return values differ");
116        $this->assertSame($expected_file,$actual_file, "files differ");
117        $actual_move_Operator = $mock->getMoveOperator();
118        $this->assertSame(array('oldns:page18' => 'newns:page18',),$actual_move_Operator->movedPages[0]);
119        $this->assertSame(array('oldns:page09' => 'newns:page09',),$actual_move_Operator->movedPages[9]);
120        $this->assertTrue(!isset($actual_move_Operator->movedPages[10]));
121
122        $expected_log = array('P','oldns:page18','newns:page18',true);
123        $this->assertSame($expected_log,$mock->moveLog[0]);
124
125        $expected_log = array('P','oldns:page09','newns:page09',true);
126        $this->assertSame($expected_log,$mock->moveLog[9]);
127        $this->assertTrue(!isset($mock->moveLog[10]));
128
129        $opts_file = dirname(DOKU_CONF) . '/data/meta/__move_opts';
130        $actual_options = unserialize(io_readFile($opts_file));
131        $this->assertSame($expected_pages_run,$actual_options['pages_run'],'saved options are wrong');
132    }
133
134    /**
135     * @covers helper_plugin_move_plan::stepThroughDocuments
136     */
137    public function test_stepThroughPages_skip() {
138
139        $file_path = dirname(DOKU_CONF) . '/data/meta/__move_pagelist';
140        $mock = new helper_plugin_move_plan_mock();
141        $actual_return = $mock->stepThroughDocumentsCall(1,true);
142        $actual_file = file_get_contents($file_path);
143        $expected_file = "oldns:page01\tnewns:page01\n"
144            . "oldns:page02\tnewns:page02\n"
145            . "oldns:page03\tnewns:page03\n"
146            . "oldns:page04\tnewns:page04\n"
147            . "oldns:page05\tnewns:page05\n"
148            . "oldns:page06\tnewns:page06\n"
149            . "oldns:page07\tnewns:page07\n"
150            . "oldns:page08\tnewns:page08";
151        $expected_pages_run = -10;
152        $this->assertSame($expected_pages_run,$actual_return,"return values differ");
153        $this->assertSame($expected_file,$actual_file, "files differ");
154        $actual_move_Operator = $mock->getMoveOperator();
155        $this->assertSame(array('oldns:page17' => 'newns:page17',),$actual_move_Operator->movedPages[0]);
156        $this->assertSame(array('oldns:page09' => 'newns:page09',),$actual_move_Operator->movedPages[8]);
157        $this->assertTrue(!isset($actual_move_Operator->movedPages[9]));
158
159        $expected_log = array('P','oldns:page17','newns:page17',true);
160        $this->assertSame($expected_log,$mock->moveLog[0]);
161
162        $expected_log = array('P','oldns:page09','newns:page09',true);
163        $this->assertSame($expected_log,$mock->moveLog[8]);
164        $this->assertTrue(!isset($mock->moveLog[9]));
165
166        $opts_file = dirname(DOKU_CONF) . '/data/meta/__move_opts';
167        $actual_options = unserialize(io_readFile($opts_file));
168        $this->assertSame($expected_pages_run,$actual_options['pages_run'],'saved options are wrong');
169    }
170
171    /**
172     * @covers helper_plugin_move_plan::stepThroughDocuments
173     */
174    public function test_stepThroughPages_fail() {
175
176        $file_path = dirname(DOKU_CONF) . '/data/meta/__move_pagelist';
177        $mock = new helper_plugin_move_plan_mock();
178        $fail_at_item = 5;
179        $actual_move_Operator = $mock->getMoveOperator();
180        $actual_move_Operator->fail = $fail_at_item;
181        $mock->setMoveOperator($actual_move_Operator);
182        $actual_return = $mock->stepThroughDocumentsCall();
183        $actual_file = file_get_contents($file_path);
184        $expected_file = "oldns:page01\tnewns:page01\n"
185            . "oldns:page02\tnewns:page02\n"
186            . "oldns:page03\tnewns:page03\n"
187            . "oldns:page04\tnewns:page04\n"
188            . "oldns:page05\tnewns:page05\n"
189            . "oldns:page06\tnewns:page06\n"
190            . "oldns:page07\tnewns:page07\n"
191            . "oldns:page08\tnewns:page08\n"
192            . "oldns:page09\tnewns:page09\n"
193            . "oldns:page10\tnewns:page10\n"
194            . "oldns:page11\tnewns:page11\n"
195            . "oldns:page12\tnewns:page12\n"
196            . "oldns:page13\tnewns:page13";
197
198        $expected_pages_run = false;
199        $this->assertSame($expected_pages_run,$actual_return,"return values differ");
200        $this->assertSame($expected_file,$actual_file, "files differ");
201        $actual_move_Operator = $mock->getMoveOperator();
202        $this->assertSame(array('oldns:page18' => 'newns:page18',),$actual_move_Operator->movedPages[0]);
203        $lastIndex = 4;
204        $this->assertSame(array('oldns:page14' => 'newns:page14',),$actual_move_Operator->movedPages[$lastIndex]);
205        $this->assertTrue(!isset($actual_move_Operator->movedPages[$lastIndex + 1]));
206
207        $expected_log = array('P','oldns:page13','newns:page13',false);
208        $this->assertSame($expected_log,$mock->moveLog[5]);
209        $this->assertTrue(!isset($mock->moveLog[6]));
210
211        $opts_file = dirname(DOKU_CONF) . '/data/meta/__move_opts';
212        $actual_options = unserialize(io_readFile($opts_file));
213        $this->assertSame(-$fail_at_item,$actual_options['pages_run'],'saved options are wrong');
214    }
215
216
217    /**
218     * @covers helper_plugin_move_plan::stepThroughDocuments
219     */
220    public function test_stepThroughPages_fail_autoskip() {
221        global $conf;
222        $conf['plugin']['move']['autoskip'] = '1';
223
224        $file_path = dirname(DOKU_CONF) . '/data/meta/__move_pagelist';
225        $mock = new helper_plugin_move_plan_mock();
226        $actual_move_Operator = $mock->getMoveOperator();
227        $actual_move_Operator->fail = 5;
228        $mock->setMoveOperator($actual_move_Operator);
229        $actual_return = $mock->stepThroughDocumentsCall();
230
231        $expected_pages_run = -10;
232        $this->assertSame($expected_pages_run,$actual_return,"return values differ");
233
234        $actual_file = file_get_contents($file_path);
235        $expected_file = "oldns:page01\tnewns:page01\n"
236            . "oldns:page02\tnewns:page02\n"
237            . "oldns:page03\tnewns:page03\n"
238            . "oldns:page04\tnewns:page04\n"
239            . "oldns:page05\tnewns:page05\n"
240            . "oldns:page06\tnewns:page06\n"
241            . "oldns:page07\tnewns:page07\n"
242            . "oldns:page08\tnewns:page08";
243
244        $this->assertSame($expected_file,$actual_file, "files differ");
245
246
247        $actual_move_Operator = $mock->getMoveOperator();
248        $this->assertSame(array('oldns:page18' => 'newns:page18',),$actual_move_Operator->movedPages[0]);
249
250        $lastIndex = 8;
251        $this->assertSame(array('oldns:page09' => 'newns:page09',),$actual_move_Operator->movedPages[$lastIndex]);
252        $this->assertTrue(!isset($actual_move_Operator->movedPages[$lastIndex + 1]), "The number of moved pages is incorrect");
253
254
255        $expected_log = array('P','oldns:page18','newns:page18',true);
256        $this->assertSame($expected_log,$mock->moveLog[0]);
257
258        $expected_log = array('P','oldns:page13','newns:page13',false);
259        $this->assertSame($expected_log,$mock->moveLog[5]);
260
261        $expected_log = array('P','oldns:page09','newns:page09',true);
262        $this->assertSame($expected_log,$mock->moveLog[9]);
263
264        $this->assertTrue(!isset($mock->moveLog[10]), "The number of logged items is incorrect");
265
266        $opts_file = dirname(DOKU_CONF) . '/data/meta/__move_opts';
267        $actual_options = unserialize(io_readFile($opts_file));
268        $this->assertSame($expected_pages_run,$actual_options['pages_run'],'saved options are wrong');
269    }
270
271
272}
273