c551cb7b53b3007f9036ecd2c99d1224fee82f0e
[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() {
295         $pluginman = testable_core_plugin_manager::instance();
297         foreach ($pluginman->get_plugins() as $type => $infos) {
298             foreach ($infos as $name => $info) {
299                 $updates = $info->available_updates();
300                 if ($info->component != 'mod_forum') {
301                     $this->assertNull($updates);
302                 } else {
303                     $this->assertEquals(1, count($updates));
304                     $update = array_shift($updates);
305                     $this->assertInstanceOf('\core\update\info', $update);
306                     $this->assertEquals('mod_forum', $update->component);
307                     $this->assertEquals('2999122400', $update->version);
308                 }
309             }
310         }
311     }
313     public function test_some_plugins_updatable() {
314         $pluginman = testable_core_plugin_manager::instance();
315         $this->assertTrue($pluginman->some_plugins_updatable());
316     }
318     public function test_get_remote_plugin_info() {
319         $pluginman = testable_core_plugin_manager::instance();
321         $this->assertFalse($pluginman->get_remote_plugin_info('not_exists', ANY_VERSION, false));
323         $info = $pluginman->get_remote_plugin_info('foo_bar', 2015093000, true);
324         $this->assertEquals(2015093000, $info->version->version);
326         $info = $pluginman->get_remote_plugin_info('foo_bar', 2015093000, false);
327         $this->assertEquals(2015100400, $info->version->version);
328     }
330     /**
331      * @expectedException moodle_exception
332      */
333     public function test_get_remote_plugin_info_exception() {
334         $pluginman = testable_core_plugin_manager::instance();
335         // The combination of ANY_VERSION + $exactmatch is illegal.
336         $pluginman->get_remote_plugin_info('any_thing', ANY_VERSION, true);
337     }
339     public function test_is_remote_plugin_available() {
340         $pluginman = testable_core_plugin_manager::instance();
342         $this->assertFalse($pluginman->is_remote_plugin_available('not_exists', ANY_VERSION, false));
343         $this->assertTrue($pluginman->is_remote_plugin_available('foo_bar', 2013131313, false));
344         $this->assertFalse($pluginman->is_remote_plugin_available('foo_bar', 2013131313, true));
345     }
347     public function test_resolve_requirements() {
348         $pluginman = testable_core_plugin_manager::instance();
350         // Prepare a fake pluginfo instance.
351         $pluginfo = testable_plugininfo_base::fake_plugin_instance('fake', '/dev/null', 'one', '/dev/null/fake',
352             'testable_plugininfo_base', $pluginman);
353         $pluginfo->versiondisk = 2015060600;
355         // Test no $plugin->requires is specified in version.php.
356         $pluginfo->versionrequires = null;
357         $this->assertTrue($pluginfo->is_core_dependency_satisfied(2015100100));
358         $reqs = $pluginman->resolve_requirements($pluginfo, 2015100100, 29);
359         $this->assertEquals(2015100100, $reqs['core']->hasver);
360         $this->assertEquals(ANY_VERSION, $reqs['core']->reqver);
361         $this->assertEquals($pluginman::REQUIREMENT_STATUS_OK, $reqs['core']->status);
363         // Test plugin requires higher core version.
364         $pluginfo->versionrequires = 2015110900;
365         $this->assertFalse($pluginfo->is_core_dependency_satisfied(2015100100));
366         $reqs = $pluginman->resolve_requirements($pluginfo, 2015100100, 29);
367         $this->assertEquals(2015100100, $reqs['core']->hasver);
368         $this->assertEquals(2015110900, $reqs['core']->reqver);
369         $this->assertEquals($pluginman::REQUIREMENT_STATUS_OUTDATED, $reqs['core']->status);
371         // Test plugin requires current core version.
372         $pluginfo->versionrequires = 2015110900;
373         $this->assertTrue($pluginfo->is_core_dependency_satisfied(2015110900));
374         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
375         $this->assertEquals(2015110900, $reqs['core']->hasver);
376         $this->assertEquals(2015110900, $reqs['core']->reqver);
377         $this->assertEquals($pluginman::REQUIREMENT_STATUS_OK, $reqs['core']->status);
379         // Test plugin requires lower core version.
380         $pluginfo->versionrequires = 2014122400;
381         $this->assertTrue($pluginfo->is_core_dependency_satisfied(2015100100));
382         $reqs = $pluginman->resolve_requirements($pluginfo, 2015100100, 29);
383         $this->assertEquals(2015100100, $reqs['core']->hasver);
384         $this->assertEquals(2014122400, $reqs['core']->reqver);
385         $this->assertEquals($pluginman::REQUIREMENT_STATUS_OK, $reqs['core']->status);
387         // Test plugin dependencies and their availability.
388         // See {@link \core\update\testable_api} class.
390         $pluginfo->dependencies = array('foo_bar' => ANY_VERSION, 'not_exists' => ANY_VERSION);
391         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
392         $this->assertNull($reqs['foo_bar']->hasver);
393         $this->assertEquals(ANY_VERSION, $reqs['foo_bar']->reqver);
394         $this->assertEquals($pluginman::REQUIREMENT_STATUS_MISSING, $reqs['foo_bar']->status);
395         $this->assertEquals($pluginman::REQUIREMENT_AVAILABLE, $reqs['foo_bar']->availability);
396         $this->assertEquals($pluginman::REQUIREMENT_UNAVAILABLE, $reqs['not_exists']->availability);
398         $pluginfo->dependencies = array('foo_bar' => 2013122400);
399         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
400         $this->assertEquals($pluginman::REQUIREMENT_AVAILABLE, $reqs['foo_bar']->availability);
402         $pluginfo->dependencies = array('foo_bar' => 2015093000);
403         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
404         $this->assertEquals($pluginman::REQUIREMENT_AVAILABLE, $reqs['foo_bar']->availability);
406         $pluginfo->dependencies = array('foo_bar' => 2015100500);
407         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
408         $this->assertEquals($pluginman::REQUIREMENT_AVAILABLE, $reqs['foo_bar']->availability);
410         $pluginfo->dependencies = array('foo_bar' => 2025010100);
411         $reqs = $pluginman->resolve_requirements($pluginfo, 2015110900, 30);
412         $this->assertEquals($pluginman::REQUIREMENT_UNAVAILABLE, $reqs['foo_bar']->availability);
414         // Plugin missing from disk - no version.php available.
415         $pluginfo = testable_plugininfo_base::fake_plugin_instance('fake', '/dev/null', 'missing', '/dev/null/fake',
416             'testable_plugininfo_base', $pluginman);
417         $pluginfo->versiondisk = null;
418         $this->assertEmpty($pluginman->resolve_requirements($pluginfo, 2015110900, 30));
419     }
421     public function test_missing_dependencies() {
422         $pluginman = testable_core_plugin_manager::instance();
424         $one = testable_plugininfo_base::fake_plugin_instance('fake', '/dev/null', 'one', '/dev/null/fake',
425             'testable_plugininfo_base', $pluginman);
426         $one->versiondisk = 2015070800;
428         $two = testable_plugininfo_base::fake_plugin_instance('fake', '/dev/null', 'two', '/dev/null/fake',
429             'testable_plugininfo_base', $pluginman);
430         $two->versiondisk = 2015070900;
432         $pluginman->inject_testable_plugininfo('fake', 'one', $one);
433         $pluginman->inject_testable_plugininfo('fake', 'two', $two);
435         $this->assertEmpty($pluginman->missing_dependencies());
437         $one->dependencies = array('foo_bar' => ANY_VERSION);
438         $misdeps = $pluginman->missing_dependencies();
439         $this->assertEquals(2015100400, $misdeps['foo_bar']->version->version);
441         $two->dependencies = array('foo_bar' => 2015100500);
442         $misdeps = $pluginman->missing_dependencies();
443         $this->assertEquals(2015100500, $misdeps['foo_bar']->version->version);
444     }