MDL-64506 phpunit: Update unit tests to use classic
[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         $course = $DB->get_record('course', array('shortname' => 'newcourse'), '*', MUST_EXIST);
103         $this->assertEquals(0, course_get_format($course)->get_course()->coursedisplay);
105         // Try to add a new course, that already exists.
106         $coursecount = $DB->count_records('course', array());
107         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
108         $data = array('shortname' => 'c1', 'fullname' => 'C1FN', 'summary' => 'C1', 'category' => 1);
109         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
110         $this->assertFalse($co->prepare());
111         $this->assertArrayHasKey('courseexistsanduploadnotallowed', $co->get_errors());
112         $this->assertEquals($coursecount, $DB->count_records('course', array()));
113         $this->assertNotEquals('C1', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
115         // Try to add new with shortname incrementation.
116         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
117         $data = array('shortname' => 'c1', 'fullname' => 'C1FN', 'summary' => 'C1', 'category' => 1);
118         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
119         $this->assertTrue($co->prepare());
120         $co->proceed();
121         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c2')));
123         // Add a new course with non-default course format option.
124         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
125         $data = array('shortname' => 'c3', 'fullname' => 'C3', 'summary' => 'New c3', 'category' => 1,
126             'format' => 'weeks', 'coursedisplay' => 1);
127         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
128         $this->assertTrue($co->prepare());
129         $co->proceed();
130         $course = $DB->get_record('course', array('shortname' => 'c3'), '*', MUST_EXIST);
131         $this->assertEquals(1, course_get_format($course)->get_course()->coursedisplay);
132     }
134     public function test_create_with_sections() {
135         global $DB;
136         $this->resetAfterTest(true);
137         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
138         $defaultnumsections = get_config('moodlecourse', 'numsections');
140         // Add new course, make sure default number of sections is created.
141         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
142         $data = array('shortname' => 'newcourse1', 'fullname' => 'New course1', 'format' => 'topics', 'category' => 1);
143         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
144         $this->assertTrue($co->prepare());
145         $co->proceed();
146         $courseid = $DB->get_field('course', 'id', array('shortname' => 'newcourse1'));
147         $this->assertNotEmpty($courseid);
148         $this->assertEquals($defaultnumsections + 1,
149             $DB->count_records('course_sections', ['course' => $courseid]));
151         // Add new course specifying number of sections.
152         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
153         $data = array('shortname' => 'newcourse2', 'fullname' => 'New course2', 'format' => 'topics', 'category' => 1,
154             'numsections' => 15);
155         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
156         $this->assertTrue($co->prepare());
157         $co->proceed();
158         $courseid = $DB->get_field('course', 'id', array('shortname' => 'newcourse2'));
159         $this->assertNotEmpty($courseid);
160         $this->assertEquals(15 + 1,
161             $DB->count_records('course_sections', ['course' => $courseid]));
162     }
164     public function test_delete() {
165         global $DB;
166         $this->resetAfterTest(true);
168         $c1 = $this->getDataGenerator()->create_course();
169         $c2 = $this->getDataGenerator()->create_course();
171         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
172         $this->assertFalse($DB->record_exists('course', array('shortname' => 'DoesNotExist')));
174         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
175         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
177         // Try delete when option not available.
178         $importoptions = array('candelete' => false);
179         $data = array('shortname' => $c1->shortname, 'delete' => 1);
180         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
181         $this->assertFalse($co->prepare());
182         $this->assertArrayHasKey('coursedeletionnotallowed', $co->get_errors());
183         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
185         // Try delete when not requested.
186         $importoptions = array('candelete' => true);
187         $data = array('shortname' => $c1->shortname, 'delete' => 0);
188         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
189         $this->assertTrue($co->prepare());
190         $co->proceed();
191         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
193         // Try delete when requested.
194         $importoptions = array('candelete' => true);
195         $data = array('shortname' => $c1->shortname, 'delete' => 1);
196         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
197         $this->assertTrue($co->prepare());
198         $co->proceed();
199         $this->assertFalse($DB->record_exists('course', array('shortname' => $c1->shortname)));
200         $this->assertTrue($DB->record_exists('course', array('shortname' => $c2->shortname)));
202         // Try deleting non-existing record, this should not fail.
203         $data = array('shortname' => 'DoesNotExist', 'delete' => 1);
204         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
205         $this->assertFalse($co->prepare());
206         $this->assertArrayHasKey('cannotdeletecoursenotexist', $co->get_errors());
207     }
209     public function test_update() {
210         global $DB;
211         $this->resetAfterTest(true);
213         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1'));
215         // Try to update with existing shortnames, not allowing creation, and updating nothing.
216         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
217         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
218         $data = array('shortname' => 'c1', 'fullname' => 'New fullname');
219         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
220         $this->assertFalse($co->prepare());
221         $this->assertArrayHasKey('updatemodedoessettonothing', $co->get_errors());
223         // Try to update with non-existing shortnames.
224         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
225         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
226         $data = array('shortname' => 'DoesNotExist', 'fullname' => 'New fullname');
227         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
228         $this->assertFalse($co->prepare());
229         $this->assertArrayHasKey('coursedoesnotexistandcreatenotallowed', $co->get_errors());
231         // Try a proper update.
232         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
233         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
234         $data = array('shortname' => 'c1', 'fullname' => 'New fullname');
235         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
236         $this->assertTrue($co->prepare());
237         $co->proceed();
238         $this->assertEquals('New fullname', $DB->get_field_select('course', 'fullname', 'shortname = :s', array('s' => 'c1')));
240         // Try a proper update with defaults.
241         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
242         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
243         $data = array('shortname' => 'c1', 'fullname' => 'Another fullname');
244         $defaults = array('fullname' => 'Not this one', 'summary' => 'Awesome summary');
245         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
246         $this->assertTrue($co->prepare());
247         $co->proceed();
248         $this->assertEquals('Another fullname', $DB->get_field_select('course', 'fullname', 'shortname = :s', array('s' => 'c1')));
249         $this->assertEquals('Awesome summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
251         // Try a proper update missing only.
252         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
253         $updatemode = tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS;
254         $DB->set_field('course', 'summary', '', array('shortname' => 'c1'));
255         $this->assertEquals('', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
256         $data = array('shortname' => 'c1', 'summary' => 'Fill in summary');
257         $defaults = array('summary' => 'Do not use this summary');
258         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
259         $this->assertTrue($co->prepare());
260         $co->proceed();
261         $this->assertEquals('Fill in summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
263         // Try a proper update missing only using defaults.
264         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
265         $updatemode = tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS;
266         $DB->set_field('course', 'summary', '', array('shortname' => 'c1'));
267         $this->assertEquals('', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
268         $data = array('shortname' => 'c1');
269         $defaults = array('summary' => 'Use this summary');
270         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
271         $this->assertTrue($co->prepare());
272         $co->proceed();
273         $this->assertEquals('Use this summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
275         // Update course format option.
276         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
277         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
278         $data = array('shortname' => 'c1', 'coursedisplay' => 1);
279         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
280         $this->assertTrue($co->prepare());
281         $co->proceed();
282         $course = $DB->get_record('course', array('shortname' => 'c1'), '*', MUST_EXIST);
283         $this->assertEquals(1, course_get_format($course)->get_course()->coursedisplay);
284     }
286     public function test_data_saved() {
287         global $DB;
288         $this->resetAfterTest(true);
290         $this->setAdminUser(); // To avoid warnings related to 'moodle/course:setforcedlanguage' capability check.
292         // Create.
293         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
294         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
295         $data = array(
296             'shortname' => 'c1',
297             'fullname' => 'Fullname',
298             'category' => '1',
299             'visible' => '0',
300             'idnumber' => '123abc',
301             'summary' => 'Summary',
302             'format' => 'topics',
303             'theme' => 'afterburner',
304             'lang' => 'en',
305             'newsitems' => '7',
306             'showgrades' => '0',
307             'showreports' => '1',
308             'legacyfiles' => '1',
309             'maxbytes' => '1234',
310             'groupmode' => '2',
311             'groupmodeforce' => '1',
312             'enablecompletion' => '1',
313             'tags' => 'Cat, Dog',
315             'role_teacher' => 'Knight',
316             'role_manager' => 'Jedi',
318             'enrolment_1' => 'guest',
319             'enrolment_2' => 'self',
320             'enrolment_2_roleid' => '1',
321             'enrolment_3' => 'manual',
322             'enrolment_3_disable' => '1',
323         );
325         // There should be a start date if there is a end date.
326         $data['enddate'] = '7 June 1990';
327         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
328         $this->assertFalse($co->prepare());
329         $this->assertArrayHasKey('nostartdatenoenddate', $co->get_errors());
331         $data['startdate'] = '8 June 1990';
332         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
333         $this->assertFalse($co->prepare());
334         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
336         // They are correct now.
337         $data['enddate'] = '18 June 1990';
339         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
340         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
341         $this->assertTrue($co->prepare());
342         $co->proceed();
343         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
344         $course = $DB->get_record('course', array('shortname' => 'c1'));
345         $ctx = context_course::instance($course->id);
347         $this->assertEquals($data['fullname'], $course->fullname);
348         $this->assertEquals($data['category'], $course->category);
349         $this->assertEquals($data['visible'], $course->visible);
350         $this->assertEquals(mktime(0, 0, 0, 6, 8, 1990), $course->startdate);
351         $this->assertEquals(mktime(0, 0, 0, 6, 18, 1990), $course->enddate);
352         $this->assertEquals($data['idnumber'], $course->idnumber);
353         $this->assertEquals($data['summary'], $course->summary);
354         $this->assertEquals($data['format'], $course->format);
355         $this->assertEquals($data['theme'], $course->theme);
356         $this->assertEquals($data['lang'], $course->lang);
357         $this->assertEquals($data['newsitems'], $course->newsitems);
358         $this->assertEquals($data['showgrades'], $course->showgrades);
359         $this->assertEquals($data['showreports'], $course->showreports);
360         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
361         $this->assertEquals($data['maxbytes'], $course->maxbytes);
362         $this->assertEquals($data['groupmode'], $course->groupmode);
363         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
364         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
365         $this->assertEquals($data['tags'], join(', ', core_tag_tag::get_item_tags_array('core', 'course', $course->id)));
367         // Roles.
368         $roleids = array();
369         $roles = get_all_roles();
370         foreach ($roles as $role) {
371             $roleids[$role->shortname] = $role->id;
372         }
373         $this->assertEquals('Knight', $DB->get_field_select('role_names', 'name',
374             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
375         $this->assertEquals('Jedi', $DB->get_field_select('role_names', 'name',
376             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
378         // Enrolment methods.
379         $enroldata = array();
380         $instances = enrol_get_instances($course->id, false);
381         $this->assertCount(3, $instances);
382         foreach ($instances as $instance) {
383             $enroldata[$instance->enrol] = $instance;
384         }
386         $this->assertNotEmpty($enroldata['guest']);
387         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['guest']->status);
388         $this->assertNotEmpty($enroldata['self']);
389         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
390         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
391         $this->assertNotEmpty($enroldata['manual']);
392         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['manual']->status);
394         // Update existing course.
395         $cat = $this->getDataGenerator()->create_category();
396         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
397         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
398         $data = array(
399             'shortname' => 'c1',
400             'fullname' => 'Fullname 2',
401             'category' => $cat->id,
402             'visible' => '1',
403             'idnumber' => 'changeidn',
404             'summary' => 'Summary 2',
405             'format' => 'topics',
406             'theme' => 'classic',
407             'lang' => '',
408             'newsitems' => '2',
409             'showgrades' => '1',
410             'showreports' => '0',
411             'legacyfiles' => '0',
412             'maxbytes' => '4321',
413             'groupmode' => '1',
414             'groupmodeforce' => '0',
415             'enablecompletion' => '0',
417             'role_teacher' => 'Teacher',
418             'role_manager' => 'Manager',
420             'enrolment_1' => 'guest',
421             'enrolment_1_disable' => '1',
422             'enrolment_2' => 'self',
423             'enrolment_2_roleid' => '2',
424             'enrolment_3' => 'manual',
425             'enrolment_3_delete' => '1',
426         );
428         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
430         $data['enddate'] = '31 June 1984';
431         // Previous start and end dates are 8 and 18 June 1990.
432         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
433         $this->assertFalse($co->prepare());
434         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
436         $data['startdate'] = '19 June 1990';
437         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
438         $this->assertFalse($co->prepare());
439         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
441         // They are correct now.
442         $data['startdate'] = '11 June 1984';
444         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
445         $this->assertTrue($co->prepare());
446         $co->proceed();
447         $course = $DB->get_record('course', array('shortname' => 'c1'));
448         $ctx = context_course::instance($course->id);
450         $this->assertEquals($data['fullname'], $course->fullname);
451         $this->assertEquals($data['category'], $course->category);
452         $this->assertEquals($data['visible'], $course->visible);
453         $this->assertEquals(mktime(0, 0, 0, 6, 11, 1984), $course->startdate);
454         $this->assertEquals(mktime(0, 0, 0, 6, 31, 1984), $course->enddate);
455         $this->assertEquals($data['idnumber'], $course->idnumber);
456         $this->assertEquals($data['summary'], $course->summary);
457         $this->assertEquals($data['format'], $course->format);
458         $this->assertEquals($data['theme'], $course->theme);
459         $this->assertEquals($data['lang'], $course->lang);
460         $this->assertEquals($data['newsitems'], $course->newsitems);
461         $this->assertEquals($data['showgrades'], $course->showgrades);
462         $this->assertEquals($data['showreports'], $course->showreports);
463         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
464         $this->assertEquals($data['maxbytes'], $course->maxbytes);
465         $this->assertEquals($data['groupmode'], $course->groupmode);
466         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
467         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
469         // Roles.
470         $roleids = array();
471         $roles = get_all_roles();
472         foreach ($roles as $role) {
473             $roleids[$role->shortname] = $role->id;
474         }
475         $this->assertEquals('Teacher', $DB->get_field_select('role_names', 'name',
476             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
477         $this->assertEquals('Manager', $DB->get_field_select('role_names', 'name',
478             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
480         // Enrolment methods.
481         $enroldata = array();
482         $instances = enrol_get_instances($course->id, false);
483         $this->assertCount(2, $instances);
484         foreach ($instances as $instance) {
485             $enroldata[$instance->enrol] = $instance;
486         }
488         $this->assertNotEmpty($enroldata['guest']);
489         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['guest']->status);
490         $this->assertNotEmpty($enroldata['self']);
491         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
492         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
493     }
495     public function test_default_data_saved() {
496         global $DB;
497         $this->resetAfterTest(true);
499         // Create.
500         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
501         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
502         $data = array(
503             'shortname' => 'c1',
504         );
505         $defaultdata = array(
506             'fullname' => 'Fullname',
507             'category' => '1',
508             'visible' => '0',
509             'startdate' => 644803200,
510             'enddate' => 645667200,
511             'idnumber' => '123abc',
512             'summary' => 'Summary',
513             'format' => 'topics',
514             'theme' => 'afterburner',
515             'lang' => 'en',
516             'newsitems' => '7',
517             'showgrades' => '0',
518             'showreports' => '1',
519             'legacyfiles' => '1',
520             'maxbytes' => '1234',
521             'groupmode' => '2',
522             'groupmodeforce' => '1',
523             'enablecompletion' => '1',
524         );
526         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
527         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
528         $this->assertTrue($co->prepare());
529         $co->proceed();
530         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
531         $course = $DB->get_record('course', array('shortname' => 'c1'));
532         $ctx = context_course::instance($course->id);
534         $this->assertEquals($defaultdata['fullname'], $course->fullname);
535         $this->assertEquals($defaultdata['category'], $course->category);
536         $this->assertEquals($defaultdata['visible'], $course->visible);
537         $this->assertEquals($defaultdata['startdate'], $course->startdate);
538         $this->assertEquals($defaultdata['enddate'], $course->enddate);
539         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
540         $this->assertEquals($defaultdata['summary'], $course->summary);
541         $this->assertEquals($defaultdata['format'], $course->format);
542         $this->assertEquals($defaultdata['theme'], $course->theme);
543         $this->assertEquals($defaultdata['lang'], $course->lang);
544         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
545         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
546         $this->assertEquals($defaultdata['showreports'], $course->showreports);
547         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
548         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
549         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
550         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
551         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
553         // Update.
554         $cat = $this->getDataGenerator()->create_category();
555         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
556         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
557         $data = array(
558             'shortname' => 'c1',
559         );
560         $defaultdata = array(
561             'fullname' => 'Fullname 2',
562             'category' => $cat->id,
563             'visible' => '1',
564             'startdate' => 455760000,
565             'enddate' => 457488000,
566             'idnumber' => 'changedid',
567             'summary' => 'Summary 2',
568             'format' => 'topics',
569             'theme' => 'classic',
570             'lang' => '',
571             'newsitems' => '2',
572             'showgrades' => '1',
573             'showreports' => '0',
574             'legacyfiles' => '0',
575             'maxbytes' => '1111',
576             'groupmode' => '1',
577             'groupmodeforce' => '0',
578             'enablecompletion' => '0',
579         );
581         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
582         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
583         $this->assertTrue($co->prepare());
584         $co->proceed();
585         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
586         $course = $DB->get_record('course', array('shortname' => 'c1'));
587         $ctx = context_course::instance($course->id);
589         $this->assertEquals($defaultdata['fullname'], $course->fullname);
590         $this->assertEquals($defaultdata['category'], $course->category);
591         $this->assertEquals($defaultdata['visible'], $course->visible);
592         $this->assertEquals($defaultdata['startdate'], $course->startdate);
593         $this->assertEquals($defaultdata['enddate'], $course->enddate);
594         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
595         $this->assertEquals($defaultdata['summary'], $course->summary);
596         $this->assertEquals($defaultdata['format'], $course->format);
597         $this->assertEquals($defaultdata['theme'], $course->theme);
598         $this->assertEquals($defaultdata['lang'], $course->lang);
599         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
600         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
601         $this->assertEquals($defaultdata['showreports'], $course->showreports);
602         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
603         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
604         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
605         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
606         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
607     }
609     public function test_rename() {
610         global $DB;
611         $this->resetAfterTest(true);
613         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1'));
614         $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'c2'));
616         // Cannot rename when creating.
617         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
618         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
619         $importoptions = array('canrename' => true);
620         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
621         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
622         $this->assertFalse($co->prepare());
623         $this->assertArrayHasKey('courseexistsanduploadnotallowed', $co->get_errors());
625         // Cannot rename when creating.
626         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
627         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
628         $importoptions = array('canrename' => true);
629         $data = array('shortname' => 'c1', 'rename' => 'newshortname', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
630         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
631         $this->assertFalse($co->prepare());
632         $this->assertArrayHasKey('canonlyrenameinupdatemode', $co->get_errors());
634         // Error when not allowed to rename the course.
635         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
636         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
637         $importoptions = array('canrename' => false);
638         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
639         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
640         $this->assertFalse($co->prepare());
641         $this->assertArrayHasKey('courserenamingnotallowed', $co->get_errors());
643         // Can rename when updating.
644         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
645         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
646         $importoptions = array('canrename' => true);
647         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
648         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
649         $this->assertTrue($co->prepare());
650         $co->proceed();
651         $this->assertEquals('newshortname', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
653         // Can rename when updating.
654         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
655         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
656         $importoptions = array('canrename' => true);
657         $data = array('shortname' => 'newshortname', 'rename' => 'newshortname2');
658         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
659         $this->assertTrue($co->prepare());
660         $co->proceed();
661         $this->assertEquals('newshortname2', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
663         // Error when course does not exist.
664         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
665         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
666         $importoptions = array('canrename' => true);
667         $data = array('shortname' => 'DoesNotExist', 'rename' => 'c1', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
668         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
669         $this->assertFalse($co->prepare());
670         $this->assertArrayHasKey('cannotrenamecoursenotexist', $co->get_errors());
672         // Renaming still updates the other values.
673         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
674         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
675         $importoptions = array('canrename' => true);
676         $data = array('shortname' => 'newshortname2', 'rename' => 'c1', 'fullname' => 'Another fullname!');
677         $defaultdata = array('summary' => 'New summary!');
678         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata, $importoptions);
679         $this->assertTrue($co->prepare());
680         $co->proceed();
681         $this->assertEquals('c1', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
682         $this->assertEquals('New summary!', $DB->get_field_select('course', 'summary', 'id = :id', array('id' => $c1->id)));
683         $this->assertEquals('Another fullname!', $DB->get_field_select('course', 'fullname', 'id = :id', array('id' => $c1->id)));
685         // Renaming with invalid shortname.
686         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
687         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
688         $importoptions = array('canrename' => true);
689         $data = array('shortname' => 'c1', 'rename' => '<span>invalid</span>');
690         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
691         $this->assertFalse($co->prepare());
692         $this->assertArrayHasKey('invalidshortname', $co->get_errors());
694         // Renaming with invalid shortname.
695         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
696         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
697         $importoptions = array('canrename' => true);
698         $data = array('shortname' => 'c1', 'rename' => 'c2');
699         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
700         $this->assertFalse($co->prepare());
701         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
702     }
704     public function test_restore_course() {
705         global $DB;
706         $this->resetAfterTest(true);
707         $this->setAdminUser();
709         $c1 = $this->getDataGenerator()->create_course();
710         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
712         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
713         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
714         $data = array('shortname' => 'A1', 'templatecourse' => $c1->shortname, 'summary' => 'A', 'category' => 1,
715             'fullname' => 'A1');
716         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
717         $this->assertTrue($co->prepare());
718         $co->proceed();
719         $course = $DB->get_record('course', array('shortname' => 'A1'));
720         $modinfo = get_fast_modinfo($course);
721         $found = false;
722         foreach ($modinfo->get_cms() as $cmid => $cm) {
723             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
724                 $found = true;
725                 break;
726             }
727         }
728         $this->assertTrue($found);
730         // Restoring twice from the same course should work.
731         $data = array('shortname' => 'B1', 'templatecourse' => $c1->shortname, 'summary' => 'B', 'category' => 1,
732             'fullname' => 'B1');
733         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
734         $this->assertTrue($co->prepare());
735         $co->proceed();
736         $course = $DB->get_record('course', array('shortname' => 'B1'));
737         $modinfo = get_fast_modinfo($course);
738         $found = false;
739         foreach ($modinfo->get_cms() as $cmid => $cm) {
740             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
741                 $found = true;
742                 break;
743             }
744         }
745         $this->assertTrue($found);
746     }
748     public function test_restore_file() {
749         global $DB;
750         $this->resetAfterTest(true);
751         $this->setAdminUser();
753         $c1 = $this->getDataGenerator()->create_course();
754         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
756         // Restore from a file, checking that the file takes priority over the templatecourse.
757         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
758         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
759         $data = array('shortname' => 'A1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
760             'summary' => 'A', 'category' => 1, 'fullname' => 'A1', 'templatecourse' => $c1->shortname);
761         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
762         $this->assertTrue($co->prepare());
763         $co->proceed();
764         $course = $DB->get_record('course', array('shortname' => 'A1'));
765         $modinfo = get_fast_modinfo($course);
766         $found = false;
767         foreach ($modinfo->get_cms() as $cmid => $cm) {
768             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
769                 $found = true;
770             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
771                 // We should not find this!
772                 $this->assertTrue(false);
773             }
774         }
775         $this->assertTrue($found);
777         // Restoring twice from the same file should work.
778         $data = array('shortname' => 'B1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
779             'summary' => 'B', 'category' => 1, 'fullname' => 'B1');
780         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
781         $this->assertTrue($co->prepare());
782         $co->proceed();
783         $course = $DB->get_record('course', array('shortname' => 'B1'));
784         $modinfo = get_fast_modinfo($course);
785         $found = false;
786         foreach ($modinfo->get_cms() as $cmid => $cm) {
787             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
788                 $found = true;
789             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
790                 // We should not find this!
791                 $this->assertTrue(false);
792             }
793         }
794         $this->assertTrue($found);
795     }
797     /**
798      * Test that specifying course template respects default restore settings
799      */
800     public function test_restore_file_settings() {
801         global $DB;
802         $this->resetAfterTest(true);
803         $this->setAdminUser();
805         // Set admin config setting so that activities are not restored by default.
806         set_config('restore_general_activities', 0, 'restore');
808         $c1 = $this->getDataGenerator()->create_course();
810         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
811         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
812         $data = array('shortname' => 'A1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
813             'summary' => 'A', 'category' => 1, 'fullname' => 'A1', 'templatecourse' => $c1->shortname);
814         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
815         $this->assertTrue($co->prepare());
816         $co->proceed();
817         $course = $DB->get_record('course', array('shortname' => 'A1'));
819         // Make sure the glossary is not restored.
820         $modinfo = get_fast_modinfo($course);
821         $this->assertEmpty($modinfo->get_instances_of('glossary'));
822     }
824     public function test_restore_invalid_file() {
825         $this->resetAfterTest();
827         // Restore from a non-existing file should not be allowed.
828         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
829         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
830         $data = array('shortname' => 'A1', 'backupfile' => '/lead/no/where',
831             'category' => 1, 'fullname' => 'A1');
832         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
833         $this->assertFalse($co->prepare());
834         $this->assertArrayHasKey('cannotreadbackupfile', $co->get_errors());
836         // Restore from an invalid file should not be allowed.
837         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
838         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
839         $data = array('shortname' => 'A1', 'backupfile' => __FILE__,
840             'category' => 1, 'fullname' => 'A1');
841         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
843         $this->assertFalse($co->prepare());
844         $this->assertArrayHasKey('invalidbackupfile', $co->get_errors());
846         // Zip packer throws a debugging message, this assertion is only here to prevent
847         // the message from being displayed.
848         $this->assertDebuggingCalled();
849     }
851     public function test_restore_invalid_course() {
852         $this->resetAfterTest();
854         // Restore from an invalid file should not be allowed.
855         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
856         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
857         $data = array('shortname' => 'A1', 'templatecourse' => 'iamnotavalidcourse',
858             'category' => 1, 'fullname' => 'A1');
859         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
860         $this->assertFalse($co->prepare());
861         $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $co->get_errors());
862     }
864     /**
865      * Testing the reset on groups, group members and enrolments.
866      */
867     public function test_reset() {
868         global $DB;
869         $this->resetAfterTest(true);
871         $c1 = $this->getDataGenerator()->create_course();
872         $c1ctx = context_course::instance($c1->id);
873         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
874         $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
876         $u1 = $this->getDataGenerator()->create_user();
877         $this->getDataGenerator()->enrol_user($u1->id, $c1->id, $studentrole->id);
878         $this->assertCount(1, get_enrolled_users($c1ctx));
880         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
881         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
882         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
883         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
885         // Wrong mode.
886         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
887         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
888         $data = array('shortname' => 'DoesNotExist', 'reset' => '1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 1);
889         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
890         $this->assertFalse($co->prepare());
891         $this->assertArrayHasKey('canonlyresetcourseinupdatemode', $co->get_errors());
892         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
893         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
894         $this->assertCount(1, get_enrolled_users($c1ctx));
896         // Reset not allowed.
897         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
898         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
899         $data = array('shortname' => $c1->shortname, 'reset' => '1');
900         $importoptions = array('canreset' => false);
901         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
902         $this->assertFalse($co->prepare());
903         $this->assertArrayHasKey('courseresetnotallowed', $co->get_errors());
904         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
905         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
906         $this->assertCount(1, get_enrolled_users($c1ctx));
908         // Reset allowed but not requested.
909         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
910         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
911         $data = array('shortname' => $c1->shortname, 'reset' => '0');
912         $importoptions = array('canreset' => true);
913         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
914         $this->assertTrue($co->prepare());
915         $co->proceed();
916         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
917         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
918         $this->assertCount(1, get_enrolled_users($c1ctx));
920         // Reset passed as a default parameter, should not be taken in account.
921         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
922         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
923         $data = array('shortname' => $c1->shortname);
924         $importoptions = array('canreset' => true);
925         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array('reset' => 1), $importoptions);
926         $this->assertTrue($co->prepare());
927         $co->proceed();
928         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
929         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
930         $this->assertCount(1, get_enrolled_users($c1ctx));
932         // Reset executed from data.
933         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
934         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
935         $data = array('shortname' => $c1->shortname, 'reset' => 1);
936         $importoptions = array('canreset' => true);
937         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
938         $this->assertTrue($co->prepare());
939         $co->proceed();
940         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
941         $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
942         $this->assertCount(0, get_enrolled_users($c1ctx));
944         // Reset executed from import option.
945         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
946         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
947         $data = array('shortname' => $c1->shortname, 'reset' => 0);
948         $importoptions = array('reset' => 1, 'canreset' => true);
949         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
951         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
952         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
953         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
954         $this->assertTrue($co->prepare());
955         $co->proceed();
956         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
957     }
959     public function test_create_bad_category() {
960         global $DB;
961         $this->resetAfterTest(true);
963         // Ensure fails when category cannot be resolved upon creation.
964         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
965         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
966         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 'Wrong cat');
967         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
968         $this->assertFalse($co->prepare());
969         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
971         // Ensure fails when category is 0 on create.
972         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
973         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
974         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '0');
975         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
976         $this->assertFalse($co->prepare());
977         $this->assertArrayHasKey('missingmandatoryfields', $co->get_errors());
979         // Ensure fails when category cannot be resolved upon update.
980         $c1 = $this->getDataGenerator()->create_course();
981         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
982         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
983         $data = array('shortname' => $c1->shortname, 'category' => 'Wrong cat');
984         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
985         $this->assertFalse($co->prepare());
986         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
988         // Ensure does not update the category when it is 0.
989         $c1 = $this->getDataGenerator()->create_course();
990         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
991         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
992         $data = array('shortname' => $c1->shortname, 'category' => '0');
993         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
994         $this->assertTrue($co->prepare());
995         $this->assertEmpty($co->get_errors());
996         $this->assertEmpty($co->get_statuses());
997         $co->proceed();
998         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
1000         // Ensure does not update the category when it is set to 0 in the defaults.
1001         $c1 = $this->getDataGenerator()->create_course();
1002         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1003         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
1004         $data = array('shortname' => $c1->shortname);
1005         $defaults = array('category' => '0');
1006         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
1007         $this->assertTrue($co->prepare());
1008         $this->assertEmpty($co->get_errors());
1009         $this->assertEmpty($co->get_statuses());
1010         $co->proceed();
1011         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
1012     }
1014     public function test_enrolment_data() {
1015         $this->resetAfterTest(true);
1017         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1018         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1019         $data = array('shortname' => 'c1', 'summary' => 'S', 'fullname' => 'FN', 'category' => '1');
1020         $data['enrolment_1'] = 'manual';
1021         $data['enrolment_1_role'] = 'teacher';
1022         $data['enrolment_1_startdate'] = '2nd July 2013';
1023         $data['enrolment_1_enddate'] = '2nd August 2013';
1024         $data['enrolment_1_enrolperiod'] = '10 days';
1025         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
1026         $this->assertTrue($co->prepare());
1027         $co->proceed();
1029         // Enrolment methods.
1030         $enroldata = array();
1031         $instances = enrol_get_instances($co->get_id(), false);
1032         foreach ($instances as $instance) {
1033             $enroldata[$instance->enrol] = $instance;
1034         }
1036         $this->assertNotEmpty($enroldata['manual']);
1037         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['manual']->status);
1038         $this->assertEquals(strtotime($data['enrolment_1_startdate']), $enroldata['manual']->enrolstartdate);
1039         $this->assertEquals(strtotime('1970-01-01 GMT + ' . $data['enrolment_1_enrolperiod']), $enroldata['manual']->enrolperiod);
1040         $this->assertEquals(strtotime('12th July 2013'), $enroldata['manual']->enrolenddate);
1041     }
1043     public function test_idnumber_problems() {
1044         $this->resetAfterTest(true);
1046         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'sntaken', 'idnumber' => 'taken'));
1047         $c2 = $this->getDataGenerator()->create_course();
1049         // Create with existing ID number.
1050         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1051         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1052         $data = array('shortname' => 'c2', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '1',
1053             'idnumber' => $c1->idnumber);
1054         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
1055         $this->assertFalse($co->prepare());
1056         $this->assertArrayHasKey('idnumberalreadyinuse', $co->get_errors());
1058         // Rename to existing ID number.
1059         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1060         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1061         $data = array('shortname' => $c2->shortname, 'rename' => 'SN', 'idnumber' => $c1->idnumber);
1062         $importoptions = array('canrename' => true);
1063         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1064         $this->assertFalse($co->prepare());
1065         $this->assertArrayHasKey('cannotrenameidnumberconflict', $co->get_errors());
1067         // Incrementing shortname increments idnumber.
1068         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1069         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1070         $data = array('shortname' => $c1->shortname, 'idnumber' => $c1->idnumber, 'summary' => 'S', 'fullname' => 'F',
1071             'category' => 1);
1072         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1073         $this->assertTrue($co->prepare());
1074         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1075         $this->assertArrayHasKey('courseidnumberincremented', $co->get_statuses());
1076         $data = $co->get_data();
1077         $this->assertEquals('sntaken_2', $data['shortname']);
1078         $this->assertEquals('taken_2', $data['idnumber']);
1080         // Incrementing shortname increments idnumber unless available.
1081         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1082         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1083         $data = array('shortname' => $c1->shortname, 'idnumber' => 'nottaken', 'summary' => 'S', 'fullname' => 'F',
1084             'category' => 1);
1085         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1086         $this->assertTrue($co->prepare());
1087         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1088         $this->assertArrayNotHasKey('courseidnumberincremented', $co->get_statuses());
1089         $data = $co->get_data();
1090         $this->assertEquals('sntaken_2', $data['shortname']);
1091         $this->assertEquals('nottaken', $data['idnumber']);
1092     }
1094     public function test_generate_shortname() {
1095         $this->resetAfterTest(true);
1097         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'taken'));
1099         // Generate a shortname.
1100         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1101         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1102         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1', 'idnumber' => 'IDN');
1103         $importoptions = array('shortnametemplate' => '%i');
1104         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1105         $this->assertTrue($co->prepare());
1106         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1108         // Generate a shortname without a template.
1109         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1110         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1111         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1112         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1113         $this->assertFalse($co->prepare());
1114         $this->assertArrayHasKey('missingshortnamenotemplate', $co->get_errors());
1116         // Generate a shortname in update mode.
1117         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1118         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1119         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1120         $importoptions = array('shortnametemplate' => '%f');
1121         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1122         $this->assertFalse($co->prepare());
1123         // Commented because we never get here as the course without shortname does not exist.
1124         // $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1126         // Generate a shortname to a course that already exists.
1127         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1128         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1129         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1130         $importoptions = array('shortnametemplate' => '%f');
1131         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1132         $this->assertFalse($co->prepare());
1133         $this->assertArrayHasKey('generatedshortnamealreadyinuse', $co->get_errors());
1135         // Generate a shortname to a course that already exists will be incremented.
1136         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1137         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1138         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1139         $importoptions = array('shortnametemplate' => '%f');
1140         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1141         $this->assertTrue($co->prepare());
1142         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1143         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1144     }
1146     public function test_mess_with_frontpage() {
1147         global $SITE;
1148         $this->resetAfterTest(true);
1150         // Updating the front page.
1151         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1152         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1153         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1154         $importoptions = array();
1155         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1156         $this->assertFalse($co->prepare());
1157         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1159         // Updating the front page.
1160         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1161         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1162         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1163         $importoptions = array();
1164         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1165         $this->assertFalse($co->prepare());
1166         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1168         // Generating a shortname should not be allowed in update mode, and so we cannot update the front page.
1169         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1170         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1171         $data = array('idnumber' => 'NewIDN', 'fullname' => 'FN', 'category' => 1);
1172         $importoptions = array('shortnametemplate' => $SITE->shortname);
1173         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1174         $this->assertFalse($co->prepare());
1175         $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1177         // Renaming to the front page should not be allowed.
1178         $c1 = $this->getDataGenerator()->create_course();
1179         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1180         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1181         $data = array('shortname' => $c1->shortname, 'fullname' => 'FN', 'idnumber' => 'NewIDN', 'rename' => $SITE->shortname);
1182         $importoptions = array('canrename' => true);
1183         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1184         $this->assertFalse($co->prepare());
1185         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
1187     }