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