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