2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for the lib/pluginlib.php library
22 * @copyright 2012 David Mudrak <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->libdir.'/pluginlib.php');
33 * Tests of the basic API of the plugin manager
35 class plugin_manager_test extends advanced_testcase {
37 public function test_plugin_manager_instance() {
38 $pluginman = testable_plugin_manager::instance();
39 $this->assertTrue($pluginman instanceof testable_plugin_manager);
42 public function test_get_plugins() {
43 $pluginman = testable_plugin_manager::instance();
44 $plugins = $pluginman->get_plugins();
45 $this->assertTrue(isset($plugins['mod']['foo']));
46 $this->assertTrue($plugins['mod']['foo'] instanceof testable_plugininfo_mod);
49 public function test_get_status() {
50 $pluginman = testable_plugin_manager::instance();
51 $plugins = $pluginman->get_plugins();
52 $modfoo = $plugins['mod']['foo'];
53 $this->assertEquals($modfoo->get_status(), plugin_manager::PLUGIN_STATUS_UPGRADE);
56 public function test_available_update() {
57 $pluginman = testable_plugin_manager::instance();
58 $plugins = $pluginman->get_plugins();
59 $this->assertNull($plugins['mod']['bar']->available_updates());
60 $this->assertEquals('array', gettype($plugins['mod']['foo']->available_updates()));
61 foreach ($plugins['mod']['foo']->available_updates() as $availableupdate) {
62 $this->assertInstanceOf('available_update_info', $availableupdate);
69 * Tests of the basic API of the available update checker
71 class available_update_checker_test extends advanced_testcase {
73 public function test_core_available_update() {
74 $provider = testable_available_update_checker::instance();
75 $this->assertTrue($provider instanceof available_update_checker);
77 $provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array());
78 $updates = $provider->get_update_info('core');
79 $this->assertEquals(count($updates), 2);
81 $provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array());
82 $updates = $provider->get_update_info('core');
83 $this->assertEquals(count($updates), 1);
85 $provider->fake_current_environment(2012060103.00, '2.3.3 (Build: 20121212)', '2.3', array());
86 $updates = $provider->get_update_info('core', array('minmaturity' => MATURITY_STABLE));
87 $this->assertNull($updates);
91 * If there are no fetched data yet, the first cron should fetch them
93 public function test_cron_initial_fetch() {
94 $provider = testable_available_update_checker::instance();
95 $provider->fakerecentfetch = null;
96 $provider->fakecurrenttimestamp = -1;
97 $this->setExpectedException('testable_available_update_checker_cron_executed');
102 * If there is a fresh fetch available, no cron execution is expected
104 public function test_cron_has_fresh_fetch() {
105 $provider = testable_available_update_checker::instance();
106 $provider->fakerecentfetch = time() - 23 * HOURSECS; // fetched 23 hours ago
107 $provider->fakecurrenttimestamp = -1;
109 $this->assertTrue(true); // we should get here with no exception thrown
113 * If there is an outdated fetch, the cron execution is expected
115 public function test_cron_has_outdated_fetch() {
116 $provider = testable_available_update_checker::instance();
117 $provider->fakerecentfetch = time() - 49 * HOURSECS; // fetched 49 hours ago
118 $provider->fakecurrenttimestamp = -1;
119 $this->setExpectedException('testable_available_update_checker_cron_executed');
124 * The first cron after 01:42 AM today should fetch the data
126 * @see testable_available_update_checker::cron_execution_offset()
128 public function test_cron_offset_execution_not_yet() {
129 $provider = testable_available_update_checker::instance();
130 $provider->fakecurrenttimestamp = mktime(1, 40, 02); // 01:40:02 AM today
131 $provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS;
133 $this->assertTrue(true); // we should get here with no exception thrown
137 * The first cron after 01:42 AM today should fetch the data and then
138 * it is supposed to wait next 24 hours.
140 * @see testable_available_update_checker::cron_execution_offset()
142 public function test_cron_offset_execution() {
143 $provider = testable_available_update_checker::instance();
145 // the cron at 01:45 should fetch the data
146 $provider->fakecurrenttimestamp = mktime(1, 45, 02); // 01:45:02 AM today
147 $provider->fakerecentfetch = $provider->fakecurrenttimestamp - 24 * HOURSECS - 1;
151 } catch (testable_available_update_checker_cron_executed $e) {
154 $this->assertTrue($executed, 'Cron should be executed at 01:45:02 but it was not.');
156 // another cron at 06:45 should still consider data as fresh enough
157 $provider->fakerecentfetch = $provider->fakecurrenttimestamp;
158 $provider->fakecurrenttimestamp = mktime(6, 45, 03); // 06:45:03 AM
162 } catch (testable_available_update_checker_cron_executed $e) {
165 $this->assertFalse($executed, 'Cron should not be executed at 06:45:03 but it was.');
167 // the next scheduled execution should happen the next day
168 $provider->fakecurrenttimestamp = $provider->fakerecentfetch + 24 * HOURSECS + 1;
172 } catch (testable_available_update_checker_cron_executed $e) {
175 $this->assertTrue($executed, 'Cron should be executed the next night but it was not.');
178 public function test_compare_responses_both_empty() {
179 $provider = testable_available_update_checker::instance();
182 $cmp = $provider->compare_responses($old, $new);
183 $this->assertEquals('array', gettype($cmp));
184 $this->assertTrue(empty($cmp));
187 public function test_compare_responses_old_empty() {
188 $provider = testable_available_update_checker::instance();
194 'version' => 2012060103
199 $cmp = $provider->compare_responses($old, $new);
200 $this->assertEquals('array', gettype($cmp));
201 $this->assertFalse(empty($cmp));
202 $this->assertTrue(isset($cmp['core'][0]['version']));
203 $this->assertEquals($cmp['core'][0]['version'], 2012060103);
206 public function test_compare_responses_no_change() {
207 $provider = testable_available_update_checker::instance();
212 'version' => 2012060104
215 'version' => 2012120100
220 'version' => 2011010101
225 $cmp = $provider->compare_responses($old, $new);
226 $this->assertEquals('array', gettype($cmp));
227 $this->assertTrue(empty($cmp));
230 public function test_compare_responses_new_and_missing_update() {
231 $provider = testable_available_update_checker::instance();
236 'version' => 2012060104
241 'version' => 2011010101
250 'version' => 2012060104
253 'version' => 2012120100
258 $cmp = $provider->compare_responses($old, $new);
259 $this->assertEquals('array', gettype($cmp));
260 $this->assertFalse(empty($cmp));
261 $this->assertEquals(count($cmp), 1);
262 $this->assertEquals(count($cmp['core']), 1);
263 $this->assertEquals($cmp['core'][0]['version'], 2012120100);
266 public function test_compare_responses_modified_update() {
267 $provider = testable_available_update_checker::instance();
272 'version' => 2011010101
281 'version' => 2011010102
286 $cmp = $provider->compare_responses($old, $new);
287 $this->assertEquals('array', gettype($cmp));
288 $this->assertFalse(empty($cmp));
289 $this->assertEquals(count($cmp), 1);
290 $this->assertEquals(count($cmp['mod_foo']), 1);
291 $this->assertEquals($cmp['mod_foo'][0]['version'], 2011010102);
294 public function test_compare_responses_invalid_format() {
295 $provider = testable_available_update_checker::instance();
297 'status' => 'ERROR' // no 'updates' key here
299 $this->setExpectedException('available_update_checker_exception');
300 $cmp = $provider->compare_responses($broken, $broken);
303 public function test_is_same_release_explicit() {
304 $provider = testable_available_update_checker::instance();
305 $this->assertTrue($provider->is_same_release('2.3dev (Build: 20120323)', '2.3dev (Build: 20120323)'));
306 $this->assertTrue($provider->is_same_release('2.3dev (Build: 20120323)', '2.3dev (Build: 20120330)'));
307 $this->assertFalse($provider->is_same_release('2.3dev (Build: 20120529)', '2.3 (Build: 20120601)'));
308 $this->assertFalse($provider->is_same_release('2.3dev', '2.3 dev'));
309 $this->assertFalse($provider->is_same_release('2.3.1', '2.3'));
310 $this->assertFalse($provider->is_same_release('2.3.1', '2.3.2'));
311 $this->assertTrue($provider->is_same_release('2.3.2+', '2.3.2')); // yes, really
312 $this->assertTrue($provider->is_same_release('2.3.2 (Build: 123456)', '2.3.2+ (Build: 123457)'));
313 $this->assertFalse($provider->is_same_release('3.0 Community Edition', '3.0 Enterprise Edition'));
314 $this->assertTrue($provider->is_same_release('3.0 Community Edition', '3.0 Community Edition (Build: 20290101)'));
317 public function test_is_same_release_implicit() {
318 $provider = testable_available_update_checker::instance();
319 $provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array());
320 $this->assertTrue($provider->is_same_release('2.3.2'));
321 $this->assertTrue($provider->is_same_release('2.3.2+'));
322 $this->assertTrue($provider->is_same_release('2.3.2+ (Build: 20121013)'));
323 $this->assertFalse($provider->is_same_release('2.4dev (Build: 20121012)'));
329 * Modified {@link plugininfo_mod} suitable for testing purposes
331 class testable_plugininfo_mod extends plugininfo_mod {
333 public function init_display_name() {
334 $this->displayname = ucfirst($this->name);
337 public function load_disk_version() {
338 $this->versiondisk = 2012030500;
341 protected function load_version_php() {
342 return (object)array(
343 'version' => 2012030500,
344 'requires' => 2012010100,
345 'component' => $this->type.'_'.$this->name);
348 public function load_db_version() {
349 $this->versiondb = 2012022900;
355 * Modified {@link plugin_manager} suitable for testing purposes
357 class testable_plugin_manager extends plugin_manager {
360 * Factory method for this class
362 * @return plugin_manager the singleton instance
364 public static function instance() {
367 if (is_null(self::$singletoninstance)) {
368 self::$singletoninstance = new self();
370 return self::$singletoninstance;
374 * A version of {@link plugin_manager::get_plugins()} that prepares some faked
375 * testable instances.
377 * @param bool $disablecache ignored in this class
380 public function get_plugins($disablecache=false) {
383 $this->pluginsinfo = array(
385 'foo' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/mod', 'foo',
386 $CFG->dirroot.'/mod/foo', 'testable_plugininfo_mod'),
387 'bar' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/bar', 'bar',
388 $CFG->dirroot.'/mod/bar', 'testable_plugininfo_mod'),
392 $checker = testable_available_update_checker::instance();
393 $this->pluginsinfo['mod']['foo']->check_available_updates($checker);
394 $this->pluginsinfo['mod']['bar']->check_available_updates($checker);
396 return $this->pluginsinfo;
402 * Modified version of {@link available_update_checker} suitable for testing
404 class testable_available_update_checker extends available_update_checker {
406 /** @var replaces the default DB table storage for the fetched response */
407 protected $fakeresponsestorage;
408 /** @var int stores the fake recentfetch value */
409 public $fakerecentfetch = -1;
410 /** @var int stores the fake value of time() */
411 public $fakecurrenttimestamp = -1;
414 * Factory method for this class
416 * @return testable_available_update_checker the singleton instance
418 public static function instance() {
421 if (is_null(self::$singletoninstance)) {
422 self::$singletoninstance = new self();
424 return self::$singletoninstance;
427 protected function validate_response($response) {
430 protected function store_response($response) {
431 $this->fakeresponsestorage = $response;
434 protected function restore_response($forcereload = false) {
435 $this->recentfetch = time();
436 $this->recentresponse = $this->decode_response($this->get_fake_response());
439 public function compare_responses(array $old, array $new) {
440 return parent::compare_responses($old, $new);
443 public function is_same_release($remote, $local=null) {
444 return parent::is_same_release($remote, $local);
447 protected function load_current_environment($forcereload=false) {
450 public function fake_current_environment($version, $release, $branch, array $plugins) {
451 $this->currentversion = $version;
452 $this->currentrelease = $release;
453 $this->currentbranch = $branch;
454 $this->currentplugins = $plugins;
457 public function get_last_timefetched() {
458 if ($this->fakerecentfetch == -1) {
459 return parent::get_last_timefetched();
461 return $this->fakerecentfetch;
465 private function get_fake_response() {
466 $fakeresponse = array(
468 'provider' => 'http://download.moodle.org/api/1.0/updates.php',
470 'timegenerated' => time(),
471 'forversion' => '2012010100.00',
472 'forbranch' => '2.3',
473 'ticket' => sha1('No, I am not going to mention the word "frog" here. Oh crap. I just did.'),
477 'version' => 2012060103.00,
478 'release' => '2.3.3 (Build: 20121201)',
480 'url' => 'http://download.moodle.org/',
481 'download' => 'http://download.moodle.org/download.php/MOODLE_23_STABLE/moodle-2.3.3-latest.zip',
484 'version' => 2012120100.00,
485 'release' => '2.4dev (Build: 20121201)',
487 'url' => 'http://download.moodle.org/',
488 'download' => 'http://download.moodle.org/download.php/MOODLE_24_STABLE/moodle-2.4.0-latest.zip',
493 'version' => 2012030501,
494 'requires' => 2012010100,
497 'url' => 'http://moodle.org/plugins/blahblahblah/',
498 'download' => 'http://moodle.org/plugins/download.php/blahblahblah',
501 'version' => 2012030502,
502 'requires' => 2012010100,
504 'release' => '1.2 beta',
505 'url' => 'http://moodle.org/plugins/',
511 return json_encode($fakeresponse);
514 protected function cron_current_timestamp() {
515 if ($this->fakecurrenttimestamp == -1) {
516 return parent::cron_current_timestamp();
518 return $this->fakecurrenttimestamp;
522 protected function cron_mtrace($msg, $eol = PHP_EOL) {
525 protected function cron_autocheck_enabled() {
529 protected function cron_execution_offset() {
530 // autofetch should run by the first cron after 01:42 AM
534 protected function cron_execute() {
535 throw new testable_available_update_checker_cron_executed('Cron executed!');
541 * Exception used to detect {@link available_update_checker::cron_execute()} calls
543 class testable_available_update_checker_cron_executed extends Exception {