mirrored from git://git.moodle.org/moodle.git
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
pluginlib_test.php
559 lines (488 loc) · 19.8 KB
/
pluginlib_test.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?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/>.
/**
* Unit tests for the lib/pluginlib.php library
*
* @package core
* @category phpunit
* @copyright 2012 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir.'/pluginlib.php');
/**
* Tests of the basic API of the plugin manager
*/
class plugin_manager_test extends advanced_testcase {
public function test_plugin_manager_instance() {
$pluginman = testable_plugin_manager::instance();
$this->assertTrue($pluginman instanceof testable_plugin_manager);
}
public function test_get_plugins() {
$pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins();
$this->assertTrue(isset($plugins['mod']['foo']));
$this->assertTrue($plugins['mod']['foo'] instanceof testable_plugininfo_mod);
}
public function test_get_status() {
$pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins();
$modfoo = $plugins['mod']['foo'];
$this->assertEquals($modfoo->get_status(), plugin_manager::PLUGIN_STATUS_UPGRADE);
}
public function test_available_update() {
$pluginman = testable_plugin_manager::instance();
$plugins = $pluginman->get_plugins();
$this->assertNull($plugins['mod']['bar']->available_updates());
$this->assertEquals('array', gettype($plugins['mod']['foo']->available_updates()));
foreach ($plugins['mod']['foo']->available_updates() as $availableupdate) {
$this->assertInstanceOf('available_update_info', $availableupdate);
}
}
}
/**
* Tests of the basic API of the available update checker
*/
class available_update_checker_test extends advanced_testcase {
public function test_core_available_update() {
$provider = testable_available_update_checker::instance();
$this->assertTrue($provider instanceof available_update_checker);
$provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array());
$updates = $provider->get_update_info('core');
$this->assertEquals(count($updates), 2);
$provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array());
$updates = $provider->get_update_info('core');
$this->assertEquals(count($updates), 1);
$provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array());
$updates = $provider->get_update_info('core', array('minmaturity' => MATURITY_STABLE));
$this->assertNull($updates);
}
/**
* If there are no fetched data yet, the first cron should fetch them
*/
public function test_cron_initial_fetch() {
$provider = testable_available_update_checker::instance();
$provider->fakerecentfetch = null;
$provider->fakecurrenttimestamp = -1;
$this->setExpectedException('testable_available_update_checker_cron_executed');
$provider->cron();
}
/**
* If there is a fresh fetch available, no cron execution is expected
*/
public function test_cron_has_fresh_fetch() {
$provider = testable_available_update_checker::instance();
$provider->fakerecentfetch = time() - 23 * HOURSECS; // fetched 23 hours ago
$provider->fakecurrenttimestamp = -1;
$provider->cron();
$this->assertTrue(true); // we should get here with no exception thrown
}
/**
* If there is an outdated fetch, the cron execution is expected
*/
public function test_cron_has_outdated_fetch() {
$provider = testable_available_update_checker::instance();
$provider->fakerecentfetch = time() - 49 * HOURSECS; // fetched 49 hours ago
$provider->fakecurrenttimestamp = -1;
$this->setExpectedException('testable_available_update_checker_cron_executed');
$provider->cron();
}
/**
* The first cron after 01:42 AM today should fetch the data
*
* @see testable_available_update_checker::cron_execution_offset()
*/
public function test_cron_offset_execution_not_yet() {
$provider = testable_available_update_checker::instance();
$provider->fakecurrenttimestamp = mktime(1, 40, 02); // 01:40:02 AM today
$provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS;
$provider->cron();
$this->assertTrue(true); // we should get here with no exception thrown
}
/**
* The first cron after 01:42 AM today should fetch the data and then
* it is supposed to wait next 24 hours.
*
* @see testable_available_update_checker::cron_execution_offset()
*/
public function test_cron_offset_execution() {
$provider = testable_available_update_checker::instance();
// the cron at 01:45 should fetch the data
$provider->fakecurrenttimestamp = mktime(1, 45, 02); // 01:45:02 AM today
$provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS - 1;
$executed = false;
try {
$provider->cron();
} catch (testable_available_update_checker_cron_executed $e) {
$executed = true;
}
$this->assertTrue($executed, 'Cron should be executed at 01:45:02 but it was not.');
// another cron at 06:45 should still consider data as fresh enough
$provider->fakerecentfetch = $provider->fakecurrenttimestamp;
$provider->fakecurrenttimestamp = mktime(6, 45, 03); // 06:45:03 AM
$executed = false;
try {
$provider->cron();
} catch (testable_available_update_checker_cron_executed $e) {
$executed = true;
}
$this->assertFalse($executed, 'Cron should not be executed at 06:45:03 but it was.');
// the next scheduled execution should happen the next day
$provider->fakecurrenttimestamp = $provider->fakerecentfetch + 24 * HOURSECS + 1;
$executed = false;
try {
$provider->cron();
} catch (testable_available_update_checker_cron_executed $e) {
$executed = true;
}
$this->assertTrue($executed, 'Cron should be executed the next night but it was not.');
}
public function test_compare_responses_both_empty() {
$provider = testable_available_update_checker::instance();
$old = array();
$new = array();
$cmp = $provider->compare_responses($old, $new);
$this->assertEquals('array', gettype($cmp));
$this->assertTrue(empty($cmp));
}
public function test_compare_responses_old_empty() {
$provider = testable_available_update_checker::instance();
$old = array();
$new = array(
'updates' => array(
'core' => array(
array(
'version' => 2012060103
)
)
)
);
$cmp = $provider->compare_responses($old, $new);
$this->assertEquals('array', gettype($cmp));
$this->assertFalse(empty($cmp));
$this->assertTrue(isset($cmp['core'][0]['version']));
$this->assertEquals($cmp['core'][0]['version'], 2012060103);
}
public function test_compare_responses_no_change() {
$provider = testable_available_update_checker::instance();
$old = $new = array(
'updates' => array(
'core' => array(
array(
'version' => 2012060104
),
array(
'version' => 2012120100
)
),
'mod_foo' => array(
array(
'version' => 2011010101
)
)
)
);
$cmp = $provider->compare_responses($old, $new);
$this->assertEquals('array', gettype($cmp));
$this->assertTrue(empty($cmp));
}
public function test_compare_responses_new_and_missing_update() {
$provider = testable_available_update_checker::instance();
$old = array(
'updates' => array(
'core' => array(
array(
'version' => 2012060104
)
),
'mod_foo' => array(
array(
'version' => 2011010101
)
)
)
);
$new = array(
'updates' => array(
'core' => array(
array(
'version' => 2012060104
),
array(
'version' => 2012120100
)
)
)
);
$cmp = $provider->compare_responses($old, $new);
$this->assertEquals('array', gettype($cmp));
$this->assertFalse(empty($cmp));
$this->assertEquals(count($cmp), 1);
$this->assertEquals(count($cmp['core']), 1);
$this->assertEquals($cmp['core'][0]['version'], 2012120100);
}
public function test_compare_responses_modified_update() {
$provider = testable_available_update_checker::instance();
$old = array(
'updates' => array(
'mod_foo' => array(
array(
'version' => 2011010101
)
)
)
);
$new = array(
'updates' => array(
'mod_foo' => array(
array(
'version' => 2011010102
)
)
)
);
$cmp = $provider->compare_responses($old, $new);
$this->assertEquals('array', gettype($cmp));
$this->assertFalse(empty($cmp));
$this->assertEquals(count($cmp), 1);
$this->assertEquals(count($cmp['mod_foo']), 1);
$this->assertEquals($cmp['mod_foo'][0]['version'], 2011010102);
}
public function test_compare_responses_invalid_format() {
$provider = testable_available_update_checker::instance();
$broken = array(
'status' => 'ERROR' // no 'updates' key here
);
$this->setExpectedException('available_update_checker_exception');
$cmp = $provider->compare_responses($broken, $broken);
}
public function test_is_same_release_explicit() {
$provider = testable_available_update_checker::instance();
$this->assertTrue($provider->is_same_release('2.3dev (Build: 20120323)', '2.3dev (Build: 20120323)'));
$this->assertTrue($provider->is_same_release('2.3dev (Build: 20120323)', '2.3dev (Build: 20120330)'));
$this->assertFalse($provider->is_same_release('2.3dev (Build: 20120529)', '2.3 (Build: 20120601)'));
$this->assertFalse($provider->is_same_release('2.3dev', '2.3 dev'));
$this->assertFalse($provider->is_same_release('2.3.1', '2.3'));
$this->assertFalse($provider->is_same_release('2.3.1', '2.3.2'));
$this->assertTrue($provider->is_same_release('2.3.2+', '2.3.2')); // yes, really
$this->assertTrue($provider->is_same_release('2.3.2 (Build: 123456)', '2.3.2+ (Build: 123457)'));
$this->assertFalse($provider->is_same_release('3.0 Community Edition', '3.0 Enterprise Edition'));
$this->assertTrue($provider->is_same_release('3.0 Community Edition', '3.0 Community Edition (Build: 20290101)'));
}
public function test_is_same_release_implicit() {
$provider = testable_available_update_checker::instance();
$provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array());
$this->assertTrue($provider->is_same_release('2.3.2'));
$this->assertTrue($provider->is_same_release('2.3.2+'));
$this->assertTrue($provider->is_same_release('2.3.2+ (Build: 20121013)'));
$this->assertFalse($provider->is_same_release('2.4dev (Build: 20121012)'));
}
}
/**
* Modified {@link plugininfo_mod} suitable for testing purposes
*/
class testable_plugininfo_mod extends plugininfo_mod {
public function init_display_name() {
$this->displayname = ucfirst($this->name);
}
public function load_disk_version() {
$this->versiondisk = 2012030500;
}
protected function load_version_php() {
return (object)array(
'version' => 2012030500,
'requires' => 2012010100,
'component' => $this->type.'_'.$this->name);
}
public function load_db_version() {
$this->versiondb = 2012022900;
}
}
/**
* Modified {@link plugin_manager} suitable for testing purposes
*/
class testable_plugin_manager extends plugin_manager {
/**
* Factory method for this class
*
* @return plugin_manager the singleton instance
*/
public static function instance() {
global $CFG;
if (is_null(self::$singletoninstance)) {
self::$singletoninstance = new self();
}
return self::$singletoninstance;
}
/**
* A version of {@link plugin_manager::get_plugins()} that prepares some faked
* testable instances.
*
* @param bool $disablecache ignored in this class
* @return array
*/
public function get_plugins($disablecache=false) {
global $CFG;
$this->pluginsinfo = array(
'mod' => array(
'foo' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/mod', 'foo',
$CFG->dirroot.'/mod/foo', 'testable_plugininfo_mod'),
'bar' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/bar', 'bar',
$CFG->dirroot.'/mod/bar', 'testable_plugininfo_mod'),
)
);
$checker = testable_available_update_checker::instance();
$this->pluginsinfo['mod']['foo']->check_available_updates($checker);
$this->pluginsinfo['mod']['bar']->check_available_updates($checker);
return $this->pluginsinfo;
}
}
/**
* Modified version of {@link available_update_checker} suitable for testing
*/
class testable_available_update_checker extends available_update_checker {
/** @var replaces the default DB table storage for the fetched response */
protected $fakeresponsestorage;
/** @var int stores the fake recentfetch value */
public $fakerecentfetch = -1;
/** @var int stores the fake value of time() */
public $fakecurrenttimestamp = -1;
/**
* Factory method for this class
*
* @return testable_available_update_checker the singleton instance
*/
public static function instance() {
global $CFG;
if (is_null(self::$singletoninstance)) {
self::$singletoninstance = new self();
}
return self::$singletoninstance;
}
protected function validate_response($response) {
}
protected function store_response($response) {
$this->fakeresponsestorage = $response;
}
protected function restore_response($forcereload = false) {
$this->recentfetch = time();
$this->recentresponse = $this->decode_response($this->get_fake_response());
}
public function compare_responses(array $old, array $new) {
return parent::compare_responses($old, $new);
}
public function is_same_release($remote, $local=null) {
return parent::is_same_release($remote, $local);
}
protected function load_current_environment($forcereload=false) {
}
public function fake_current_environment($version, $release, $branch, array $plugins) {
$this->currentversion = $version;
$this->currentrelease = $release;
$this->currentbranch = $branch;
$this->currentplugins = $plugins;
}
public function get_last_timefetched() {
if ($this->fakerecentfetch == -1) {
return parent::get_last_timefetched();
} else {
return $this->fakerecentfetch;
}
}
private function get_fake_response() {
$fakeresponse = array(
'status' => 'OK',
'provider' => 'http://download.moodle.org/api/1.0/updates.php',
'apiver' => '1.0',
'timegenerated' => time(),
'forversion' => '2012010100.00',
'forbranch' => '2.3',
'ticket' => sha1('No, I am not going to mention the word "frog" here. Oh crap. I just did.'),
'updates' => array(
'core' => array(
array(
'version' => 2012060103.00,
'release' => '2.3.3 (Build: 20121201)',
'maturity' => 200,
'url' => 'http://download.moodle.org/',
'download' => 'http://download.moodle.org/download.php/MOODLE_23_STABLE/moodle-2.3.3-latest.zip',
),
array(
'version' => 2012120100.00,
'release' => '2.4dev (Build: 20121201)',
'maturity' => 50,
'url' => 'http://download.moodle.org/',
'download' => 'http://download.moodle.org/download.php/MOODLE_24_STABLE/moodle-2.4.0-latest.zip',
),
),
'mod_foo' => array(
array(
'version' => 2012030501,
'requires' => 2012010100,
'maturity' => 200,
'release' => '1.1',
'url' => 'http://moodle.org/plugins/blahblahblah/',
'download' => 'http://moodle.org/plugins/download.php/blahblahblah',
),
array(
'version' => 2012030502,
'requires' => 2012010100,
'maturity' => 100,
'release' => '1.2 beta',
'url' => 'http://moodle.org/plugins/',
),
),
),
);
return json_encode($fakeresponse);
}
protected function cron_current_timestamp() {
if ($this->fakecurrenttimestamp == -1) {
return parent::cron_current_timestamp();
} else {
return $this->fakecurrenttimestamp;
}
}
protected function cron_mtrace($msg, $eol = PHP_EOL) {
}
protected function cron_autocheck_enabled() {
return true;
}
protected function cron_execution_offset() {
// autofetch should run by the first cron after 01:42 AM
return 42 * MINSECS;
}
protected function cron_execute() {
throw new testable_available_update_checker_cron_executed('Cron executed!');
}
}
/**
* Exception used to detect {@link available_update_checker::cron_execute()} calls
*/
class testable_available_update_checker_cron_executed extends Exception {
}
/**
* Test cases for {@link available_update_deployer} class
*/
class available_update_deployer_test extends advanced_testcase {
public function test_magic_setters() {
$deployer = available_update_deployer::instance();
$value = new moodle_url('/');
$deployer->set_returnurl($value);
$this->assertSame($deployer->get_returnurl(), $value);
}
}