MDL-22078 course: End date tests
[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_delete() {
124         global $DB;
125         $this->resetAfterTest(true);
127         $c1 = $this->getDataGenerator()->create_course();
128         $c2 = $this->getDataGenerator()->create_course();
130         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
131         $this->assertFalse($DB->record_exists('course', array('shortname' => 'DoesNotExist')));
133         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
134         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
136         // Try delete when option not available.
137         $importoptions = array('candelete' => false);
138         $data = array('shortname' => $c1->shortname, 'delete' => 1);
139         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
140         $this->assertFalse($co->prepare());
141         $this->assertArrayHasKey('coursedeletionnotallowed', $co->get_errors());
142         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
144         // Try delete when not requested.
145         $importoptions = array('candelete' => true);
146         $data = array('shortname' => $c1->shortname, 'delete' => 0);
147         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
148         $this->assertTrue($co->prepare());
149         $co->proceed();
150         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
152         // Try delete when requested.
153         $importoptions = array('candelete' => true);
154         $data = array('shortname' => $c1->shortname, 'delete' => 1);
155         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
156         $this->assertTrue($co->prepare());
157         $co->proceed();
158         $this->assertFalse($DB->record_exists('course', array('shortname' => $c1->shortname)));
159         $this->assertTrue($DB->record_exists('course', array('shortname' => $c2->shortname)));
161         // Try deleting non-existing record, this should not fail.
162         $data = array('shortname' => 'DoesNotExist', 'delete' => 1);
163         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
164         $this->assertFalse($co->prepare());
165         $this->assertArrayHasKey('cannotdeletecoursenotexist', $co->get_errors());
166     }
168     public function test_update() {
169         global $DB;
170         $this->resetAfterTest(true);
172         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1'));
174         // Try to update with existing shortnames, not allowing creation, and updating nothing.
175         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
176         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
177         $data = array('shortname' => 'c1', 'fullname' => 'New fullname');
178         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
179         $this->assertFalse($co->prepare());
180         $this->assertArrayHasKey('updatemodedoessettonothing', $co->get_errors());
182         // Try to update with non-existing shortnames.
183         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
184         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
185         $data = array('shortname' => 'DoesNotExist', 'fullname' => 'New fullname');
186         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
187         $this->assertFalse($co->prepare());
188         $this->assertArrayHasKey('coursedoesnotexistandcreatenotallowed', $co->get_errors());
190         // Try a proper update.
191         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
192         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
193         $data = array('shortname' => 'c1', 'fullname' => 'New fullname');
194         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
195         $this->assertTrue($co->prepare());
196         $co->proceed();
197         $this->assertEquals('New fullname', $DB->get_field_select('course', 'fullname', 'shortname = :s', array('s' => 'c1')));
199         // Try a proper update with defaults.
200         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
201         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
202         $data = array('shortname' => 'c1', 'fullname' => 'Another fullname');
203         $defaults = array('fullname' => 'Not this one', 'summary' => 'Awesome summary');
204         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
205         $this->assertTrue($co->prepare());
206         $co->proceed();
207         $this->assertEquals('Another fullname', $DB->get_field_select('course', 'fullname', 'shortname = :s', array('s' => 'c1')));
208         $this->assertEquals('Awesome summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
210         // Try a proper update missing only.
211         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
212         $updatemode = tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS;
213         $DB->set_field('course', 'summary', '', array('shortname' => 'c1'));
214         $this->assertEquals('', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
215         $data = array('shortname' => 'c1', 'summary' => 'Fill in summary');
216         $defaults = array('summary' => 'Do not use this summary');
217         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
218         $this->assertTrue($co->prepare());
219         $co->proceed();
220         $this->assertEquals('Fill in summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
222         // Try a proper update missing only using defaults.
223         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
224         $updatemode = tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS;
225         $DB->set_field('course', 'summary', '', array('shortname' => 'c1'));
226         $this->assertEquals('', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
227         $data = array('shortname' => 'c1');
228         $defaults = array('summary' => 'Use this summary');
229         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
230         $this->assertTrue($co->prepare());
231         $co->proceed();
232         $this->assertEquals('Use this summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
233     }
235     public function test_data_saved() {
236         global $DB;
237         $this->resetAfterTest(true);
239         // Create.
240         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
241         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
242         $data = array(
243             'shortname' => 'c1',
244             'fullname' => 'Fullname',
245             'category' => '1',
246             'visible' => '0',
247             'idnumber' => '123abc',
248             'summary' => 'Summary',
249             'format' => 'weeks',
250             'theme' => 'afterburner',
251             'lang' => 'en',
252             'newsitems' => '7',
253             'showgrades' => '0',
254             'showreports' => '1',
255             'legacyfiles' => '1',
256             'maxbytes' => '1234',
257             'groupmode' => '2',
258             'groupmodeforce' => '1',
259             'enablecompletion' => '1',
260             'tags' => 'Cat, Dog',
262             'role_teacher' => 'Knight',
263             'role_manager' => 'Jedi',
265             'enrolment_1' => 'guest',
266             'enrolment_2' => 'self',
267             'enrolment_2_roleid' => '1',
268             'enrolment_3' => 'manual',
269             'enrolment_3_disable' => '1',
270         );
272         // There should be a start date if there is a end date.
273         $data['enddate'] = '7 June 1990';
274         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
275         $this->assertFalse($co->prepare());
276         $this->assertArrayHasKey('nostartdatenoenddate', $co->get_errors());
278         $data['startdate'] = '8 June 1990';
279         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
280         $this->assertFalse($co->prepare());
281         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
283         // They are correct now.
284         $data['enddate'] = '18 June 1990';
286         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
287         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
288         $this->assertTrue($co->prepare());
289         $co->proceed();
290         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
291         $course = $DB->get_record('course', array('shortname' => 'c1'));
292         $ctx = context_course::instance($course->id);
294         $this->assertEquals($data['fullname'], $course->fullname);
295         $this->assertEquals($data['category'], $course->category);
296         $this->assertEquals($data['visible'], $course->visible);
297         $this->assertEquals(mktime(0, 0, 0, 6, 8, 1990), $course->startdate);
298         $this->assertEquals(mktime(0, 0, 0, 6, 18, 1990), $course->enddate);
299         $this->assertEquals($data['idnumber'], $course->idnumber);
300         $this->assertEquals($data['summary'], $course->summary);
301         $this->assertEquals($data['format'], $course->format);
302         $this->assertEquals($data['theme'], $course->theme);
303         $this->assertEquals($data['lang'], $course->lang);
304         $this->assertEquals($data['newsitems'], $course->newsitems);
305         $this->assertEquals($data['showgrades'], $course->showgrades);
306         $this->assertEquals($data['showreports'], $course->showreports);
307         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
308         $this->assertEquals($data['maxbytes'], $course->maxbytes);
309         $this->assertEquals($data['groupmode'], $course->groupmode);
310         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
311         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
312         $this->assertEquals($data['tags'], join(', ', core_tag_tag::get_item_tags_array('core', 'course', $course->id)));
314         // Roles.
315         $roleids = array();
316         $roles = get_all_roles();
317         foreach ($roles as $role) {
318             $roleids[$role->shortname] = $role->id;
319         }
320         $this->assertEquals('Knight', $DB->get_field_select('role_names', 'name',
321             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
322         $this->assertEquals('Jedi', $DB->get_field_select('role_names', 'name',
323             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
325         // Enrolment methods.
326         $enroldata = array();
327         $instances = enrol_get_instances($course->id, false);
328         $this->assertCount(3, $instances);
329         foreach ($instances as $instance) {
330             $enroldata[$instance->enrol] = $instance;
331         }
333         $this->assertNotEmpty($enroldata['guest']);
334         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['guest']->status);
335         $this->assertNotEmpty($enroldata['self']);
336         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
337         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
338         $this->assertNotEmpty($enroldata['manual']);
339         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['manual']->status);
341         // Update existing course.
342         $cat = $this->getDataGenerator()->create_category();
343         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
344         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
345         $data = array(
346             'shortname' => 'c1',
347             'fullname' => 'Fullname 2',
348             'category' => $cat->id,
349             'visible' => '1',
350             'idnumber' => 'changeidn',
351             'summary' => 'Summary 2',
352             'format' => 'topics',
353             'theme' => 'clean',
354             'lang' => '',
355             'newsitems' => '2',
356             'showgrades' => '1',
357             'showreports' => '0',
358             'legacyfiles' => '0',
359             'maxbytes' => '4321',
360             'groupmode' => '1',
361             'groupmodeforce' => '0',
362             'enablecompletion' => '0',
364             'role_teacher' => 'Teacher',
365             'role_manager' => 'Manager',
367             'enrolment_1' => 'guest',
368             'enrolment_1_disable' => '1',
369             'enrolment_2' => 'self',
370             'enrolment_2_roleid' => '2',
371             'enrolment_3' => 'manual',
372             'enrolment_3_delete' => '1',
373         );
375         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
377         $data['enddate'] = '31 June 1984';
378         // Previous start and end dates are 8 and 18 June 1990.
379         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
380         $this->assertFalse($co->prepare());
381         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
383         $data['startdate'] = '19 June 1990';
384         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
385         $this->assertFalse($co->prepare());
386         $this->assertArrayHasKey('enddatebeforestartdate', $co->get_errors());
388         // They are correct now.
389         $data['startdate'] = '11 June 1984';
391         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
392         $this->assertTrue($co->prepare());
393         $co->proceed();
394         $course = $DB->get_record('course', array('shortname' => 'c1'));
395         $ctx = context_course::instance($course->id);
397         $this->assertEquals($data['fullname'], $course->fullname);
398         $this->assertEquals($data['category'], $course->category);
399         $this->assertEquals($data['visible'], $course->visible);
400         $this->assertEquals(mktime(0, 0, 0, 6, 11, 1984), $course->startdate);
401         $this->assertEquals(mktime(0, 0, 0, 6, 31, 1984), $course->enddate);
402         $this->assertEquals($data['idnumber'], $course->idnumber);
403         $this->assertEquals($data['summary'], $course->summary);
404         $this->assertEquals($data['format'], $course->format);
405         $this->assertEquals($data['theme'], $course->theme);
406         $this->assertEquals($data['lang'], $course->lang);
407         $this->assertEquals($data['newsitems'], $course->newsitems);
408         $this->assertEquals($data['showgrades'], $course->showgrades);
409         $this->assertEquals($data['showreports'], $course->showreports);
410         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
411         $this->assertEquals($data['maxbytes'], $course->maxbytes);
412         $this->assertEquals($data['groupmode'], $course->groupmode);
413         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
414         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
416         // Roles.
417         $roleids = array();
418         $roles = get_all_roles();
419         foreach ($roles as $role) {
420             $roleids[$role->shortname] = $role->id;
421         }
422         $this->assertEquals('Teacher', $DB->get_field_select('role_names', 'name',
423             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
424         $this->assertEquals('Manager', $DB->get_field_select('role_names', 'name',
425             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
427         // Enrolment methods.
428         $enroldata = array();
429         $instances = enrol_get_instances($course->id, false);
430         $this->assertCount(2, $instances);
431         foreach ($instances as $instance) {
432             $enroldata[$instance->enrol] = $instance;
433         }
435         $this->assertNotEmpty($enroldata['guest']);
436         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['guest']->status);
437         $this->assertNotEmpty($enroldata['self']);
438         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
439         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
440     }
442     public function test_default_data_saved() {
443         global $DB;
444         $this->resetAfterTest(true);
446         // Create.
447         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
448         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
449         $data = array(
450             'shortname' => 'c1',
451         );
452         $defaultdata = array(
453             'fullname' => 'Fullname',
454             'category' => '1',
455             'visible' => '0',
456             'startdate' => 644803200,
457             'enddate' => 645667200,
458             'idnumber' => '123abc',
459             'summary' => 'Summary',
460             'format' => 'weeks',
461             'theme' => 'afterburner',
462             'lang' => 'en',
463             'newsitems' => '7',
464             'showgrades' => '0',
465             'showreports' => '1',
466             'legacyfiles' => '1',
467             'maxbytes' => '1234',
468             'groupmode' => '2',
469             'groupmodeforce' => '1',
470             'enablecompletion' => '1',
471         );
473         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
474         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
475         $this->assertTrue($co->prepare());
476         $co->proceed();
477         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
478         $course = $DB->get_record('course', array('shortname' => 'c1'));
479         $ctx = context_course::instance($course->id);
481         $this->assertEquals($defaultdata['fullname'], $course->fullname);
482         $this->assertEquals($defaultdata['category'], $course->category);
483         $this->assertEquals($defaultdata['visible'], $course->visible);
484         $this->assertEquals($defaultdata['startdate'], $course->startdate);
485         $this->assertEquals($defaultdata['enddate'], $course->enddate);
486         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
487         $this->assertEquals($defaultdata['summary'], $course->summary);
488         $this->assertEquals($defaultdata['format'], $course->format);
489         $this->assertEquals($defaultdata['theme'], $course->theme);
490         $this->assertEquals($defaultdata['lang'], $course->lang);
491         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
492         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
493         $this->assertEquals($defaultdata['showreports'], $course->showreports);
494         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
495         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
496         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
497         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
498         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
500         // Update.
501         $cat = $this->getDataGenerator()->create_category();
502         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
503         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
504         $data = array(
505             'shortname' => 'c1',
506         );
507         $defaultdata = array(
508             'fullname' => 'Fullname 2',
509             'category' => $cat->id,
510             'visible' => '1',
511             'startdate' => 455760000,
512             'enddate' => 457488000,
513             'idnumber' => 'changedid',
514             'summary' => 'Summary 2',
515             'format' => 'topics',
516             'theme' => 'clean',
517             'lang' => '',
518             'newsitems' => '2',
519             'showgrades' => '1',
520             'showreports' => '0',
521             'legacyfiles' => '0',
522             'maxbytes' => '1111',
523             'groupmode' => '1',
524             'groupmodeforce' => '0',
525             'enablecompletion' => '0',
526         );
528         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
529         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
530         $this->assertTrue($co->prepare());
531         $co->proceed();
532         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
533         $course = $DB->get_record('course', array('shortname' => 'c1'));
534         $ctx = context_course::instance($course->id);
536         $this->assertEquals($defaultdata['fullname'], $course->fullname);
537         $this->assertEquals($defaultdata['category'], $course->category);
538         $this->assertEquals($defaultdata['visible'], $course->visible);
539         $this->assertEquals($defaultdata['startdate'], $course->startdate);
540         $this->assertEquals($defaultdata['enddate'], $course->enddate);
541         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
542         $this->assertEquals($defaultdata['summary'], $course->summary);
543         $this->assertEquals($defaultdata['format'], $course->format);
544         $this->assertEquals($defaultdata['theme'], $course->theme);
545         $this->assertEquals($defaultdata['lang'], $course->lang);
546         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
547         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
548         $this->assertEquals($defaultdata['showreports'], $course->showreports);
549         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
550         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
551         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
552         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
553         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
554     }
556     public function test_rename() {
557         global $DB;
558         $this->resetAfterTest(true);
560         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1'));
561         $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'c2'));
563         // Cannot rename when creating.
564         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
565         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
566         $importoptions = array('canrename' => true);
567         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
568         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
569         $this->assertFalse($co->prepare());
570         $this->assertArrayHasKey('courseexistsanduploadnotallowed', $co->get_errors());
572         // Cannot rename when creating.
573         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
574         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
575         $importoptions = array('canrename' => true);
576         $data = array('shortname' => 'c1', 'rename' => 'newshortname', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
577         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
578         $this->assertFalse($co->prepare());
579         $this->assertArrayHasKey('canonlyrenameinupdatemode', $co->get_errors());
581         // Error when not allowed to rename the course.
582         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
583         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
584         $importoptions = array('canrename' => false);
585         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
586         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
587         $this->assertFalse($co->prepare());
588         $this->assertArrayHasKey('courserenamingnotallowed', $co->get_errors());
590         // Can rename when updating.
591         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
592         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
593         $importoptions = array('canrename' => true);
594         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
595         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
596         $this->assertTrue($co->prepare());
597         $co->proceed();
598         $this->assertEquals('newshortname', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
600         // Can rename when updating.
601         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
602         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
603         $importoptions = array('canrename' => true);
604         $data = array('shortname' => 'newshortname', 'rename' => 'newshortname2');
605         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
606         $this->assertTrue($co->prepare());
607         $co->proceed();
608         $this->assertEquals('newshortname2', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
610         // Error when course does not exist.
611         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
612         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
613         $importoptions = array('canrename' => true);
614         $data = array('shortname' => 'DoesNotExist', 'rename' => 'c1', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
615         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
616         $this->assertFalse($co->prepare());
617         $this->assertArrayHasKey('cannotrenamecoursenotexist', $co->get_errors());
619         // Renaming still updates the other values.
620         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
621         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
622         $importoptions = array('canrename' => true);
623         $data = array('shortname' => 'newshortname2', 'rename' => 'c1', 'fullname' => 'Another fullname!');
624         $defaultdata = array('summary' => 'New summary!');
625         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata, $importoptions);
626         $this->assertTrue($co->prepare());
627         $co->proceed();
628         $this->assertEquals('c1', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
629         $this->assertEquals('New summary!', $DB->get_field_select('course', 'summary', 'id = :id', array('id' => $c1->id)));
630         $this->assertEquals('Another fullname!', $DB->get_field_select('course', 'fullname', 'id = :id', array('id' => $c1->id)));
632         // Renaming with invalid shortname.
633         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
634         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
635         $importoptions = array('canrename' => true);
636         $data = array('shortname' => 'c1', 'rename' => '<span>invalid</span>');
637         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
638         $this->assertFalse($co->prepare());
639         $this->assertArrayHasKey('invalidshortname', $co->get_errors());
641         // Renaming with invalid shortname.
642         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
643         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
644         $importoptions = array('canrename' => true);
645         $data = array('shortname' => 'c1', 'rename' => 'c2');
646         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
647         $this->assertFalse($co->prepare());
648         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
649     }
651     public function test_restore_course() {
652         global $DB;
653         $this->resetAfterTest(true);
654         $this->setAdminUser();
656         $c1 = $this->getDataGenerator()->create_course();
657         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
659         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
660         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
661         $data = array('shortname' => 'A1', 'templatecourse' => $c1->shortname, 'summary' => 'A', 'category' => 1,
662             'fullname' => 'A1');
663         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
664         $this->assertTrue($co->prepare());
665         $co->proceed();
666         $course = $DB->get_record('course', array('shortname' => 'A1'));
667         $modinfo = get_fast_modinfo($course);
668         $found = false;
669         foreach ($modinfo->get_cms() as $cmid => $cm) {
670             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
671                 $found = true;
672                 break;
673             }
674         }
675         $this->assertTrue($found);
677         // Restoring twice from the same course should work.
678         $data = array('shortname' => 'B1', 'templatecourse' => $c1->shortname, 'summary' => 'B', 'category' => 1,
679             'fullname' => 'B1');
680         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
681         $this->assertTrue($co->prepare());
682         $co->proceed();
683         $course = $DB->get_record('course', array('shortname' => 'B1'));
684         $modinfo = get_fast_modinfo($course);
685         $found = false;
686         foreach ($modinfo->get_cms() as $cmid => $cm) {
687             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
688                 $found = true;
689                 break;
690             }
691         }
692         $this->assertTrue($found);
693     }
695     public function test_restore_file() {
696         global $DB;
697         $this->resetAfterTest(true);
698         $this->setAdminUser();
700         $c1 = $this->getDataGenerator()->create_course();
701         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
703         // Restore from a file, checking that the file takes priority over the templatecourse.
704         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
705         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
706         $data = array('shortname' => 'A1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
707             'summary' => 'A', 'category' => 1, 'fullname' => 'A1', 'templatecourse' => $c1->shortname);
708         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
709         $this->assertTrue($co->prepare());
710         $co->proceed();
711         $course = $DB->get_record('course', array('shortname' => 'A1'));
712         $modinfo = get_fast_modinfo($course);
713         $found = false;
714         foreach ($modinfo->get_cms() as $cmid => $cm) {
715             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
716                 $found = true;
717             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
718                 // We should not find this!
719                 $this->assertTrue(false);
720             }
721         }
722         $this->assertTrue($found);
724         // Restoring twice from the same file should work.
725         $data = array('shortname' => 'B1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
726             'summary' => 'B', 'category' => 1, 'fullname' => 'B1');
727         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
728         $this->assertTrue($co->prepare());
729         $co->proceed();
730         $course = $DB->get_record('course', array('shortname' => 'B1'));
731         $modinfo = get_fast_modinfo($course);
732         $found = false;
733         foreach ($modinfo->get_cms() as $cmid => $cm) {
734             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
735                 $found = true;
736             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
737                 // We should not find this!
738                 $this->assertTrue(false);
739             }
740         }
741         $this->assertTrue($found);
742     }
744     public function test_restore_invalid_file() {
745         $this->resetAfterTest();
747         // Restore from a non-existing file should not be allowed.
748         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
749         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
750         $data = array('shortname' => 'A1', 'backupfile' => '/lead/no/where',
751             'category' => 1, 'fullname' => 'A1');
752         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
753         $this->assertFalse($co->prepare());
754         $this->assertArrayHasKey('cannotreadbackupfile', $co->get_errors());
756         // Restore from an invalid file should not be allowed.
757         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
758         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
759         $data = array('shortname' => 'A1', 'backupfile' => __FILE__,
760             'category' => 1, 'fullname' => 'A1');
761         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
763         $this->assertFalse($co->prepare());
764         $this->assertArrayHasKey('invalidbackupfile', $co->get_errors());
766         // Zip packer throws a debugging message, this assertion is only here to prevent
767         // the message from being displayed.
768         $this->assertDebuggingCalled();
769     }
771     public function test_restore_invalid_course() {
772         $this->resetAfterTest();
774         // Restore from an invalid file should not be allowed.
775         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
776         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
777         $data = array('shortname' => 'A1', 'templatecourse' => 'iamnotavalidcourse',
778             'category' => 1, 'fullname' => 'A1');
779         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
780         $this->assertFalse($co->prepare());
781         $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $co->get_errors());
782     }
784     /**
785      * Testing the reset on groups, group members and enrolments.
786      */
787     public function test_reset() {
788         global $DB;
789         $this->resetAfterTest(true);
791         $c1 = $this->getDataGenerator()->create_course();
792         $c1ctx = context_course::instance($c1->id);
793         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
794         $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
796         $u1 = $this->getDataGenerator()->create_user();
797         $this->getDataGenerator()->enrol_user($u1->id, $c1->id, $studentrole->id);
798         $this->assertCount(1, get_enrolled_users($c1ctx));
800         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
801         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
802         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
803         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
805         // Wrong mode.
806         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
807         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
808         $data = array('shortname' => 'DoesNotExist', 'reset' => '1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 1);
809         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
810         $this->assertFalse($co->prepare());
811         $this->assertArrayHasKey('canonlyresetcourseinupdatemode', $co->get_errors());
812         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
813         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
814         $this->assertCount(1, get_enrolled_users($c1ctx));
816         // Reset not allowed.
817         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
818         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
819         $data = array('shortname' => $c1->shortname, 'reset' => '1');
820         $importoptions = array('canreset' => false);
821         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
822         $this->assertFalse($co->prepare());
823         $this->assertArrayHasKey('courseresetnotallowed', $co->get_errors());
824         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
825         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
826         $this->assertCount(1, get_enrolled_users($c1ctx));
828         // Reset allowed but not requested.
829         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
830         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
831         $data = array('shortname' => $c1->shortname, 'reset' => '0');
832         $importoptions = array('canreset' => true);
833         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
834         $this->assertTrue($co->prepare());
835         $co->proceed();
836         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
837         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
838         $this->assertCount(1, get_enrolled_users($c1ctx));
840         // Reset passed as a default parameter, should not be taken in account.
841         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
842         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
843         $data = array('shortname' => $c1->shortname);
844         $importoptions = array('canreset' => true);
845         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array('reset' => 1), $importoptions);
846         $this->assertTrue($co->prepare());
847         $co->proceed();
848         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
849         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
850         $this->assertCount(1, get_enrolled_users($c1ctx));
852         // Reset executed from data.
853         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
854         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
855         $data = array('shortname' => $c1->shortname, 'reset' => 1);
856         $importoptions = array('canreset' => true);
857         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
858         $this->assertTrue($co->prepare());
859         $co->proceed();
860         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
861         $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
862         $this->assertCount(0, get_enrolled_users($c1ctx));
864         // Reset executed from import option.
865         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
866         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
867         $data = array('shortname' => $c1->shortname, 'reset' => 0);
868         $importoptions = array('reset' => 1, 'canreset' => true);
869         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
871         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
872         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
873         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
874         $this->assertTrue($co->prepare());
875         $co->proceed();
876         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
877     }
879     public function test_create_bad_category() {
880         global $DB;
881         $this->resetAfterTest(true);
883         // Ensure fails when category cannot be resolved upon creation.
884         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
885         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
886         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 'Wrong cat');
887         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
888         $this->assertFalse($co->prepare());
889         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
891         // Ensure fails when category is 0 on create.
892         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
893         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
894         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '0');
895         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
896         $this->assertFalse($co->prepare());
897         $this->assertArrayHasKey('missingmandatoryfields', $co->get_errors());
899         // Ensure fails when category cannot be resolved upon update.
900         $c1 = $this->getDataGenerator()->create_course();
901         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
902         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
903         $data = array('shortname' => $c1->shortname, 'category' => 'Wrong cat');
904         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
905         $this->assertFalse($co->prepare());
906         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
908         // Ensure does not update the category when it is 0.
909         $c1 = $this->getDataGenerator()->create_course();
910         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
911         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
912         $data = array('shortname' => $c1->shortname, 'category' => '0');
913         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
914         $this->assertTrue($co->prepare());
915         $this->assertEmpty($co->get_errors());
916         $this->assertEmpty($co->get_statuses());
917         $co->proceed();
918         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
920         // Ensure does not update the category when it is set to 0 in the defaults.
921         $c1 = $this->getDataGenerator()->create_course();
922         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
923         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
924         $data = array('shortname' => $c1->shortname);
925         $defaults = array('category' => '0');
926         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
927         $this->assertTrue($co->prepare());
928         $this->assertEmpty($co->get_errors());
929         $this->assertEmpty($co->get_statuses());
930         $co->proceed();
931         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
932     }
934     public function test_enrolment_data() {
935         $this->resetAfterTest(true);
937         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
938         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
939         $data = array('shortname' => 'c1', 'summary' => 'S', 'fullname' => 'FN', 'category' => '1');
940         $data['enrolment_1'] = 'manual';
941         $data['enrolment_1_role'] = 'teacher';
942         $data['enrolment_1_startdate'] = '2nd July 2013';
943         $data['enrolment_1_enddate'] = '2nd August 2013';
944         $data['enrolment_1_enrolperiod'] = '10 days';
945         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
946         $this->assertTrue($co->prepare());
947         $co->proceed();
949         // Enrolment methods.
950         $enroldata = array();
951         $instances = enrol_get_instances($co->get_id(), false);
952         foreach ($instances as $instance) {
953             $enroldata[$instance->enrol] = $instance;
954         }
956         $this->assertNotEmpty($enroldata['manual']);
957         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['manual']->status);
958         $this->assertEquals(strtotime($data['enrolment_1_startdate']), $enroldata['manual']->enrolstartdate);
959         $this->assertEquals(strtotime('1970-01-01 GMT + ' . $data['enrolment_1_enrolperiod']), $enroldata['manual']->enrolperiod);
960         $this->assertEquals(strtotime('12th July 2013'), $enroldata['manual']->enrolenddate);
961     }
963     public function test_idnumber_problems() {
964         $this->resetAfterTest(true);
966         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'sntaken', 'idnumber' => 'taken'));
967         $c2 = $this->getDataGenerator()->create_course();
969         // Create with existing ID number.
970         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
971         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
972         $data = array('shortname' => 'c2', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '1',
973             'idnumber' => $c1->idnumber);
974         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
975         $this->assertFalse($co->prepare());
976         $this->assertArrayHasKey('idnumberalreadyinuse', $co->get_errors());
978         // Rename to existing ID number.
979         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
980         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
981         $data = array('shortname' => $c2->shortname, 'rename' => 'SN', 'idnumber' => $c1->idnumber);
982         $importoptions = array('canrename' => true);
983         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
984         $this->assertFalse($co->prepare());
985         $this->assertArrayHasKey('cannotrenameidnumberconflict', $co->get_errors());
987         // Incrementing shortname increments idnumber.
988         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
989         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
990         $data = array('shortname' => $c1->shortname, 'idnumber' => $c1->idnumber, 'summary' => 'S', 'fullname' => 'F',
991             'category' => 1);
992         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
993         $this->assertTrue($co->prepare());
994         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
995         $this->assertArrayHasKey('courseidnumberincremented', $co->get_statuses());
996         $data = $co->get_data();
997         $this->assertEquals('sntaken_2', $data['shortname']);
998         $this->assertEquals('taken_2', $data['idnumber']);
1000         // Incrementing shortname increments idnumber unless available.
1001         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1002         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1003         $data = array('shortname' => $c1->shortname, 'idnumber' => 'nottaken', 'summary' => 'S', 'fullname' => 'F',
1004             'category' => 1);
1005         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1006         $this->assertTrue($co->prepare());
1007         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1008         $this->assertArrayNotHasKey('courseidnumberincremented', $co->get_statuses());
1009         $data = $co->get_data();
1010         $this->assertEquals('sntaken_2', $data['shortname']);
1011         $this->assertEquals('nottaken', $data['idnumber']);
1012     }
1014     public function test_generate_shortname() {
1015         $this->resetAfterTest(true);
1017         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'taken'));
1019         // Generate a shortname.
1020         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1021         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1022         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1', 'idnumber' => 'IDN');
1023         $importoptions = array('shortnametemplate' => '%i');
1024         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1025         $this->assertTrue($co->prepare());
1026         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1028         // Generate a shortname without a template.
1029         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1030         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1031         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1032         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1033         $this->assertFalse($co->prepare());
1034         $this->assertArrayHasKey('missingshortnamenotemplate', $co->get_errors());
1036         // Generate a shortname in update mode.
1037         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1038         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1039         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1040         $importoptions = array('shortnametemplate' => '%f');
1041         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1042         $this->assertFalse($co->prepare());
1043         // Commented because we never get here as the course without shortname does not exist.
1044         // $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1046         // Generate a shortname to a course that already exists.
1047         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1048         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1049         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1050         $importoptions = array('shortnametemplate' => '%f');
1051         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1052         $this->assertFalse($co->prepare());
1053         $this->assertArrayHasKey('generatedshortnamealreadyinuse', $co->get_errors());
1055         // Generate a shortname to a course that already exists will be incremented.
1056         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1057         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1058         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1059         $importoptions = array('shortnametemplate' => '%f');
1060         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1061         $this->assertTrue($co->prepare());
1062         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1063         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1064     }
1066     public function test_mess_with_frontpage() {
1067         global $SITE;
1068         $this->resetAfterTest(true);
1070         // Updating the front page.
1071         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1072         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1073         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1074         $importoptions = array();
1075         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1076         $this->assertFalse($co->prepare());
1077         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1079         // Updating the front page.
1080         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1081         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1082         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1083         $importoptions = array();
1084         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1085         $this->assertFalse($co->prepare());
1086         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1088         // Generating a shortname should not be allowed in update mode, and so we cannot update the front page.
1089         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1090         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1091         $data = array('idnumber' => 'NewIDN', 'fullname' => 'FN', 'category' => 1);
1092         $importoptions = array('shortnametemplate' => $SITE->shortname);
1093         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1094         $this->assertFalse($co->prepare());
1095         $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1097         // Renaming to the front page should not be allowed.
1098         $c1 = $this->getDataGenerator()->create_course();
1099         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1100         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1101         $data = array('shortname' => $c1->shortname, 'fullname' => 'FN', 'idnumber' => 'NewIDN', 'rename' => $SITE->shortname);
1102         $importoptions = array('canrename' => true);
1103         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1104         $this->assertFalse($co->prepare());
1105         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
1107     }