MDL-41437 rework plugin_manager caching and version info in blocks and modules
[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
28global $CFG;
29require_once($CFG->libdir.'/pluginlib.php');
30
31
32/**
33 * Tests of the basic API of the plugin manager.
34 */
35class core_plugin_manager_testcase extends advanced_testcase {
36
37 public function test_instance() {
38 $pluginman = plugin_manager::instance();
39 $this->assertInstanceOf('plugin_manager', $pluginman);
40 $pluginman2 = plugin_manager::instance();
41 $this->assertSame($pluginman, $pluginman2);
42 }
43
44 public function test_reset_caches() {
45 // Make sure there are no warnings or errors.
46 plugin_manager::reset_caches();
47 }
48
49 public function test_get_plugin_types() {
50 // Make sure there are no warnings or errors.
51 $types = plugin_manager::instance()->get_plugin_types();
52 $this->assertInternalType('array', $types);
53 foreach ($types as $type => $fulldir) {
54 $this->assertFileExists($fulldir);
55 }
56 }
57
58 public function test_get_installed_plugins() {
59 $types = plugin_manager::instance()->get_plugin_types();
60 foreach ($types as $type => $fulldir) {
61 $installed = plugin_manager::instance()->get_installed_plugins($type);
62 foreach ($installed as $plugin => $version) {
63 $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $plugin);
64 $this->assertTrue(is_numeric($version), 'All plugins should have a version, plugin '.$type.'_'.$plugin.' does not have version info.');
65 }
66 }
67 }
68
69 public function test_get_enabled_plugins() {
70 $types = plugin_manager::instance()->get_plugin_types();
71 foreach ($types as $type => $fulldir) {
72 $enabled = plugin_manager::instance()->get_enabled_plugins($type);
73 if (is_array($enabled)) {
74 foreach ($enabled as $key => $val) {
75 $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $key);
76 $this->assertSame($key, $val);
77 }
78 } else {
79 $this->assertNull($enabled);
80 }
81 }
82 }
83
84 public function test_get_present_plugins() {
85 $types = plugin_manager::instance()->get_plugin_types();
86 foreach ($types as $type => $fulldir) {
87 $present = plugin_manager::instance()->get_present_plugins($type);
88 if (is_array($present)) {
89 foreach ($present as $plugin => $version) {
90 $this->assertRegExp('/^[a-z]+[a-z0-9_]*$/', $plugin, 'All plugins are supposed to have version.php file.');
91 $this->assertInternalType('object', $version);
92 $this->assertTrue(is_numeric($version->version), 'All plugins should have a version, plugin '.$type.'_'.$plugin.' does not have version info.');
93 }
94 } else {
95 // No plugins of this type exist.
96 $this->assertNull($present);
97 }
98 }
99 }
100
101 public function test_get_plugins() {
102 $plugininfos = plugin_manager::instance()->get_plugins();
103 foreach ($plugininfos as $type => $infos) {
104 foreach ($infos as $name => $info) {
105 $this->assertInstanceOf('plugininfo_base', $info);
106 }
107 }
108 }
109
110 public function test_get_plugins_of_type() {
111 $plugininfos = plugin_manager::instance()->get_plugins();
112 foreach ($plugininfos as $type => $infos) {
113 $this->assertSame($infos, plugin_manager::instance()->get_plugins_of_type($type));
114 }
115 }
116
117 public function test_get_subplugins_of_plugin() {
118 global $CFG;
119
120 // Any standard plugin with subplugins is suitable.
121 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
122
123 $subplugins = plugin_manager::instance()->get_subplugins_of_plugin('editor_tinymce');
124 foreach ($subplugins as $component => $info) {
125 $this->assertInstanceOf('plugininfo_base', $info);
126 }
127 }
128
129 public function test_get_subplugins() {
130 // Tested already indirectly from test_get_subplugins_of_plugin().
131 $subplugins = plugin_manager::instance()->get_subplugins();
132 $this->assertInternalType('array', $subplugins);
133 }
134
135 public function test_get_parent_of_subplugin() {
136 global $CFG;
137
138 // Any standard plugin with subplugins is suitable.
139 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
140
141 $parent = plugin_manager::instance()->get_parent_of_subplugin('tinymce');
142 $this->assertSame('editor_tinymce', $parent);
143 }
144
145 public function test_plugin_name() {
146 global $CFG;
147
148 // Any standard plugin is suitable.
149 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
150
151 $name = plugin_manager::instance()->plugin_name('editor_tinymce');
152 $this->assertSame(get_string('pluginname', 'editor_tinymce'), $name);
153 }
154
155 public function test_plugintype_name() {
156 $name = plugin_manager::instance()->plugintype_name('editor');
157 $this->assertSame(get_string('type_editor', 'core_plugin'), $name);
158 }
159
160 public function test_plugintype_name_plural() {
161 $name = plugin_manager::instance()->plugintype_name_plural('editor');
162 $this->assertSame(get_string('type_editor_plural', 'core_plugin'), $name);
163 }
164
165 public function test_get_plugin_info() {
166 global $CFG;
167
168 // Any standard plugin is suitable.
169 $this->assertFileExists("$CFG->dirroot/lib/editor/tinymce", 'TinyMCE is not present.');
170
171 $info = plugin_manager::instance()->get_plugin_info('editor_tinymce');
172 $this->assertInstanceOf('plugininfo_editor', $info);
173 }
174
175 public function test_can_uninstall_plugin() {
176 global $CFG;
177
178 // Any standard plugin that is required by some other standard plugin is ok.
179 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/assignmentupgrade", 'assign upgrade tool is not present');
180 $this->assertFileExists("$CFG->dirroot/mod/assign", 'assign module is not present');
181
182 $this->assertFalse(plugin_manager::instance()->can_uninstall_plugin('mod_assign'));
183 $this->assertTrue(plugin_manager::instance()->can_uninstall_plugin('tool_assignmentupgrade'));
184 }
185
186 public function test_plugin_states() {
187 global $CFG;
188 $this->resetAfterTest();
189
190 // Any standard plugin that is ok.
191 $this->assertFileExists("$CFG->dirroot/mod/assign", 'assign module is not present');
192 $this->assertFileExists("$CFG->dirroot/mod/forum", 'forum module is not present');
193 $this->assertFileExists("$CFG->dirroot/$CFG->admin/tool/phpunit", 'phpunit tool is not present');
194 $this->assertFileNotExists("$CFG->dirroot/mod/xxxxxxx");
195 $this->assertFileNotExists("$CFG->dirroot/enrol/autorize");
196
197 // Ready for upgrade.
198 $assignversion = get_config('mod_assign', 'version');
199 set_config('version', $assignversion - 1, 'mod_assign');
200 // Downgrade problem.
201 $forumversion = get_config('mod_forum', 'version');
202 set_config('version', $forumversion + 1, 'mod_forum');
203 // Not installed yet.
204 unset_config('version', 'tool_phpunit');
205 // Missing already installed.
206 set_config('version', 2013091300, 'mod_xxxxxxx');
207 // Deleted present.
208 set_config('version', 2013091300, 'enrol_authorize');
209
210 plugin_manager::reset_caches();
211
212 $plugininfos = plugin_manager::instance()->get_plugins();
213 foreach ($plugininfos as $type => $infos) {
214 foreach ($infos as $name => $info) {
215 /** @var plugininfo_base $info */
216 if ($info->component === 'mod_assign') {
217 $this->assertSame(plugin_manager::PLUGIN_STATUS_UPGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
218 } else if ($info->component === 'mod_forum') {
219 $this->assertSame(plugin_manager::PLUGIN_STATUS_DOWNGRADE, $info->get_status(), 'Invalid '.$info->component.' state');
220 } else if ($info->component === 'tool_phpunit') {
221 $this->assertSame(plugin_manager::PLUGIN_STATUS_NEW, $info->get_status(), 'Invalid '.$info->component.' state');
222 } else if ($info->component === 'mod_xxxxxxx') {
223 $this->assertSame(plugin_manager::PLUGIN_STATUS_MISSING, $info->get_status(), 'Invalid '.$info->component.' state');
224 } else if ($info->component === 'enrol_authorize') {
225 $this->assertSame(plugin_manager::PLUGIN_STATUS_DELETE, $info->get_status(), 'Invalid '.$info->component.' state');
226 } else {
227 $this->assertSame(plugin_manager::PLUGIN_STATUS_UPTODATE, $info->get_status(), 'Invalid '.$info->component.' state');
228 }
229 }
230 }
231 }
232}