eb29268a55b2b51e5f8571ae419b9a2bbddbb3af
[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() - 59 * MINSECS; // fetched an hour 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->fakerecentfetch = time() - 24 * HOURSECS;
131         $provider->fakecurrenttimestamp = mktime(1, 40, 02); // 01:40:02 AM
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
138      *
139      * @see testable_available_update_checker::cron_execution_offset()
140      */
141     public function test_cron_offset_execution() {
142         $provider = testable_available_update_checker::instance();
143         $provider->fakerecentfetch = time() - 24 * HOURSECS;
144         $provider->fakecurrenttimestamp = mktime(1, 45, 02); // 01:45:02 AM
145         $this->setExpectedException('testable_available_update_checker_cron_executed');
146         $provider->cron();
147     }
149     public function test_compare_responses_both_empty() {
150         $provider = testable_available_update_checker::instance();
151         $old = array();
152         $new = array();
153         $cmp = $provider->compare_responses($old, $new);
154         $this->assertEquals('array', gettype($cmp));
155         $this->assertTrue(empty($cmp));
156     }
158     public function test_compare_responses_old_empty() {
159         $provider = testable_available_update_checker::instance();
160         $old = array();
161         $new = array(
162             'updates' => array(
163                 'core' => array(
164                     array(
165                         'version' => 2012060103
166                     )
167                 )
168             )
169         );
170         $cmp = $provider->compare_responses($old, $new);
171         $this->assertEquals('array', gettype($cmp));
172         $this->assertFalse(empty($cmp));
173         $this->assertTrue(isset($cmp['core'][0]['version']));
174         $this->assertEquals($cmp['core'][0]['version'], 2012060103);
175     }
177     public function test_compare_responses_no_change() {
178         $provider = testable_available_update_checker::instance();
179         $old = $new = array(
180             'updates' => array(
181                 'core' => array(
182                     array(
183                         'version' => 2012060104
184                     ),
185                     array(
186                         'version' => 2012120100
187                     )
188                 ),
189                 'mod_foo' => array(
190                     array(
191                         'version' => 2011010101
192                     )
193                 )
194             )
195         );
196         $cmp = $provider->compare_responses($old, $new);
197         $this->assertEquals('array', gettype($cmp));
198         $this->assertTrue(empty($cmp));
199     }
201     public function test_compare_responses_new_and_missing_update() {
202         $provider = testable_available_update_checker::instance();
203         $old = array(
204             'updates' => array(
205                 'core' => array(
206                     array(
207                         'version' => 2012060104
208                     )
209                 ),
210                 'mod_foo' => array(
211                     array(
212                         'version' => 2011010101
213                     )
214                 )
215             )
216         );
217         $new = array(
218             'updates' => array(
219                 'core' => array(
220                     array(
221                         'version' => 2012060104
222                     ),
223                     array(
224                         'version' => 2012120100
225                     )
226                 )
227             )
228         );
229         $cmp = $provider->compare_responses($old, $new);
230         $this->assertEquals('array', gettype($cmp));
231         $this->assertFalse(empty($cmp));
232         $this->assertEquals(count($cmp), 1);
233         $this->assertEquals(count($cmp['core']), 1);
234         $this->assertEquals($cmp['core'][0]['version'], 2012120100);
235     }
237     public function test_compare_responses_modified_update() {
238         $provider = testable_available_update_checker::instance();
239         $old = array(
240             'updates' => array(
241                 'mod_foo' => array(
242                     array(
243                         'version' => 2011010101
244                     )
245                 )
246             )
247         );
248         $new = array(
249             'updates' => array(
250                 'mod_foo' => array(
251                     array(
252                         'version' => 2011010102
253                     )
254                 )
255             )
256         );
257         $cmp = $provider->compare_responses($old, $new);
258         $this->assertEquals('array', gettype($cmp));
259         $this->assertFalse(empty($cmp));
260         $this->assertEquals(count($cmp), 1);
261         $this->assertEquals(count($cmp['mod_foo']), 1);
262         $this->assertEquals($cmp['mod_foo'][0]['version'], 2011010102);
263     }
265     public function test_compare_responses_invalid_format() {
266         $provider = testable_available_update_checker::instance();
267         $broken = array(
268             'status' => 'ERROR' // no 'updates' key here
269         );
270         $this->setExpectedException('available_update_checker_exception');
271         $cmp = $provider->compare_responses($broken, $broken);
272     }
274     public function test_is_same_release_explicit() {
275         $provider = testable_available_update_checker::instance();
276         $this->assertTrue($provider->is_same_release('2.3dev (Build: 20120323)', '2.3dev (Build: 20120323)'));
277         $this->assertTrue($provider->is_same_release('2.3dev (Build: 20120323)', '2.3dev (Build: 20120330)'));
278         $this->assertFalse($provider->is_same_release('2.3dev (Build: 20120529)', '2.3 (Build: 20120601)'));
279         $this->assertFalse($provider->is_same_release('2.3dev', '2.3 dev'));
280         $this->assertFalse($provider->is_same_release('2.3.1', '2.3'));
281         $this->assertFalse($provider->is_same_release('2.3.1', '2.3.2'));
282         $this->assertTrue($provider->is_same_release('2.3.2+', '2.3.2')); // yes, really
283         $this->assertTrue($provider->is_same_release('2.3.2 (Build: 123456)', '2.3.2+ (Build: 123457)'));
284         $this->assertFalse($provider->is_same_release('3.0 Community Edition', '3.0 Enterprise Edition'));
285         $this->assertTrue($provider->is_same_release('3.0 Community Edition', '3.0 Community Edition (Build: 20290101)'));
286     }
288     public function test_is_same_release_implicit() {
289         $provider = testable_available_update_checker::instance();
290         $provider->fake_current_environment(2012060102.00, '2.3.2 (Build: 20121012)', '2.3', array());
291         $this->assertTrue($provider->is_same_release('2.3.2'));
292         $this->assertTrue($provider->is_same_release('2.3.2+'));
293         $this->assertTrue($provider->is_same_release('2.3.2+ (Build: 20121013)'));
294         $this->assertFalse($provider->is_same_release('2.4dev (Build: 20121012)'));
295     }
299 /**
300  * Modified {@link plugininfo_mod} suitable for testing purposes
301  */
302 class testable_plugininfo_mod extends plugininfo_mod {
304     public function init_display_name() {
305         $this->displayname = ucfirst($this->name);
306     }
308     public function load_disk_version() {
309         $this->versiondisk = 2012030500;
310     }
312     protected function load_version_php() {
313         return (object)array(
314             'version' => 2012030500,
315             'requires' => 2012010100,
316             'component' => $this->type.'_'.$this->name);
317     }
319     public function load_db_version() {
320         $this->versiondb = 2012022900;
321     }
325 /**
326  * Modified {@link plugin_manager} suitable for testing purposes
327  */
328 class testable_plugin_manager extends plugin_manager {
330     /**
331      * Factory method for this class
332      *
333      * @return plugin_manager the singleton instance
334      */
335     public static function instance() {
336         global $CFG;
338         if (is_null(self::$singletoninstance)) {
339             self::$singletoninstance = new self();
340         }
341         return self::$singletoninstance;
342     }
344     /**
345      * A version of {@link plugin_manager::get_plugins()} that prepares some faked
346      * testable instances.
347      *
348      * @param bool $disablecache ignored in this class
349      * @return array
350      */
351     public function get_plugins($disablecache=false) {
352         global $CFG;
354         $this->pluginsinfo = array(
355             'mod' => array(
356                 'foo' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/mod', 'foo',
357                     $CFG->dirroot.'/mod/foo', 'testable_plugininfo_mod'),
358                 'bar' => plugininfo_default_factory::make('mod', $CFG->dirroot.'/bar', 'bar',
359                     $CFG->dirroot.'/mod/bar', 'testable_plugininfo_mod'),
360             )
361         );
363         $checker = testable_available_update_checker::instance();
364         $this->pluginsinfo['mod']['foo']->check_available_updates($checker);
365         $this->pluginsinfo['mod']['bar']->check_available_updates($checker);
367         return $this->pluginsinfo;
368     }
372 /**
373  * Modified version of {@link available_update_checker} suitable for testing
374  */
375 class testable_available_update_checker extends available_update_checker {
377     /** @var replaces the default DB table storage for the fetched response */
378     protected $fakeresponsestorage;
379     /** @var int stores the fake recentfetch value */
380     public $fakerecentfetch = -1;
381     /** @var int stores the fake value of time() */
382     public $fakecurrenttimestamp = -1;
384     /**
385      * Factory method for this class
386      *
387      * @return testable_available_update_checker the singleton instance
388      */
389     public static function instance() {
390         global $CFG;
392         if (is_null(self::$singletoninstance)) {
393             self::$singletoninstance = new self();
394         }
395         return self::$singletoninstance;
396     }
398     protected function validate_response($response) {
399     }
401     protected function store_response($response) {
402         $this->fakeresponsestorage = $response;
403     }
405     protected function restore_response($forcereload = false) {
406         $this->recentfetch = time();
407         $this->recentresponse = $this->decode_response($this->get_fake_response());
408     }
410     public function compare_responses(array $old, array $new) {
411         return parent::compare_responses($old, $new);
412     }
414     public function is_same_release($remote, $local=null) {
415         return parent::is_same_release($remote, $local);
416     }
418     protected function load_current_environment($forcereload=false) {
419     }
421     public function fake_current_environment($version, $release, $branch, array $plugins) {
422         $this->currentversion = $version;
423         $this->currentrelease = $release;
424         $this->currentbranch = $branch;
425         $this->currentplugins = $plugins;
426     }
428     public function get_last_timefetched() {
429         if ($this->fakerecentfetch == -1) {
430             return parent::get_last_timefetched();
431         } else {
432             return $this->fakerecentfetch;
433         }
434     }
436     private function get_fake_response() {
437         $fakeresponse = array(
438             'status' => 'OK',
439             'provider' => 'http://download.moodle.org/api/1.0/updates.php',
440             'apiver' => '1.0',
441             'timegenerated' => time(),
442             'forversion' => '2012010100.00',
443             'forbranch' => '2.3',
444             'ticket' => sha1('No, I am not going to mention the word "frog" here. Oh crap. I just did.'),
445             'updates' => array(
446                 'core' => array(
447                     array(
448                         'version' => 2012060103.00,
449                         'release' => '2.3.3 (Build: 20121201)',
450                         'maturity' => 200,
451                         'url' => 'http://download.moodle.org/',
452                         'download' => 'http://download.moodle.org/download.php/MOODLE_23_STABLE/moodle-2.3.3-latest.zip',
453                     ),
454                     array(
455                         'version' => 2012120100.00,
456                         'release' => '2.4dev (Build: 20121201)',
457                         'maturity' => 50,
458                         'url' => 'http://download.moodle.org/',
459                         'download' => 'http://download.moodle.org/download.php/MOODLE_24_STABLE/moodle-2.4.0-latest.zip',
460                     ),
461                 ),
462                 'mod_foo' => array(
463                     array(
464                         'version' => 2012030501,
465                         'requires' => 2012010100,
466                         'maturity' => 200,
467                         'release' => '1.1',
468                         'url' => 'http://moodle.org/plugins/blahblahblah/',
469                         'download' => 'http://moodle.org/plugins/download.php/blahblahblah',
470                     ),
471                     array(
472                         'version' => 2012030502,
473                         'requires' => 2012010100,
474                         'maturity' => 100,
475                         'release' => '1.2 beta',
476                         'url' => 'http://moodle.org/plugins/',
477                     ),
478                 ),
479             ),
480         );
482         return json_encode($fakeresponse);
483     }
485     protected function cron_current_timestamp() {
486         if ($this->fakecurrenttimestamp == -1) {
487             return parent::cron_current_timestamp();
488         } else {
489             return $this->fakecurrenttimestamp;
490         }
491     }
493     protected function cron_mtrace($msg, $eol = PHP_EOL) {
494     }
496     protected function cron_autocheck_enabled() {
497         return true;
498     }
500     protected function cron_execution_offset() {
501         // autofetch should run by the first cron after 01:42 AM
502         return 42 * MINSECS;
503     }
505     protected function cron_execute() {
506         throw new testable_available_update_checker_cron_executed('Cron executed but it should not!');
507     }
511 /**
512  * Exception used to detect {@link available_update_checker::cron_execute()} calls
513  */
514 class testable_available_update_checker_cron_executed extends Exception {