weekly release 2.6dev
[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 );
104 $this->assertSame(tool_uploadcourse_helper::get_enrolment_data($data), $expected);
105 }
106
3f57c87e 107 public function test_get_enrolment_plugins() {
509dd695
FM
108 $this->resetAfterTest(true);
109 $actual = tool_uploadcourse_helper::get_enrolment_plugins();
110 $this->assertSame(array_keys(enrol_get_plugins(false)), array_keys($actual));
111 // This should be identical as cached.
112 $secondactual = tool_uploadcourse_helper::get_enrolment_plugins();
3f57c87e 113 $this->assertEquals($actual, $secondactual);
509dd695
FM
114 }
115
3f57c87e
FM
116 public function test_get_restore_content_dir() {
117 global $CFG;
509dd695
FM
118 $this->resetAfterTest(true);
119 $this->setAdminUser();
120
121 $c1 = $this->getDataGenerator()->create_course();
122 $c2 = $this->getDataGenerator()->create_course((object) array('shortname' => 'Yay'));
123
124 // Creating backup file.
125 $bc = new backup_controller(backup::TYPE_1COURSE, $c1->id, backup::FORMAT_MOODLE,
126 backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
127 $bc->execute_plan();
128 $result = $bc->get_results();
129 $this->assertTrue(isset($result['backup_destination']));
130 $c1backupfile = $result['backup_destination']->copy_content_to_temp();
131 $bc->destroy();
c25a5adb 132 unset($bc); // File logging is a mess, we can only try to rely on gc to close handles.
509dd695
FM
133
134 // Creating backup file.
135 $bc = new backup_controller(backup::TYPE_1COURSE, $c2->id, backup::FORMAT_MOODLE,
136 backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
137 $bc->execute_plan();
138 $result = $bc->get_results();
139 $this->assertTrue(isset($result['backup_destination']));
140 $c2backupfile = $result['backup_destination']->copy_content_to_temp();
141 $bc->destroy();
c25a5adb 142 unset($bc); // File logging is a mess, we can only try to rely on gc to close handles.
509dd695
FM
143
144 // Checking restore dir.
145 $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
146 $bcinfo = backup_general_helper::get_backup_information($dir);
147 $this->assertEquals($bcinfo->original_course_id, $c1->id);
148 $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
149
150 // Do it again, it should be the same directory.
151 $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
152 $this->assertEquals($dir, $dir2);
153
154 // Get the second course.
155 $dir = tool_uploadcourse_helper::get_restore_content_dir($c2backupfile, null);
156 $bcinfo = backup_general_helper::get_backup_information($dir);
157 $this->assertEquals($bcinfo->original_course_id, $c2->id);
158 $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
159
509dd695
FM
160 // Checking with a shortname.
161 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
162 $bcinfo = backup_general_helper::get_backup_information($dir);
163 $this->assertEquals($bcinfo->original_course_id, $c1->id);
164 $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
165
166 // Do it again, it should be the same directory.
167 $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
168 $this->assertEquals($dir, $dir2);
169
170 // Get the second course.
171 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c2->shortname);
172 $bcinfo = backup_general_helper::get_backup_information($dir);
173 $this->assertEquals($bcinfo->original_course_id, $c2->id);
174 $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
175
f802b04f
FM
176 // Get a course that does not exist.
177 $errors = array();
178 $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
179 $this->assertFalse($dir);
180 $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
181
3f57c87e
FM
182 // Cleaning content directories.
183 $oldcfg = isset($CFG->keeptempdirectoriesonbackup) ? $CFG->keeptempdirectoriesonbackup : false;
184 $dir = "$CFG->tempdir/backup/$dir";
185 $this->assertTrue(file_exists($dir));
186
187 $CFG->keeptempdirectoriesonbackup = false;
188 tool_uploadcourse_helper::clean_restore_content();
189 $this->assertTrue(file_exists($dir));
190
191 $CFG->keeptempdirectoriesonbackup = true;
192 tool_uploadcourse_helper::clean_restore_content();
193 $this->assertFalse(file_exists($dir));
194
195 $CFG->keeptempdirectoriesonbackup = $oldcfg;
196
509dd695
FM
197 // Restore the time limit to prevent warning.
198 set_time_limit(0);
199 }
200
201 public function test_get_role_ids() {
202 $this->getDataGenerator();
203 // Mimic function result.
204 $expected = array();
205 $roles = get_all_roles();
206 foreach ($roles as $role) {
207 $expected[$role->shortname] = $role->id;
208 }
209
210 $actual = tool_uploadcourse_helper::get_role_ids();
211 $this->assertSame($actual, $expected);
212
213 // Check cache.
214 $this->assertSame($actual, tool_uploadcourse_helper::get_role_ids());
215 }
216
217 public function test_get_role_names() {
218 $this->resetAfterTest(true);
219
220 create_role('Villain', 'villain', 'The bad guys');
221 $data = array(
222 'role_student' => 'Padawan',
223 'role_teacher' => 'Guardian',
224 'role_editingteacher' => 'Knight',
225 'role_manager' => 'Master',
226 'role_villain' => 'Jabba the Hutt',
227 'role_android' => 'R2D2',
228 );
229
230 // Get the role IDs, but need to force the cache reset as a new role is defined.
231 $roleids = tool_uploadcourse_helper::get_role_ids(true);
232
233 $expected = array(
234 'role_' . $roleids['student'] => 'Padawan',
235 'role_' . $roleids['teacher'] => 'Guardian',
236 'role_' . $roleids['editingteacher'] => 'Knight',
237 'role_' . $roleids['manager'] => 'Master',
238 'role_' . $roleids['villain'] => 'Jabba the Hutt',
239 );
240
1d1898ac
FM
241 $errors = array();
242 $actual = tool_uploadcourse_helper::get_role_names($data, $errors);
509dd695 243 $this->assertSame($actual, $expected);
1d1898ac 244 $this->assertArrayHasKey('invalidroles', $errors);
509dd695
FM
245 }
246
247 public function test_increment_idnumber() {
248 $this->resetAfterTest(true);
249
250 $c1 = $this->getDataGenerator()->create_course(array('idnumber' => 'C1'));
251 $c2 = $this->getDataGenerator()->create_course(array('idnumber' => 'C2'));
252 $c3 = $this->getDataGenerator()->create_course(array('idnumber' => 'Yo'));
253
254 $this->assertEquals('C3', tool_uploadcourse_helper::increment_idnumber('C1'));
255 $this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_idnumber('Yo'));
256 $this->assertEquals('DoesNotExist', tool_uploadcourse_helper::increment_idnumber('DoesNotExist'));
257 }
258
259 public function test_increment_shortname() {
260 $this->resetAfterTest(true);
261
262 $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'C1'));
263 $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'C2'));
264 $c3 = $this->getDataGenerator()->create_course(array('shortname' => 'Yo'));
265
266 // FYI: increment_shortname assumes that the course exists, and so increment the shortname immediately.
267 $this->assertEquals('C3', tool_uploadcourse_helper::increment_shortname('C1'));
268 $this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_shortname('Yo'));
269 $this->assertEquals('DoesNotExist_2', tool_uploadcourse_helper::increment_shortname('DoesNotExist'));
270 }
271
272 public function test_resolve_category() {
273 $this->resetAfterTest(true);
274
275 $c1 = $this->getDataGenerator()->create_category(array('name' => 'First level'));
276 $c2 = $this->getDataGenerator()->create_category(array('name' => 'Second level', 'parent' => $c1->id));
277 $c3 = $this->getDataGenerator()->create_category(array('idnumber' => 'C3'));
278
279 $data = array(
280 'category' => $c1->id,
281 'category_path' => $c1->name . ' / ' . $c2->name,
282 'category_idnumber' => $c3->idnumber,
283 );
284
285 $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category($data));
286 unset($data['category']);
287 $this->assertEquals($c3->id, tool_uploadcourse_helper::resolve_category($data));
288 unset($data['category_idnumber']);
289 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data));
290
291 // Adding unexisting data.
1d1898ac 292 $errors = array();
509dd695 293 $data['category_idnumber'] = 1234;
1d1898ac
FM
294 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
295 $this->assertArrayHasKey('couldnotresolvecatgorybyidnumber', $errors);
296 $errors = array();
509dd695 297 $data['category'] = 1234;
1d1898ac
FM
298 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
299 $this->assertArrayHasKey('couldnotresolvecatgorybyid', $errors);
300 $errors = array();
509dd695 301 $data['category_path'] = 'Not exist';
1d1898ac
FM
302 $this->assertEmpty(tool_uploadcourse_helper::resolve_category($data, $errors));
303 $this->assertArrayHasKey('couldnotresolvecatgorybypath', $errors);
509dd695
FM
304 }
305
306 public function test_resolve_category_by_idnumber() {
307 $this->resetAfterTest(true);
308
309 $c1 = $this->getDataGenerator()->create_category(array('idnumber' => 'C1'));
310 $c2 = $this->getDataGenerator()->create_category(array('idnumber' => 'C2'));
311
312 // Doubled for cache check.
313 $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
314 $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
315 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
316 $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
317 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
318 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
319 }
320
321 public function test_resolve_category_by_path() {
322 $this->resetAfterTest(true);
323
324 $cat1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1'));
325 $cat1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1', 'parent' => $cat1->id));
326 $cat1_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.1', 'parent' => $cat1_1->id));
327 $cat1_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.2', 'parent' => $cat1_1->id));
328 $cat1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.2', 'parent' => $cat1->id));
329
330 $cat2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2'));
331 $cat2_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1', 'parent' => $cat2->id, 'visible' => false));
332 $cat2_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.1', 'parent' => $cat2_1->id));
333 $cat2_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.2', 'parent' => $cat2_1->id));
334 $cat2_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.2', 'parent' => $cat2->id));
335
336 $cat3 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3'));
337 $cat3_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
338 $cat3_1b = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
339 $cat3_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3_1->id));
340 $cat3_fakedouble = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3->id));
341
342 // Existing categories. Doubled for cache testing.
343 $path = array('Cat 1');
344 $this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
345 $this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
346
347 $path = array('Cat 1', 'Cat 1.1', 'Cat 1.1.2');
348 $this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
349 $this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
350
351 $path = array('Cat 1', 'Cat 1.2');
352 $this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
353 $this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
354
355 $path = array('Cat 2');
356 $this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
357 $this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
358
359 // Hidden category.
360 $path = array('Cat 2', 'Cat 2.1');
361 $this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
362 $this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
363
364 // Hidden parent.
365 $path = array('Cat 2', 'Cat 2.1', 'Cat 2.1.2');
366 $this->assertEquals($cat2_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
367 $this->assertEquals($cat2_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
368
369 // Does not exist.
370 $path = array('No cat 3', 'Cat 1.2');
371 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
372 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
373
374 $path = array('Cat 2', 'Cat 2.x');
375 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
376 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
377
378 // Name conflict.
379 $path = array('Cat 3', 'Cat 3.1 Doubled');
380 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
381 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
382
383 $path = array('Cat 3', 'Cat 3.1 Doubled', 'Cat 3.1.1');
384 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
385 $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
386
387 $path = array('Cat 3', 'Cat 3.1.1');
388 $this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
389 $this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
390 }
391}