forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdeploytest.php
370 lines (315 loc) · 12.5 KB
/
mdeploytest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* PHPUnit tests for the mdeploy.php utility
*
* Because the mdeploy.php can't be part of the Moodle code itself, this tests must be
* executed using something like:
*
* $ phpunit --no-configuration mdeploytest
*
* @package core
* @copyright 2012 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__.'/mdeploy.php');
/**
* Provides testable input options.
*
* @copyright 2012 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class input_fake_provider extends input_provider {
/** @var array */
protected $fakeoptions = array();
/**
* Sets fake raw options.
*
* @param array $options
*/
public function set_fake_options(array $options) {
$this->fakeoptions = $options;
$this->populate_options();
}
/**
* Returns the explicitly set fake options.
*
* @return array
*/
protected function parse_raw_options() {
return $this->fakeoptions;
}
}
/**
* Testable subclass.
*
* @copyright 2012 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testable_input_manager extends input_manager {
/**
* Provides access to the protected method so we can test it explicitly.
*/
public function cast_value($raw, $type) {
return parent::cast_value($raw, $type);
}
/**
* Sets the fake input provider.
*/
protected function initialize() {
$this->inputprovider = input_fake_provider::instance();
}
}
/**
* Testable subclass
*
* @copyright 2012 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class testable_worker extends worker {
/**
* Provides access to the protected method.
*/
public function move_directory($source, $target, $keepsourceroot = false) {
return parent::move_directory($source, $target, $keepsourceroot);
}
/**
* Provides access to the protected method.
*/
public function remove_directory($path, $keeppathroot = false) {
return parent::remove_directory($path, $keeppathroot);
}
/**
* Provides access to the protected method.
*/
public function create_directory_precheck($path) {
return parent::create_directory_precheck($path);
}
public function get_env($key) {
return parent::get_env($key);
}
}
/**
* Test cases for the mdeploy utility
*
* @copyright 2012 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mdeploytest extends PHPUnit_Framework_TestCase {
public function test_same_singletons() {
$a = input_manager::instance();
$b = input_manager::instance();
$this->assertSame($a, $b);
}
/**
* @dataProvider data_for_cast_value
*/
public function test_cast_value($raw, $type, $result) {
$input = testable_input_manager::instance();
$this->assertSame($input->cast_value($raw, $type), $result);
}
public function data_for_cast_value() {
return array(
array('3', input_manager::TYPE_INT, 3),
array(4, input_manager::TYPE_INT, 4),
array('', input_manager::TYPE_INT, 0),
array(true, input_manager::TYPE_FLAG, true),
array(false, input_manager::TYPE_FLAG, true),
array(0, input_manager::TYPE_FLAG, true),
array('1', input_manager::TYPE_FLAG, true),
array('0', input_manager::TYPE_FLAG, true),
array('muhehe', input_manager::TYPE_FLAG, true),
array('C:\\WINDOWS\\user.dat', input_manager::TYPE_PATH, 'C:/WINDOWS/user.dat'),
array('D:\xampp\htdocs\24_integration/mdeploy.php', input_manager::TYPE_PATH, 'D:/xampp/htdocs/24_integration/mdeploy.php'),
array('d:/xampp/htdocs/24_integration/mdeploy.php', input_manager::TYPE_PATH, 'd:/xampp/htdocs/24_integration/mdeploy.php'),
array('../../../etc/passwd', input_manager::TYPE_PATH, '/etc/passwd'),
array('///////.././public_html/test.php', input_manager::TYPE_PATH, '/public_html/test.php'),
array("!@#$%|/etc/qwerty\n\n\t\n\r", input_manager::TYPE_RAW, "!@#$%|/etc/qwerty\n\n\t\n\r"),
array("\nrock'n'roll.mp3\t.exe", input_manager::TYPE_FILE, 'rocknroll.mp3.exe'),
array('http://localhost/moodle/dev/plugin.zip', input_manager::TYPE_URL, 'http://localhost/moodle/dev/plugin.zip'),
array(
'https://moodle.org/plugins/download.php/1292/mod_stampcoll_moodle23_2012062201.zip',
input_manager::TYPE_URL,
'https://moodle.org/plugins/download.php/1292/mod_stampcoll_moodle23_2012062201.zip'
),
array('5e8d2ea4f50d154730100b1645fbad67', input_manager::TYPE_MD5, '5e8d2ea4f50d154730100b1645fbad67'),
);
}
/**
* @expectedException invalid_option_exception
*/
public function test_input_type_path_multiple_colons() {
$input = testable_input_manager::instance();
$input->cast_value('C:\apache\log:file', input_manager::TYPE_PATH); // must throw exception
}
/**
* @expectedException invalid_option_exception
*/
public function test_input_type_path_invalid_drive_label() {
$input = testable_input_manager::instance();
$input->cast_value('0:/srv/moodledata', input_manager::TYPE_PATH); // must throw exception
}
/**
* @expectedException invalid_option_exception
*/
public function test_input_type_path_invalid_colon() {
$input = testable_input_manager::instance();
$input->cast_value('/var/www/moodle:2.5', input_manager::TYPE_PATH); // must throw exception
}
/**
* @expectedException invalid_coding_exception
*/
public function test_cast_array_argument() {
$input = testable_input_manager::instance();
$input->cast_value(array(1, 2, 3), input_manager::TYPE_INT); // must throw exception
}
/**
* @expectedException invalid_coding_exception
*/
public function test_cast_object_argument() {
$input = testable_input_manager::instance();
$o = new stdClass();
$input->cast_value($o, input_manager::TYPE_INT); // must throw exception
}
/**
* @expectedException invalid_option_exception
*/
public function test_cast_invalid_url_value() {
$input = testable_input_manager::instance();
$invalid = 'file:///etc/passwd';
$input->cast_value($invalid, input_manager::TYPE_URL); // must throw exception
}
/**
* @expectedException invalid_option_exception
*/
public function test_cast_invalid_md5_value() {
$input = testable_input_manager::instance();
$invalid = 'this is not a valid md5 hash';
$input->cast_value($invalid, input_manager::TYPE_MD5); // must throw exception
}
/**
* @expectedException invalid_option_exception
*/
public function test_cast_tilde_in_path() {
$input = testable_input_manager::instance();
$invalid = '~/public_html/moodle_dev';
$input->cast_value($invalid, input_manager::TYPE_PATH); // must throw exception
}
public function test_has_option() {
$provider = input_fake_provider::instance();
$provider->set_fake_options(array());
$this->assertFalse($provider->has_option('foo')); // foo not passed
$provider->set_fake_options(array('foo' => 1));
$this->assertFalse($provider->has_option('foo')); // foo passed but not a known option
$provider->set_fake_options(array('foo' => 1, 'help' => false));
$this->assertTrue($provider->has_option('help')); // help passed and it is a flag (value ignored)
$this->assertTrue($provider->has_option('h')); // 'h' is a shortname for 'help'
}
public function test_get_option() {
$input = testable_input_manager::instance();
$provider = input_fake_provider::instance();
$provider->set_fake_options(array('help' => false, 'passfile' => '_mdeploy.123456'));
$this->assertTrue($input->get_option('h'));
$this->assertEquals($input->get_option('passfile'), '_mdeploy.123456');
$this->assertEquals($input->get_option('password', 'admin123'), 'admin123');
try {
$this->assertEquals($input->get_option('password'), 'admin123'); // must throw exception (not passed but required)
$this->assertTrue(false);
} catch (missing_option_exception $e) {
$this->assertTrue(true);
}
}
public function test_moving_and_removing_directories() {
$worker = testable_worker::instance();
$root = sys_get_temp_dir().'/'.uniqid('mdeploytest', true);
mkdir($root.'/a', 0777, true);
touch($root.'/a/a.txt');
$this->assertTrue(file_exists($root.'/a/a.txt'));
$this->assertFalse(file_exists($root.'/b/a.txt'));
$this->assertTrue($worker->move_directory($root.'/a', $root.'/b'));
$this->assertFalse(is_dir($root.'/a'));
$this->assertTrue(file_exists($root.'/b/a.txt'));
$this->assertTrue($worker->move_directory($root.'/b', $root.'/c', true));
$this->assertTrue(file_exists($root.'/c/a.txt'));
$this->assertFalse(file_exists($root.'/b/a.txt'));
$this->assertTrue(is_dir($root.'/b'));
$this->assertTrue($worker->remove_directory($root.'/c', true));
$this->assertFalse(file_exists($root.'/c/a.txt'));
$this->assertTrue($worker->remove_directory($root.'/c'));
$this->assertFalse(is_dir($root.'/c'));
}
public function test_create_directory_precheck() {
$worker = testable_worker::instance();
$root = sys_get_temp_dir().'/'.uniqid('mdeploytest', true);
$this->assertFalse(file_exists($root));
$this->assertTrue($worker->create_directory_precheck($root));
$this->assertFalse(file_exists($root)); // The precheck is supposed to remove it again.
}
/**
* Test that an invalid setting throws an exception.
*
* @dataProvider get_env_unlisted_provider
* @expectedException invalid_setting_exception
* @expectedExceptionMessageRegExp /^Requested environment setting '[^']*' is invalid.$/
*/
public function test_get_env_unlisted($key) {
$worker = testable_worker::instance();
$worker->get_env($key);
}
public function get_env_unlisted_provider() {
return array(
// Completely invalid environment variables.
array('example'),
array('invalid'),
// Valid ones which have not been whitelisted.
array('noemailever'),
array('dbname'),
);
}
/**
* Test that a valid, but unset setting throws an exception.
*
* @dataProvider get_env_valid_provider
* @expectedException invalid_setting_exception
* @expectedExceptionMessageRegExp /^Requested environment setting '[^']*' is not current set.$/
*/
public function test_get_env_unset($key) {
// Ensure that the setting is currently unset.
global $CFG;
$CFG->$key = null;
$worker = testable_worker::instance();
$worker->get_env($key);
}
/**
* Test that a valid setting with data returns that data.
*
* @dataProvider get_env_valid_provider
*/
public function test_get_env_valid($key) {
// Ensure that the setting is currently unset.
global $CFG;
$CFG->$key = rand(0, 1000);
$worker = testable_worker::instance();
$value = $worker->get_env($key);
$this->assertEquals($CFG->$key, $value);
}
public function get_env_valid_provider() {
return array(
array('dataroot'),
array('dirroot'),
);
}
}