MDL-49329 admin: Introduce new \core\update\remote_info class
[moodle.git] / lib / tests / plugin_manager_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 plugin manager class.
19  *
20  * @package   core
21  * @category  phpunit
22  * @copyright 2013 Petr Skoda {@link http://skodak.org}
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->dirroot.'/lib/tests/fixtures/testable_plugin_manager.php');
30 require_once($CFG->dirroot.'/lib/tests/fixtures/testable_plugininfo_base.php');
32 /**
33  * Tests of the basic API of the plugin manager.
34  */
35 class core_plugin_manager_testcase extends advanced_testcase {
37     public function tearDown() {
38         // The caches of the testable singleton must be reset explicitly. It is
39         // safer to kill the whole testable singleton at the end of every test.
40         testable_core_plugin_manager::reset_caches();
41     }
43     public function test_instance() {
44         $pluginman1 = core_plugin_manager::instance();
45         $this->assertInstanceOf('core_plugin_manager', $pluginman1);
46         $pluginman2 = core_plugin_manager::instance();
47         $this->assertSame($pluginman1, $pluginman2);
48         $pluginman3 = testable_core_plugin_manager::instance();
49         $this->assertInstanceOf('core_plugin_manager', $pluginman3);
50         $this->assertInstanceOf('testable_core_plugin_manager', $pluginman3);
51         $pluginman4 = testable_core_plugin_manager::instance();
52         $this->assertSame($pluginman3, $pluginman4);
53         $this->assertNotSame($pluginman1, $pluginman3);
54     }
56     public function test_reset_caches() {
57         // Make sure there are no warnings or errors.
58         core_plugin_manager::reset_caches();
59         testable_core_plugin_manager::reset_caches();
60     }
62     /**
63      * Make sure that the tearDown() really kills the singleton after this test.
64      */
65     public function test_teardown_works_precheck() {
66         $pluginman = testable_core_plugin_manager::instance();
67         $pluginfo = testable_plugininfo_base::fake_plugin_instance('fake', '/dev/null', 'one', '/dev/null/fake',
68             'testable_plugininfo_base', $pluginman);
69         $pluginman->inject_testable_plugininfo('fake', 'one', $pluginfo);
71         $this->assertInstanceOf('\core\plugininfo\base', $pluginman->get_plugin_info('fake_one'));
72         $this->assertNull($pluginman->get_plugin_info('fake_two'));
73     }
75     public function test_teardown_works_postcheck() {
76         $pluginman = testable_core_plugin_manager::instance();
77         $this->assertNull($pluginman->get_plugin_info('fake_one'));
78         $this->assertNull($pluginman->get_plugin_info('fake_two'));
79     }
81     public function test_get_plugin_types() {
82         // Make sure there are no warnings or errors.
83         $types = core_plugin_manager::instance()->get_plugin_types();
84         $this->assertInternalType('array', $types);
85         foreach ($types as $type => $fulldir) {
86             $this->assertFileExists($fulldir);
87         }
88     }
90     public function test_get_installed_plugins() {
91         $types = core_plugin_manager::instance()->get_plugin_types();
92         foreach ($types as $type => $fulldir) {
93             $installed = core_plugin_manager::instance()->get_installed_plugins($type);
94             foreach ($installed as $plugin => $version) {
95                 $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $plugin);
96                 $this->assertTrue(is_numeric($version), 'All plugins should have a version, plugin '.$type.'_'.$plugin.' does not have version info.');
97             }
98         }
99     }
101     public function test_get_enabled_plugins() {
102         $types = core_plugin_manager::instance()->get_plugin_types();
103         foreach ($types as $type => $fulldir) {
104             $enabled = core_plugin_manager::instance()->get_enabled_plugins($type);
105             if (is_array($enabled)) {
106                 foreach ($enabled as $key => $val) {
107                     $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $key);
108                     $this->assertSame($key, $val);
109                 }
110             } else {
111                 $this->assertNull($enabled);
112             }
113         }
114     }
116     public function test_get_present_plugins() {
117         $types = core_plugin_manager::instance()->get_plugin_types();
118         foreach ($types as $type => $fulldir) {
119             $present = core_plugin_manager::instance()->get_present_plugins($type);
120             if (is_array($present)) {
121                 foreach ($present as $plugin => $version) {
122                     $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $plugin, 'All plugins are supposed to have version.php file.');
123                     $this->assertInternalType('object', $version);
124                     $this->assertTrue(is_numeric($version->version), 'All plugins should have a version, plugin '.$type.'_'.$plugin.' does not have version info.');
125                 }
126             } else {
127                 // No plugins of this type exist.
128                 $this->assertNull($present);
129             }
130         }
131     }
133     public function test_get_plugins() {
134         $plugininfos1 = core_plugin_manager::instance()->get_plugins();
135         foreach ($plugininfos1 as $type => $infos) {
136             foreach ($infos as $name => $info) {
137                 $this->assertInstanceOf('\core\plugininfo\base', $info);
138             }
139         }
141         // The testable variant of the manager holds its own tree of the
142         // plugininfo objects.
143         $plugininfos2 = testable_core_plugin_manager::instance()->get_plugins();
144         $this->assertNotSame($plugininfos1['mod']['forum'], $plugininfos2['mod']['forum']);
146         // Singletons of each manager class share the same tree.
147         $plugininfos3 = core_plugin_manager::instance()->get_plugins();
148         $this->assertSame($plugininfos1['mod']['forum'], $plugininfos3['mod']['forum']);
149         $plugininfos4 = testable_core_plugin_manager::instance()->get_plugins();
150         $this->assertSame($plugininfos2['mod']['forum'], $plugininfos4['mod']['forum']);
151     }
153     public function test_plugininfo_back_reference_to_the_plugin_manager() {
154         $plugman1 = core_plugin_manager::instance();
155         $plugman2 = testable_core_plugin_manager::instance();
157         foreach ($plugman1->get_plugins() as $type => $infos) {
158             foreach ($infos as $info) {
159                 $this->assertSame($info->pluginman, $plugman1);
160             }
161         }
163         foreach ($plugman2->get_plugins() as $type => $infos) {
164             foreach ($infos as $info) {
165                 $this->assertSame($info->pluginman, $plugman2);
166             }
167         }
168     }
170     public function test_get_plugins_of_type() {
171         $plugininfos = core_plugin_manager::instance()->get_plugins();
172         foreach ($plugininfos as $type => $infos) {
173             $this->assertSame($infos, core_plugin_manager::instance()->get_plugins_of_type($type));
174         }
175     }
177     public function test_get_subplugins_of_plugin() {
178         global $CFG;
180         // Any standard plugin with subplugins is suitable.
181         $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
183         $subplugins = core_plugin_manager::instance()->get_subplugins_of_plugin('editor_tinymce');
184         foreach ($subplugins as $component => $info) {
185             $this->assertInstanceOf('\core\plugininfo\base', $info);
186         }
187     }
189     public function test_get_subplugins() {
190         // Tested already indirectly from test_get_subplugins_of_plugin().
191         $subplugins = core_plugin_manager::instance()->get_subplugins();
192         $this->assertInternalType('array', $subplugins);
193     }
195     public function test_get_parent_of_subplugin() {
196         global $CFG;
198         // Any standard plugin with subplugins is suitable.
199         $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
201         $parent = core_plugin_manager::instance()->get_parent_of_subplugin('tinymce');
202         $this->assertSame('editor_tinymce', $parent);
203     }
205     public function test_plugin_name() {
206         global $CFG;
208         // Any standard plugin is suitable.
209         $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
211         $name = core_plugin_manager::instance()->plugin_name('editor_tinymce');
212         $this->assertSame(get_string('pluginname', 'editor_tinymce'), $name);
213     }
215     public function test_plugintype_name() {
216         $name = core_plugin_manager::instance()->plugintype_name('editor');
217         $this->assertSame(get_string('type_editor', 'core_plugin'), $name);
218     }
220     public function test_plugintype_name_plural() {
221         $name = core_plugin_manager::instance()->plugintype_name_plural('editor');
222         $this->assertSame(get_string('type_editor_plural', 'core_plugin'), $name);
223     }
225     public function test_get_plugin_info() {
226         global $CFG;
228         // Any standard plugin is suitable.
229         $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
231         $info = core_plugin_manager::instance()->get_plugin_info('editor_tinymce');
232         $this->assertInstanceOf('\core\plugininfo\editor', $info);
233     }
235     public function test_can_uninstall_plugin() {
236         global $CFG;
238         // Any standard plugin that is required by some other standard plugin is ok.
239         $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/assignmentupgrade", 'assign upgrade tool is not present');
240         $this->assertFileExists("$CFG->dirroot/mod/assign", 'assign module is not present');
242         $this->assertFalse(core_plugin_manager::instance()->can_uninstall_plugin('mod_assign'));
243         $this->assertTrue(core_plugin_manager::instance()->can_uninstall_plugin('tool_assignmentupgrade'));
244     }
246     public function test_plugin_states() {
247         global $CFG;
248         $this->resetAfterTest();
250         // Any standard plugin that is ok.
251         $this->assertFileExists("$CFG->dirroot/mod/assign", 'assign module is not present');
252         $this->assertFileExists("$CFG->dirroot/mod/forum", 'forum module is not present');
253         $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/phpunit", 'phpunit tool is not present');
254         $this->assertFileNotExists("$CFG->dirroot/mod/xxxxxxx");
255         $this->assertFileNotExists("$CFG->dirroot/enrol/autorize");
257         // Ready for upgrade.
258         $assignversion = get_config('mod_assign', 'version');
259         set_config('version', $assignversion - 1, 'mod_assign');
260         // Downgrade problem.
261         $forumversion = get_config('mod_forum', 'version');
262         set_config('version', $forumversion + 1, 'mod_forum');
263         // Not installed yet.
264         unset_config('version', 'tool_phpunit');
265         // Missing already installed.
266         set_config('version', 2013091300, 'mod_xxxxxxx');
267         // Deleted present.
268         set_config('version', 2013091300, 'enrol_authorize');
270         core_plugin_manager::reset_caches();
272         $plugininfos = core_plugin_manager::instance()->get_plugins();
273         foreach ($plugininfos as $type => $infos) {
274             foreach ($infos as $name => $info) {
275                 /** @var core\plugininfo\base $info */
276                 if ($info->component === 'mod_assign') {
277                     $this->assertSame(core_plugin_manager::PLUGIN_STATUS_UPGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
278                 } else if ($info->component === 'mod_forum') {
279                     $this->assertSame(core_plugin_manager::PLUGIN_STATUS_DOWNGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
280                 } else if ($info->component === 'tool_phpunit') {
281                     $this->assertSame(core_plugin_manager::PLUGIN_STATUS_NEW, $info->get_status(), 'Invalid '.$info->component.' state');
282                 } else if ($info->component === 'mod_xxxxxxx') {
283                     $this->assertSame(core_plugin_manager::PLUGIN_STATUS_MISSING, $info->get_status(), 'Invalid '.$info->component.' state');
284                 } else if ($info->component === 'enrol_authorize') {
285                     $this->assertSame(core_plugin_manager::PLUGIN_STATUS_DELETE, $info->get_status(), 'Invalid '.$info->component.' state');
286                 } else {
287                     $this->assertSame(core_plugin_manager::PLUGIN_STATUS_UPTODATE, $info->get_status(), 'Invalid '.$info->component.' state');
288                 }
289             }
290         }
291     }
293     public function test_plugin_available_updates() {
294         $pluginman = testable_core_plugin_manager::instance();
296         $foobar = testable_plugininfo_base::fake_plugin_instance('foo', '/dev/null', 'bar', '/dev/null/fake',
297             'testable_plugininfo_base', $pluginman);
298         $foobar->versiondb = 2015092900;
299         $foobar->versiondisk = 2015092900;
300         $pluginman->inject_testable_plugininfo('foo', 'bar', $foobar);
302         $washere = false;
303         foreach ($pluginman->get_plugins() as $type => $infos) {
304             foreach ($infos as $name => $plugin) {
305                 $updates = $plugin->available_updates();
306                 if ($plugin->component != 'foo_bar') {
307                     $this->assertNull($updates);
308                 } else {
309                     $this->assertTrue(is_array($updates));
310                     $this->assertEquals(3, count($updates));
311                     foreach ($updates as $update) {
312                         $washere = true;
313                         $this->assertInstanceOf('\core\update\info', $update);
314                         $this->assertEquals($update->component, $plugin->component);
315                         $this->assertTrue($update->version > $plugin->versiondb);
316                     }
317                 }
318             }
319         }
320         $this->assertTrue($washere);
321     }
323     public function test_some_plugins_updatable_none() {
324         $pluginman = testable_core_plugin_manager::instance();
325         $this->assertFalse($pluginman->some_plugins_updatable());
326     }
328     public function test_some_plugins_updatable_some() {
329         $pluginman = testable_core_plugin_manager::instance();
331         $foobar = testable_plugininfo_base::fake_plugin_instance('foo', '/dev/null', 'bar', '/dev/null/fake',
332             'testable_plugininfo_base', $pluginman);
333         $foobar->versiondb = 2015092900;
334         $foobar->versiondisk = 2015092900;
335         $pluginman->inject_testable_plugininfo('foo', 'bar', $foobar);
337         $this->assertTrue($pluginman->some_plugins_updatable());
338     }
340     public function test_available_updates() {
341         $pluginman = testable_core_plugin_manager::instance();
343         $foobar = testable_plugininfo_base::fake_plugin_instance('foo', '/dev/null', 'bar', '/dev/null/fake',
344             'testable_plugininfo_base', $pluginman);
345         $foobar->versiondb = 2015092900;
346         $foobar->versiondisk = 2015092900;
347         $pluginman->inject_testable_plugininfo('foo', 'bar', $foobar);
349         $updates = $pluginman->available_updates();
351         $this->assertTrue(is_array($updates));
352         $this->assertEquals(1, count($updates));
353         $update = $updates['foo_bar'];
354         $this->assertInstanceOf('\core\update\remote_info', $update);
355         $this->assertEquals('foo_bar', $update->component);
356         $this->assertEquals(2015100400, $update->version->version);
357     }
359     public function test_get_remote_plugin_info() {
360         $pluginman = testable_core_plugin_manager::instance();
362         $this->assertFalse($pluginman->get_remote_plugin_info('not_exists', ANY_VERSION, false));
364         $info = $pluginman->get_remote_plugin_info('foo_bar', 2015093000, true);
365         $this->assertEquals(2015093000, $info->version->version);
367         $info = $pluginman->get_remote_plugin_info('foo_bar', 2015093000, false);
368         $this->assertEquals(2015100400, $info->version->version);
369     }
371     /**
372      * @expectedException moodle_exception
373      */
374     public function test_get_remote_plugin_info_exception() {
375         $pluginman = testable_core_plugin_manager::instance();
376         // The combination of ANY_VERSION + $exactmatch is illegal.
377         $pluginman->get_remote_plugin_info('any_thing', ANY_VERSION, true);
378     }
380     public function test_is_remote_plugin_available() {
381         $pluginman = testable_core_plugin_manager::instance();
383         $this->assertFalse($pluginman->is_remote_plugin_available('not_exists', ANY_VERSION, false));
384         $this->assertTrue($pluginman->is_remote_plugin_available('foo_bar', 2013131313, false));
385         $this->assertFalse($pluginman->is_remote_plugin_available('foo_bar', 2013131313, true));
386     }
388     public function test_resolve_requirements() {
389         $pluginman = testable_core_plugin_manager::instance();
391         // Prepare a fake pluginfo instance.
392         $pluginfo = testable_plugininfo_base::fake_plugin_instance('fake', '/dev/null', 'one', '/dev/null/fake',
393             'testable_plugininfo_base', $pluginman);
394         $pluginfo->versiondisk = 2015060600;
396         // Test no $plugin->requires is specified in version.php.
397         $pluginfo->versionrequires = null;
398         $this->assertTrue($pluginfo->is_core_dependency_satisfied(2015100100));
399         $reqs = $pluginman->resolve_requirements($pluginfo, 2015100100, 29);
400         $this->assertEquals(2015100100, $reqs['core']->hasver);
401         $this->assertEquals(ANY_VERSION, $reqs['core']->reqver);
402         $this->assertEquals($pluginman::REQUIREMENT_STATUS_OK, $reqs['core']->status);
404         // Test plugin requires higher core version.
405         $pluginfo->versionrequires = 2015110900;
406         $this->assertFalse($pluginfo->is_core_dependency_satisfied(2015100100));
407         $reqs = $pluginman->resolve_requirements($pluginfo, 2015100100, 29);
408         $this->assertEquals(2015100100, $reqs['core']->hasver);
409         $this->assertEquals(2015110900, $reqs['core']->reqver);
410         $this->assertEquals($pluginman::REQUIREMENT_STATUS_OUTDATED, $reqs['core']->status);
412         // Test plugin requires current core version.
413         $pluginfo->versionrequires = 2015110900;
414         $this->assertTrue($pluginfo->is_core_dependency_satisfied(2015110900));
415         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
416         $this->assertEquals(2015110900, $reqs['core']->hasver);
417         $this->assertEquals(2015110900, $reqs['core']->reqver);
418         $this->assertEquals($pluginman::REQUIREMENT_STATUS_OK, $reqs['core']->status);
420         // Test plugin requires lower core version.
421         $pluginfo->versionrequires = 2014122400;
422         $this->assertTrue($pluginfo->is_core_dependency_satisfied(2015100100));
423         $reqs = $pluginman->resolve_requirements($pluginfo, 2015100100, 29);
424         $this->assertEquals(2015100100, $reqs['core']->hasver);
425         $this->assertEquals(2014122400, $reqs['core']->reqver);
426         $this->assertEquals($pluginman::REQUIREMENT_STATUS_OK, $reqs['core']->status);
428         // Test plugin dependencies and their availability.
429         // See {@link \core\update\testable_api} class.
431         $pluginfo->dependencies = array('foo_bar' => ANY_VERSION, 'not_exists' => ANY_VERSION);
432         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
433         $this->assertNull($reqs['foo_bar']->hasver);
434         $this->assertEquals(ANY_VERSION, $reqs['foo_bar']->reqver);
435         $this->assertEquals($pluginman::REQUIREMENT_STATUS_MISSING, $reqs['foo_bar']->status);
436         $this->assertEquals($pluginman::REQUIREMENT_AVAILABLE, $reqs['foo_bar']->availability);
437         $this->assertEquals($pluginman::REQUIREMENT_UNAVAILABLE, $reqs['not_exists']->availability);
439         $pluginfo->dependencies = array('foo_bar' => 2013122400);
440         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
441         $this->assertEquals($pluginman::REQUIREMENT_AVAILABLE, $reqs['foo_bar']->availability);
443         $pluginfo->dependencies = array('foo_bar' => 2015093000);
444         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
445         $this->assertEquals($pluginman::REQUIREMENT_AVAILABLE, $reqs['foo_bar']->availability);
447         $pluginfo->dependencies = array('foo_bar' => 2015100500);
448         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
449         $this->assertEquals($pluginman::REQUIREMENT_AVAILABLE, $reqs['foo_bar']->availability);
451         $pluginfo->dependencies = array('foo_bar' => 2025010100);
452         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
453         $this->assertEquals($pluginman::REQUIREMENT_UNAVAILABLE, $reqs['foo_bar']->availability);
455         // Plugin missing from disk - no version.php available.
456         $pluginfo = testable_plugininfo_base::fake_plugin_instance('fake', '/dev/null', 'missing', '/dev/null/fake',
457             'testable_plugininfo_base', $pluginman);
458         $pluginfo->versiondisk = null;
459         $this->assertEmpty($pluginman->resolve_requirements($pluginfo, 2015110900, 30));
460     }
462     public function test_missing_dependencies() {
463         $pluginman = testable_core_plugin_manager::instance();
465         $one = testable_plugininfo_base::fake_plugin_instance('fake', '/dev/null', 'one', '/dev/null/fake',
466             'testable_plugininfo_base', $pluginman);
467         $one->versiondisk = 2015070800;
469         $two = testable_plugininfo_base::fake_plugin_instance('fake', '/dev/null', 'two', '/dev/null/fake',
470             'testable_plugininfo_base', $pluginman);
471         $two->versiondisk = 2015070900;
473         $pluginman->inject_testable_plugininfo('fake', 'one', $one);
474         $pluginman->inject_testable_plugininfo('fake', 'two', $two);
476         $this->assertEmpty($pluginman->missing_dependencies());
478         $one->dependencies = array('foo_bar' => ANY_VERSION);
479         $misdeps = $pluginman->missing_dependencies();
480         $this->assertInstanceOf('\core\update\remote_info', $misdeps['foo_bar']);
481         $this->assertEquals(2015100400, $misdeps['foo_bar']->version->version);
483         $two->dependencies = array('foo_bar' => 2015100500);
484         $misdeps = $pluginman->missing_dependencies();
485         $this->assertInstanceOf('\core\update\remote_info', $misdeps['foo_bar']);
486         $this->assertEquals(2015100500, $misdeps['foo_bar']->version->version);
487     }