Merge branch 'MDL-42592_master' of https://github.com/markn86/moodle
[moodle.git] / lib / tests / plugin_manager_test.php
blobf91f722bd0a1aaad3ed9813490318e3a92aa9e77
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.
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.
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
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Tests of the basic API of the plugin manager.
31 class core_plugin_manager_testcase extends advanced_testcase {
33 public function test_instance() {
34 $pluginman = core_plugin_manager::instance();
35 $this->assertInstanceOf('core_plugin_manager', $pluginman);
36 $pluginman2 = core_plugin_manager::instance();
37 $this->assertSame($pluginman, $pluginman2);
40 public function test_reset_caches() {
41 // Make sure there are no warnings or errors.
42 core_plugin_manager::reset_caches();
45 public function test_get_plugin_types() {
46 // Make sure there are no warnings or errors.
47 $types = core_plugin_manager::instance()->get_plugin_types();
48 $this->assertInternalType('array', $types);
49 foreach ($types as $type => $fulldir) {
50 $this->assertFileExists($fulldir);
54 public function test_get_installed_plugins() {
55 $types = core_plugin_manager::instance()->get_plugin_types();
56 foreach ($types as $type => $fulldir) {
57 $installed = core_plugin_manager::instance()->get_installed_plugins($type);
58 foreach ($installed as $plugin => $version) {
59 $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $plugin);
60 $this->assertTrue(is_numeric($version), 'All plugins should have a version, plugin '.$type.'_'.$plugin.' does not have version info.');
65 public function test_get_enabled_plugins() {
66 $types = core_plugin_manager::instance()->get_plugin_types();
67 foreach ($types as $type => $fulldir) {
68 $enabled = core_plugin_manager::instance()->get_enabled_plugins($type);
69 if (is_array($enabled)) {
70 foreach ($enabled as $key => $val) {
71 $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $key);
72 $this->assertSame($key, $val);
74 } else {
75 $this->assertNull($enabled);
80 public function test_get_present_plugins() {
81 $types = core_plugin_manager::instance()->get_plugin_types();
82 foreach ($types as $type => $fulldir) {
83 $present = core_plugin_manager::instance()->get_present_plugins($type);
84 if (is_array($present)) {
85 foreach ($present as $plugin => $version) {
86 $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $plugin, 'All plugins are supposed to have version.php file.');
87 $this->assertInternalType('object', $version);
88 $this->assertTrue(is_numeric($version->version), 'All plugins should have a version, plugin '.$type.'_'.$plugin.' does not have version info.');
90 } else {
91 // No plugins of this type exist.
92 $this->assertNull($present);
97 public function test_get_plugins() {
98 $plugininfos = core_plugin_manager::instance()->get_plugins();
99 foreach ($plugininfos as $type => $infos) {
100 foreach ($infos as $name => $info) {
101 $this->assertInstanceOf('\core\plugininfo\base', $info);
106 public function test_get_plugins_of_type() {
107 $plugininfos = core_plugin_manager::instance()->get_plugins();
108 foreach ($plugininfos as $type => $infos) {
109 $this->assertSame($infos, core_plugin_manager::instance()->get_plugins_of_type($type));
113 public function test_get_subplugins_of_plugin() {
114 global $CFG;
116 // Any standard plugin with subplugins is suitable.
117 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
119 $subplugins = core_plugin_manager::instance()->get_subplugins_of_plugin('editor_tinymce');
120 foreach ($subplugins as $component => $info) {
121 $this->assertInstanceOf('\core\plugininfo\base', $info);
125 public function test_get_subplugins() {
126 // Tested already indirectly from test_get_subplugins_of_plugin().
127 $subplugins = core_plugin_manager::instance()->get_subplugins();
128 $this->assertInternalType('array', $subplugins);
131 public function test_get_parent_of_subplugin() {
132 global $CFG;
134 // Any standard plugin with subplugins is suitable.
135 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
137 $parent = core_plugin_manager::instance()->get_parent_of_subplugin('tinymce');
138 $this->assertSame('editor_tinymce', $parent);
141 public function test_plugin_name() {
142 global $CFG;
144 // Any standard plugin is suitable.
145 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
147 $name = core_plugin_manager::instance()->plugin_name('editor_tinymce');
148 $this->assertSame(get_string('pluginname', 'editor_tinymce'), $name);
151 public function test_plugintype_name() {
152 $name = core_plugin_manager::instance()->plugintype_name('editor');
153 $this->assertSame(get_string('type_editor', 'core_plugin'), $name);
156 public function test_plugintype_name_plural() {
157 $name = core_plugin_manager::instance()->plugintype_name_plural('editor');
158 $this->assertSame(get_string('type_editor_plural', 'core_plugin'), $name);
161 public function test_get_plugin_info() {
162 global $CFG;
164 // Any standard plugin is suitable.
165 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
167 $info = core_plugin_manager::instance()->get_plugin_info('editor_tinymce');
168 $this->assertInstanceOf('\core\plugininfo\editor', $info);
171 public function test_can_uninstall_plugin() {
172 global $CFG;
174 // Any standard plugin that is required by some other standard plugin is ok.
175 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/assignmentupgrade", 'assign upgrade tool is not present');
176 $this->assertFileExists("$CFG->dirroot/mod/assign", 'assign module is not present');
178 $this->assertFalse(core_plugin_manager::instance()->can_uninstall_plugin('mod_assign'));
179 $this->assertTrue(core_plugin_manager::instance()->can_uninstall_plugin('tool_assignmentupgrade'));
182 public function test_plugin_states() {
183 global $CFG;
184 $this->resetAfterTest();
186 // Any standard plugin that is ok.
187 $this->assertFileExists("$CFG->dirroot/mod/assign", 'assign module is not present');
188 $this->assertFileExists("$CFG->dirroot/mod/forum", 'forum module is not present');
189 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/phpunit", 'phpunit tool is not present');
190 $this->assertFileNotExists("$CFG->dirroot/mod/xxxxxxx");
191 $this->assertFileNotExists("$CFG->dirroot/enrol/autorize");
193 // Ready for upgrade.
194 $assignversion = get_config('mod_assign', 'version');
195 set_config('version', $assignversion - 1, 'mod_assign');
196 // Downgrade problem.
197 $forumversion = get_config('mod_forum', 'version');
198 set_config('version', $forumversion + 1, 'mod_forum');
199 // Not installed yet.
200 unset_config('version', 'tool_phpunit');
201 // Missing already installed.
202 set_config('version', 2013091300, 'mod_xxxxxxx');
203 // Deleted present.
204 set_config('version', 2013091300, 'enrol_authorize');
206 core_plugin_manager::reset_caches();
208 $plugininfos = core_plugin_manager::instance()->get_plugins();
209 foreach ($plugininfos as $type => $infos) {
210 foreach ($infos as $name => $info) {
211 /** @var plugininfo_base $info */
212 if ($info->component === 'mod_assign') {
213 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_UPGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
214 } else if ($info->component === 'mod_forum') {
215 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_DOWNGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
216 } else if ($info->component === 'tool_phpunit') {
217 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_NEW, $info->get_status(), 'Invalid '.$info->component.' state');
218 } else if ($info->component === 'mod_xxxxxxx') {
219 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_MISSING, $info->get_status(), 'Invalid '.$info->component.' state');
220 } else if ($info->component === 'enrol_authorize') {
221 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_DELETE, $info->get_status(), 'Invalid '.$info->component.' state');
222 } else {
223 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_UPTODATE, $info->get_status(), 'Invalid '.$info->component.' state');