33561636c7e0b8fb2e1afd8d8a1ef7c6519c0543
[moodle.git] / admin / tool / uploadcourse / tests / course_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 course class.
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  * Course 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 late
35  */
36 class tool_uploadcourse_course_testcase extends advanced_testcase {
38     /**
39      * @expectedException coding_exception
40      */
41     public function test_proceed_without_prepare() {
42         $this->resetAfterTest(true);
43         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
44         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
45         $data = array();
46         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
47         $co->proceed();
48     }
50     /**
51      * @expectedException moodle_exception
52      */
53     public function test_proceed_when_prepare_failed() {
54         $this->resetAfterTest(true);
55         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
56         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
57         $data = array();
58         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
59         $this->assertFalse($co->prepare());
60         $co->proceed();
61     }
63     public function test_proceed_when_already_started() {
64         $this->resetAfterTest(true);
65         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
66         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
67         $data = array('shortname' => 'test', 'fullname' => 'New course', 'summary' => 'New', 'category' => 1);
68         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
69         $this->assertTrue($co->prepare());
70         $co->proceed();
71         $this->expectException('coding_exception');
72         $co->proceed();
73     }
75     public function test_invalid_shortname() {
76         $this->resetAfterTest(true);
77         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
78         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
79         $data = array('shortname' => '<invalid>', 'fullname' => 'New course', 'summary' => 'New', 'category' => 1);
80         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
81         $this->assertFalse($co->prepare());
82         $this->assertArrayHasKey('invalidshortname', $co->get_errors());
83     }
85     public function test_create() {
86         global $DB;
87         $this->resetAfterTest(true);
89         // Existing course.
90         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1', 'summary' => 'Yay!'));
91         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
93         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
95         // Try to add a new course.
96         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
97         $data = array('shortname' => 'newcourse', 'fullname' => 'New course', 'summary' => 'New', 'category' => 1);
98         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
99         $this->assertTrue($co->prepare());
100         $this->assertFalse($DB->record_exists('course', array('shortname' => 'newcourse')));
101         $co->proceed();
102         $this->assertTrue($DB->record_exists('course', array('shortname' => 'newcourse')));
104         // Try to add a new course, that already exists.
105         $coursecount = $DB->count_records('course', array());
106         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
107         $data = array('shortname' => 'c1', 'fullname' => 'C1FN', 'summary' => 'C1', 'category' => 1);
108         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
109         $this->assertFalse($co->prepare());
110         $this->assertArrayHasKey('courseexistsanduploadnotallowed', $co->get_errors());
111         $this->assertEquals($coursecount, $DB->count_records('course', array()));
112         $this->assertNotEquals('C1', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
114         // Try to add new with shortname incrementation.
115         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
116         $data = array('shortname' => 'c1', 'fullname' => 'C1FN', 'summary' => 'C1', 'category' => 1);
117         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
118         $this->assertTrue($co->prepare());
119         $co->proceed();
120         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c2')));
121     }
123     public function test_create_with_sections() {
124         global $DB;
125         $this->resetAfterTest(true);
126         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
127         $defaultnumsections = get_config('moodlecourse', 'numsections');
129         // Add new course, make sure default number of sections is created.
130         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
131         $data = array('shortname' => 'newcourse1', 'fullname' => 'New course1', 'format' => 'topics', 'category' => 1);
132         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
133         $this->assertTrue($co->prepare());
134         $co->proceed();
135         $courseid = $DB->get_field('course', 'id', array('shortname' => 'newcourse1'));
136         $this->assertNotEmpty($courseid);
137         $this->assertEquals($defaultnumsections + 1,
138             $DB->count_records('course_sections', ['course' => $courseid]));
140         // Add new course specifying number of sections.
141         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
142         $data = array('shortname' => 'newcourse2', 'fullname' => 'New course2', 'format' => 'topics', 'category' => 1,
143             'numsections' => 15);
144         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
145         $this->assertTrue($co->prepare());
146         $co->proceed();
147         $courseid = $DB->get_field('course', 'id', array('shortname' => 'newcourse2'));
148         $this->assertNotEmpty($courseid);
149         $this->assertEquals(15 + 1,
150             $DB->count_records('course_sections', ['course' => $courseid]));
151     }
153     public function test_delete() {
154         global $DB;
155         $this->resetAfterTest(true);
157         $c1 = $this->getDataGenerator()->create_course();
158         $c2 = $this->getDataGenerator()->create_course();
160         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
161         $this->assertFalse($DB->record_exists('course', array('shortname' => 'DoesNotExist')));
163         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
164         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
166         // Try delete when option not available.
167         $importoptions = array('candelete' => false);
168         $data = array('shortname' => $c1->shortname, 'delete' => 1);
169         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
170         $this->assertFalse($co->prepare());
171         $this->assertArrayHasKey('coursedeletionnotallowed', $co->get_errors());
172         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
174         // Try delete when not requested.
175         $importoptions = array('candelete' => true);
176         $data = array('shortname' => $c1->shortname, 'delete' => 0);
177         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
178         $this->assertTrue($co->prepare());
179         $co->proceed();
180         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
182         // Try delete when requested.
183         $importoptions = array('candelete' => true);
184         $data = array('shortname' => $c1->shortname, 'delete' => 1);
185         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
186         $this->assertTrue($co->prepare());
187         $co->proceed();
188         $this->assertFalse($DB->record_exists('course', array('shortname' => $c1->shortname)));
189         $this->assertTrue($DB->record_exists('course', array('shortname' => $c2->shortname)));
191         // Try deleting non-existing record, this should not fail.
192         $data = array('shortname' => 'DoesNotExist', 'delete' => 1);
193         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
194         $this->assertFalse($co->prepare());
195         $this->assertArrayHasKey('cannotdeletecoursenotexist', $co->get_errors());
196     }
198     public function test_update() {
199         global $DB;
200         $this->resetAfterTest(true);
202         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1'));
204         // Try to update with existing shortnames, not allowing creation, and updating nothing.
205         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
206         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
207         $data = array('shortname' => 'c1', 'fullname' => 'New fullname');
208         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
209         $this->assertFalse($co->prepare());
210         $this->assertArrayHasKey('updatemodedoessettonothing', $co->get_errors());
212         // Try to update with non-existing shortnames.
213         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
214         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
215         $data = array('shortname' => 'DoesNotExist', 'fullname' => 'New fullname');
216         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
217         $this->assertFalse($co->prepare());
218         $this->assertArrayHasKey('coursedoesnotexistandcreatenotallowed', $co->get_errors());
220         // Try a proper update.
221         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
222         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
223         $data = array('shortname' => 'c1', 'fullname' => 'New fullname');
224         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
225         $this->assertTrue($co->prepare());
226         $co->proceed();
227         $this->assertEquals('New fullname', $DB->get_field_select('course', 'fullname', 'shortname = :s', array('s' => 'c1')));
229         // Try a proper update with defaults.
230         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
231         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
232         $data = array('shortname' => 'c1', 'fullname' => 'Another fullname');
233         $defaults = array('fullname' => 'Not this one', 'summary' => 'Awesome summary');
234         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
235         $this->assertTrue($co->prepare());
236         $co->proceed();
237         $this->assertEquals('Another fullname', $DB->get_field_select('course', 'fullname', 'shortname = :s', array('s' => 'c1')));
238         $this->assertEquals('Awesome summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
240         // Try a proper update missing only.
241         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
242         $updatemode = tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS;
243         $DB->set_field('course', 'summary', '', array('shortname' => 'c1'));
244         $this->assertEquals('', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
245         $data = array('shortname' => 'c1', 'summary' => 'Fill in summary');
246         $defaults = array('summary' => 'Do not use this summary');
247         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
248         $this->assertTrue($co->prepare());
249         $co->proceed();
250         $this->assertEquals('Fill in summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
252         // Try a proper update missing only using defaults.
253         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
254         $updatemode = tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS;
255         $DB->set_field('course', 'summary', '', array('shortname' => 'c1'));
256         $this->assertEquals('', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
257         $data = array('shortname' => 'c1');
258         $defaults = array('summary' => 'Use this summary');
259         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
260         $this->assertTrue($co->prepare());
261         $co->proceed();
262         $this->assertEquals('Use this summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
263     }
265     public function test_data_saved() {
266         global $DB;
267         $this->resetAfterTest(true);
269         // Create.
270         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
271         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
272         $data = array(
273             'shortname' => 'c1',
274             'fullname' => 'Fullname',
275             'category' => '1',
276             'visible' => '0',
277             'idnumber' => '123abc',
278             'summary' => 'Summary',
279             'format' => 'topics',
280             'theme' => 'afterburner',
281             'lang' => 'en',
282             'newsitems' => '7',
283             'showgrades' => '0',
284             'showreports' => '1',
285             'legacyfiles' => '1',
286             'maxbytes' => '1234',
287             'groupmode' => '2',
288             'groupmodeforce' => '1',
289             'enablecompletion' => '1',
290             'tags' => 'Cat, Dog',
292             'role_teacher' => 'Knight',
293             'role_manager' => 'Jedi',
295             'enrolment_1' => 'guest',
296             'enrolment_2' => 'self',
297             'enrolment_2_roleid' => '1',
298             'enrolment_3' => 'manual',
299             'enrolment_3_disable' => '1',
300         );
302         // There should be a start date if there is a end date.
303         $data['enddate'] = '7 June 1990';
304         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
305         $this->assertFalse($co->prepare());
306         $this->assertArrayHasKey('nostartdatenoenddate', $co->get_errors());
308         $data['startdate'] = '8 June 1990';
309         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
310         $this->assertFalse($co->prepare());
311         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
313         // They are correct now.
314         $data['enddate'] = '18 June 1990';
316         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
317         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
318         $this->assertTrue($co->prepare());
319         $co->proceed();
320         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
321         $course = $DB->get_record('course', array('shortname' => 'c1'));
322         $ctx = context_course::instance($course->id);
324         $this->assertEquals($data['fullname'], $course->fullname);
325         $this->assertEquals($data['category'], $course->category);
326         $this->assertEquals($data['visible'], $course->visible);
327         $this->assertEquals(mktime(0, 0, 0, 6, 8, 1990), $course->startdate);
328         $this->assertEquals(mktime(0, 0, 0, 6, 18, 1990), $course->enddate);
329         $this->assertEquals($data['idnumber'], $course->idnumber);
330         $this->assertEquals($data['summary'], $course->summary);
331         $this->assertEquals($data['format'], $course->format);
332         $this->assertEquals($data['theme'], $course->theme);
333         $this->assertEquals($data['lang'], $course->lang);
334         $this->assertEquals($data['newsitems'], $course->newsitems);
335         $this->assertEquals($data['showgrades'], $course->showgrades);
336         $this->assertEquals($data['showreports'], $course->showreports);
337         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
338         $this->assertEquals($data['maxbytes'], $course->maxbytes);
339         $this->assertEquals($data['groupmode'], $course->groupmode);
340         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
341         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
342         $this->assertEquals($data['tags'], join(', ', core_tag_tag::get_item_tags_array('core', 'course', $course->id)));
344         // Roles.
345         $roleids = array();
346         $roles = get_all_roles();
347         foreach ($roles as $role) {
348             $roleids[$role->shortname] = $role->id;
349         }
350         $this->assertEquals('Knight', $DB->get_field_select('role_names', 'name',
351             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
352         $this->assertEquals('Jedi', $DB->get_field_select('role_names', 'name',
353             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
355         // Enrolment methods.
356         $enroldata = array();
357         $instances = enrol_get_instances($course->id, false);
358         $this->assertCount(3, $instances);
359         foreach ($instances as $instance) {
360             $enroldata[$instance->enrol] = $instance;
361         }
363         $this->assertNotEmpty($enroldata['guest']);
364         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['guest']->status);
365         $this->assertNotEmpty($enroldata['self']);
366         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
367         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
368         $this->assertNotEmpty($enroldata['manual']);
369         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['manual']->status);
371         // Update existing course.
372         $cat = $this->getDataGenerator()->create_category();
373         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
374         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
375         $data = array(
376             'shortname' => 'c1',
377             'fullname' => 'Fullname 2',
378             'category' => $cat->id,
379             'visible' => '1',
380             'idnumber' => 'changeidn',
381             'summary' => 'Summary 2',
382             'format' => 'topics',
383             'theme' => 'clean',
384             'lang' => '',
385             'newsitems' => '2',
386             'showgrades' => '1',
387             'showreports' => '0',
388             'legacyfiles' => '0',
389             'maxbytes' => '4321',
390             'groupmode' => '1',
391             'groupmodeforce' => '0',
392             'enablecompletion' => '0',
394             'role_teacher' => 'Teacher',
395             'role_manager' => 'Manager',
397             'enrolment_1' => 'guest',
398             'enrolment_1_disable' => '1',
399             'enrolment_2' => 'self',
400             'enrolment_2_roleid' => '2',
401             'enrolment_3' => 'manual',
402             'enrolment_3_delete' => '1',
403         );
405         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
407         $data['enddate'] = '31 June 1984';
408         // Previous start and end dates are 8 and 18 June 1990.
409         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
410         $this->assertFalse($co->prepare());
411         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
413         $data['startdate'] = '19 June 1990';
414         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
415         $this->assertFalse($co->prepare());
416         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
418         // They are correct now.
419         $data['startdate'] = '11 June 1984';
421         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
422         $this->assertTrue($co->prepare());
423         $co->proceed();
424         $course = $DB->get_record('course', array('shortname' => 'c1'));
425         $ctx = context_course::instance($course->id);
427         $this->assertEquals($data['fullname'], $course->fullname);
428         $this->assertEquals($data['category'], $course->category);
429         $this->assertEquals($data['visible'], $course->visible);
430         $this->assertEquals(mktime(0, 0, 0, 6, 11, 1984), $course->startdate);
431         $this->assertEquals(mktime(0, 0, 0, 6, 31, 1984), $course->enddate);
432         $this->assertEquals($data['idnumber'], $course->idnumber);
433         $this->assertEquals($data['summary'], $course->summary);
434         $this->assertEquals($data['format'], $course->format);
435         $this->assertEquals($data['theme'], $course->theme);
436         $this->assertEquals($data['lang'], $course->lang);
437         $this->assertEquals($data['newsitems'], $course->newsitems);
438         $this->assertEquals($data['showgrades'], $course->showgrades);
439         $this->assertEquals($data['showreports'], $course->showreports);
440         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
441         $this->assertEquals($data['maxbytes'], $course->maxbytes);
442         $this->assertEquals($data['groupmode'], $course->groupmode);
443         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
444         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
446         // Roles.
447         $roleids = array();
448         $roles = get_all_roles();
449         foreach ($roles as $role) {
450             $roleids[$role->shortname] = $role->id;
451         }
452         $this->assertEquals('Teacher', $DB->get_field_select('role_names', 'name',
453             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
454         $this->assertEquals('Manager', $DB->get_field_select('role_names', 'name',
455             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
457         // Enrolment methods.
458         $enroldata = array();
459         $instances = enrol_get_instances($course->id, false);
460         $this->assertCount(2, $instances);
461         foreach ($instances as $instance) {
462             $enroldata[$instance->enrol] = $instance;
463         }
465         $this->assertNotEmpty($enroldata['guest']);
466         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['guest']->status);
467         $this->assertNotEmpty($enroldata['self']);
468         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
469         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
470     }
472     public function test_default_data_saved() {
473         global $DB;
474         $this->resetAfterTest(true);
476         // Create.
477         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
478         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
479         $data = array(
480             'shortname' => 'c1',
481         );
482         $defaultdata = array(
483             'fullname' => 'Fullname',
484             'category' => '1',
485             'visible' => '0',
486             'startdate' => 644803200,
487             'enddate' => 645667200,
488             'idnumber' => '123abc',
489             'summary' => 'Summary',
490             'format' => 'topics',
491             'theme' => 'afterburner',
492             'lang' => 'en',
493             'newsitems' => '7',
494             'showgrades' => '0',
495             'showreports' => '1',
496             'legacyfiles' => '1',
497             'maxbytes' => '1234',
498             'groupmode' => '2',
499             'groupmodeforce' => '1',
500             'enablecompletion' => '1',
501         );
503         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
504         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
505         $this->assertTrue($co->prepare());
506         $co->proceed();
507         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
508         $course = $DB->get_record('course', array('shortname' => 'c1'));
509         $ctx = context_course::instance($course->id);
511         $this->assertEquals($defaultdata['fullname'], $course->fullname);
512         $this->assertEquals($defaultdata['category'], $course->category);
513         $this->assertEquals($defaultdata['visible'], $course->visible);
514         $this->assertEquals($defaultdata['startdate'], $course->startdate);
515         $this->assertEquals($defaultdata['enddate'], $course->enddate);
516         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
517         $this->assertEquals($defaultdata['summary'], $course->summary);
518         $this->assertEquals($defaultdata['format'], $course->format);
519         $this->assertEquals($defaultdata['theme'], $course->theme);
520         $this->assertEquals($defaultdata['lang'], $course->lang);
521         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
522         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
523         $this->assertEquals($defaultdata['showreports'], $course->showreports);
524         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
525         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
526         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
527         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
528         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
530         // Update.
531         $cat = $this->getDataGenerator()->create_category();
532         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
533         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
534         $data = array(
535             'shortname' => 'c1',
536         );
537         $defaultdata = array(
538             'fullname' => 'Fullname 2',
539             'category' => $cat->id,
540             'visible' => '1',
541             'startdate' => 455760000,
542             'enddate' => 457488000,
543             'idnumber' => 'changedid',
544             'summary' => 'Summary 2',
545             'format' => 'topics',
546             'theme' => 'clean',
547             'lang' => '',
548             'newsitems' => '2',
549             'showgrades' => '1',
550             'showreports' => '0',
551             'legacyfiles' => '0',
552             'maxbytes' => '1111',
553             'groupmode' => '1',
554             'groupmodeforce' => '0',
555             'enablecompletion' => '0',
556         );
558         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
559         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
560         $this->assertTrue($co->prepare());
561         $co->proceed();
562         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
563         $course = $DB->get_record('course', array('shortname' => 'c1'));
564         $ctx = context_course::instance($course->id);
566         $this->assertEquals($defaultdata['fullname'], $course->fullname);
567         $this->assertEquals($defaultdata['category'], $course->category);
568         $this->assertEquals($defaultdata['visible'], $course->visible);
569         $this->assertEquals($defaultdata['startdate'], $course->startdate);
570         $this->assertEquals($defaultdata['enddate'], $course->enddate);
571         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
572         $this->assertEquals($defaultdata['summary'], $course->summary);
573         $this->assertEquals($defaultdata['format'], $course->format);
574         $this->assertEquals($defaultdata['theme'], $course->theme);
575         $this->assertEquals($defaultdata['lang'], $course->lang);
576         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
577         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
578         $this->assertEquals($defaultdata['showreports'], $course->showreports);
579         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
580         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
581         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
582         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
583         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
584     }
586     public function test_rename() {
587         global $DB;
588         $this->resetAfterTest(true);
590         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1'));
591         $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'c2'));
593         // Cannot rename when creating.
594         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
595         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
596         $importoptions = array('canrename' => true);
597         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
598         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
599         $this->assertFalse($co->prepare());
600         $this->assertArrayHasKey('courseexistsanduploadnotallowed', $co->get_errors());
602         // Cannot rename when creating.
603         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
604         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
605         $importoptions = array('canrename' => true);
606         $data = array('shortname' => 'c1', 'rename' => 'newshortname', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
607         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
608         $this->assertFalse($co->prepare());
609         $this->assertArrayHasKey('canonlyrenameinupdatemode', $co->get_errors());
611         // Error when not allowed to rename the course.
612         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
613         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
614         $importoptions = array('canrename' => false);
615         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
616         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
617         $this->assertFalse($co->prepare());
618         $this->assertArrayHasKey('courserenamingnotallowed', $co->get_errors());
620         // Can rename when updating.
621         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
622         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
623         $importoptions = array('canrename' => true);
624         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
625         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
626         $this->assertTrue($co->prepare());
627         $co->proceed();
628         $this->assertEquals('newshortname', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
630         // Can rename when updating.
631         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
632         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
633         $importoptions = array('canrename' => true);
634         $data = array('shortname' => 'newshortname', 'rename' => 'newshortname2');
635         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
636         $this->assertTrue($co->prepare());
637         $co->proceed();
638         $this->assertEquals('newshortname2', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
640         // Error when course does not exist.
641         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
642         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
643         $importoptions = array('canrename' => true);
644         $data = array('shortname' => 'DoesNotExist', 'rename' => 'c1', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
645         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
646         $this->assertFalse($co->prepare());
647         $this->assertArrayHasKey('cannotrenamecoursenotexist', $co->get_errors());
649         // Renaming still updates the other values.
650         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
651         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
652         $importoptions = array('canrename' => true);
653         $data = array('shortname' => 'newshortname2', 'rename' => 'c1', 'fullname' => 'Another fullname!');
654         $defaultdata = array('summary' => 'New summary!');
655         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata, $importoptions);
656         $this->assertTrue($co->prepare());
657         $co->proceed();
658         $this->assertEquals('c1', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
659         $this->assertEquals('New summary!', $DB->get_field_select('course', 'summary', 'id = :id', array('id' => $c1->id)));
660         $this->assertEquals('Another fullname!', $DB->get_field_select('course', 'fullname', 'id = :id', array('id' => $c1->id)));
662         // Renaming with invalid shortname.
663         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
664         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
665         $importoptions = array('canrename' => true);
666         $data = array('shortname' => 'c1', 'rename' => '<span>invalid</span>');
667         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
668         $this->assertFalse($co->prepare());
669         $this->assertArrayHasKey('invalidshortname', $co->get_errors());
671         // Renaming with invalid shortname.
672         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
673         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
674         $importoptions = array('canrename' => true);
675         $data = array('shortname' => 'c1', 'rename' => 'c2');
676         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
677         $this->assertFalse($co->prepare());
678         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
679     }
681     public function test_restore_course() {
682         global $DB;
683         $this->resetAfterTest(true);
684         $this->setAdminUser();
686         $c1 = $this->getDataGenerator()->create_course();
687         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
689         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
690         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
691         $data = array('shortname' => 'A1', 'templatecourse' => $c1->shortname, 'summary' => 'A', 'category' => 1,
692             'fullname' => 'A1');
693         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
694         $this->assertTrue($co->prepare());
695         $co->proceed();
696         $course = $DB->get_record('course', array('shortname' => 'A1'));
697         $modinfo = get_fast_modinfo($course);
698         $found = false;
699         foreach ($modinfo->get_cms() as $cmid => $cm) {
700             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
701                 $found = true;
702                 break;
703             }
704         }
705         $this->assertTrue($found);
707         // Restoring twice from the same course should work.
708         $data = array('shortname' => 'B1', 'templatecourse' => $c1->shortname, 'summary' => 'B', 'category' => 1,
709             'fullname' => 'B1');
710         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
711         $this->assertTrue($co->prepare());
712         $co->proceed();
713         $course = $DB->get_record('course', array('shortname' => 'B1'));
714         $modinfo = get_fast_modinfo($course);
715         $found = false;
716         foreach ($modinfo->get_cms() as $cmid => $cm) {
717             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
718                 $found = true;
719                 break;
720             }
721         }
722         $this->assertTrue($found);
723     }
725     public function test_restore_file() {
726         global $DB;
727         $this->resetAfterTest(true);
728         $this->setAdminUser();
730         $c1 = $this->getDataGenerator()->create_course();
731         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
733         // Restore from a file, checking that the file takes priority over the templatecourse.
734         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
735         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
736         $data = array('shortname' => 'A1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
737             'summary' => 'A', 'category' => 1, 'fullname' => 'A1', 'templatecourse' => $c1->shortname);
738         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
739         $this->assertTrue($co->prepare());
740         $co->proceed();
741         $course = $DB->get_record('course', array('shortname' => 'A1'));
742         $modinfo = get_fast_modinfo($course);
743         $found = false;
744         foreach ($modinfo->get_cms() as $cmid => $cm) {
745             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
746                 $found = true;
747             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
748                 // We should not find this!
749                 $this->assertTrue(false);
750             }
751         }
752         $this->assertTrue($found);
754         // Restoring twice from the same file should work.
755         $data = array('shortname' => 'B1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
756             'summary' => 'B', 'category' => 1, 'fullname' => 'B1');
757         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
758         $this->assertTrue($co->prepare());
759         $co->proceed();
760         $course = $DB->get_record('course', array('shortname' => 'B1'));
761         $modinfo = get_fast_modinfo($course);
762         $found = false;
763         foreach ($modinfo->get_cms() as $cmid => $cm) {
764             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
765                 $found = true;
766             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
767                 // We should not find this!
768                 $this->assertTrue(false);
769             }
770         }
771         $this->assertTrue($found);
772     }
774     /**
775      * Test that specifying course template respects default restore settings
776      */
777     public function test_restore_file_settings() {
778         global $DB;
779         $this->resetAfterTest(true);
780         $this->setAdminUser();
782         // Set admin config setting so that activities are not restored by default.
783         set_config('restore_general_activities', 0, 'restore');
785         $c1 = $this->getDataGenerator()->create_course();
787         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
788         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
789         $data = array('shortname' => 'A1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
790             'summary' => 'A', 'category' => 1, 'fullname' => 'A1', 'templatecourse' => $c1->shortname);
791         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
792         $this->assertTrue($co->prepare());
793         $co->proceed();
794         $course = $DB->get_record('course', array('shortname' => 'A1'));
796         // Make sure the glossary is not restored.
797         $modinfo = get_fast_modinfo($course);
798         $this->assertEmpty($modinfo->get_instances_of('glossary'));
799     }
801     public function test_restore_invalid_file() {
802         $this->resetAfterTest();
804         // Restore from a non-existing file should not be allowed.
805         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
806         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
807         $data = array('shortname' => 'A1', 'backupfile' => '/lead/no/where',
808             'category' => 1, 'fullname' => 'A1');
809         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
810         $this->assertFalse($co->prepare());
811         $this->assertArrayHasKey('cannotreadbackupfile', $co->get_errors());
813         // Restore from an invalid file should not be allowed.
814         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
815         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
816         $data = array('shortname' => 'A1', 'backupfile' => __FILE__,
817             'category' => 1, 'fullname' => 'A1');
818         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
820         $this->assertFalse($co->prepare());
821         $this->assertArrayHasKey('invalidbackupfile', $co->get_errors());
823         // Zip packer throws a debugging message, this assertion is only here to prevent
824         // the message from being displayed.
825         $this->assertDebuggingCalled();
826     }
828     public function test_restore_invalid_course() {
829         $this->resetAfterTest();
831         // Restore from an invalid file should not be allowed.
832         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
833         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
834         $data = array('shortname' => 'A1', 'templatecourse' => 'iamnotavalidcourse',
835             'category' => 1, 'fullname' => 'A1');
836         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
837         $this->assertFalse($co->prepare());
838         $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $co->get_errors());
839     }
841     /**
842      * Testing the reset on groups, group members and enrolments.
843      */
844     public function test_reset() {
845         global $DB;
846         $this->resetAfterTest(true);
848         $c1 = $this->getDataGenerator()->create_course();
849         $c1ctx = context_course::instance($c1->id);
850         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
851         $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
853         $u1 = $this->getDataGenerator()->create_user();
854         $this->getDataGenerator()->enrol_user($u1->id, $c1->id, $studentrole->id);
855         $this->assertCount(1, get_enrolled_users($c1ctx));
857         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
858         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
859         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
860         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
862         // Wrong mode.
863         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
864         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
865         $data = array('shortname' => 'DoesNotExist', 'reset' => '1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 1);
866         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
867         $this->assertFalse($co->prepare());
868         $this->assertArrayHasKey('canonlyresetcourseinupdatemode', $co->get_errors());
869         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
870         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
871         $this->assertCount(1, get_enrolled_users($c1ctx));
873         // Reset not allowed.
874         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
875         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
876         $data = array('shortname' => $c1->shortname, 'reset' => '1');
877         $importoptions = array('canreset' => false);
878         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
879         $this->assertFalse($co->prepare());
880         $this->assertArrayHasKey('courseresetnotallowed', $co->get_errors());
881         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
882         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
883         $this->assertCount(1, get_enrolled_users($c1ctx));
885         // Reset allowed but not requested.
886         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
887         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
888         $data = array('shortname' => $c1->shortname, 'reset' => '0');
889         $importoptions = array('canreset' => true);
890         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
891         $this->assertTrue($co->prepare());
892         $co->proceed();
893         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
894         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
895         $this->assertCount(1, get_enrolled_users($c1ctx));
897         // Reset passed as a default parameter, should not be taken in account.
898         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
899         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
900         $data = array('shortname' => $c1->shortname);
901         $importoptions = array('canreset' => true);
902         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array('reset' => 1), $importoptions);
903         $this->assertTrue($co->prepare());
904         $co->proceed();
905         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
906         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
907         $this->assertCount(1, get_enrolled_users($c1ctx));
909         // Reset executed from data.
910         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
911         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
912         $data = array('shortname' => $c1->shortname, 'reset' => 1);
913         $importoptions = array('canreset' => true);
914         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
915         $this->assertTrue($co->prepare());
916         $co->proceed();
917         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
918         $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
919         $this->assertCount(0, get_enrolled_users($c1ctx));
921         // Reset executed from import option.
922         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
923         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
924         $data = array('shortname' => $c1->shortname, 'reset' => 0);
925         $importoptions = array('reset' => 1, 'canreset' => true);
926         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
928         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
929         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
930         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
931         $this->assertTrue($co->prepare());
932         $co->proceed();
933         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
934     }
936     public function test_create_bad_category() {
937         global $DB;
938         $this->resetAfterTest(true);
940         // Ensure fails when category cannot be resolved upon creation.
941         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
942         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
943         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 'Wrong cat');
944         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
945         $this->assertFalse($co->prepare());
946         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
948         // Ensure fails when category is 0 on create.
949         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
950         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
951         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '0');
952         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
953         $this->assertFalse($co->prepare());
954         $this->assertArrayHasKey('missingmandatoryfields', $co->get_errors());
956         // Ensure fails when category cannot be resolved upon update.
957         $c1 = $this->getDataGenerator()->create_course();
958         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
959         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
960         $data = array('shortname' => $c1->shortname, 'category' => 'Wrong cat');
961         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
962         $this->assertFalse($co->prepare());
963         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
965         // Ensure does not update the category when it is 0.
966         $c1 = $this->getDataGenerator()->create_course();
967         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
968         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
969         $data = array('shortname' => $c1->shortname, 'category' => '0');
970         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
971         $this->assertTrue($co->prepare());
972         $this->assertEmpty($co->get_errors());
973         $this->assertEmpty($co->get_statuses());
974         $co->proceed();
975         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
977         // Ensure does not update the category when it is set to 0 in the defaults.
978         $c1 = $this->getDataGenerator()->create_course();
979         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
980         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
981         $data = array('shortname' => $c1->shortname);
982         $defaults = array('category' => '0');
983         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
984         $this->assertTrue($co->prepare());
985         $this->assertEmpty($co->get_errors());
986         $this->assertEmpty($co->get_statuses());
987         $co->proceed();
988         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
989     }
991     public function test_enrolment_data() {
992         $this->resetAfterTest(true);
994         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
995         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
996         $data = array('shortname' => 'c1', 'summary' => 'S', 'fullname' => 'FN', 'category' => '1');
997         $data['enrolment_1'] = 'manual';
998         $data['enrolment_1_role'] = 'teacher';
999         $data['enrolment_1_startdate'] = '2nd July 2013';
1000         $data['enrolment_1_enddate'] = '2nd August 2013';
1001         $data['enrolment_1_enrolperiod'] = '10 days';
1002         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
1003         $this->assertTrue($co->prepare());
1004         $co->proceed();
1006         // Enrolment methods.
1007         $enroldata = array();
1008         $instances = enrol_get_instances($co->get_id(), false);
1009         foreach ($instances as $instance) {
1010             $enroldata[$instance->enrol] = $instance;
1011         }
1013         $this->assertNotEmpty($enroldata['manual']);
1014         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['manual']->status);
1015         $this->assertEquals(strtotime($data['enrolment_1_startdate']), $enroldata['manual']->enrolstartdate);
1016         $this->assertEquals(strtotime('1970-01-01 GMT + ' . $data['enrolment_1_enrolperiod']), $enroldata['manual']->enrolperiod);
1017         $this->assertEquals(strtotime('12th July 2013'), $enroldata['manual']->enrolenddate);
1018     }
1020     public function test_idnumber_problems() {
1021         $this->resetAfterTest(true);
1023         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'sntaken', 'idnumber' => 'taken'));
1024         $c2 = $this->getDataGenerator()->create_course();
1026         // Create with existing ID number.
1027         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1028         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1029         $data = array('shortname' => 'c2', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '1',
1030             'idnumber' => $c1->idnumber);
1031         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
1032         $this->assertFalse($co->prepare());
1033         $this->assertArrayHasKey('idnumberalreadyinuse', $co->get_errors());
1035         // Rename to existing ID number.
1036         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1037         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1038         $data = array('shortname' => $c2->shortname, 'rename' => 'SN', 'idnumber' => $c1->idnumber);
1039         $importoptions = array('canrename' => true);
1040         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1041         $this->assertFalse($co->prepare());
1042         $this->assertArrayHasKey('cannotrenameidnumberconflict', $co->get_errors());
1044         // Incrementing shortname increments idnumber.
1045         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1046         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1047         $data = array('shortname' => $c1->shortname, 'idnumber' => $c1->idnumber, 'summary' => 'S', 'fullname' => 'F',
1048             'category' => 1);
1049         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1050         $this->assertTrue($co->prepare());
1051         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1052         $this->assertArrayHasKey('courseidnumberincremented', $co->get_statuses());
1053         $data = $co->get_data();
1054         $this->assertEquals('sntaken_2', $data['shortname']);
1055         $this->assertEquals('taken_2', $data['idnumber']);
1057         // Incrementing shortname increments idnumber unless available.
1058         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1059         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1060         $data = array('shortname' => $c1->shortname, 'idnumber' => 'nottaken', 'summary' => 'S', 'fullname' => 'F',
1061             'category' => 1);
1062         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1063         $this->assertTrue($co->prepare());
1064         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1065         $this->assertArrayNotHasKey('courseidnumberincremented', $co->get_statuses());
1066         $data = $co->get_data();
1067         $this->assertEquals('sntaken_2', $data['shortname']);
1068         $this->assertEquals('nottaken', $data['idnumber']);
1069     }
1071     public function test_generate_shortname() {
1072         $this->resetAfterTest(true);
1074         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'taken'));
1076         // Generate a shortname.
1077         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1078         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1079         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1', 'idnumber' => 'IDN');
1080         $importoptions = array('shortnametemplate' => '%i');
1081         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1082         $this->assertTrue($co->prepare());
1083         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1085         // Generate a shortname without a template.
1086         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1087         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1088         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1089         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1090         $this->assertFalse($co->prepare());
1091         $this->assertArrayHasKey('missingshortnamenotemplate', $co->get_errors());
1093         // Generate a shortname in update mode.
1094         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1095         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1096         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1097         $importoptions = array('shortnametemplate' => '%f');
1098         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1099         $this->assertFalse($co->prepare());
1100         // Commented because we never get here as the course without shortname does not exist.
1101         // $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1103         // Generate a shortname to a course that already exists.
1104         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1105         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1106         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1107         $importoptions = array('shortnametemplate' => '%f');
1108         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1109         $this->assertFalse($co->prepare());
1110         $this->assertArrayHasKey('generatedshortnamealreadyinuse', $co->get_errors());
1112         // Generate a shortname to a course that already exists will be incremented.
1113         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1114         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1115         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1116         $importoptions = array('shortnametemplate' => '%f');
1117         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1118         $this->assertTrue($co->prepare());
1119         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1120         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1121     }
1123     public function test_mess_with_frontpage() {
1124         global $SITE;
1125         $this->resetAfterTest(true);
1127         // Updating the front page.
1128         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1129         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1130         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1131         $importoptions = array();
1132         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1133         $this->assertFalse($co->prepare());
1134         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1136         // Updating the front page.
1137         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1138         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1139         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1140         $importoptions = array();
1141         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1142         $this->assertFalse($co->prepare());
1143         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1145         // Generating a shortname should not be allowed in update mode, and so we cannot update the front page.
1146         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1147         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1148         $data = array('idnumber' => 'NewIDN', 'fullname' => 'FN', 'category' => 1);
1149         $importoptions = array('shortnametemplate' => $SITE->shortname);
1150         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1151         $this->assertFalse($co->prepare());
1152         $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1154         // Renaming to the front page should not be allowed.
1155         $c1 = $this->getDataGenerator()->create_course();
1156         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1157         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1158         $data = array('shortname' => $c1->shortname, 'fullname' => 'FN', 'idnumber' => 'NewIDN', 'rename' => $SITE->shortname);
1159         $importoptions = array('canrename' => true);
1160         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1161         $this->assertFalse($co->prepare());
1162         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
1164     }