MDL-52811 course: fix unit tests broken by setforcedlanguage
[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         cron_setup_user(); // To avoid warnings related to 'moodle/course:setforcedlanguage' capability check.
271         // Create.
272         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
273         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
274         $data = array(
275             'shortname' => 'c1',
276             'fullname' => 'Fullname',
277             'category' => '1',
278             'visible' => '0',
279             'idnumber' => '123abc',
280             'summary' => 'Summary',
281             'format' => 'topics',
282             'theme' => 'afterburner',
283             'lang' => 'en',
284             'newsitems' => '7',
285             'showgrades' => '0',
286             'showreports' => '1',
287             'legacyfiles' => '1',
288             'maxbytes' => '1234',
289             'groupmode' => '2',
290             'groupmodeforce' => '1',
291             'enablecompletion' => '1',
292             'tags' => 'Cat, Dog',
294             'role_teacher' => 'Knight',
295             'role_manager' => 'Jedi',
297             'enrolment_1' => 'guest',
298             'enrolment_2' => 'self',
299             'enrolment_2_roleid' => '1',
300             'enrolment_3' => 'manual',
301             'enrolment_3_disable' => '1',
302         );
304         // There should be a start date if there is a end date.
305         $data['enddate'] = '7 June 1990';
306         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
307         $this->assertFalse($co->prepare());
308         $this->assertArrayHasKey('nostartdatenoenddate', $co->get_errors());
310         $data['startdate'] = '8 June 1990';
311         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
312         $this->assertFalse($co->prepare());
313         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
315         // They are correct now.
316         $data['enddate'] = '18 June 1990';
318         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
319         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
320         $this->assertTrue($co->prepare());
321         $co->proceed();
322         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
323         $course = $DB->get_record('course', array('shortname' => 'c1'));
324         $ctx = context_course::instance($course->id);
326         $this->assertEquals($data['fullname'], $course->fullname);
327         $this->assertEquals($data['category'], $course->category);
328         $this->assertEquals($data['visible'], $course->visible);
329         $this->assertEquals(mktime(0, 0, 0, 6, 8, 1990), $course->startdate);
330         $this->assertEquals(mktime(0, 0, 0, 6, 18, 1990), $course->enddate);
331         $this->assertEquals($data['idnumber'], $course->idnumber);
332         $this->assertEquals($data['summary'], $course->summary);
333         $this->assertEquals($data['format'], $course->format);
334         $this->assertEquals($data['theme'], $course->theme);
335         $this->assertEquals($data['lang'], $course->lang);
336         $this->assertEquals($data['newsitems'], $course->newsitems);
337         $this->assertEquals($data['showgrades'], $course->showgrades);
338         $this->assertEquals($data['showreports'], $course->showreports);
339         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
340         $this->assertEquals($data['maxbytes'], $course->maxbytes);
341         $this->assertEquals($data['groupmode'], $course->groupmode);
342         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
343         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
344         $this->assertEquals($data['tags'], join(', ', core_tag_tag::get_item_tags_array('core', 'course', $course->id)));
346         // Roles.
347         $roleids = array();
348         $roles = get_all_roles();
349         foreach ($roles as $role) {
350             $roleids[$role->shortname] = $role->id;
351         }
352         $this->assertEquals('Knight', $DB->get_field_select('role_names', 'name',
353             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
354         $this->assertEquals('Jedi', $DB->get_field_select('role_names', 'name',
355             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
357         // Enrolment methods.
358         $enroldata = array();
359         $instances = enrol_get_instances($course->id, false);
360         $this->assertCount(3, $instances);
361         foreach ($instances as $instance) {
362             $enroldata[$instance->enrol] = $instance;
363         }
365         $this->assertNotEmpty($enroldata['guest']);
366         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['guest']->status);
367         $this->assertNotEmpty($enroldata['self']);
368         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
369         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
370         $this->assertNotEmpty($enroldata['manual']);
371         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['manual']->status);
373         // Update existing course.
374         $cat = $this->getDataGenerator()->create_category();
375         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
376         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
377         $data = array(
378             'shortname' => 'c1',
379             'fullname' => 'Fullname 2',
380             'category' => $cat->id,
381             'visible' => '1',
382             'idnumber' => 'changeidn',
383             'summary' => 'Summary 2',
384             'format' => 'topics',
385             'theme' => 'clean',
386             'lang' => '',
387             'newsitems' => '2',
388             'showgrades' => '1',
389             'showreports' => '0',
390             'legacyfiles' => '0',
391             'maxbytes' => '4321',
392             'groupmode' => '1',
393             'groupmodeforce' => '0',
394             'enablecompletion' => '0',
396             'role_teacher' => 'Teacher',
397             'role_manager' => 'Manager',
399             'enrolment_1' => 'guest',
400             'enrolment_1_disable' => '1',
401             'enrolment_2' => 'self',
402             'enrolment_2_roleid' => '2',
403             'enrolment_3' => 'manual',
404             'enrolment_3_delete' => '1',
405         );
407         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
409         $data['enddate'] = '31 June 1984';
410         // Previous start and end dates are 8 and 18 June 1990.
411         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
412         $this->assertFalse($co->prepare());
413         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
415         $data['startdate'] = '19 June 1990';
416         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
417         $this->assertFalse($co->prepare());
418         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
420         // They are correct now.
421         $data['startdate'] = '11 June 1984';
423         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
424         $this->assertTrue($co->prepare());
425         $co->proceed();
426         $course = $DB->get_record('course', array('shortname' => 'c1'));
427         $ctx = context_course::instance($course->id);
429         $this->assertEquals($data['fullname'], $course->fullname);
430         $this->assertEquals($data['category'], $course->category);
431         $this->assertEquals($data['visible'], $course->visible);
432         $this->assertEquals(mktime(0, 0, 0, 6, 11, 1984), $course->startdate);
433         $this->assertEquals(mktime(0, 0, 0, 6, 31, 1984), $course->enddate);
434         $this->assertEquals($data['idnumber'], $course->idnumber);
435         $this->assertEquals($data['summary'], $course->summary);
436         $this->assertEquals($data['format'], $course->format);
437         $this->assertEquals($data['theme'], $course->theme);
438         $this->assertEquals($data['lang'], $course->lang);
439         $this->assertEquals($data['newsitems'], $course->newsitems);
440         $this->assertEquals($data['showgrades'], $course->showgrades);
441         $this->assertEquals($data['showreports'], $course->showreports);
442         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
443         $this->assertEquals($data['maxbytes'], $course->maxbytes);
444         $this->assertEquals($data['groupmode'], $course->groupmode);
445         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
446         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
448         // Roles.
449         $roleids = array();
450         $roles = get_all_roles();
451         foreach ($roles as $role) {
452             $roleids[$role->shortname] = $role->id;
453         }
454         $this->assertEquals('Teacher', $DB->get_field_select('role_names', 'name',
455             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
456         $this->assertEquals('Manager', $DB->get_field_select('role_names', 'name',
457             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
459         // Enrolment methods.
460         $enroldata = array();
461         $instances = enrol_get_instances($course->id, false);
462         $this->assertCount(2, $instances);
463         foreach ($instances as $instance) {
464             $enroldata[$instance->enrol] = $instance;
465         }
467         $this->assertNotEmpty($enroldata['guest']);
468         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['guest']->status);
469         $this->assertNotEmpty($enroldata['self']);
470         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
471         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
472     }
474     public function test_default_data_saved() {
475         global $DB;
476         $this->resetAfterTest(true);
478         // Create.
479         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
480         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
481         $data = array(
482             'shortname' => 'c1',
483         );
484         $defaultdata = array(
485             'fullname' => 'Fullname',
486             'category' => '1',
487             'visible' => '0',
488             'startdate' => 644803200,
489             'enddate' => 645667200,
490             'idnumber' => '123abc',
491             'summary' => 'Summary',
492             'format' => 'topics',
493             'theme' => 'afterburner',
494             'lang' => 'en',
495             'newsitems' => '7',
496             'showgrades' => '0',
497             'showreports' => '1',
498             'legacyfiles' => '1',
499             'maxbytes' => '1234',
500             'groupmode' => '2',
501             'groupmodeforce' => '1',
502             'enablecompletion' => '1',
503         );
505         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
506         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
507         $this->assertTrue($co->prepare());
508         $co->proceed();
509         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
510         $course = $DB->get_record('course', array('shortname' => 'c1'));
511         $ctx = context_course::instance($course->id);
513         $this->assertEquals($defaultdata['fullname'], $course->fullname);
514         $this->assertEquals($defaultdata['category'], $course->category);
515         $this->assertEquals($defaultdata['visible'], $course->visible);
516         $this->assertEquals($defaultdata['startdate'], $course->startdate);
517         $this->assertEquals($defaultdata['enddate'], $course->enddate);
518         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
519         $this->assertEquals($defaultdata['summary'], $course->summary);
520         $this->assertEquals($defaultdata['format'], $course->format);
521         $this->assertEquals($defaultdata['theme'], $course->theme);
522         $this->assertEquals($defaultdata['lang'], $course->lang);
523         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
524         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
525         $this->assertEquals($defaultdata['showreports'], $course->showreports);
526         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
527         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
528         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
529         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
530         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
532         // Update.
533         $cat = $this->getDataGenerator()->create_category();
534         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
535         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
536         $data = array(
537             'shortname' => 'c1',
538         );
539         $defaultdata = array(
540             'fullname' => 'Fullname 2',
541             'category' => $cat->id,
542             'visible' => '1',
543             'startdate' => 455760000,
544             'enddate' => 457488000,
545             'idnumber' => 'changedid',
546             'summary' => 'Summary 2',
547             'format' => 'topics',
548             'theme' => 'clean',
549             'lang' => '',
550             'newsitems' => '2',
551             'showgrades' => '1',
552             'showreports' => '0',
553             'legacyfiles' => '0',
554             'maxbytes' => '1111',
555             'groupmode' => '1',
556             'groupmodeforce' => '0',
557             'enablecompletion' => '0',
558         );
560         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
561         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
562         $this->assertTrue($co->prepare());
563         $co->proceed();
564         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
565         $course = $DB->get_record('course', array('shortname' => 'c1'));
566         $ctx = context_course::instance($course->id);
568         $this->assertEquals($defaultdata['fullname'], $course->fullname);
569         $this->assertEquals($defaultdata['category'], $course->category);
570         $this->assertEquals($defaultdata['visible'], $course->visible);
571         $this->assertEquals($defaultdata['startdate'], $course->startdate);
572         $this->assertEquals($defaultdata['enddate'], $course->enddate);
573         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
574         $this->assertEquals($defaultdata['summary'], $course->summary);
575         $this->assertEquals($defaultdata['format'], $course->format);
576         $this->assertEquals($defaultdata['theme'], $course->theme);
577         $this->assertEquals($defaultdata['lang'], $course->lang);
578         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
579         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
580         $this->assertEquals($defaultdata['showreports'], $course->showreports);
581         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
582         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
583         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
584         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
585         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
586     }
588     public function test_rename() {
589         global $DB;
590         $this->resetAfterTest(true);
592         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1'));
593         $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'c2'));
595         // Cannot rename when creating.
596         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
597         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
598         $importoptions = array('canrename' => true);
599         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
600         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
601         $this->assertFalse($co->prepare());
602         $this->assertArrayHasKey('courseexistsanduploadnotallowed', $co->get_errors());
604         // Cannot rename when creating.
605         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
606         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
607         $importoptions = array('canrename' => true);
608         $data = array('shortname' => 'c1', 'rename' => 'newshortname', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
609         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
610         $this->assertFalse($co->prepare());
611         $this->assertArrayHasKey('canonlyrenameinupdatemode', $co->get_errors());
613         // Error when not allowed to rename the course.
614         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
615         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
616         $importoptions = array('canrename' => false);
617         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
618         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
619         $this->assertFalse($co->prepare());
620         $this->assertArrayHasKey('courserenamingnotallowed', $co->get_errors());
622         // Can rename when updating.
623         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
624         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
625         $importoptions = array('canrename' => true);
626         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
627         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
628         $this->assertTrue($co->prepare());
629         $co->proceed();
630         $this->assertEquals('newshortname', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
632         // Can rename when updating.
633         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
634         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
635         $importoptions = array('canrename' => true);
636         $data = array('shortname' => 'newshortname', 'rename' => 'newshortname2');
637         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
638         $this->assertTrue($co->prepare());
639         $co->proceed();
640         $this->assertEquals('newshortname2', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
642         // Error when course does not exist.
643         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
644         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
645         $importoptions = array('canrename' => true);
646         $data = array('shortname' => 'DoesNotExist', 'rename' => 'c1', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
647         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
648         $this->assertFalse($co->prepare());
649         $this->assertArrayHasKey('cannotrenamecoursenotexist', $co->get_errors());
651         // Renaming still updates the other values.
652         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
653         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
654         $importoptions = array('canrename' => true);
655         $data = array('shortname' => 'newshortname2', 'rename' => 'c1', 'fullname' => 'Another fullname!');
656         $defaultdata = array('summary' => 'New summary!');
657         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata, $importoptions);
658         $this->assertTrue($co->prepare());
659         $co->proceed();
660         $this->assertEquals('c1', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
661         $this->assertEquals('New summary!', $DB->get_field_select('course', 'summary', 'id = :id', array('id' => $c1->id)));
662         $this->assertEquals('Another fullname!', $DB->get_field_select('course', 'fullname', 'id = :id', array('id' => $c1->id)));
664         // Renaming with invalid shortname.
665         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
666         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
667         $importoptions = array('canrename' => true);
668         $data = array('shortname' => 'c1', 'rename' => '<span>invalid</span>');
669         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
670         $this->assertFalse($co->prepare());
671         $this->assertArrayHasKey('invalidshortname', $co->get_errors());
673         // Renaming with invalid shortname.
674         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
675         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
676         $importoptions = array('canrename' => true);
677         $data = array('shortname' => 'c1', 'rename' => 'c2');
678         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
679         $this->assertFalse($co->prepare());
680         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
681     }
683     public function test_restore_course() {
684         global $DB;
685         $this->resetAfterTest(true);
686         $this->setAdminUser();
688         $c1 = $this->getDataGenerator()->create_course();
689         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
691         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
692         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
693         $data = array('shortname' => 'A1', 'templatecourse' => $c1->shortname, 'summary' => 'A', 'category' => 1,
694             'fullname' => 'A1');
695         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
696         $this->assertTrue($co->prepare());
697         $co->proceed();
698         $course = $DB->get_record('course', array('shortname' => 'A1'));
699         $modinfo = get_fast_modinfo($course);
700         $found = false;
701         foreach ($modinfo->get_cms() as $cmid => $cm) {
702             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
703                 $found = true;
704                 break;
705             }
706         }
707         $this->assertTrue($found);
709         // Restoring twice from the same course should work.
710         $data = array('shortname' => 'B1', 'templatecourse' => $c1->shortname, 'summary' => 'B', 'category' => 1,
711             'fullname' => 'B1');
712         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
713         $this->assertTrue($co->prepare());
714         $co->proceed();
715         $course = $DB->get_record('course', array('shortname' => 'B1'));
716         $modinfo = get_fast_modinfo($course);
717         $found = false;
718         foreach ($modinfo->get_cms() as $cmid => $cm) {
719             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
720                 $found = true;
721                 break;
722             }
723         }
724         $this->assertTrue($found);
725     }
727     public function test_restore_file() {
728         global $DB;
729         $this->resetAfterTest(true);
730         $this->setAdminUser();
732         $c1 = $this->getDataGenerator()->create_course();
733         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
735         // Restore from a file, checking that the file takes priority over the templatecourse.
736         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
737         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
738         $data = array('shortname' => 'A1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
739             'summary' => 'A', 'category' => 1, 'fullname' => 'A1', 'templatecourse' => $c1->shortname);
740         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
741         $this->assertTrue($co->prepare());
742         $co->proceed();
743         $course = $DB->get_record('course', array('shortname' => 'A1'));
744         $modinfo = get_fast_modinfo($course);
745         $found = false;
746         foreach ($modinfo->get_cms() as $cmid => $cm) {
747             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
748                 $found = true;
749             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
750                 // We should not find this!
751                 $this->assertTrue(false);
752             }
753         }
754         $this->assertTrue($found);
756         // Restoring twice from the same file should work.
757         $data = array('shortname' => 'B1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
758             'summary' => 'B', 'category' => 1, 'fullname' => 'B1');
759         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
760         $this->assertTrue($co->prepare());
761         $co->proceed();
762         $course = $DB->get_record('course', array('shortname' => 'B1'));
763         $modinfo = get_fast_modinfo($course);
764         $found = false;
765         foreach ($modinfo->get_cms() as $cmid => $cm) {
766             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
767                 $found = true;
768             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
769                 // We should not find this!
770                 $this->assertTrue(false);
771             }
772         }
773         $this->assertTrue($found);
774     }
776     /**
777      * Test that specifying course template respects default restore settings
778      */
779     public function test_restore_file_settings() {
780         global $DB;
781         $this->resetAfterTest(true);
782         $this->setAdminUser();
784         // Set admin config setting so that activities are not restored by default.
785         set_config('restore_general_activities', 0, 'restore');
787         $c1 = $this->getDataGenerator()->create_course();
789         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
790         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
791         $data = array('shortname' => 'A1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
792             'summary' => 'A', 'category' => 1, 'fullname' => 'A1', 'templatecourse' => $c1->shortname);
793         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
794         $this->assertTrue($co->prepare());
795         $co->proceed();
796         $course = $DB->get_record('course', array('shortname' => 'A1'));
798         // Make sure the glossary is not restored.
799         $modinfo = get_fast_modinfo($course);
800         $this->assertEmpty($modinfo->get_instances_of('glossary'));
801     }
803     public function test_restore_invalid_file() {
804         $this->resetAfterTest();
806         // Restore from a non-existing file should not be allowed.
807         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
808         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
809         $data = array('shortname' => 'A1', 'backupfile' => '/lead/no/where',
810             'category' => 1, 'fullname' => 'A1');
811         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
812         $this->assertFalse($co->prepare());
813         $this->assertArrayHasKey('cannotreadbackupfile', $co->get_errors());
815         // Restore from an invalid file should not be allowed.
816         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
817         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
818         $data = array('shortname' => 'A1', 'backupfile' => __FILE__,
819             'category' => 1, 'fullname' => 'A1');
820         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
822         $this->assertFalse($co->prepare());
823         $this->assertArrayHasKey('invalidbackupfile', $co->get_errors());
825         // Zip packer throws a debugging message, this assertion is only here to prevent
826         // the message from being displayed.
827         $this->assertDebuggingCalled();
828     }
830     public function test_restore_invalid_course() {
831         $this->resetAfterTest();
833         // Restore from an invalid file should not be allowed.
834         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
835         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
836         $data = array('shortname' => 'A1', 'templatecourse' => 'iamnotavalidcourse',
837             'category' => 1, 'fullname' => 'A1');
838         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
839         $this->assertFalse($co->prepare());
840         $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $co->get_errors());
841     }
843     /**
844      * Testing the reset on groups, group members and enrolments.
845      */
846     public function test_reset() {
847         global $DB;
848         $this->resetAfterTest(true);
850         $c1 = $this->getDataGenerator()->create_course();
851         $c1ctx = context_course::instance($c1->id);
852         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
853         $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
855         $u1 = $this->getDataGenerator()->create_user();
856         $this->getDataGenerator()->enrol_user($u1->id, $c1->id, $studentrole->id);
857         $this->assertCount(1, get_enrolled_users($c1ctx));
859         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
860         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
861         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
862         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
864         // Wrong mode.
865         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
866         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
867         $data = array('shortname' => 'DoesNotExist', 'reset' => '1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 1);
868         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
869         $this->assertFalse($co->prepare());
870         $this->assertArrayHasKey('canonlyresetcourseinupdatemode', $co->get_errors());
871         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
872         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
873         $this->assertCount(1, get_enrolled_users($c1ctx));
875         // Reset not allowed.
876         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
877         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
878         $data = array('shortname' => $c1->shortname, 'reset' => '1');
879         $importoptions = array('canreset' => false);
880         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
881         $this->assertFalse($co->prepare());
882         $this->assertArrayHasKey('courseresetnotallowed', $co->get_errors());
883         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
884         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
885         $this->assertCount(1, get_enrolled_users($c1ctx));
887         // Reset allowed but not requested.
888         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
889         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
890         $data = array('shortname' => $c1->shortname, 'reset' => '0');
891         $importoptions = array('canreset' => true);
892         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
893         $this->assertTrue($co->prepare());
894         $co->proceed();
895         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
896         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
897         $this->assertCount(1, get_enrolled_users($c1ctx));
899         // Reset passed as a default parameter, should not be taken in account.
900         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
901         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
902         $data = array('shortname' => $c1->shortname);
903         $importoptions = array('canreset' => true);
904         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array('reset' => 1), $importoptions);
905         $this->assertTrue($co->prepare());
906         $co->proceed();
907         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
908         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
909         $this->assertCount(1, get_enrolled_users($c1ctx));
911         // Reset executed from data.
912         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
913         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
914         $data = array('shortname' => $c1->shortname, 'reset' => 1);
915         $importoptions = array('canreset' => true);
916         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
917         $this->assertTrue($co->prepare());
918         $co->proceed();
919         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
920         $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
921         $this->assertCount(0, get_enrolled_users($c1ctx));
923         // Reset executed from import option.
924         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
925         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
926         $data = array('shortname' => $c1->shortname, 'reset' => 0);
927         $importoptions = array('reset' => 1, 'canreset' => true);
928         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
930         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
931         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
932         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
933         $this->assertTrue($co->prepare());
934         $co->proceed();
935         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
936     }
938     public function test_create_bad_category() {
939         global $DB;
940         $this->resetAfterTest(true);
942         // Ensure fails when category cannot be resolved upon creation.
943         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
944         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
945         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 'Wrong cat');
946         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
947         $this->assertFalse($co->prepare());
948         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
950         // Ensure fails when category is 0 on create.
951         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
952         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
953         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '0');
954         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
955         $this->assertFalse($co->prepare());
956         $this->assertArrayHasKey('missingmandatoryfields', $co->get_errors());
958         // Ensure fails when category cannot be resolved upon update.
959         $c1 = $this->getDataGenerator()->create_course();
960         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
961         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
962         $data = array('shortname' => $c1->shortname, 'category' => 'Wrong cat');
963         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
964         $this->assertFalse($co->prepare());
965         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
967         // Ensure does not update the category when it is 0.
968         $c1 = $this->getDataGenerator()->create_course();
969         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
970         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
971         $data = array('shortname' => $c1->shortname, 'category' => '0');
972         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
973         $this->assertTrue($co->prepare());
974         $this->assertEmpty($co->get_errors());
975         $this->assertEmpty($co->get_statuses());
976         $co->proceed();
977         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
979         // Ensure does not update the category when it is set to 0 in the defaults.
980         $c1 = $this->getDataGenerator()->create_course();
981         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
982         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
983         $data = array('shortname' => $c1->shortname);
984         $defaults = array('category' => '0');
985         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
986         $this->assertTrue($co->prepare());
987         $this->assertEmpty($co->get_errors());
988         $this->assertEmpty($co->get_statuses());
989         $co->proceed();
990         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
991     }
993     public function test_enrolment_data() {
994         $this->resetAfterTest(true);
996         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
997         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
998         $data = array('shortname' => 'c1', 'summary' => 'S', 'fullname' => 'FN', 'category' => '1');
999         $data['enrolment_1'] = 'manual';
1000         $data['enrolment_1_role'] = 'teacher';
1001         $data['enrolment_1_startdate'] = '2nd July 2013';
1002         $data['enrolment_1_enddate'] = '2nd August 2013';
1003         $data['enrolment_1_enrolperiod'] = '10 days';
1004         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
1005         $this->assertTrue($co->prepare());
1006         $co->proceed();
1008         // Enrolment methods.
1009         $enroldata = array();
1010         $instances = enrol_get_instances($co->get_id(), false);
1011         foreach ($instances as $instance) {
1012             $enroldata[$instance->enrol] = $instance;
1013         }
1015         $this->assertNotEmpty($enroldata['manual']);
1016         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['manual']->status);
1017         $this->assertEquals(strtotime($data['enrolment_1_startdate']), $enroldata['manual']->enrolstartdate);
1018         $this->assertEquals(strtotime('1970-01-01 GMT + ' . $data['enrolment_1_enrolperiod']), $enroldata['manual']->enrolperiod);
1019         $this->assertEquals(strtotime('12th July 2013'), $enroldata['manual']->enrolenddate);
1020     }
1022     public function test_idnumber_problems() {
1023         $this->resetAfterTest(true);
1025         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'sntaken', 'idnumber' => 'taken'));
1026         $c2 = $this->getDataGenerator()->create_course();
1028         // Create with existing ID number.
1029         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1030         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1031         $data = array('shortname' => 'c2', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '1',
1032             'idnumber' => $c1->idnumber);
1033         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
1034         $this->assertFalse($co->prepare());
1035         $this->assertArrayHasKey('idnumberalreadyinuse', $co->get_errors());
1037         // Rename to existing ID number.
1038         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1039         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1040         $data = array('shortname' => $c2->shortname, 'rename' => 'SN', 'idnumber' => $c1->idnumber);
1041         $importoptions = array('canrename' => true);
1042         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1043         $this->assertFalse($co->prepare());
1044         $this->assertArrayHasKey('cannotrenameidnumberconflict', $co->get_errors());
1046         // Incrementing shortname increments idnumber.
1047         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1048         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1049         $data = array('shortname' => $c1->shortname, 'idnumber' => $c1->idnumber, 'summary' => 'S', 'fullname' => 'F',
1050             'category' => 1);
1051         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1052         $this->assertTrue($co->prepare());
1053         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1054         $this->assertArrayHasKey('courseidnumberincremented', $co->get_statuses());
1055         $data = $co->get_data();
1056         $this->assertEquals('sntaken_2', $data['shortname']);
1057         $this->assertEquals('taken_2', $data['idnumber']);
1059         // Incrementing shortname increments idnumber unless available.
1060         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1061         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1062         $data = array('shortname' => $c1->shortname, 'idnumber' => 'nottaken', 'summary' => 'S', 'fullname' => 'F',
1063             'category' => 1);
1064         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1065         $this->assertTrue($co->prepare());
1066         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1067         $this->assertArrayNotHasKey('courseidnumberincremented', $co->get_statuses());
1068         $data = $co->get_data();
1069         $this->assertEquals('sntaken_2', $data['shortname']);
1070         $this->assertEquals('nottaken', $data['idnumber']);
1071     }
1073     public function test_generate_shortname() {
1074         $this->resetAfterTest(true);
1076         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'taken'));
1078         // Generate a shortname.
1079         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1080         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1081         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1', 'idnumber' => 'IDN');
1082         $importoptions = array('shortnametemplate' => '%i');
1083         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1084         $this->assertTrue($co->prepare());
1085         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1087         // Generate a shortname without a template.
1088         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1089         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1090         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1091         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1092         $this->assertFalse($co->prepare());
1093         $this->assertArrayHasKey('missingshortnamenotemplate', $co->get_errors());
1095         // Generate a shortname in update mode.
1096         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1097         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1098         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1099         $importoptions = array('shortnametemplate' => '%f');
1100         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1101         $this->assertFalse($co->prepare());
1102         // Commented because we never get here as the course without shortname does not exist.
1103         // $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1105         // Generate a shortname to a course that already exists.
1106         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1107         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1108         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1109         $importoptions = array('shortnametemplate' => '%f');
1110         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1111         $this->assertFalse($co->prepare());
1112         $this->assertArrayHasKey('generatedshortnamealreadyinuse', $co->get_errors());
1114         // Generate a shortname to a course that already exists will be incremented.
1115         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1116         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1117         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1118         $importoptions = array('shortnametemplate' => '%f');
1119         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1120         $this->assertTrue($co->prepare());
1121         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1122         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1123     }
1125     public function test_mess_with_frontpage() {
1126         global $SITE;
1127         $this->resetAfterTest(true);
1129         // Updating the front page.
1130         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1131         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1132         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1133         $importoptions = array();
1134         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1135         $this->assertFalse($co->prepare());
1136         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1138         // Updating the front page.
1139         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1140         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1141         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1142         $importoptions = array();
1143         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1144         $this->assertFalse($co->prepare());
1145         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1147         // Generating a shortname should not be allowed in update mode, and so we cannot update the front page.
1148         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1149         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1150         $data = array('idnumber' => 'NewIDN', 'fullname' => 'FN', 'category' => 1);
1151         $importoptions = array('shortnametemplate' => $SITE->shortname);
1152         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1153         $this->assertFalse($co->prepare());
1154         $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1156         // Renaming to the front page should not be allowed.
1157         $c1 = $this->getDataGenerator()->create_course();
1158         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1159         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1160         $data = array('shortname' => $c1->shortname, 'fullname' => 'FN', 'idnumber' => 'NewIDN', 'rename' => $SITE->shortname);
1161         $importoptions = array('canrename' => true);
1162         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1163         $this->assertFalse($co->prepare());
1164         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
1166     }