MDL-49329 admin: Do not always load info about available updates
[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
bde002b8
PS
28/**
29 * Tests of the basic API of the plugin manager.
30 */
31class core_plugin_manager_testcase extends advanced_testcase {
32
33 public function test_instance() {
e87214bd
PS
34 $pluginman = core_plugin_manager::instance();
35 $this->assertInstanceOf('core_plugin_manager', $pluginman);
36 $pluginman2 = core_plugin_manager::instance();
bde002b8
PS
37 $this->assertSame($pluginman, $pluginman2);
38 }
39
40 public function test_reset_caches() {
41 // Make sure there are no warnings or errors.
e87214bd 42 core_plugin_manager::reset_caches();
bde002b8
PS
43 }
44
45 public function test_get_plugin_types() {
46 // Make sure there are no warnings or errors.
e87214bd 47 $types = core_plugin_manager::instance()->get_plugin_types();
bde002b8
PS
48 $this->assertInternalType('array', $types);
49 foreach ($types as $type => $fulldir) {
50 $this->assertFileExists($fulldir);
51 }
52 }
53
54 public function test_get_installed_plugins() {
e87214bd 55 $types = core_plugin_manager::instance()->get_plugin_types();
bde002b8 56 foreach ($types as $type => $fulldir) {
e87214bd 57 $installed = core_plugin_manager::instance()->get_installed_plugins($type);
bde002b8
PS
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.');
61 }
62 }
63 }
64
65 public function test_get_enabled_plugins() {
e87214bd 66 $types = core_plugin_manager::instance()->get_plugin_types();
bde002b8 67 foreach ($types as $type => $fulldir) {
e87214bd 68 $enabled = core_plugin_manager::instance()->get_enabled_plugins($type);
bde002b8
PS
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);
73 }
74 } else {
75 $this->assertNull($enabled);
76 }
77 }
78 }
79
80 public function test_get_present_plugins() {
e87214bd 81 $types = core_plugin_manager::instance()->get_plugin_types();
bde002b8 82 foreach ($types as $type => $fulldir) {
e87214bd 83 $present = core_plugin_manager::instance()->get_present_plugins($type);
bde002b8
PS
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.');
89 }
90 } else {
91 // No plugins of this type exist.
92 $this->assertNull($present);
93 }
94 }
95 }
96
97 public function test_get_plugins() {
e87214bd 98 $plugininfos = core_plugin_manager::instance()->get_plugins();
bde002b8
PS
99 foreach ($plugininfos as $type => $infos) {
100 foreach ($infos as $name => $info) {
e87214bd 101 $this->assertInstanceOf('\core\plugininfo\base', $info);
bde002b8
PS
102 }
103 }
104 }
105
106 public function test_get_plugins_of_type() {
e87214bd 107 $plugininfos = core_plugin_manager::instance()->get_plugins();
bde002b8 108 foreach ($plugininfos as $type => $infos) {
e87214bd 109 $this->assertSame($infos, core_plugin_manager::instance()->get_plugins_of_type($type));
bde002b8
PS
110 }
111 }
112
113 public function test_get_subplugins_of_plugin() {
114 global $CFG;
115
116 // Any standard plugin with subplugins is suitable.
117 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
118
e87214bd 119 $subplugins = core_plugin_manager::instance()->get_subplugins_of_plugin('editor_tinymce');
bde002b8 120 foreach ($subplugins as $component => $info) {
e87214bd 121 $this->assertInstanceOf('\core\plugininfo\base', $info);
bde002b8
PS
122 }
123 }
124
125 public function test_get_subplugins() {
126 // Tested already indirectly from test_get_subplugins_of_plugin().
e87214bd 127 $subplugins = core_plugin_manager::instance()->get_subplugins();
bde002b8
PS
128 $this->assertInternalType('array', $subplugins);
129 }
130
131 public function test_get_parent_of_subplugin() {
132 global $CFG;
133
134 // Any standard plugin with subplugins is suitable.
135 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
136
e87214bd 137 $parent = core_plugin_manager::instance()->get_parent_of_subplugin('tinymce');
bde002b8
PS
138 $this->assertSame('editor_tinymce', $parent);
139 }
140
141 public function test_plugin_name() {
142 global $CFG;
143
144 // Any standard plugin is suitable.
145 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
146
e87214bd 147 $name = core_plugin_manager::instance()->plugin_name('editor_tinymce');
bde002b8
PS
148 $this->assertSame(get_string('pluginname', 'editor_tinymce'), $name);
149 }
150
151 public function test_plugintype_name() {
e87214bd 152 $name = core_plugin_manager::instance()->plugintype_name('editor');
bde002b8
PS
153 $this->assertSame(get_string('type_editor', 'core_plugin'), $name);
154 }
155
156 public function test_plugintype_name_plural() {
e87214bd 157 $name = core_plugin_manager::instance()->plugintype_name_plural('editor');
bde002b8
PS
158 $this->assertSame(get_string('type_editor_plural', 'core_plugin'), $name);
159 }
160
161 public function test_get_plugin_info() {
162 global $CFG;
163
164 // Any standard plugin is suitable.
165 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
166
e87214bd
PS
167 $info = core_plugin_manager::instance()->get_plugin_info('editor_tinymce');
168 $this->assertInstanceOf('\core\plugininfo\editor', $info);
bde002b8
PS
169 }
170
171 public function test_can_uninstall_plugin() {
172 global $CFG;
173
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');
177
e87214bd
PS
178 $this->assertFalse(core_plugin_manager::instance()->can_uninstall_plugin('mod_assign'));
179 $this->assertTrue(core_plugin_manager::instance()->can_uninstall_plugin('tool_assignmentupgrade'));
bde002b8
PS
180 }
181
182 public function test_plugin_states() {
183 global $CFG;
184 $this->resetAfterTest();
185
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");
192
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');
205
e87214bd 206 core_plugin_manager::reset_caches();
bde002b8 207
e87214bd 208 $plugininfos = core_plugin_manager::instance()->get_plugins();
bde002b8
PS
209 foreach ($plugininfos as $type => $infos) {
210 foreach ($infos as $name => $info) {
6b61c2c4 211 /** @var core\plugininfo\base $info */
bde002b8 212 if ($info->component === 'mod_assign') {
e87214bd 213 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_UPGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 214 } else if ($info->component === 'mod_forum') {
e87214bd 215 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_DOWNGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 216 } else if ($info->component === 'tool_phpunit') {
e87214bd 217 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_NEW, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 218 } else if ($info->component === 'mod_xxxxxxx') {
e87214bd 219 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_MISSING, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 220 } else if ($info->component === 'enrol_authorize') {
e87214bd 221 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_DELETE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8 222 } else {
e87214bd 223 $this->assertSame(core_plugin_manager::PLUGIN_STATUS_UPTODATE, $info->get_status(), 'Invalid '.$info->component.' state');
bde002b8
PS
224 }
225 }
226 }
227 }
228}