Commit | Line | Data |
---|---|---|
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 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
28 | ||
29 | /** | |
30 | * Class core_component_testcase. | |
31 | */ | |
32 | class 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!!! | |
79252219 | 37 | const SUBSYSTEMCOUNT = 62; |
9e19a0f0 PS |
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) { | |
84192d78 | 50 | if ($subsystem === 'filepicker' or $subsystem === 'help') { |
9e19a0f0 PS |
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); | |
402a974e | 154 | $dirs = array_values($dirs); |
9e19a0f0 PS |
155 | $this->assertDebuggingCalled(); |
156 | $this->assertSame($dirs, array_keys($plugins)); | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
161 | public function test_deprecated_get_plugin_list() { | |
162 | $plugintypes = core_component::get_plugin_types(); | |
163 | ||
164 | foreach ($plugintypes as $plugintype => $fulldir) { | |
165 | $plugins = core_component::get_plugin_list($plugintype); | |
166 | $this->assertSame($plugins, get_plugin_list($plugintype)); | |
167 | } | |
168 | } | |
169 | ||
170 | public function test_get_plugin_directory() { | |
171 | $plugintypes = core_component::get_plugin_types(); | |
172 | ||
173 | foreach ($plugintypes as $plugintype => $fulldir) { | |
174 | $plugins = core_component::get_plugin_list($plugintype); | |
175 | foreach($plugins as $pluginname => $plugindir) { | |
176 | $this->assertSame($plugindir, core_component::get_plugin_directory($plugintype, $pluginname)); | |
177 | } | |
178 | } | |
179 | } | |
180 | ||
181 | public function test_deprecated_get_plugin_directory() { | |
182 | $plugintypes = core_component::get_plugin_types(); | |
183 | ||
184 | foreach ($plugintypes as $plugintype => $fulldir) { | |
185 | $plugins = core_component::get_plugin_list($plugintype); | |
186 | foreach($plugins as $pluginname => $plugindir) { | |
187 | $this->assertSame(core_component::get_plugin_directory($plugintype, $pluginname), get_plugin_directory($plugintype, $pluginname)); | |
188 | } | |
189 | } | |
190 | } | |
191 | ||
192 | public function test_get_subsystem_directory() { | |
193 | $subsystems = core_component::get_core_subsystems(); | |
194 | foreach ($subsystems as $subsystem => $fulldir) { | |
195 | $this->assertSame($fulldir, core_component::get_subsystem_directory($subsystem)); | |
196 | } | |
197 | } | |
198 | ||
199 | public function test_is_valid_plugin_name() { | |
200 | $this->assertTrue(core_component::is_valid_plugin_name('mod', 'example1')); | |
201 | $this->assertFalse(core_component::is_valid_plugin_name('mod', '1example')); | |
202 | $this->assertFalse(core_component::is_valid_plugin_name('mod', 'example.xx')); | |
203 | $this->assertFalse(core_component::is_valid_plugin_name('mod', '.example')); | |
204 | $this->assertFalse(core_component::is_valid_plugin_name('mod', '_example')); | |
205 | $this->assertFalse(core_component::is_valid_plugin_name('mod', 'example_x1')); | |
206 | $this->assertFalse(core_component::is_valid_plugin_name('mod', 'example-x1')); | |
207 | $this->assertFalse(core_component::is_valid_plugin_name('mod', 'role')); | |
208 | ||
209 | $this->assertTrue(core_component::is_valid_plugin_name('tool', 'example1')); | |
210 | $this->assertTrue(core_component::is_valid_plugin_name('tool', 'example_x1')); | |
211 | $this->assertTrue(core_component::is_valid_plugin_name('tool', 'example_x1_xxx')); | |
212 | $this->assertTrue(core_component::is_valid_plugin_name('tool', 'role')); | |
213 | $this->assertFalse(core_component::is_valid_plugin_name('tool', '1example')); | |
214 | $this->assertFalse(core_component::is_valid_plugin_name('tool', 'example.xx')); | |
215 | $this->assertFalse(core_component::is_valid_plugin_name('tool', 'example-xx')); | |
216 | $this->assertFalse(core_component::is_valid_plugin_name('tool', '.example')); | |
217 | $this->assertFalse(core_component::is_valid_plugin_name('tool', '_example')); | |
218 | } | |
219 | ||
220 | public function test_normalize_component() { | |
221 | global $CFG; | |
222 | ||
223 | // Moodle core. | |
224 | $this->assertSame(array('core', null), core_component::normalize_component('core')); | |
225 | $this->assertSame(array('core', null), core_component::normalize_component('moodle')); | |
226 | $this->assertSame(array('core', null), core_component::normalize_component('')); | |
227 | ||
228 | // Moodle core subsystems. | |
229 | $this->assertSame(array('core', 'admin'), core_component::normalize_component('admin')); | |
230 | $this->assertSame(array('core', 'admin'), core_component::normalize_component('core_admin')); | |
231 | $this->assertSame(array('core', 'admin'), core_component::normalize_component('moodle_admin')); | |
232 | ||
233 | // Activity modules and their subplugins. | |
234 | $this->assertSame(array('mod', 'workshop'), core_component::normalize_component('workshop')); | |
235 | $this->assertSame(array('mod', 'workshop'), core_component::normalize_component('mod_workshop')); | |
236 | $this->assertSame(array('workshopform', 'accumulative'), core_component::normalize_component('workshopform_accumulative')); | |
237 | $this->assertSame(array('mod', 'quiz'), core_component::normalize_component('quiz')); | |
238 | $this->assertSame(array('quiz', 'grading'), core_component::normalize_component('quiz_grading')); | |
239 | $this->assertSame(array('mod', 'data'), core_component::normalize_component('data')); | |
240 | $this->assertSame(array('datafield', 'checkbox'), core_component::normalize_component('datafield_checkbox')); | |
241 | ||
242 | // Other plugin types. | |
243 | $this->assertSame(array('auth', 'mnet'), core_component::normalize_component('auth_mnet')); | |
244 | $this->assertSame(array('enrol', 'self'), core_component::normalize_component('enrol_self')); | |
245 | $this->assertSame(array('block', 'html'), core_component::normalize_component('block_html')); | |
246 | $this->assertSame(array('block', 'mnet_hosts'), core_component::normalize_component('block_mnet_hosts')); | |
247 | $this->assertSame(array('local', 'amos'), core_component::normalize_component('local_amos')); | |
248 | $this->assertSame(array('local', 'admin'), core_component::normalize_component('local_admin')); | |
249 | ||
250 | // Unknown words without underscore are supposed to be activity modules. | |
251 | $this->assertSame(array('mod', 'whothefuckwouldcomewithsuchastupidnameofcomponent'), | |
252 | core_component::normalize_component('whothefuckwouldcomewithsuchastupidnameofcomponent')); | |
253 | // Module names can not contain underscores, this must be a subplugin. | |
254 | $this->assertSame(array('whothefuck', 'wouldcomewithsuchastupidnameofcomponent'), | |
255 | core_component::normalize_component('whothefuck_wouldcomewithsuchastupidnameofcomponent')); | |
256 | $this->assertSame(array('whothefuck', 'would_come_withsuchastupidnameofcomponent'), | |
257 | core_component::normalize_component('whothefuck_would_come_withsuchastupidnameofcomponent')); | |
258 | } | |
259 | ||
260 | public function test_deprecated_normalize_component() { | |
261 | global $CFG; | |
262 | ||
263 | // Moodle core. | |
264 | $this->assertSame(array('core', null), normalize_component('core')); | |
265 | $this->assertSame(array('core', null), normalize_component('')); | |
266 | $this->assertSame(array('core', null), normalize_component('moodle')); | |
267 | ||
268 | // Moodle core subsystems. | |
269 | $this->assertSame(array('core', 'admin'), normalize_component('admin')); | |
270 | $this->assertSame(array('core', 'admin'), normalize_component('core_admin')); | |
271 | $this->assertSame(array('core', 'admin'), normalize_component('moodle_admin')); | |
272 | ||
273 | // Activity modules and their subplugins. | |
274 | $this->assertSame(array('mod', 'workshop'), normalize_component('workshop')); | |
275 | $this->assertSame(array('mod', 'workshop'), normalize_component('mod_workshop')); | |
276 | $this->assertSame(array('workshopform', 'accumulative'), normalize_component('workshopform_accumulative')); | |
277 | $this->assertSame(array('mod', 'quiz'), normalize_component('quiz')); | |
278 | $this->assertSame(array('quiz', 'grading'), normalize_component('quiz_grading')); | |
279 | $this->assertSame(array('mod', 'data'), normalize_component('data')); | |
280 | $this->assertSame(array('datafield', 'checkbox'), normalize_component('datafield_checkbox')); | |
281 | ||
282 | // Other plugin types. | |
283 | $this->assertSame(array('auth', 'mnet'), normalize_component('auth_mnet')); | |
284 | $this->assertSame(array('enrol', 'self'), normalize_component('enrol_self')); | |
285 | $this->assertSame(array('block', 'html'), normalize_component('block_html')); | |
286 | $this->assertSame(array('block', 'mnet_hosts'), normalize_component('block_mnet_hosts')); | |
287 | $this->assertSame(array('local', 'amos'), normalize_component('local_amos')); | |
288 | $this->assertSame(array('local', 'admin'), normalize_component('local_admin')); | |
289 | ||
290 | // Unknown words without underscore are supposed to be activity modules. | |
291 | $this->assertSame(array('mod', 'whothefuckwouldcomewithsuchastupidnameofcomponent'), | |
292 | normalize_component('whothefuckwouldcomewithsuchastupidnameofcomponent')); | |
293 | // Module names can not contain underscores, this must be a subplugin. | |
294 | $this->assertSame(array('whothefuck', 'wouldcomewithsuchastupidnameofcomponent'), | |
295 | normalize_component('whothefuck_wouldcomewithsuchastupidnameofcomponent')); | |
296 | $this->assertSame(array('whothefuck', 'would_come_withsuchastupidnameofcomponent'), | |
297 | normalize_component('whothefuck_would_come_withsuchastupidnameofcomponent')); | |
298 | } | |
299 | ||
300 | ||
301 | public function test_get_component_directory() { | |
302 | $plugintypes = core_component::get_plugin_types(); | |
303 | foreach ($plugintypes as $plugintype => $fulldir) { | |
304 | $plugins = core_component::get_plugin_list($plugintype); | |
305 | foreach($plugins as $pluginname => $plugindir) { | |
306 | $this->assertSame($plugindir, core_component::get_component_directory(($plugintype.'_'.$pluginname))); | |
307 | } | |
308 | } | |
309 | ||
310 | $subsystems = core_component::get_core_subsystems(); | |
311 | foreach($subsystems as $subsystem => $fulldir) { | |
312 | $this->assertSame($fulldir, core_component::get_component_directory(('core_'.$subsystem))); | |
313 | } | |
314 | } | |
315 | ||
316 | public function test_deprecated_get_component_directory() { | |
317 | $plugintypes = core_component::get_plugin_types(); | |
318 | foreach ($plugintypes as $plugintype => $fulldir) { | |
319 | $plugins = core_component::get_plugin_list($plugintype); | |
320 | foreach($plugins as $pluginname => $plugindir) { | |
321 | $this->assertSame($plugindir, get_component_directory(($plugintype.'_'.$pluginname))); | |
322 | } | |
323 | } | |
324 | ||
325 | $subsystems = core_component::get_core_subsystems(); | |
326 | foreach($subsystems as $subsystem => $fulldir) { | |
327 | $this->assertSame($fulldir, get_component_directory(('core_'.$subsystem))); | |
328 | } | |
329 | } | |
3601c5f0 PS |
330 | |
331 | public function test_get_plugin_types_with_subplugins() { | |
332 | global $CFG; | |
333 | ||
334 | $types = core_component::get_plugin_types_with_subplugins(); | |
335 | ||
336 | // Hardcode it here to detect if anybody hacks the code to include more types. | |
337 | $expected = array( | |
338 | 'mod' => "$CFG->dirroot/mod", | |
339 | 'editor' => "$CFG->dirroot/lib/editor", | |
340 | 'local' => "$CFG->dirroot/local", | |
341 | ); | |
342 | ||
343 | $this->assertSame($expected, $types); | |
344 | } | |
9e19a0f0 | 345 | } |