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