Revert "Merge branch 'MDL-43127-master-fixup' of https://github.com/FMCorz/moodle"
[moodle.git] / admin / tool / uploadcourse / tests / helper_test.php
CommitLineData
509dd695
FM
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 * File containing tests for the helper.
19 *
20 * @package tool_uploadcourse
21 * @copyright 2013 Frédéric Massart
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27global $CFG;
509dd695
FM
28
29/**
30 * Helper test case.
3f57c87e
FM
31 *
32 * @package tool_uploadcourse
33 * @copyright 2013 Frédéric Massart
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
509dd695
FM
35 */
36class tool_uploadcourse_helper_testcase extends advanced_testcase {
37
3f57c87e 38 public function test_generate_shortname() {
f802b04f 39 $data = (object) array('fullname' => 'Ah bh Ch 01 02 03', 'idnumber' => 'ID123');
509dd695
FM
40
41 $this->assertSame($data->fullname, tool_uploadcourse_helper::generate_shortname($data, '%f'));
42 $this->assertSame($data->idnumber, tool_uploadcourse_helper::generate_shortname($data, '%i'));
f802b04f 43 $this->assertSame('Ah Bh Ch', tool_uploadcourse_helper::generate_shortname($data, '%~8f'));
509dd695
FM
44 $this->assertSame('AH BH CH', tool_uploadcourse_helper::generate_shortname($data, '%+8f'));
45 $this->assertSame('id123', tool_uploadcourse_helper::generate_shortname($data, '%-i'));
f802b04f
FM
46 $this->assertSame('[Ah bh Ch] = ID123', tool_uploadcourse_helper::generate_shortname($data, '[%8f] = %i'));
47 $this->assertSame('0', tool_uploadcourse_helper::generate_shortname($data, '0'));
48 $this->assertSame('%unknown', tool_uploadcourse_helper::generate_shortname($data, '%unknown'));
49
50 $this->assertNull(tool_uploadcourse_helper::generate_shortname($data, ''));
51 $this->assertNull(tool_uploadcourse_helper::generate_shortname(array(), '%f'));
509dd695
FM
52 }
53
3f57c87e 54 public function test_get_course_formats() {
509dd695 55 $result = tool_uploadcourse_helper::get_course_formats();
bd3b3bba 56 $this->assertSame(array_keys(core_component::get_plugin_list('format')), $result);
509dd695
FM
57 // Should be similar as first result, as cached.
58 $this->assertSame($result, tool_uploadcourse_helper::get_course_formats());
59 }
60
3f57c87e 61 public function test_get_enrolment_data() {
509dd695
FM
62 $this->resetAfterTest(true);
63 $data = array(
64 'enrolment_1' => 'unknown',
65 'enrolment_1_foo' => '1',
66 'enrolment_1_bar' => '2',
67 'enrolment_2' => 'self',
68 'enrolment_2_delete' => '1',
69 'enrolment_2_foo' => 'a',
70 'enrolment_2_bar' => '1',
71 'enrolment_3' => 'manual',
72 'enrolment_3_disable' => '2',
73 'enrolment_3_foo' => 'b',
74 'enrolment_3_bar' => '2',
75 'enrolment_4' => 'database',
76 'enrolment_4_foo' => 'x',
77 'enrolment_4_bar' => '3',
78 'enrolment_5_test3' => 'test3',
79 'enrolment_5_test2' => 'test2',
80 'enrolment_5_test1' => 'test1',
81 'enrolment_5' => 'flatfile',
82 );
83 $expected = array(
84 'self' => array(
85 'delete' => '1',
86 'foo' => 'a',
87 'bar' => '1',
88 ),
89 'manual' => array(
90 'disable' => '2',
91 'foo' => 'b',
92 'bar' => '2',
93 ),
94 'database' => array(
95 'foo' => 'x',
96 'bar' => '3',
97 ),
98 'flatfile' => array(
99 'test3' => 'test3',
100 'test2' => 'test2',
101 'test1' => 'test1',
102 )
103 );
61380ed2
FM
104 $errors = array();
105 $this->assertSame(tool_uploadcourse_helper::get_enrolment_data($data, $errors), $expected);
106 $this->assertArrayHasKey('unknownenrolmentmethods', $errors);
509dd695
FM
107 }
108
3f57c87e 109 public function test_get_enrolment_plugins() {
509dd695
FM
110 $this->resetAfterTest(true);
111 $actual = tool_uploadcourse_helper::get_enrolment_plugins();
112 $this->assertSame(array_keys(enrol_get_plugins(false)), array_keys($actual));
113 // This should be identical as cached.
114 $secondactual = tool_uploadcourse_helper::get_enrolment_plugins();
3f57c87e 115 $this->assertEquals($actual, $secondactual);
509dd695
FM
116 }
117
3f57c87e
FM
118 public function test_get_restore_content_dir() {
119 global $CFG;
509dd695
FM
120 $this->resetAfterTest(true);
121 $this->setAdminUser();
122
123 $c1 = $this->getDataGenerator()->create_course();
124 $c2 = $this->getDataGenerator()->create_course((object) array('shortname' => 'Yay'));
125
126 // Creating backup file.
127 $bc = new backup_controller(backup::TYPE_1COURSE, $c1->id, backup::FORMAT_MOODLE,
128 backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
129 $bc->execute_plan();
130 $result = $bc->get_results();
131 $this->assertTrue(isset($result['backup_destination']));
132 $c1backupfile = $result['backup_destination']->copy_content_to_temp();
133 $bc->destroy();
c25a5adb 134 unset($bc); // File logging is a mess, we can only try to rely on gc to close handles.
509dd695
FM
135
136 // Creating backup file.
137 $bc = new backup_controller(backup::TYPE_1COURSE, $c2->id, backup::FORMAT_MOODLE,
138 backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
139 $bc->execute_plan();
140 $result = $bc->get_results();
141 $this->assertTrue(isset($result['backup_destination']));
142 $c2backupfile = $result['backup_destination']->copy_content_to_temp();
143 $bc->destroy();
c25a5adb 144 unset($bc); // File logging is a mess, we can only try to rely on gc to close handles.
509dd695 145
d90b2eb9
FM
146 $oldcfg = isset($CFG->keeptempdirectoriesonbackup) ? $CFG->keeptempdirectoriesonbackup : false;
147 $CFG->keeptempdirectoriesonbackup = true;
148
509dd695
FM
149 // Checking restore dir.
150 $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
151 $bcinfo = backup_general_helper::get_backup_information($dir);
152 $this->assertEquals($bcinfo->original_course_id, $c1->id);
153 $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
154
155 // Do it again, it should be the same directory.
156 $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
157 $this->assertEquals($dir, $dir2);
158
159 // Get the second course.
160 $dir = tool_uploadcourse_helper::get_restore_content_dir($c2backupfile, null);
161 $bcinfo = backup_general_helper::get_backup_information($dir);
162 $this->assertEquals($bcinfo->original_course_id, $c2->id);
163 $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
164
509dd695
FM
165 // Checking with a shortname.
166 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
167 $bcinfo = backup_general_helper::get_backup_information($dir);
168 $this->assertEquals($bcinfo->original_course_id, $c1->id);
169 $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
170
171 // Do it again, it should be the same directory.
172 $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
173 $this->assertEquals($dir, $dir2);
174
175 // Get the second course.
176 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c2->shortname);
177 $bcinfo = backup_general_helper::get_backup_information($dir);
178 $this->assertEquals($bcinfo->original_course_id, $c2->id);
179 $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
180
f802b04f
FM
181 // Get a course that does not exist.
182 $errors = array();
183 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
184 $this->assertFalse($dir);
185 $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
186
d90b2eb9 187 // Trying again without caching. $CFG->keeptempdirectoriesonbackup is required for caching.
3f57c87e 188 $CFG->keeptempdirectoriesonbackup = false;
3f57c87e 189
d90b2eb9
FM
190 // Checking restore dir.
191 $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
192 $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
193 $this->assertNotEquals($dir, $dir2);
194
195 // Checking with a shortname.
196 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
197 $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
198 $this->assertNotEquals($dir, $dir2);
199
200 // Get a course that does not exist.
201 $errors = array();
202 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
203 $this->assertFalse($dir);
204 $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
205 $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
206 $this->assertEquals($dir, $dir2);
3f57c87e
FM
207
208 $CFG->keeptempdirectoriesonbackup = $oldcfg;
509dd695
FM
209 }
210
211 public function test_get_role_ids() {
212 $this->getDataGenerator();
213 // Mimic function result.
214 $expected = array();
215 $roles = get_all_roles();
216 foreach ($roles as $role) {
217 $expected[$role->shortname] = $role->id;
218 }
219
220 $actual = tool_uploadcourse_helper::get_role_ids();
221 $this->assertSame($actual, $expected);
222
223 // Check cache.
224 $this->assertSame($actual, tool_uploadcourse_helper::get_role_ids());
225 }
226
227 public function test_get_role_names() {
228 $this->resetAfterTest(true);
229
230 create_role('Villain', 'villain', 'The bad guys');
231 $data = array(
232 'role_student' => 'Padawan',
233 'role_teacher' => 'Guardian',
234 'role_editingteacher' => 'Knight',
235 'role_manager' => 'Master',
236 'role_villain' => 'Jabba the Hutt',
237 'role_android' => 'R2D2',
238 );
239
240 // Get the role IDs, but need to force the cache reset as a new role is defined.
241 $roleids = tool_uploadcourse_helper::get_role_ids(true);
242
243 $expected = array(
244 'role_' . $roleids['student'] => 'Padawan',
245 'role_' . $roleids['teacher'] => 'Guardian',
246 'role_' . $roleids['editingteacher'] => 'Knight',
247 'role_' . $roleids['manager'] => 'Master',
248 'role_' . $roleids['villain'] => 'Jabba the Hutt',
249 );
250
1d1898ac
FM
251 $errors = array();
252 $actual = tool_uploadcourse_helper::get_role_names($data, $errors);
509dd695 253 $this->assertSame($actual, $expected);
1d1898ac 254 $this->assertArrayHasKey('invalidroles', $errors);
509dd695
FM
255 }
256
257 public function test_increment_idnumber() {
258 $this->resetAfterTest(true);
259
260 $c1 = $this->getDataGenerator()->create_course(array('idnumber' => 'C1'));
261 $c2 = $this->getDataGenerator()->create_course(array('idnumber' => 'C2'));
262 $c3 = $this->getDataGenerator()->create_course(array('idnumber' => 'Yo'));
263
264 $this->assertEquals('C3', tool_uploadcourse_helper::increment_idnumber('C1'));
265 $this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_idnumber('Yo'));
266 $this->assertEquals('DoesNotExist', tool_uploadcourse_helper::increment_idnumber('DoesNotExist'));
267 }
268
269 public function test_increment_shortname() {
270 $this->resetAfterTest(true);
271
272 $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'C1'));
273 $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'C2'));
274 $c3 = $this->getDataGenerator()->create_course(array('shortname' => 'Yo'));
275
276 // FYI: increment_shortname assumes that the course exists, and so increment the shortname immediately.
277 $this->assertEquals('C3', tool_uploadcourse_helper::increment_shortname('C1'));
278 $this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_shortname('Yo'));
279 $this->assertEquals('DoesNotExist_2', tool_uploadcourse_helper::increment_shortname('DoesNotExist'));
280 }
281
282 public function test_resolve_category() {
283 $this->resetAfterTest(true);
284
285 $c1 = $this->getDataGenerator()->create_category(array('name' => 'First level'));
286 $c2 = $this->getDataGenerator()->create_category(array('name' => 'Second level', 'parent' => $c1->id));
287 $c3 = $this->getDataGenerator()->create_category(array('idnumber' => 'C3'));
288
289 $data = array(
290 'category' => $c1->id,
291 'category_path' => $c1->name . ' / ' . $c2->name,
292 'category_idnumber' => $c3->idnumber,
293 );
294
295 $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category($data));
296 unset($data['category']);
297 $this->assertEquals($c3->id, tool_uploadcourse_helper::resolve_category($data));
298 unset($data['category_idnumber']);
299 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data));
300
301 // Adding unexisting data.
1d1898ac 302 $errors = array();
509dd695 303 $data['category_idnumber'] = 1234;
1d1898ac
FM
304 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
305 $this->assertArrayHasKey('couldnotresolvecatgorybyidnumber', $errors);
306 $errors = array();
509dd695 307 $data['category'] = 1234;
1d1898ac
FM
308 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
309 $this->assertArrayHasKey('couldnotresolvecatgorybyid', $errors);
310 $errors = array();
509dd695 311 $data['category_path'] = 'Not exist';
1d1898ac
FM
312 $this->assertEmpty(tool_uploadcourse_helper::resolve_category($data, $errors));
313 $this->assertArrayHasKey('couldnotresolvecatgorybypath', $errors);
509dd695
FM
314 }
315
316 public function test_resolve_category_by_idnumber() {
317 $this->resetAfterTest(true);
318
319 $c1 = $this->getDataGenerator()->create_category(array('idnumber' => 'C1'));
320 $c2 = $this->getDataGenerator()->create_category(array('idnumber' => 'C2'));
321
322 // Doubled for cache check.
323 $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
324 $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
325 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
326 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
327 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
328 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
329 }
330
331 public function test_resolve_category_by_path() {
332 $this->resetAfterTest(true);
333
334 $cat1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1'));
335 $cat1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1', 'parent' => $cat1->id));
336 $cat1_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.1', 'parent' => $cat1_1->id));
337 $cat1_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.2', 'parent' => $cat1_1->id));
338 $cat1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.2', 'parent' => $cat1->id));
339
340 $cat2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2'));
341 $cat2_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1', 'parent' => $cat2->id, 'visible' => false));
342 $cat2_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.1', 'parent' => $cat2_1->id));
343 $cat2_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.2', 'parent' => $cat2_1->id));
344 $cat2_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.2', 'parent' => $cat2->id));
345
346 $cat3 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3'));
347 $cat3_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
348 $cat3_1b = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
349 $cat3_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3_1->id));
350 $cat3_fakedouble = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3->id));
351
352 // Existing categories. Doubled for cache testing.
353 $path = array('Cat 1');
354 $this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
355 $this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
356
357 $path = array('Cat 1', 'Cat 1.1', 'Cat 1.1.2');
358 $this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
359 $this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
360
361 $path = array('Cat 1', 'Cat 1.2');
362 $this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
363 $this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
364
365 $path = array('Cat 2');
366 $this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
367 $this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
368
369 // Hidden category.
370 $path = array('Cat 2', 'Cat 2.1');
371 $this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
372 $this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
373
374 // Hidden parent.
375 $path = array('Cat 2', 'Cat 2.1', 'Cat 2.1.2');
376 $this->assertEquals($cat2_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
377 $this->assertEquals($cat2_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
378
379 // Does not exist.
380 $path = array('No cat 3', 'Cat 1.2');
381 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
382 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
383
384 $path = array('Cat 2', 'Cat 2.x');
385 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
386 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
387
388 // Name conflict.
389 $path = array('Cat 3', 'Cat 3.1 Doubled');
390 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
391 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
392
393 $path = array('Cat 3', 'Cat 3.1 Doubled', 'Cat 3.1.1');
394 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
395 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
396
397 $path = array('Cat 3', 'Cat 3.1.1');
398 $this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
399 $this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
400 }
401}