MDL-35109 Improve unittests for cron based fetching of available updates
[moodle.git] / lib / tests / pluginlib_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * Unit tests for the lib/pluginlib.php library
19  *
20  * @package   core
21  * @category  phpunit
22  * @copyright 2012 David Mudrak <david@moodle.com>
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir.'/pluginlib.php');
32 /**
33  * Tests of the basic API of the plugin manager
34  */
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);
40     }
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);
47     }
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);
54     }
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);
63         }
64     }
65 }
68 /**
69  * Tests of the basic API of the available update checker
70  */
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);
88     }
90     /**
91      * If there are no fetched data yet, the first cron should fetch them
92      */
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');
98         $provider->cron();
99     }
101     /**
102      * If there is a fresh fetch available, no cron execution is expected
103      */
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;
108         $provider->cron();
109         $this->assertTrue(true); // we should get here with no exception thrown
110     }
112     /**
113      * If there is an outdated fetch, the cron execution is expected
114      */
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');
120         $provider->cron();
121     }
123     /**
124      * The first cron after 01:42 AM today should fetch the data
125      *
126      * @see testable_available_update_checker::cron_execution_offset()
127      */
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;
132         $provider->cron();
133         $this->assertTrue(true); // we should get here with no exception thrown
134     }
136     /**
137      * The first cron after 01:42 AM today should fetch the data and then
138      * it is supposed to wait next 24 hours.
139      *
140      * @see testable_available_update_checker::cron_execution_offset()
141      */
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;
148         $executed = false;
149         try {
150             $provider->cron();
151         } catch (testable_available_update_checker_cron_executed $e) {
152             $executed = true;
153         }
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
159         $executed = false;
160         try {
161             $provider->cron();
162         } catch (testable_available_update_checker_cron_executed $e) {
163             $executed = true;
164         }
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;
169         $executed = false;
170         try {
171             $provider->cron();
172         } catch (testable_available_update_checker_cron_executed $e) {
173             $executed = true;
174         }
175         $this->assertTrue($executed, 'Cron should be executed the next night but it was not.');
176     }
178     public function test_compare_responses_both_empty() {
179         $provider = testable_available_update_checker::instance();
180         $old = array();
181         $new = array();
182         $cmp = $provider->compare_responses($old, $new);
183         $this->assertEquals('array', gettype($cmp));
184         $this->assertTrue(empty($cmp));
185     }
187     public function test_compare_responses_old_empty() {
188         $provider = testable_available_update_checker::instance();
189         $old = array();
190         $new = array(
191             'updates' => array(
192                 'core' => array(
193                     array(
194                         'version' => 2012060103
195                     )
196                 )
197             )
198         );
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);
204     }
206     public function test_compare_responses_no_change() {
207         $provider = testable_available_update_checker::instance();
208         $old = $new = array(
209             'updates' => array(
210                 'core' => array(
211                     array(
212                         'version' => 2012060104
213                     ),
214                     array(
215                         'version' => 2012120100
216                     )
217                 ),
218                 'mod_foo' => array(
219                     array(
220                         'version' => 2011010101
221                     )
222                 )
223             )
224         );
225         $cmp = $provider->compare_responses($old, $new);
226         $this->assertEquals('array', gettype($cmp));
227         $this->assertTrue(empty($cmp));
228     }
230     public function test_compare_responses_new_and_missing_update() {
231         $provider = testable_available_update_checker::instance();
232         $old = array(
233             'updates' => array(
234                 'core' => array(
235                     array(
236                         'version' => 2012060104
237                     )
238                 ),
239                 'mod_foo' => array(
240                     array(
241                         'version' => 2011010101
242                     )
243                 )
244             )
245         );
246         $new = array(
247             'updates' => array(
248                 'core' => array(
249                     array(
250                         'version' => 2012060104
251                     ),
252                     array(
253                         'version' => 2012120100
254                     )
255                 )
256             )
257         );
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);
264     }
266     public function test_compare_responses_modified_update() {
267         $provider = testable_available_update_checker::instance();
268         $old = array(
269             'updates' => array(
270                 'mod_foo' => array(
271                     array(
272                         'version' => 2011010101
273                     )
274                 )
275             )
276         );
277         $new = array(
278             'updates' => array(
279                 'mod_foo' => array(
280                     array(
281                         'version' => 2011010102
282                     )
283                 )
284             )
285         );
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);
292     }
294     public function test_compare_responses_invalid_format() {
295         $provider = testable_available_update_checker::instance();
296         $broken = array(
297             'status' => 'ERROR' // no 'updates' key here
298         );
299         $this->setExpectedException('available_update_checker_exception');
300         $cmp = $provider->compare_responses($broken, $broken);
301     }
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)'));
315     }
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)'));
324     }
328 /**
329  * Modified {@link plugininfo_mod} suitable for testing purposes
330  */
331 class testable_plugininfo_mod extends plugininfo_mod {
333     public function init_display_name() {
334         $this->displayname = ucfirst($this->name);
335     }
337     public function load_disk_version() {
338         $this->versiondisk = 2012030500;
339     }
341     protected function load_version_php() {
342         return (object)array(
343             'version' => 2012030500,
344             'requires' => 2012010100,
345             'component' => $this->type.'_'.$this->name);
346     }
348     public function load_db_version() {
349         $this->versiondb = 2012022900;
350     }
354 /**
355  * Modified {@link plugin_manager} suitable for testing purposes
356  */
357 class testable_plugin_manager extends plugin_manager {
359     /**
360      * Factory method for this class
361      *
362      * @return plugin_manager the singleton instance
363      */
364     public static function instance() {
365         global $CFG;
367         if (is_null(self::$singletoninstance)) {
368             self::$singletoninstance = new self();
369         }
370         return self::$singletoninstance;
371     }
373     /**
374      * A version of {@link plugin_manager::get_plugins()} that prepares some faked
375      * testable instances.
376      *
377      * @param bool $disablecache ignored in this class
378      * @return array
379      */
380     public function get_plugins($disablecache=false) {
381         global $CFG;
383         $this->pluginsinfo = array(
384             'mod' => 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'),
389             )
390         );
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;
397     }
401 /**
402  * Modified version of {@link available_update_checker} suitable for testing
403  */
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;
413     /**
414      * Factory method for this class
415      *
416      * @return testable_available_update_checker the singleton instance
417      */
418     public static function instance() {
419         global $CFG;
421         if (is_null(self::$singletoninstance)) {
422             self::$singletoninstance = new self();
423         }
424         return self::$singletoninstance;
425     }
427     protected function validate_response($response) {
428     }
430     protected function store_response($response) {
431         $this->fakeresponsestorage = $response;
432     }
434     protected function restore_response($forcereload = false) {
435         $this->recentfetch = time();
436         $this->recentresponse = $this->decode_response($this->get_fake_response());
437     }
439     public function compare_responses(array $old, array $new) {
440         return parent::compare_responses($old, $new);
441     }
443     public function is_same_release($remote, $local=null) {
444         return parent::is_same_release($remote, $local);
445     }
447     protected function load_current_environment($forcereload=false) {
448     }
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;
455     }
457     public function get_last_timefetched() {
458         if ($this->fakerecentfetch == -1) {
459             return parent::get_last_timefetched();
460         } else {
461             return $this->fakerecentfetch;
462         }
463     }
465     private function get_fake_response() {
466         $fakeresponse = array(
467             'status' => 'OK',
468             'provider' => 'http://download.moodle.org/api/1.0/updates.php',
469             'apiver' => '1.0',
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.'),
474             'updates' => array(
475                 'core' => array(
476                     array(
477                         'version' => 2012060103.00,
478                         'release' => '2.3.3 (Build: 20121201)',
479                         'maturity' => 200,
480                         'url' => 'http://download.moodle.org/',
481                         'download' => 'http://download.moodle.org/download.php/MOODLE_23_STABLE/moodle-2.3.3-latest.zip',
482                     ),
483                     array(
484                         'version' => 2012120100.00,
485                         'release' => '2.4dev (Build: 20121201)',
486                         'maturity' => 50,
487                         'url' => 'http://download.moodle.org/',
488                         'download' => 'http://download.moodle.org/download.php/MOODLE_24_STABLE/moodle-2.4.0-latest.zip',
489                     ),
490                 ),
491                 'mod_foo' => array(
492                     array(
493                         'version' => 2012030501,
494                         'requires' => 2012010100,
495                         'maturity' => 200,
496                         'release' => '1.1',
497                         'url' => 'http://moodle.org/plugins/blahblahblah/',
498                         'download' => 'http://moodle.org/plugins/download.php/blahblahblah',
499                     ),
500                     array(
501                         'version' => 2012030502,
502                         'requires' => 2012010100,
503                         'maturity' => 100,
504                         'release' => '1.2 beta',
505                         'url' => 'http://moodle.org/plugins/',
506                     ),
507                 ),
508             ),
509         );
511         return json_encode($fakeresponse);
512     }
514     protected function cron_current_timestamp() {
515         if ($this->fakecurrenttimestamp == -1) {
516             return parent::cron_current_timestamp();
517         } else {
518             return $this->fakecurrenttimestamp;
519         }
520     }
522     protected function cron_mtrace($msg, $eol = PHP_EOL) {
523     }
525     protected function cron_autocheck_enabled() {
526         return true;
527     }
529     protected function cron_execution_offset() {
530         // autofetch should run by the first cron after 01:42 AM
531         return 42 * MINSECS;
532     }
534     protected function cron_execute() {
535         throw new testable_available_update_checker_cron_executed('Cron executed!');
536     }
540 /**
541  * Exception used to detect {@link available_update_checker::cron_execute()} calls
542  */
543 class testable_available_update_checker_cron_executed extends Exception {