a15de21e3fb795dd0061b8edd8e61b348ff33cfd
[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             'startdate' => '8 June 1990',
248             'enddate' => '18 June 1990',
249             'idnumber' => '123abc',
250             'summary' => 'Summary',
251             'format' => 'weeks',
252             'theme' => 'afterburner',
253             'lang' => 'en',
254             'newsitems' => '7',
255             'showgrades' => '0',
256             'showreports' => '1',
257             'legacyfiles' => '1',
258             'maxbytes' => '1234',
259             'groupmode' => '2',
260             'groupmodeforce' => '1',
261             'enablecompletion' => '1',
262             'tags' => 'Cat, Dog',
264             'role_teacher' => 'Knight',
265             'role_manager' => 'Jedi',
267             'enrolment_1' => 'guest',
268             'enrolment_2' => 'self',
269             'enrolment_2_roleid' => '1',
270             'enrolment_3' => 'manual',
271             'enrolment_3_disable' => '1',
272         );
274         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
275         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
276         $this->assertTrue($co->prepare());
277         $co->proceed();
278         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
279         $course = $DB->get_record('course', array('shortname' => 'c1'));
280         $ctx = context_course::instance($course->id);
282         $this->assertEquals($data['fullname'], $course->fullname);
283         $this->assertEquals($data['category'], $course->category);
284         $this->assertEquals($data['visible'], $course->visible);
285         $this->assertEquals(mktime(0, 0, 0, 6, 8, 1990), $course->startdate);
286         $this->assertEquals(mktime(0, 0, 0, 6, 18, 1990), $course->enddate);
287         $this->assertEquals($data['idnumber'], $course->idnumber);
288         $this->assertEquals($data['summary'], $course->summary);
289         $this->assertEquals($data['format'], $course->format);
290         $this->assertEquals($data['theme'], $course->theme);
291         $this->assertEquals($data['lang'], $course->lang);
292         $this->assertEquals($data['newsitems'], $course->newsitems);
293         $this->assertEquals($data['showgrades'], $course->showgrades);
294         $this->assertEquals($data['showreports'], $course->showreports);
295         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
296         $this->assertEquals($data['maxbytes'], $course->maxbytes);
297         $this->assertEquals($data['groupmode'], $course->groupmode);
298         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
299         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
300         $this->assertEquals($data['tags'], join(', ', core_tag_tag::get_item_tags_array('core', 'course', $course->id)));
302         // Roles.
303         $roleids = array();
304         $roles = get_all_roles();
305         foreach ($roles as $role) {
306             $roleids[$role->shortname] = $role->id;
307         }
308         $this->assertEquals('Knight', $DB->get_field_select('role_names', 'name',
309             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
310         $this->assertEquals('Jedi', $DB->get_field_select('role_names', 'name',
311             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
313         // Enrolment methods.
314         $enroldata = array();
315         $instances = enrol_get_instances($course->id, false);
316         $this->assertCount(3, $instances);
317         foreach ($instances as $instance) {
318             $enroldata[$instance->enrol] = $instance;
319         }
321         $this->assertNotEmpty($enroldata['guest']);
322         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['guest']->status);
323         $this->assertNotEmpty($enroldata['self']);
324         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
325         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
326         $this->assertNotEmpty($enroldata['manual']);
327         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['manual']->status);
329         // Update existing course.
330         $cat = $this->getDataGenerator()->create_category();
331         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
332         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
333         $data = array(
334             'shortname' => 'c1',
335             'fullname' => 'Fullname 2',
336             'category' => $cat->id,
337             'visible' => '1',
338             'startdate' => '11 June 1984',
339             'enddate' => '31 June 1984',
340             'idnumber' => 'changeidn',
341             'summary' => 'Summary 2',
342             'format' => 'topics',
343             'theme' => 'clean',
344             'lang' => '',
345             'newsitems' => '2',
346             'showgrades' => '1',
347             'showreports' => '0',
348             'legacyfiles' => '0',
349             'maxbytes' => '4321',
350             'groupmode' => '1',
351             'groupmodeforce' => '0',
352             'enablecompletion' => '0',
354             'role_teacher' => 'Teacher',
355             'role_manager' => 'Manager',
357             'enrolment_1' => 'guest',
358             'enrolment_1_disable' => '1',
359             'enrolment_2' => 'self',
360             'enrolment_2_roleid' => '2',
361             'enrolment_3' => 'manual',
362             'enrolment_3_delete' => '1',
363         );
365         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
366         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
367         $this->assertTrue($co->prepare());
368         $co->proceed();
369         $course = $DB->get_record('course', array('shortname' => 'c1'));
370         $ctx = context_course::instance($course->id);
372         $this->assertEquals($data['fullname'], $course->fullname);
373         $this->assertEquals($data['category'], $course->category);
374         $this->assertEquals($data['visible'], $course->visible);
375         $this->assertEquals(mktime(0, 0, 0, 6, 11, 1984), $course->startdate);
376         $this->assertEquals(mktime(0, 0, 0, 6, 31, 1984), $course->enddate);
377         $this->assertEquals($data['idnumber'], $course->idnumber);
378         $this->assertEquals($data['summary'], $course->summary);
379         $this->assertEquals($data['format'], $course->format);
380         $this->assertEquals($data['theme'], $course->theme);
381         $this->assertEquals($data['lang'], $course->lang);
382         $this->assertEquals($data['newsitems'], $course->newsitems);
383         $this->assertEquals($data['showgrades'], $course->showgrades);
384         $this->assertEquals($data['showreports'], $course->showreports);
385         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
386         $this->assertEquals($data['maxbytes'], $course->maxbytes);
387         $this->assertEquals($data['groupmode'], $course->groupmode);
388         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
389         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
391         // Roles.
392         $roleids = array();
393         $roles = get_all_roles();
394         foreach ($roles as $role) {
395             $roleids[$role->shortname] = $role->id;
396         }
397         $this->assertEquals('Teacher', $DB->get_field_select('role_names', 'name',
398             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
399         $this->assertEquals('Manager', $DB->get_field_select('role_names', 'name',
400             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
402         // Enrolment methods.
403         $enroldata = array();
404         $instances = enrol_get_instances($course->id, false);
405         $this->assertCount(2, $instances);
406         foreach ($instances as $instance) {
407             $enroldata[$instance->enrol] = $instance;
408         }
410         $this->assertNotEmpty($enroldata['guest']);
411         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['guest']->status);
412         $this->assertNotEmpty($enroldata['self']);
413         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
414         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
415     }
417     public function test_default_data_saved() {
418         global $DB;
419         $this->resetAfterTest(true);
421         // Create.
422         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
423         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
424         $data = array(
425             'shortname' => 'c1',
426         );
427         $defaultdata = array(
428             'fullname' => 'Fullname',
429             'category' => '1',
430             'visible' => '0',
431             'startdate' => 644803200,
432             'enddate' => 645667200,
433             'idnumber' => '123abc',
434             'summary' => 'Summary',
435             'format' => 'weeks',
436             'theme' => 'afterburner',
437             'lang' => 'en',
438             'newsitems' => '7',
439             'showgrades' => '0',
440             'showreports' => '1',
441             'legacyfiles' => '1',
442             'maxbytes' => '1234',
443             'groupmode' => '2',
444             'groupmodeforce' => '1',
445             'enablecompletion' => '1',
446         );
448         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
449         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
450         $this->assertTrue($co->prepare());
451         $co->proceed();
452         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
453         $course = $DB->get_record('course', array('shortname' => 'c1'));
454         $ctx = context_course::instance($course->id);
456         $this->assertEquals($defaultdata['fullname'], $course->fullname);
457         $this->assertEquals($defaultdata['category'], $course->category);
458         $this->assertEquals($defaultdata['visible'], $course->visible);
459         $this->assertEquals($defaultdata['startdate'], $course->startdate);
460         $this->assertEquals($defaultdata['enddate'], $course->enddate);
461         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
462         $this->assertEquals($defaultdata['summary'], $course->summary);
463         $this->assertEquals($defaultdata['format'], $course->format);
464         $this->assertEquals($defaultdata['theme'], $course->theme);
465         $this->assertEquals($defaultdata['lang'], $course->lang);
466         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
467         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
468         $this->assertEquals($defaultdata['showreports'], $course->showreports);
469         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
470         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
471         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
472         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
473         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
475         // Update.
476         $cat = $this->getDataGenerator()->create_category();
477         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
478         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
479         $data = array(
480             'shortname' => 'c1',
481         );
482         $defaultdata = array(
483             'fullname' => 'Fullname 2',
484             'category' => $cat->id,
485             'visible' => '1',
486             'startdate' => 455760000,
487             'enddate' => 457488000,
488             'idnumber' => 'changedid',
489             'summary' => 'Summary 2',
490             'format' => 'topics',
491             'theme' => 'clean',
492             'lang' => '',
493             'newsitems' => '2',
494             'showgrades' => '1',
495             'showreports' => '0',
496             'legacyfiles' => '0',
497             'maxbytes' => '1111',
498             'groupmode' => '1',
499             'groupmodeforce' => '0',
500             'enablecompletion' => '0',
501         );
503         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
504         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
505         $this->assertTrue($co->prepare());
506         $co->proceed();
507         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
508         $course = $DB->get_record('course', array('shortname' => 'c1'));
509         $ctx = context_course::instance($course->id);
511         $this->assertEquals($defaultdata['fullname'], $course->fullname);
512         $this->assertEquals($defaultdata['category'], $course->category);
513         $this->assertEquals($defaultdata['visible'], $course->visible);
514         $this->assertEquals($defaultdata['startdate'], $course->startdate);
515         $this->assertEquals($defaultdata['enddate'], $course->enddate);
516         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
517         $this->assertEquals($defaultdata['summary'], $course->summary);
518         $this->assertEquals($defaultdata['format'], $course->format);
519         $this->assertEquals($defaultdata['theme'], $course->theme);
520         $this->assertEquals($defaultdata['lang'], $course->lang);
521         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
522         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
523         $this->assertEquals($defaultdata['showreports'], $course->showreports);
524         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
525         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
526         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
527         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
528         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
529     }
531     public function test_rename() {
532         global $DB;
533         $this->resetAfterTest(true);
535         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1'));
536         $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'c2'));
538         // Cannot rename when creating.
539         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
540         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
541         $importoptions = array('canrename' => true);
542         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
543         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
544         $this->assertFalse($co->prepare());
545         $this->assertArrayHasKey('courseexistsanduploadnotallowed', $co->get_errors());
547         // Cannot rename when creating.
548         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
549         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
550         $importoptions = array('canrename' => true);
551         $data = array('shortname' => 'c1', 'rename' => 'newshortname', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
552         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
553         $this->assertFalse($co->prepare());
554         $this->assertArrayHasKey('canonlyrenameinupdatemode', $co->get_errors());
556         // Error when not allowed to rename the course.
557         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
558         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
559         $importoptions = array('canrename' => false);
560         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
561         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
562         $this->assertFalse($co->prepare());
563         $this->assertArrayHasKey('courserenamingnotallowed', $co->get_errors());
565         // Can rename when updating.
566         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
567         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
568         $importoptions = array('canrename' => true);
569         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
570         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
571         $this->assertTrue($co->prepare());
572         $co->proceed();
573         $this->assertEquals('newshortname', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
575         // Can rename when updating.
576         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
577         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
578         $importoptions = array('canrename' => true);
579         $data = array('shortname' => 'newshortname', 'rename' => 'newshortname2');
580         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
581         $this->assertTrue($co->prepare());
582         $co->proceed();
583         $this->assertEquals('newshortname2', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
585         // Error when course does not exist.
586         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
587         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
588         $importoptions = array('canrename' => true);
589         $data = array('shortname' => 'DoesNotExist', 'rename' => 'c1', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
590         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
591         $this->assertFalse($co->prepare());
592         $this->assertArrayHasKey('cannotrenamecoursenotexist', $co->get_errors());
594         // Renaming still updates the other values.
595         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
596         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
597         $importoptions = array('canrename' => true);
598         $data = array('shortname' => 'newshortname2', 'rename' => 'c1', 'fullname' => 'Another fullname!');
599         $defaultdata = array('summary' => 'New summary!');
600         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata, $importoptions);
601         $this->assertTrue($co->prepare());
602         $co->proceed();
603         $this->assertEquals('c1', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
604         $this->assertEquals('New summary!', $DB->get_field_select('course', 'summary', 'id = :id', array('id' => $c1->id)));
605         $this->assertEquals('Another fullname!', $DB->get_field_select('course', 'fullname', 'id = :id', array('id' => $c1->id)));
607         // Renaming with invalid shortname.
608         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
609         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
610         $importoptions = array('canrename' => true);
611         $data = array('shortname' => 'c1', 'rename' => '<span>invalid</span>');
612         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
613         $this->assertFalse($co->prepare());
614         $this->assertArrayHasKey('invalidshortname', $co->get_errors());
616         // Renaming with invalid shortname.
617         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
618         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
619         $importoptions = array('canrename' => true);
620         $data = array('shortname' => 'c1', 'rename' => 'c2');
621         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
622         $this->assertFalse($co->prepare());
623         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
624     }
626     public function test_restore_course() {
627         global $DB;
628         $this->resetAfterTest(true);
629         $this->setAdminUser();
631         $c1 = $this->getDataGenerator()->create_course();
632         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
634         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
635         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
636         $data = array('shortname' => 'A1', 'templatecourse' => $c1->shortname, 'summary' => 'A', 'category' => 1,
637             'fullname' => 'A1');
638         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
639         $this->assertTrue($co->prepare());
640         $co->proceed();
641         $course = $DB->get_record('course', array('shortname' => 'A1'));
642         $modinfo = get_fast_modinfo($course);
643         $found = false;
644         foreach ($modinfo->get_cms() as $cmid => $cm) {
645             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
646                 $found = true;
647                 break;
648             }
649         }
650         $this->assertTrue($found);
652         // Restoring twice from the same course should work.
653         $data = array('shortname' => 'B1', 'templatecourse' => $c1->shortname, 'summary' => 'B', 'category' => 1,
654             'fullname' => 'B1');
655         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
656         $this->assertTrue($co->prepare());
657         $co->proceed();
658         $course = $DB->get_record('course', array('shortname' => 'B1'));
659         $modinfo = get_fast_modinfo($course);
660         $found = false;
661         foreach ($modinfo->get_cms() as $cmid => $cm) {
662             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
663                 $found = true;
664                 break;
665             }
666         }
667         $this->assertTrue($found);
668     }
670     public function test_restore_file() {
671         global $DB;
672         $this->resetAfterTest(true);
673         $this->setAdminUser();
675         $c1 = $this->getDataGenerator()->create_course();
676         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
678         // Restore from a file, checking that the file takes priority over the templatecourse.
679         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
680         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
681         $data = array('shortname' => 'A1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
682             'summary' => 'A', 'category' => 1, 'fullname' => 'A1', 'templatecourse' => $c1->shortname);
683         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
684         $this->assertTrue($co->prepare());
685         $co->proceed();
686         $course = $DB->get_record('course', array('shortname' => 'A1'));
687         $modinfo = get_fast_modinfo($course);
688         $found = false;
689         foreach ($modinfo->get_cms() as $cmid => $cm) {
690             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
691                 $found = true;
692             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
693                 // We should not find this!
694                 $this->assertTrue(false);
695             }
696         }
697         $this->assertTrue($found);
699         // Restoring twice from the same file should work.
700         $data = array('shortname' => 'B1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
701             'summary' => 'B', 'category' => 1, 'fullname' => 'B1');
702         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
703         $this->assertTrue($co->prepare());
704         $co->proceed();
705         $course = $DB->get_record('course', array('shortname' => 'B1'));
706         $modinfo = get_fast_modinfo($course);
707         $found = false;
708         foreach ($modinfo->get_cms() as $cmid => $cm) {
709             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
710                 $found = true;
711             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
712                 // We should not find this!
713                 $this->assertTrue(false);
714             }
715         }
716         $this->assertTrue($found);
717     }
719     public function test_restore_invalid_file() {
720         $this->resetAfterTest();
722         // Restore from a non-existing file should not be allowed.
723         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
724         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
725         $data = array('shortname' => 'A1', 'backupfile' => '/lead/no/where',
726             'category' => 1, 'fullname' => 'A1');
727         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
728         $this->assertFalse($co->prepare());
729         $this->assertArrayHasKey('cannotreadbackupfile', $co->get_errors());
731         // Restore from an invalid file should not be allowed.
732         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
733         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
734         $data = array('shortname' => 'A1', 'backupfile' => __FILE__,
735             'category' => 1, 'fullname' => 'A1');
736         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
738         $this->assertFalse($co->prepare());
739         $this->assertArrayHasKey('invalidbackupfile', $co->get_errors());
741         // Zip packer throws a debugging message, this assertion is only here to prevent
742         // the message from being displayed.
743         $this->assertDebuggingCalled();
744     }
746     public function test_restore_invalid_course() {
747         $this->resetAfterTest();
749         // Restore from an invalid file should not be allowed.
750         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
751         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
752         $data = array('shortname' => 'A1', 'templatecourse' => 'iamnotavalidcourse',
753             'category' => 1, 'fullname' => 'A1');
754         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
755         $this->assertFalse($co->prepare());
756         $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $co->get_errors());
757     }
759     /**
760      * Testing the reset on groups, group members and enrolments.
761      */
762     public function test_reset() {
763         global $DB;
764         $this->resetAfterTest(true);
766         $c1 = $this->getDataGenerator()->create_course();
767         $c1ctx = context_course::instance($c1->id);
768         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
769         $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
771         $u1 = $this->getDataGenerator()->create_user();
772         $this->getDataGenerator()->enrol_user($u1->id, $c1->id, $studentrole->id);
773         $this->assertCount(1, get_enrolled_users($c1ctx));
775         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
776         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
777         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
778         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
780         // Wrong mode.
781         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
782         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
783         $data = array('shortname' => 'DoesNotExist', 'reset' => '1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 1);
784         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
785         $this->assertFalse($co->prepare());
786         $this->assertArrayHasKey('canonlyresetcourseinupdatemode', $co->get_errors());
787         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
788         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
789         $this->assertCount(1, get_enrolled_users($c1ctx));
791         // Reset not allowed.
792         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
793         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
794         $data = array('shortname' => $c1->shortname, 'reset' => '1');
795         $importoptions = array('canreset' => false);
796         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
797         $this->assertFalse($co->prepare());
798         $this->assertArrayHasKey('courseresetnotallowed', $co->get_errors());
799         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
800         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
801         $this->assertCount(1, get_enrolled_users($c1ctx));
803         // Reset allowed but not requested.
804         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
805         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
806         $data = array('shortname' => $c1->shortname, 'reset' => '0');
807         $importoptions = array('canreset' => true);
808         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
809         $this->assertTrue($co->prepare());
810         $co->proceed();
811         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
812         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
813         $this->assertCount(1, get_enrolled_users($c1ctx));
815         // Reset passed as a default parameter, should not be taken in account.
816         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
817         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
818         $data = array('shortname' => $c1->shortname);
819         $importoptions = array('canreset' => true);
820         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array('reset' => 1), $importoptions);
821         $this->assertTrue($co->prepare());
822         $co->proceed();
823         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
824         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
825         $this->assertCount(1, get_enrolled_users($c1ctx));
827         // Reset executed from data.
828         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
829         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
830         $data = array('shortname' => $c1->shortname, 'reset' => 1);
831         $importoptions = array('canreset' => true);
832         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
833         $this->assertTrue($co->prepare());
834         $co->proceed();
835         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
836         $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
837         $this->assertCount(0, get_enrolled_users($c1ctx));
839         // Reset executed from import option.
840         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
841         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
842         $data = array('shortname' => $c1->shortname, 'reset' => 0);
843         $importoptions = array('reset' => 1, 'canreset' => true);
844         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
846         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
847         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
848         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
849         $this->assertTrue($co->prepare());
850         $co->proceed();
851         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
852     }
854     public function test_create_bad_category() {
855         global $DB;
856         $this->resetAfterTest(true);
858         // Ensure fails when category cannot be resolved upon creation.
859         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
860         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
861         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 'Wrong cat');
862         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
863         $this->assertFalse($co->prepare());
864         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
866         // Ensure fails when category is 0 on create.
867         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
868         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
869         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '0');
870         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
871         $this->assertFalse($co->prepare());
872         $this->assertArrayHasKey('missingmandatoryfields', $co->get_errors());
874         // Ensure fails when category cannot be resolved upon update.
875         $c1 = $this->getDataGenerator()->create_course();
876         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
877         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
878         $data = array('shortname' => $c1->shortname, 'category' => 'Wrong cat');
879         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
880         $this->assertFalse($co->prepare());
881         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
883         // Ensure does not update the category when it is 0.
884         $c1 = $this->getDataGenerator()->create_course();
885         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
886         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
887         $data = array('shortname' => $c1->shortname, 'category' => '0');
888         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
889         $this->assertTrue($co->prepare());
890         $this->assertEmpty($co->get_errors());
891         $this->assertEmpty($co->get_statuses());
892         $co->proceed();
893         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
895         // Ensure does not update the category when it is set to 0 in the defaults.
896         $c1 = $this->getDataGenerator()->create_course();
897         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
898         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
899         $data = array('shortname' => $c1->shortname);
900         $defaults = array('category' => '0');
901         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
902         $this->assertTrue($co->prepare());
903         $this->assertEmpty($co->get_errors());
904         $this->assertEmpty($co->get_statuses());
905         $co->proceed();
906         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
907     }
909     public function test_enrolment_data() {
910         $this->resetAfterTest(true);
912         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
913         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
914         $data = array('shortname' => 'c1', 'summary' => 'S', 'fullname' => 'FN', 'category' => '1');
915         $data['enrolment_1'] = 'manual';
916         $data['enrolment_1_role'] = 'teacher';
917         $data['enrolment_1_startdate'] = '2nd July 2013';
918         $data['enrolment_1_enddate'] = '2nd August 2013';
919         $data['enrolment_1_enrolperiod'] = '10 days';
920         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
921         $this->assertTrue($co->prepare());
922         $co->proceed();
924         // Enrolment methods.
925         $enroldata = array();
926         $instances = enrol_get_instances($co->get_id(), false);
927         foreach ($instances as $instance) {
928             $enroldata[$instance->enrol] = $instance;
929         }
931         $this->assertNotEmpty($enroldata['manual']);
932         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['manual']->status);
933         $this->assertEquals(strtotime($data['enrolment_1_startdate']), $enroldata['manual']->enrolstartdate);
934         $this->assertEquals(strtotime('1970-01-01 GMT + ' . $data['enrolment_1_enrolperiod']), $enroldata['manual']->enrolperiod);
935         $this->assertEquals(strtotime('12th July 2013'), $enroldata['manual']->enrolenddate);
936     }
938     public function test_idnumber_problems() {
939         $this->resetAfterTest(true);
941         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'sntaken', 'idnumber' => 'taken'));
942         $c2 = $this->getDataGenerator()->create_course();
944         // Create with existing ID number.
945         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
946         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
947         $data = array('shortname' => 'c2', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '1',
948             'idnumber' => $c1->idnumber);
949         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
950         $this->assertFalse($co->prepare());
951         $this->assertArrayHasKey('idnumberalreadyinuse', $co->get_errors());
953         // Rename to existing ID number.
954         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
955         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
956         $data = array('shortname' => $c2->shortname, 'rename' => 'SN', 'idnumber' => $c1->idnumber);
957         $importoptions = array('canrename' => true);
958         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
959         $this->assertFalse($co->prepare());
960         $this->assertArrayHasKey('cannotrenameidnumberconflict', $co->get_errors());
962         // Incrementing shortname increments idnumber.
963         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
964         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
965         $data = array('shortname' => $c1->shortname, 'idnumber' => $c1->idnumber, 'summary' => 'S', 'fullname' => 'F',
966             'category' => 1);
967         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
968         $this->assertTrue($co->prepare());
969         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
970         $this->assertArrayHasKey('courseidnumberincremented', $co->get_statuses());
971         $data = $co->get_data();
972         $this->assertEquals('sntaken_2', $data['shortname']);
973         $this->assertEquals('taken_2', $data['idnumber']);
975         // Incrementing shortname increments idnumber unless available.
976         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
977         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
978         $data = array('shortname' => $c1->shortname, 'idnumber' => 'nottaken', 'summary' => 'S', 'fullname' => 'F',
979             'category' => 1);
980         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
981         $this->assertTrue($co->prepare());
982         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
983         $this->assertArrayNotHasKey('courseidnumberincremented', $co->get_statuses());
984         $data = $co->get_data();
985         $this->assertEquals('sntaken_2', $data['shortname']);
986         $this->assertEquals('nottaken', $data['idnumber']);
987     }
989     public function test_generate_shortname() {
990         $this->resetAfterTest(true);
992         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'taken'));
994         // Generate a shortname.
995         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
996         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
997         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1', 'idnumber' => 'IDN');
998         $importoptions = array('shortnametemplate' => '%i');
999         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1000         $this->assertTrue($co->prepare());
1001         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1003         // Generate a shortname without a template.
1004         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1005         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1006         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1007         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1008         $this->assertFalse($co->prepare());
1009         $this->assertArrayHasKey('missingshortnamenotemplate', $co->get_errors());
1011         // Generate a shortname in update mode.
1012         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1013         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1014         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1015         $importoptions = array('shortnametemplate' => '%f');
1016         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1017         $this->assertFalse($co->prepare());
1018         // Commented because we never get here as the course without shortname does not exist.
1019         // $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1021         // Generate a shortname to a course that already exists.
1022         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1023         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1024         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1025         $importoptions = array('shortnametemplate' => '%f');
1026         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1027         $this->assertFalse($co->prepare());
1028         $this->assertArrayHasKey('generatedshortnamealreadyinuse', $co->get_errors());
1030         // Generate a shortname to a course that already exists will be incremented.
1031         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1032         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1033         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1034         $importoptions = array('shortnametemplate' => '%f');
1035         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1036         $this->assertTrue($co->prepare());
1037         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1038         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1039     }
1041     public function test_mess_with_frontpage() {
1042         global $SITE;
1043         $this->resetAfterTest(true);
1045         // Updating the front page.
1046         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1047         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1048         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1049         $importoptions = array();
1050         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1051         $this->assertFalse($co->prepare());
1052         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1054         // Updating the front page.
1055         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1056         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1057         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1058         $importoptions = array();
1059         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1060         $this->assertFalse($co->prepare());
1061         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1063         // Generating a shortname should not be allowed in update mode, and so we cannot update the front page.
1064         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1065         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1066         $data = array('idnumber' => 'NewIDN', 'fullname' => 'FN', 'category' => 1);
1067         $importoptions = array('shortnametemplate' => $SITE->shortname);
1068         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1069         $this->assertFalse($co->prepare());
1070         $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1072         // Renaming to the front page should not be allowed.
1073         $c1 = $this->getDataGenerator()->create_course();
1074         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1075         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1076         $data = array('shortname' => $c1->shortname, 'fullname' => 'FN', 'idnumber' => 'NewIDN', 'rename' => $SITE->shortname);
1077         $importoptions = array('canrename' => true);
1078         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1079         $this->assertFalse($co->prepare());
1080         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
1082     }