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