MDL-49329 admin: Make core_plugin_manager better suited for unit testing
[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
127 public function test_get_plugins_of_type() {
e87214bd 128 $plugininfos = core_plugin_manager::instance()->get_plugins();
bde002b8 129 foreach ($plugininfos as $type => $infos) {
e87214bd 130 $this->assertSame($infos, core_plugin_manager::instance()->get_plugins_of_type($type));
bde002b8
PS
131 }
132 }
133
134 public function test_get_subplugins_of_plugin() {
135 global $CFG;
136
137 // Any standard plugin with subplugins is suitable.
138 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
139
e87214bd 140 $subplugins = core_plugin_manager::instance()->get_subplugins_of_plugin('editor_tinymce');
bde002b8 141 foreach ($subplugins as $component => $info) {
e87214bd 142 $this->assertInstanceOf('\core\plugininfo\base', $info);
bde002b8
PS
143 }
144 }
145
146 public function test_get_subplugins() {
147 // Tested already indirectly from test_get_subplugins_of_plugin().
e87214bd 148 $subplugins = core_plugin_manager::instance()->get_subplugins();
bde002b8
PS
149 $this->assertInternalType('array', $subplugins);
150 }
151
152 public function test_get_parent_of_subplugin() {
153 global $CFG;
154
155 // Any standard plugin with subplugins is suitable.
156 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
157
e87214bd 158 $parent = core_plugin_manager::instance()->get_parent_of_subplugin('tinymce');
bde002b8
PS
159 $this->assertSame('editor_tinymce', $parent);
160 }
161
162 public function test_plugin_name() {
163 global $CFG;
164
165 // Any standard plugin is suitable.
166 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
167
e87214bd 168 $name = core_plugin_manager::instance()->plugin_name('editor_tinymce');
bde002b8
PS
169 $this->assertSame(get_string('pluginname', 'editor_tinymce'), $name);
170 }
171
172 public function test_plugintype_name() {
e87214bd 173 $name = core_plugin_manager::instance()->plugintype_name('editor');
bde002b8
PS
174 $this->assertSame(get_string('type_editor', 'core_plugin'), $name);
175 }
176
177 public function test_plugintype_name_plural() {
e87214bd 178 $name = core_plugin_manager::instance()->plugintype_name_plural('editor');
bde002b8
PS
179 $this->assertSame(get_string('type_editor_plural', 'core_plugin'), $name);
180 }
181
182 public function test_get_plugin_info() {
183 global $CFG;
184
185 // Any standard plugin is suitable.
186 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
187
e87214bd
PS
188 $info = core_plugin_manager::instance()->get_plugin_info('editor_tinymce');
189 $this->assertInstanceOf('\core\plugininfo\editor', $info);
bde002b8
PS
190 }
191
192 public function test_can_uninstall_plugin() {
193 global $CFG;
194
195 // Any standard plugin that is required by some other standard plugin is ok.
196 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/assignmentupgrade", 'assign upgrade tool is not present');
197 $this->assertFileExists("$CFG->dirroot/mod/assign", 'assign module is not present');
198
e87214bd
PS
199 $this->assertFalse(core_plugin_manager::instance()->can_uninstall_plugin('mod_assign'));
200 $this->assertTrue(core_plugin_manager::instance()->can_uninstall_plugin('tool_assignmentupgrade'));
bde002b8
PS
201 }
202
203 public function test_plugin_states() {
204 global $CFG;
205 $this->resetAfterTest();
206
207 // Any standard plugin that is ok.
208 $this->assertFileExists("$CFG->dirroot/mod/assign", 'assign module is not present');
209 $this->assertFileExists("$CFG->dirroot/mod/forum", 'forum module is not present');
210 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/phpunit", 'phpunit tool is not present');
211 $this->assertFileNotExists("$CFG->dirroot/mod/xxxxxxx");
212 $this->assertFileNotExists("$CFG->dirroot/enrol/autorize");
213
214 // Ready for upgrade.
215 $assignversion = get_config('mod_assign', 'version');
216 set_config('version', $assignversion - 1, 'mod_assign');
217 // Downgrade problem.
218 $forumversion = get_config('mod_forum', 'version');
219 set_config('version', $forumversion + 1, 'mod_forum');
220 // Not installed yet.
221 unset_config('version', 'tool_phpunit');
222 // Missing already installed.
223 set_config('version', 2013091300, 'mod_xxxxxxx');
224 // Deleted present.
225 set_config('version', 2013091300, 'enrol_authorize');
226
e87214bd 227 core_plugin_manager::reset_caches();
bde002b8 228
e87214bd 229 $plugininfos = core_plugin_manager::instance()->get_plugins();
bde002b8
PS
230 foreach ($plugininfos as $type => $infos) {
231 foreach ($infos as $name => $info) {
6b61c2c4 232 /** @var core\plugininfo\base $info */
bde002b8 233 if ($info->component === 'mod_assign') {
e87214bd 234 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_UPGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 235 } else if ($info->component === 'mod_forum') {
e87214bd 236 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_DOWNGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 237 } else if ($info->component === 'tool_phpunit') {
e87214bd 238 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_NEW, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 239 } else if ($info->component === 'mod_xxxxxxx') {
e87214bd 240 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_MISSING, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 241 } else if ($info->component === 'enrol_authorize') {
e87214bd 242 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_DELETE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 243 } else {
e87214bd 244 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_UPTODATE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8
PS
245 }
246 }
247 }
248 }
249}