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