MDL-39854 reimplement Frankenstyle support and enable classloader
[moodle.git] / lib / tests / component_test.php
CommitLineData
9e19a0f0
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 * core_component related tests.
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
28
29/**
30 * Class core_component_testcase.
31 */
32class core_component_testcase extends advanced_testcase {
33
34 // To be changed if number of subsystems increases/decreases,
35 // this is defined here to annoy devs that try to add more without any thinking,
36 // always verify that it does not collide with any existing add-on modules and subplugins!!!
37 const SUBSYSTEMCOUNT = 63;
38
39 public function test_get_core_subsystems() {
40 global $CFG;
41
42 $subsystems = core_component::get_core_subsystems();
43
44 $this->assertCount(self::SUBSYSTEMCOUNT, $subsystems, 'Oh, somebody added or removed a core subsystem, think twice before doing that!');
45
46 // Make sure all paths are full/null, exist and are inside dirroot.
47 foreach($subsystems as $subsystem => $fulldir) {
48 $this->assertFalse(strpos($subsystem, '_'), 'Core subsystems must be one work without underscores');
49 if ($fulldir === null) {
50 if ($subsystem === 'dock' or $subsystem === 'filepicker' or $subsystem === 'help') {
51 // Arrgghh, let's not introduce more subsystems for no real reason...
52 } else {
53 // Lang strings.
54 $this->assertFileExists("$CFG->dirroot/lang/en/$subsystem.php", 'Core subsystems without fulldir are usually used for lang strings.');
55 }
56 continue;
57 }
58 $this->assertFileExists($fulldir);
59 // Check that base uses realpath() separators and "/" in the subdirs.
60 $this->assertStringStartsWith($CFG->dirroot.'/', $fulldir);
61 $reldir = substr($fulldir, strlen($CFG->dirroot)+1);
62 $this->assertFalse(strpos($reldir, '\\'));
63 }
64
65 // Make sure all core language files are also subsystems!
66 $items = new DirectoryIterator("$CFG->dirroot/lang/en");
67 foreach ($items as $item) {
68 if ($item->isDot() or $item->isDir()) {
69 continue;
70 }
71 $file = $item->getFilename();
72 if ($file === 'moodle.php') {
73 // Do not add new lang strings unless really necessary!!!
74 continue;
75 }
76
77 if (substr($file, -4) !== '.php') {
78 continue;
79 }
80 $file = substr($file, 0, strlen($file)-4);
81 $this->assertArrayHasKey($file, $subsystems, 'All core lang files should be subsystems, think twice before adding anything!');
82 }
83 unset($item);
84 unset($items);
85
86 }
87
88 public function test_deprecated_get_core_subsystems() {
89 global $CFG;
90
91 $subsystems = core_component::get_core_subsystems();
92
93 $this->assertSame($subsystems, get_core_subsystems(true));
94
95 $realsubsystems = get_core_subsystems();
96 $this->assertDebuggingCalled();
97 $this->assertSame($realsubsystems, get_core_subsystems(false));
98 $this->assertDebuggingCalled();
99
100 $this->assertEquals(count($subsystems), count($realsubsystems));
101
102 foreach ($subsystems as $subsystem => $fulldir) {
103 $this->assertArrayHasKey($subsystem, $realsubsystems);
104 if ($fulldir === null) {
105 $this->assertNull($realsubsystems[$subsystem]);
106 continue;
107 }
108 $this->assertSame($fulldir, $CFG->dirroot.'/'.$realsubsystems[$subsystem]);
109 }
110 }
111
112 public function test_get_plugin_types() {
113 global $CFG;
114
115 $this->assertTrue(empty($CFG->themedir), 'Non-empty $CFG->themedir is not covered by any tests yet, you need to disable it.');
116
117 $plugintypes = core_component::get_plugin_types();
118
119 foreach($plugintypes as $plugintype => $fulldir) {
120 $this->assertStringStartsWith("$CFG->dirroot/", $fulldir);
121 }
122 }
123
124 public function test_deprecated_get_plugin_types() {
125 global $CFG;
126
127 $plugintypes = core_component::get_plugin_types();
128
129 $this->assertSame($plugintypes, get_plugin_types());
130 $this->assertSame($plugintypes, get_plugin_types(true));
131
132 $realplugintypes = get_plugin_types(false);
133 $this->assertDebuggingCalled();
134
135 foreach($plugintypes as $plugintype => $fulldir) {
136 $this->assertSame($fulldir, $CFG->dirroot.'/'.$realplugintypes[$plugintype]);
137 }
138 }
139
140 public function test_get_plugin_list() {
141 global $CFG;
142
143 $plugintypes = core_component::get_plugin_types();
144
145 foreach ($plugintypes as $plugintype => $fulldir) {
146 $plugins = core_component::get_plugin_list($plugintype);
147 foreach($plugins as $pluginname => $plugindir) {
148 $this->assertStringStartsWith("$CFG->dirroot/", $plugindir);
149 }
150 if ($plugintype !== 'auth') {
151 // Let's crosscheck it with independent implementation (auth/db is an exception).
152 $reldir = substr($fulldir, strlen($CFG->dirroot)+1);
153 $dirs = get_list_of_plugins($reldir);
154 $this->assertDebuggingCalled();
155 $this->assertSame($dirs, array_keys($plugins));
156 }
157 }
158 }
159
160 public function test_deprecated_get_plugin_list() {
161 $plugintypes = core_component::get_plugin_types();
162
163 foreach ($plugintypes as $plugintype => $fulldir) {
164 $plugins = core_component::get_plugin_list($plugintype);
165 $this->assertSame($plugins, get_plugin_list($plugintype));
166 }
167 }
168
169 public function test_get_plugin_directory() {
170 $plugintypes = core_component::get_plugin_types();
171
172 foreach ($plugintypes as $plugintype => $fulldir) {
173 $plugins = core_component::get_plugin_list($plugintype);
174 foreach($plugins as $pluginname => $plugindir) {
175 $this->assertSame($plugindir, core_component::get_plugin_directory($plugintype, $pluginname));
176 }
177 }
178 }
179
180 public function test_deprecated_get_plugin_directory() {
181 $plugintypes = core_component::get_plugin_types();
182
183 foreach ($plugintypes as $plugintype => $fulldir) {
184 $plugins = core_component::get_plugin_list($plugintype);
185 foreach($plugins as $pluginname => $plugindir) {
186 $this->assertSame(core_component::get_plugin_directory($plugintype, $pluginname), get_plugin_directory($plugintype, $pluginname));
187 }
188 }
189 }
190
191 public function test_get_subsystem_directory() {
192 $subsystems = core_component::get_core_subsystems();
193 foreach ($subsystems as $subsystem => $fulldir) {
194 $this->assertSame($fulldir, core_component::get_subsystem_directory($subsystem));
195 }
196 }
197
198 public function test_is_valid_plugin_name() {
199 $this->assertTrue(core_component::is_valid_plugin_name('mod', 'example1'));
200 $this->assertFalse(core_component::is_valid_plugin_name('mod', '1example'));
201 $this->assertFalse(core_component::is_valid_plugin_name('mod', 'example.xx'));
202 $this->assertFalse(core_component::is_valid_plugin_name('mod', '.example'));
203 $this->assertFalse(core_component::is_valid_plugin_name('mod', '_example'));
204 $this->assertFalse(core_component::is_valid_plugin_name('mod', 'example_x1'));
205 $this->assertFalse(core_component::is_valid_plugin_name('mod', 'example-x1'));
206 $this->assertFalse(core_component::is_valid_plugin_name('mod', 'role'));
207
208 $this->assertTrue(core_component::is_valid_plugin_name('tool', 'example1'));
209 $this->assertTrue(core_component::is_valid_plugin_name('tool', 'example_x1'));
210 $this->assertTrue(core_component::is_valid_plugin_name('tool', 'example_x1_xxx'));
211 $this->assertTrue(core_component::is_valid_plugin_name('tool', 'role'));
212 $this->assertFalse(core_component::is_valid_plugin_name('tool', '1example'));
213 $this->assertFalse(core_component::is_valid_plugin_name('tool', 'example.xx'));
214 $this->assertFalse(core_component::is_valid_plugin_name('tool', 'example-xx'));
215 $this->assertFalse(core_component::is_valid_plugin_name('tool', '.example'));
216 $this->assertFalse(core_component::is_valid_plugin_name('tool', '_example'));
217 }
218
219 public function test_normalize_component() {
220 global $CFG;
221
222 // Moodle core.
223 $this->assertSame(array('core', null), core_component::normalize_component('core'));
224 $this->assertSame(array('core', null), core_component::normalize_component('moodle'));
225 $this->assertSame(array('core', null), core_component::normalize_component(''));
226
227 // Moodle core subsystems.
228 $this->assertSame(array('core', 'admin'), core_component::normalize_component('admin'));
229 $this->assertSame(array('core', 'admin'), core_component::normalize_component('core_admin'));
230 $this->assertSame(array('core', 'admin'), core_component::normalize_component('moodle_admin'));
231
232 // Activity modules and their subplugins.
233 $this->assertSame(array('mod', 'workshop'), core_component::normalize_component('workshop'));
234 $this->assertSame(array('mod', 'workshop'), core_component::normalize_component('mod_workshop'));
235 $this->assertSame(array('workshopform', 'accumulative'), core_component::normalize_component('workshopform_accumulative'));
236 $this->assertSame(array('mod', 'quiz'), core_component::normalize_component('quiz'));
237 $this->assertSame(array('quiz', 'grading'), core_component::normalize_component('quiz_grading'));
238 $this->assertSame(array('mod', 'data'), core_component::normalize_component('data'));
239 $this->assertSame(array('datafield', 'checkbox'), core_component::normalize_component('datafield_checkbox'));
240
241 // Other plugin types.
242 $this->assertSame(array('auth', 'mnet'), core_component::normalize_component('auth_mnet'));
243 $this->assertSame(array('enrol', 'self'), core_component::normalize_component('enrol_self'));
244 $this->assertSame(array('block', 'html'), core_component::normalize_component('block_html'));
245 $this->assertSame(array('block', 'mnet_hosts'), core_component::normalize_component('block_mnet_hosts'));
246 $this->assertSame(array('local', 'amos'), core_component::normalize_component('local_amos'));
247 $this->assertSame(array('local', 'admin'), core_component::normalize_component('local_admin'));
248
249 // Unknown words without underscore are supposed to be activity modules.
250 $this->assertSame(array('mod', 'whothefuckwouldcomewithsuchastupidnameofcomponent'),
251 core_component::normalize_component('whothefuckwouldcomewithsuchastupidnameofcomponent'));
252 // Module names can not contain underscores, this must be a subplugin.
253 $this->assertSame(array('whothefuck', 'wouldcomewithsuchastupidnameofcomponent'),
254 core_component::normalize_component('whothefuck_wouldcomewithsuchastupidnameofcomponent'));
255 $this->assertSame(array('whothefuck', 'would_come_withsuchastupidnameofcomponent'),
256 core_component::normalize_component('whothefuck_would_come_withsuchastupidnameofcomponent'));
257 }
258
259 public function test_deprecated_normalize_component() {
260 global $CFG;
261
262 // Moodle core.
263 $this->assertSame(array('core', null), normalize_component('core'));
264 $this->assertSame(array('core', null), normalize_component(''));
265 $this->assertSame(array('core', null), normalize_component('moodle'));
266
267 // Moodle core subsystems.
268 $this->assertSame(array('core', 'admin'), normalize_component('admin'));
269 $this->assertSame(array('core', 'admin'), normalize_component('core_admin'));
270 $this->assertSame(array('core', 'admin'), normalize_component('moodle_admin'));
271
272 // Activity modules and their subplugins.
273 $this->assertSame(array('mod', 'workshop'), normalize_component('workshop'));
274 $this->assertSame(array('mod', 'workshop'), normalize_component('mod_workshop'));
275 $this->assertSame(array('workshopform', 'accumulative'), normalize_component('workshopform_accumulative'));
276 $this->assertSame(array('mod', 'quiz'), normalize_component('quiz'));
277 $this->assertSame(array('quiz', 'grading'), normalize_component('quiz_grading'));
278 $this->assertSame(array('mod', 'data'), normalize_component('data'));
279 $this->assertSame(array('datafield', 'checkbox'), normalize_component('datafield_checkbox'));
280
281 // Other plugin types.
282 $this->assertSame(array('auth', 'mnet'), normalize_component('auth_mnet'));
283 $this->assertSame(array('enrol', 'self'), normalize_component('enrol_self'));
284 $this->assertSame(array('block', 'html'), normalize_component('block_html'));
285 $this->assertSame(array('block', 'mnet_hosts'), normalize_component('block_mnet_hosts'));
286 $this->assertSame(array('local', 'amos'), normalize_component('local_amos'));
287 $this->assertSame(array('local', 'admin'), normalize_component('local_admin'));
288
289 // Unknown words without underscore are supposed to be activity modules.
290 $this->assertSame(array('mod', 'whothefuckwouldcomewithsuchastupidnameofcomponent'),
291 normalize_component('whothefuckwouldcomewithsuchastupidnameofcomponent'));
292 // Module names can not contain underscores, this must be a subplugin.
293 $this->assertSame(array('whothefuck', 'wouldcomewithsuchastupidnameofcomponent'),
294 normalize_component('whothefuck_wouldcomewithsuchastupidnameofcomponent'));
295 $this->assertSame(array('whothefuck', 'would_come_withsuchastupidnameofcomponent'),
296 normalize_component('whothefuck_would_come_withsuchastupidnameofcomponent'));
297 }
298
299
300 public function test_get_component_directory() {
301 $plugintypes = core_component::get_plugin_types();
302 foreach ($plugintypes as $plugintype => $fulldir) {
303 $plugins = core_component::get_plugin_list($plugintype);
304 foreach($plugins as $pluginname => $plugindir) {
305 $this->assertSame($plugindir, core_component::get_component_directory(($plugintype.'_'.$pluginname)));
306 }
307 }
308
309 $subsystems = core_component::get_core_subsystems();
310 foreach($subsystems as $subsystem => $fulldir) {
311 $this->assertSame($fulldir, core_component::get_component_directory(('core_'.$subsystem)));
312 }
313 }
314
315 public function test_deprecated_get_component_directory() {
316 $plugintypes = core_component::get_plugin_types();
317 foreach ($plugintypes as $plugintype => $fulldir) {
318 $plugins = core_component::get_plugin_list($plugintype);
319 foreach($plugins as $pluginname => $plugindir) {
320 $this->assertSame($plugindir, get_component_directory(($plugintype.'_'.$pluginname)));
321 }
322 }
323
324 $subsystems = core_component::get_core_subsystems();
325 foreach($subsystems as $subsystem => $fulldir) {
326 $this->assertSame($fulldir, get_component_directory(('core_'.$subsystem)));
327 }
328 }
329}