b10cdaf6c64c40fb434fec641d8f7d0748e47735
[moodle.git] / admin / tool / uploadcourse / tests / helper_test.php
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/>.
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  */
25 defined('MOODLE_INTERNAL') || die();
27 global $CFG;
29 /**
30  * Helper test case.
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
35  */
36 class tool_uploadcourse_helper_testcase extends advanced_testcase {
38     public function test_generate_shortname() {
39         $data = (object) array('fullname' => 'Ah bh Ch 01 02 03', 'idnumber' => 'ID123');
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('AH BH CH', tool_uploadcourse_helper::generate_shortname($data, '%+8f'));
45         $this->assertSame('id123', tool_uploadcourse_helper::generate_shortname($data, '%-i'));
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'));
50         $this->assertNull(tool_uploadcourse_helper::generate_shortname($data, ''));
51         $this->assertNull(tool_uploadcourse_helper::generate_shortname(array(), '%f'));
52     }
54     public function test_get_course_formats() {
55         $result = tool_uploadcourse_helper::get_course_formats();
56         $this->assertSame(array_keys(core_component::get_plugin_list('format')), $result);
57         // Should be similar as first result, as cached.
58         $this->assertSame($result, tool_uploadcourse_helper::get_course_formats());
59     }
61     public function test_get_enrolment_data() {
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             'enrolment_6' => '',
83         );
84         $expected = array(
85             'self' => array(
86                 'delete' => '1',
87                 'foo' => 'a',
88                 'bar' => '1',
89             ),
90             'manual' => array(
91                 'disable' => '2',
92                 'foo' => 'b',
93                 'bar' => '2',
94             ),
95             'database' => array(
96                 'foo' => 'x',
97                 'bar' => '3',
98             ),
99             'flatfile' => array(
100                 'test3' => 'test3',
101                 'test2' => 'test2',
102                 'test1' => 'test1',
103             )
104         );
105         $errors = array();
106         $this->assertSame(tool_uploadcourse_helper::get_enrolment_data($data, $errors), $expected);
107         $this->assertArrayHasKey('unknownenrolmentmethods', $errors);
108     }
110     public function test_get_enrolment_plugins() {
111         $this->resetAfterTest(true);
112         $actual = tool_uploadcourse_helper::get_enrolment_plugins();
113         $this->assertSame(array_keys(enrol_get_plugins(false)), array_keys($actual));
114         // This should be identical as cached.
115         $secondactual = tool_uploadcourse_helper::get_enrolment_plugins();
116         $this->assertEquals($actual, $secondactual);
117     }
119     public function test_get_restore_content_dir() {
120         global $CFG;
121         $this->resetAfterTest(true);
122         $this->setAdminUser();
124         $c1 = $this->getDataGenerator()->create_course();
125         $c2 = $this->getDataGenerator()->create_course((object) array('shortname' => 'Yay'));
127         // Creating backup file.
128         $bc = new backup_controller(backup::TYPE_1COURSE, $c1->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         $c1backupfile = $result['backup_destination']->copy_content_to_temp();
134         $bc->destroy();
135         unset($bc); // File logging is a mess, we can only try to rely on gc to close handles.
137         // Creating backup file.
138         $bc = new backup_controller(backup::TYPE_1COURSE, $c2->id, backup::FORMAT_MOODLE,
139             backup::INTERACTIVE_NO, backup::MODE_GENERAL, 2);
140         $bc->execute_plan();
141         $result = $bc->get_results();
142         $this->assertTrue(isset($result['backup_destination']));
143         $c2backupfile = $result['backup_destination']->copy_content_to_temp();
144         $bc->destroy();
145         unset($bc); // File logging is a mess, we can only try to rely on gc to close handles.
147         $oldcfg = isset($CFG->keeptempdirectoriesonbackup) ? $CFG->keeptempdirectoriesonbackup : false;
148         $CFG->keeptempdirectoriesonbackup = true;
150         // Checking restore dir.
151         $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
152         $bcinfo = backup_general_helper::get_backup_information($dir);
153         $this->assertEquals($bcinfo->original_course_id, $c1->id);
154         $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
156         // Do it again, it should be the same directory.
157         $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
158         $this->assertEquals($dir, $dir2);
160         // Get the second course.
161         $dir = tool_uploadcourse_helper::get_restore_content_dir($c2backupfile, null);
162         $bcinfo = backup_general_helper::get_backup_information($dir);
163         $this->assertEquals($bcinfo->original_course_id, $c2->id);
164         $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
166         // Checking with a shortname.
167         $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
168         $bcinfo = backup_general_helper::get_backup_information($dir);
169         $this->assertEquals($bcinfo->original_course_id, $c1->id);
170         $this->assertEquals($bcinfo->original_course_fullname, $c1->fullname);
172         // Do it again, it should be the same directory.
173         $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
174         $this->assertEquals($dir, $dir2);
176         // Get the second course.
177         $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c2->shortname);
178         $bcinfo = backup_general_helper::get_backup_information($dir);
179         $this->assertEquals($bcinfo->original_course_id, $c2->id);
180         $this->assertEquals($bcinfo->original_course_fullname, $c2->fullname);
182         // Get a course that does not exist.
183         $errors = array();
184         $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
185         $this->assertFalse($dir);
186         $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
188         // Trying again without caching. $CFG->keeptempdirectoriesonbackup is required for caching.
189         $CFG->keeptempdirectoriesonbackup = false;
191         // Checking restore dir.
192         $dir = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
193         $dir2 = tool_uploadcourse_helper::get_restore_content_dir($c1backupfile, null);
194         $this->assertNotEquals($dir, $dir2);
196         // Checking with a shortname.
197         $dir = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
198         $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, $c1->shortname);
199         $this->assertNotEquals($dir, $dir2);
201         // Get a course that does not exist.
202         $errors = array();
203         $dir = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
204         $this->assertFalse($dir);
205         $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $errors);
206         $dir2 = tool_uploadcourse_helper::get_restore_content_dir(null, 'DoesNotExist', $errors);
207         $this->assertEquals($dir, $dir2);
209         $CFG->keeptempdirectoriesonbackup = $oldcfg;
210     }
212     public function test_get_role_ids() {
213         $this->getDataGenerator();
214         // Mimic function result.
215         $expected = array();
216         $roles = get_all_roles();
217         foreach ($roles as $role) {
218             $expected[$role->shortname] = $role->id;
219         }
221         $actual = tool_uploadcourse_helper::get_role_ids();
222         $this->assertSame($actual, $expected);
224         // Check cache.
225         $this->assertSame($actual, tool_uploadcourse_helper::get_role_ids());
226     }
228     public function test_get_role_names() {
229         $this->resetAfterTest(true);
231         create_role('Villain', 'villain', 'The bad guys');
232         $data = array(
233             'role_student' => 'Padawan',
234             'role_teacher' => 'Guardian',
235             'role_editingteacher' => 'Knight',
236             'role_manager' => 'Master',
237             'role_villain' => 'Jabba the Hutt',
238             'role_android' => 'R2D2',
239         );
241         // Get the role IDs, but need to force the cache reset as a new role is defined.
242         $roleids = tool_uploadcourse_helper::get_role_ids(true);
244         $expected = array(
245             'role_' . $roleids['student'] => 'Padawan',
246             'role_' . $roleids['teacher'] => 'Guardian',
247             'role_' . $roleids['editingteacher'] => 'Knight',
248             'role_' . $roleids['manager'] => 'Master',
249             'role_' . $roleids['villain'] => 'Jabba the Hutt',
250         );
252         $errors = array();
253         $actual = tool_uploadcourse_helper::get_role_names($data, $errors);
254         $this->assertSame($actual, $expected);
255         $this->assertArrayHasKey('invalidroles', $errors);
256     }
258     public function test_increment_idnumber() {
259         $this->resetAfterTest(true);
261         $c1 = $this->getDataGenerator()->create_course(array('idnumber' => 'C1'));
262         $c2 = $this->getDataGenerator()->create_course(array('idnumber' => 'C2'));
263         $c3 = $this->getDataGenerator()->create_course(array('idnumber' => 'Yo'));
265         $this->assertEquals('C3', tool_uploadcourse_helper::increment_idnumber('C1'));
266         $this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_idnumber('Yo'));
267         $this->assertEquals('DoesNotExist', tool_uploadcourse_helper::increment_idnumber('DoesNotExist'));
268     }
270     public function test_increment_shortname() {
271         $this->resetAfterTest(true);
273         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'C1'));
274         $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'C2'));
275         $c3 = $this->getDataGenerator()->create_course(array('shortname' => 'Yo'));
277         // FYI: increment_shortname assumes that the course exists, and so increment the shortname immediately.
278         $this->assertEquals('C3', tool_uploadcourse_helper::increment_shortname('C1'));
279         $this->assertEquals('Yo_2', tool_uploadcourse_helper::increment_shortname('Yo'));
280         $this->assertEquals('DoesNotExist_2', tool_uploadcourse_helper::increment_shortname('DoesNotExist'));
281     }
283     public function test_resolve_category() {
284         $this->resetAfterTest(true);
286         $c1 = $this->getDataGenerator()->create_category(array('name' => 'First level'));
287         $c2 = $this->getDataGenerator()->create_category(array('name' => 'Second level', 'parent' => $c1->id));
288         $c3 = $this->getDataGenerator()->create_category(array('idnumber' => 'C3'));
290         $data = array(
291             'category' => $c1->id,
292             'category_path' => $c1->name . ' / ' . $c2->name,
293             'category_idnumber' => $c3->idnumber,
294         );
296         $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category($data));
297         unset($data['category']);
298         $this->assertEquals($c3->id, tool_uploadcourse_helper::resolve_category($data));
299         unset($data['category_idnumber']);
300         $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data));
302         // Adding unexisting data.
303         $errors = array();
304         $data['category_idnumber'] = 1234;
305         $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
306         $this->assertArrayHasKey('couldnotresolvecatgorybyidnumber', $errors);
307         $errors = array();
308         $data['category'] = 1234;
309         $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category($data, $errors));
310         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $errors);
311         $errors = array();
312         $data['category_path'] = 'Not exist';
313         $this->assertEmpty(tool_uploadcourse_helper::resolve_category($data, $errors));
314         $this->assertArrayHasKey('couldnotresolvecatgorybypath', $errors);
315     }
317     public function test_resolve_category_by_idnumber() {
318         $this->resetAfterTest(true);
320         $c1 = $this->getDataGenerator()->create_category(array('idnumber' => 'C1'));
321         $c2 = $this->getDataGenerator()->create_category(array('idnumber' => 'C2'));
323         // Doubled for cache check.
324         $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
325         $this->assertEquals($c1->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C1'));
326         $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
327         $this->assertEquals($c2->id, tool_uploadcourse_helper::resolve_category_by_idnumber('C2'));
328         $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
329         $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_idnumber('DoesNotExist'));
330     }
332     public function test_resolve_category_by_path() {
333         $this->resetAfterTest(true);
335         $cat1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1'));
336         $cat1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1', 'parent' => $cat1->id));
337         $cat1_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.1', 'parent' => $cat1_1->id));
338         $cat1_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.1.2', 'parent' => $cat1_1->id));
339         $cat1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 1.2', 'parent' => $cat1->id));
341         $cat2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2'));
342         $cat2_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1', 'parent' => $cat2->id, 'visible' => false));
343         $cat2_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.1', 'parent' => $cat2_1->id));
344         $cat2_1_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.1.2', 'parent' => $cat2_1->id));
345         $cat2_2 = $this->getDataGenerator()->create_category(array('name' => 'Cat 2.2', 'parent' => $cat2->id));
347         $cat3 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3'));
348         $cat3_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
349         $cat3_1b = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1 Doubled', 'parent' => $cat3->id));
350         $cat3_1_1 = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3_1->id));
351         $cat3_fakedouble = $this->getDataGenerator()->create_category(array('name' => 'Cat 3.1.1', 'parent' => $cat3->id));
353         // Existing categories. Doubled for cache testing.
354         $path = array('Cat 1');
355         $this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
356         $this->assertEquals($cat1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
358         $path = array('Cat 1', 'Cat 1.1', 'Cat 1.1.2');
359         $this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
360         $this->assertEquals($cat1_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
362         $path = array('Cat 1', 'Cat 1.2');
363         $this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
364         $this->assertEquals($cat1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
366         $path = array('Cat 2');
367         $this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
368         $this->assertEquals($cat2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
370         // Hidden category.
371         $path = array('Cat 2', 'Cat 2.1');
372         $this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
373         $this->assertEquals($cat2_1->id, tool_uploadcourse_helper::resolve_category_by_path($path));
375         // Hidden parent.
376         $path = array('Cat 2', 'Cat 2.1', 'Cat 2.1.2');
377         $this->assertEquals($cat2_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
378         $this->assertEquals($cat2_1_2->id, tool_uploadcourse_helper::resolve_category_by_path($path));
380         // Does not exist.
381         $path = array('No cat 3', 'Cat 1.2');
382         $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
383         $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
385         $path = array('Cat 2', 'Cat 2.x');
386         $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
387         $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
389         // Name conflict.
390         $path = array('Cat 3', 'Cat 3.1 Doubled');
391         $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
392         $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
394         $path = array('Cat 3', 'Cat 3.1 Doubled', 'Cat 3.1.1');
395         $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
396         $this->assertEmpty(tool_uploadcourse_helper::resolve_category_by_path($path));
398         $path = array('Cat 3', 'Cat 3.1.1');
399         $this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
400         $this->assertEquals($cat3_fakedouble->id, tool_uploadcourse_helper::resolve_category_by_path($path));
401     }