MDL-49329 admin: Introduce new \core\update\api client class
[moodle.git] / lib / tests / plugin_manager_test.php
CommitLineData
bde002b8
PS
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/>.
16
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 */
25
26defined('MOODLE_INTERNAL') || die();
27
361feecd
DM
28global $CFG;
29require_once($CFG->dirroot.'/lib/tests/fixtures/testable_plugin_manager.php');
30
bde002b8
PS
31/**
32 * Tests of the basic API of the plugin manager.
33 */
34class core_plugin_manager_testcase extends advanced_testcase {
35
36 public function test_instance() {
361feecd
DM
37 $pluginman1 = core_plugin_manager::instance();
38 $this->assertInstanceOf('core_plugin_manager', $pluginman1);
e87214bd 39 $pluginman2 = core_plugin_manager::instance();
361feecd
DM
40 $this->assertSame($pluginman1, $pluginman2);
41 $pluginman3 = testable_core_plugin_manager::instance();
42 $this->assertInstanceOf('core_plugin_manager', $pluginman3);
43 $this->assertInstanceOf('testable_core_plugin_manager', $pluginman3);
44 $pluginman4 = testable_core_plugin_manager::instance();
45 $this->assertSame($pluginman3, $pluginman4);
46 $this->assertNotSame($pluginman1, $pluginman3);
bde002b8
PS
47 }
48
49 public function test_reset_caches() {
50 // Make sure there are no warnings or errors.
e87214bd 51 core_plugin_manager::reset_caches();
361feecd 52 testable_core_plugin_manager::reset_caches();
bde002b8
PS
53 }
54
55 public function test_get_plugin_types() {
56 // Make sure there are no warnings or errors.
e87214bd 57 $types = core_plugin_manager::instance()->get_plugin_types();
bde002b8
PS
58 $this->assertInternalType('array', $types);
59 foreach ($types as $type => $fulldir) {
60 $this->assertFileExists($fulldir);
61 }
62 }
63
64 public function test_get_installed_plugins() {
e87214bd 65 $types = core_plugin_manager::instance()->get_plugin_types();
bde002b8 66 foreach ($types as $type => $fulldir) {
e87214bd 67 $installed = core_plugin_manager::instance()->get_installed_plugins($type);
bde002b8
PS
68 foreach ($installed as $plugin => $version) {
69 $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $plugin);
70 $this->assertTrue(is_numeric($version), 'All plugins should have a version, plugin '.$type.'_'.$plugin.' does not have version info.');
71 }
72 }
73 }
74
75 public function test_get_enabled_plugins() {
e87214bd 76 $types = core_plugin_manager::instance()->get_plugin_types();
bde002b8 77 foreach ($types as $type => $fulldir) {
e87214bd 78 $enabled = core_plugin_manager::instance()->get_enabled_plugins($type);
bde002b8
PS
79 if (is_array($enabled)) {
80 foreach ($enabled as $key => $val) {
81 $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $key);
82 $this->assertSame($key, $val);
83 }
84 } else {
85 $this->assertNull($enabled);
86 }
87 }
88 }
89
90 public function test_get_present_plugins() {
e87214bd 91 $types = core_plugin_manager::instance()->get_plugin_types();
bde002b8 92 foreach ($types as $type => $fulldir) {
e87214bd 93 $present = core_plugin_manager::instance()->get_present_plugins($type);
bde002b8
PS
94 if (is_array($present)) {
95 foreach ($present as $plugin => $version) {
96 $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $plugin, 'All plugins are supposed to have version.php file.');
97 $this->assertInternalType('object', $version);
98 $this->assertTrue(is_numeric($version->version), 'All plugins should have a version, plugin '.$type.'_'.$plugin.' does not have version info.');
99 }
100 } else {
101 // No plugins of this type exist.
102 $this->assertNull($present);
103 }
104 }
105 }
106
107 public function test_get_plugins() {
361feecd
DM
108 $plugininfos1 = core_plugin_manager::instance()->get_plugins();
109 foreach ($plugininfos1 as $type => $infos) {
bde002b8 110 foreach ($infos as $name => $info) {
e87214bd 111 $this->assertInstanceOf('\core\plugininfo\base', $info);
bde002b8
PS
112 }
113 }
361feecd
DM
114
115 // The testable variant of the manager holds its own tree of the
116 // plugininfo objects.
117 $plugininfos2 = testable_core_plugin_manager::instance()->get_plugins();
118 $this->assertNotSame($plugininfos1['mod']['forum'], $plugininfos2['mod']['forum']);
119
120 // Singletons of each manager class share the same tree.
121 $plugininfos3 = core_plugin_manager::instance()->get_plugins();
122 $this->assertSame($plugininfos1['mod']['forum'], $plugininfos3['mod']['forum']);
123 $plugininfos4 = testable_core_plugin_manager::instance()->get_plugins();
124 $this->assertSame($plugininfos2['mod']['forum'], $plugininfos4['mod']['forum']);
bde002b8
PS
125 }
126
2d488c8f
DM
127 public function test_plugininfo_back_reference_to_the_plugin_manager() {
128 $plugman1 = core_plugin_manager::instance();
129 $plugman2 = testable_core_plugin_manager::instance();
130
131 foreach ($plugman1->get_plugins() as $type => $infos) {
132 foreach ($infos as $info) {
133 $this->assertSame($info->pluginman, $plugman1);
134 }
135 }
136
137 foreach ($plugman2->get_plugins() as $type => $infos) {
138 foreach ($infos as $info) {
139 $this->assertSame($info->pluginman, $plugman2);
140 }
141 }
142 }
143
bde002b8 144 public function test_get_plugins_of_type() {
e87214bd 145 $plugininfos = core_plugin_manager::instance()->get_plugins();
bde002b8 146 foreach ($plugininfos as $type => $infos) {
e87214bd 147 $this->assertSame($infos, core_plugin_manager::instance()->get_plugins_of_type($type));
bde002b8
PS
148 }
149 }
150
151 public function test_get_subplugins_of_plugin() {
152 global $CFG;
153
154 // Any standard plugin with subplugins is suitable.
155 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
156
e87214bd 157 $subplugins = core_plugin_manager::instance()->get_subplugins_of_plugin('editor_tinymce');
bde002b8 158 foreach ($subplugins as $component => $info) {
e87214bd 159 $this->assertInstanceOf('\core\plugininfo\base', $info);
bde002b8
PS
160 }
161 }
162
163 public function test_get_subplugins() {
164 // Tested already indirectly from test_get_subplugins_of_plugin().
e87214bd 165 $subplugins = core_plugin_manager::instance()->get_subplugins();
bde002b8
PS
166 $this->assertInternalType('array', $subplugins);
167 }
168
169 public function test_get_parent_of_subplugin() {
170 global $CFG;
171
172 // Any standard plugin with subplugins is suitable.
173 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
174
e87214bd 175 $parent = core_plugin_manager::instance()->get_parent_of_subplugin('tinymce');
bde002b8
PS
176 $this->assertSame('editor_tinymce', $parent);
177 }
178
179 public function test_plugin_name() {
180 global $CFG;
181
182 // Any standard plugin is suitable.
183 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
184
e87214bd 185 $name = core_plugin_manager::instance()->plugin_name('editor_tinymce');
bde002b8
PS
186 $this->assertSame(get_string('pluginname', 'editor_tinymce'), $name);
187 }
188
189 public function test_plugintype_name() {
e87214bd 190 $name = core_plugin_manager::instance()->plugintype_name('editor');
bde002b8
PS
191 $this->assertSame(get_string('type_editor', 'core_plugin'), $name);
192 }
193
194 public function test_plugintype_name_plural() {
e87214bd 195 $name = core_plugin_manager::instance()->plugintype_name_plural('editor');
bde002b8
PS
196 $this->assertSame(get_string('type_editor_plural', 'core_plugin'), $name);
197 }
198
199 public function test_get_plugin_info() {
200 global $CFG;
201
202 // Any standard plugin is suitable.
203 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
204
e87214bd
PS
205 $info = core_plugin_manager::instance()->get_plugin_info('editor_tinymce');
206 $this->assertInstanceOf('\core\plugininfo\editor', $info);
bde002b8
PS
207 }
208
209 public function test_can_uninstall_plugin() {
210 global $CFG;
211
212 // Any standard plugin that is required by some other standard plugin is ok.
213 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/assignmentupgrade", 'assign upgrade tool is not present');
214 $this->assertFileExists("$CFG->dirroot/mod/assign", 'assign module is not present');
215
e87214bd
PS
216 $this->assertFalse(core_plugin_manager::instance()->can_uninstall_plugin('mod_assign'));
217 $this->assertTrue(core_plugin_manager::instance()->can_uninstall_plugin('tool_assignmentupgrade'));
bde002b8
PS
218 }
219
220 public function test_plugin_states() {
221 global $CFG;
222 $this->resetAfterTest();
223
224 // Any standard plugin that is ok.
225 $this->assertFileExists("$CFG->dirroot/mod/assign", 'assign module is not present');
226 $this->assertFileExists("$CFG->dirroot/mod/forum", 'forum module is not present');
227 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/phpunit", 'phpunit tool is not present');
228 $this->assertFileNotExists("$CFG->dirroot/mod/xxxxxxx");
229 $this->assertFileNotExists("$CFG->dirroot/enrol/autorize");
230
231 // Ready for upgrade.
232 $assignversion = get_config('mod_assign', 'version');
233 set_config('version', $assignversion - 1, 'mod_assign');
234 // Downgrade problem.
235 $forumversion = get_config('mod_forum', 'version');
236 set_config('version', $forumversion + 1, 'mod_forum');
237 // Not installed yet.
238 unset_config('version', 'tool_phpunit');
239 // Missing already installed.
240 set_config('version', 2013091300, 'mod_xxxxxxx');
241 // Deleted present.
242 set_config('version', 2013091300, 'enrol_authorize');
243
e87214bd 244 core_plugin_manager::reset_caches();
bde002b8 245
e87214bd 246 $plugininfos = core_plugin_manager::instance()->get_plugins();
bde002b8
PS
247 foreach ($plugininfos as $type => $infos) {
248 foreach ($infos as $name => $info) {
6b61c2c4 249 /** @var core\plugininfo\base $info */
bde002b8 250 if ($info->component === 'mod_assign') {
e87214bd 251 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_UPGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 252 } else if ($info->component === 'mod_forum') {
e87214bd 253 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_DOWNGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 254 } else if ($info->component === 'tool_phpunit') {
e87214bd 255 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_NEW, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 256 } else if ($info->component === 'mod_xxxxxxx') {
e87214bd 257 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_MISSING, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 258 } else if ($info->component === 'enrol_authorize') {
e87214bd 259 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_DELETE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 260 } else {
e87214bd 261 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_UPTODATE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8
PS
262 }
263 }
264 }
265 }
c44bbe35
DM
266
267 public function test_plugin_available_updates() {
268
269 $pluginman = testable_core_plugin_manager::instance();
270
271 foreach ($pluginman->get_plugins() as $type => $infos) {
272 foreach ($infos as $name => $info) {
273 $updates = $info->available_updates();
274 if ($info->component != 'mod_forum') {
275 $this->assertNull($updates);
276 } else {
277 $this->assertEquals(1, count($updates));
278 $update = array_shift($updates);
279 $this->assertInstanceOf('\core\update\info', $update);
280 $this->assertEquals('mod_forum', $update->component);
281 $this->assertEquals('2999122400', $update->version);
282 }
283 }
284 }
285 }
286
287 public function test_some_plugins_updatable() {
288 $pluginman = testable_core_plugin_manager::instance();
289 $this->assertTrue($pluginman->some_plugins_updatable());
290 }
bde002b8 291}