Revert "Merge branch 'MDL-43127-master' of git://github.com/FMCorz/moodle"
[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      * Tidy up open files that may be left open.
40      */
41     protected function tearDown() {
42         gc_collect_cycles();
43     }
45     public function test_proceed_without_prepare() {
46         $this->resetAfterTest(true);
47         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
48         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
49         $data = array();
50         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
51         $this->setExpectedException('coding_exception');
52         $co->proceed();
53     }
55     public function test_proceed_when_prepare_failed() {
56         $this->resetAfterTest(true);
57         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
58         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
59         $data = array();
60         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
61         $this->assertFalse($co->prepare());
62         $this->setExpectedException('moodle_exception');
63         $co->proceed();
64     }
66     public function test_proceed_when_already_started() {
67         $this->resetAfterTest(true);
68         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
69         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
70         $data = array('shortname' => 'test', 'fullname' => 'New course', 'summary' => 'New', 'category' => 1);
71         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
72         $this->assertTrue($co->prepare());
73         $co->proceed();
74         $this->setExpectedException('coding_exception');
75         $co->proceed();
76     }
78     public function test_invalid_shortname() {
79         $this->resetAfterTest(true);
80         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
81         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
82         $data = array('shortname' => '<invalid>', 'fullname' => 'New course', 'summary' => 'New', 'category' => 1);
83         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
84         $this->assertFalse($co->prepare());
85         $this->assertArrayHasKey('invalidshortname', $co->get_errors());
86     }
88     public function test_create() {
89         global $DB;
90         $this->resetAfterTest(true);
92         // Existing course.
93         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1', 'summary' => 'Yay!'));
94         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
96         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
98         // Try to add a new course.
99         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
100         $data = array('shortname' => 'newcourse', 'fullname' => 'New course', 'summary' => 'New', 'category' => 1);
101         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
102         $this->assertTrue($co->prepare());
103         $this->assertFalse($DB->record_exists('course', array('shortname' => 'newcourse')));
104         $co->proceed();
105         $this->assertTrue($DB->record_exists('course', array('shortname' => 'newcourse')));
107         // Try to add a new course, that already exists.
108         $coursecount = $DB->count_records('course', array());
109         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
110         $data = array('shortname' => 'c1', 'fullname' => 'C1FN', 'summary' => 'C1', 'category' => 1);
111         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
112         $this->assertFalse($co->prepare());
113         $this->assertArrayHasKey('courseexistsanduploadnotallowed', $co->get_errors());
114         $this->assertEquals($coursecount, $DB->count_records('course', array()));
115         $this->assertNotEquals('C1', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
117         // Try to add new with shortname incrementation.
118         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
119         $data = array('shortname' => 'c1', 'fullname' => 'C1FN', 'summary' => 'C1', 'category' => 1);
120         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
121         $this->assertTrue($co->prepare());
122         $co->proceed();
123         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c2')));
124     }
126     public function test_delete() {
127         global $DB;
128         $this->resetAfterTest(true);
130         $c1 = $this->getDataGenerator()->create_course();
131         $c2 = $this->getDataGenerator()->create_course();
133         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
134         $this->assertFalse($DB->record_exists('course', array('shortname' => 'DoesNotExist')));
136         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
137         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
139         // Try delete when option not available.
140         $importoptions = array('candelete' => false);
141         $data = array('shortname' => $c1->shortname, 'delete' => 1);
142         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
143         $this->assertFalse($co->prepare());
144         $this->assertArrayHasKey('coursedeletionnotallowed', $co->get_errors());
145         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
147         // Try delete when not requested.
148         $importoptions = array('candelete' => true);
149         $data = array('shortname' => $c1->shortname, 'delete' => 0);
150         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
151         $this->assertTrue($co->prepare());
152         $co->proceed();
153         $this->assertTrue($DB->record_exists('course', array('shortname' => $c1->shortname)));
155         // Try delete when requested.
156         $importoptions = array('candelete' => true);
157         $data = array('shortname' => $c1->shortname, 'delete' => 1);
158         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
159         $this->assertTrue($co->prepare());
160         $co->proceed();
161         $this->assertFalse($DB->record_exists('course', array('shortname' => $c1->shortname)));
162         $this->assertTrue($DB->record_exists('course', array('shortname' => $c2->shortname)));
164         // Try deleting non-existing record, this should not fail.
165         $data = array('shortname' => 'DoesNotExist', 'delete' => 1);
166         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
167         $this->assertFalse($co->prepare());
168         $this->assertArrayHasKey('cannotdeletecoursenotexist', $co->get_errors());
169     }
171     public function test_update() {
172         global $DB;
173         $this->resetAfterTest(true);
175         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1'));
177         // Try to update with existing shortnames, not allowing creation, and updating nothing.
178         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
179         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
180         $data = array('shortname' => 'c1', 'fullname' => 'New fullname');
181         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
182         $this->assertFalse($co->prepare());
183         $this->assertArrayHasKey('updatemodedoessettonothing', $co->get_errors());
185         // Try to update with non-existing shortnames.
186         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
187         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
188         $data = array('shortname' => 'DoesNotExist', 'fullname' => 'New fullname');
189         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
190         $this->assertFalse($co->prepare());
191         $this->assertArrayHasKey('coursedoesnotexistandcreatenotallowed', $co->get_errors());
193         // Try a proper update.
194         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
195         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
196         $data = array('shortname' => 'c1', 'fullname' => 'New fullname');
197         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
198         $this->assertTrue($co->prepare());
199         $co->proceed();
200         $this->assertEquals('New fullname', $DB->get_field_select('course', 'fullname', 'shortname = :s', array('s' => 'c1')));
202         // Try a proper update with defaults.
203         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
204         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
205         $data = array('shortname' => 'c1', 'fullname' => 'Another fullname');
206         $defaults = array('fullname' => 'Not this one', 'summary' => 'Awesome summary');
207         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
208         $this->assertTrue($co->prepare());
209         $co->proceed();
210         $this->assertEquals('Another fullname', $DB->get_field_select('course', 'fullname', 'shortname = :s', array('s' => 'c1')));
211         $this->assertEquals('Awesome summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
213         // Try a proper update missing only.
214         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
215         $updatemode = tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS;
216         $DB->set_field('course', 'summary', '', array('shortname' => 'c1'));
217         $this->assertEquals('', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
218         $data = array('shortname' => 'c1', 'summary' => 'Fill in summary');
219         $defaults = array('summary' => 'Do not use this summary');
220         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
221         $this->assertTrue($co->prepare());
222         $co->proceed();
223         $this->assertEquals('Fill in summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
225         // Try a proper update missing only using defaults.
226         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
227         $updatemode = tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS;
228         $DB->set_field('course', 'summary', '', array('shortname' => 'c1'));
229         $this->assertEquals('', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
230         $data = array('shortname' => 'c1');
231         $defaults = array('summary' => 'Use this summary');
232         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
233         $this->assertTrue($co->prepare());
234         $co->proceed();
235         $this->assertEquals('Use this summary', $DB->get_field_select('course', 'summary', 'shortname = :s', array('s' => 'c1')));
236     }
238     public function test_data_saved() {
239         global $DB;
240         $this->resetAfterTest(true);
242         // Create.
243         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
244         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
245         $data = array(
246             'shortname' => 'c1',
247             'fullname' => 'Fullname',
248             'category' => '1',
249             'visible' => '0',
250             'startdate' => '8 June 1990',
251             'idnumber' => '123abc',
252             'summary' => 'Summary',
253             'format' => 'weeks',
254             'theme' => 'afterburner',
255             'lang' => 'en',
256             'newsitems' => '7',
257             'showgrades' => '0',
258             'showreports' => '1',
259             'legacyfiles' => '1',
260             'maxbytes' => '1234',
261             'groupmode' => '2',
262             'groupmodeforce' => '1',
263             'enablecompletion' => '1',
265             'role_teacher' => 'Knight',
266             'role_manager' => 'Jedi',
268             'enrolment_1' => 'guest',
269             'enrolment_2' => 'self',
270             'enrolment_2_roleid' => '1',
271             'enrolment_3' => 'manual',
272             'enrolment_3_disable' => '1',
273         );
275         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
276         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
277         $this->assertTrue($co->prepare());
278         $co->proceed();
279         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
280         $course = $DB->get_record('course', array('shortname' => 'c1'));
281         $ctx = context_course::instance($course->id);
283         $this->assertEquals($data['fullname'], $course->fullname);
284         $this->assertEquals($data['category'], $course->category);
285         $this->assertEquals($data['visible'], $course->visible);
286         $this->assertEquals(mktime(0, 0, 0, 6, 8, 1990), $course->startdate);
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);
301         // Roles.
302         $roleids = array();
303         $roles = get_all_roles();
304         foreach ($roles as $role) {
305             $roleids[$role->shortname] = $role->id;
306         }
307         $this->assertEquals('Knight', $DB->get_field_select('role_names', 'name',
308             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
309         $this->assertEquals('Jedi', $DB->get_field_select('role_names', 'name',
310             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
312         // Enrolment methods.
313         $enroldata = array();
314         $instances = enrol_get_instances($course->id, false);
315         $this->assertCount(3, $instances);
316         foreach ($instances as $instance) {
317             $enroldata[$instance->enrol] = $instance;
318         }
320         $this->assertNotEmpty($enroldata['guest']);
321         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['guest']->status);
322         $this->assertNotEmpty($enroldata['self']);
323         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
324         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
325         $this->assertNotEmpty($enroldata['manual']);
326         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['manual']->status);
328         // Update existing course.
329         $cat = $this->getDataGenerator()->create_category();
330         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
331         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
332         $data = array(
333             'shortname' => 'c1',
334             'fullname' => 'Fullname 2',
335             'category' => $cat->id,
336             'visible' => '1',
337             'startdate' => '11 June 1984',
338             'idnumber' => 'changeidn',
339             'summary' => 'Summary 2',
340             'format' => 'topics',
341             'theme' => 'clean',
342             'lang' => '',
343             'newsitems' => '2',
344             'showgrades' => '1',
345             'showreports' => '0',
346             'legacyfiles' => '0',
347             'maxbytes' => '4321',
348             'groupmode' => '1',
349             'groupmodeforce' => '0',
350             'enablecompletion' => '0',
352             'role_teacher' => 'Teacher',
353             'role_manager' => 'Manager',
355             'enrolment_1' => 'guest',
356             'enrolment_1_disable' => '1',
357             'enrolment_2' => 'self',
358             'enrolment_2_roleid' => '2',
359             'enrolment_3' => 'manual',
360             'enrolment_3_delete' => '1',
361         );
363         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
364         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
365         $this->assertTrue($co->prepare());
366         $co->proceed();
367         $course = $DB->get_record('course', array('shortname' => 'c1'));
368         $ctx = context_course::instance($course->id);
370         $this->assertEquals($data['fullname'], $course->fullname);
371         $this->assertEquals($data['category'], $course->category);
372         $this->assertEquals($data['visible'], $course->visible);
373         $this->assertEquals(mktime(0, 0, 0, 6, 11, 1984), $course->startdate);
374         $this->assertEquals($data['idnumber'], $course->idnumber);
375         $this->assertEquals($data['summary'], $course->summary);
376         $this->assertEquals($data['format'], $course->format);
377         $this->assertEquals($data['theme'], $course->theme);
378         $this->assertEquals($data['lang'], $course->lang);
379         $this->assertEquals($data['newsitems'], $course->newsitems);
380         $this->assertEquals($data['showgrades'], $course->showgrades);
381         $this->assertEquals($data['showreports'], $course->showreports);
382         $this->assertEquals($data['legacyfiles'], $course->legacyfiles);
383         $this->assertEquals($data['maxbytes'], $course->maxbytes);
384         $this->assertEquals($data['groupmode'], $course->groupmode);
385         $this->assertEquals($data['groupmodeforce'], $course->groupmodeforce);
386         $this->assertEquals($data['enablecompletion'], $course->enablecompletion);
388         // Roles.
389         $roleids = array();
390         $roles = get_all_roles();
391         foreach ($roles as $role) {
392             $roleids[$role->shortname] = $role->id;
393         }
394         $this->assertEquals('Teacher', $DB->get_field_select('role_names', 'name',
395             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['teacher'])));
396         $this->assertEquals('Manager', $DB->get_field_select('role_names', 'name',
397             'roleid = :roleid AND contextid = :ctxid', array('ctxid' => $ctx->id, 'roleid' => $roleids['manager'])));
399         // Enrolment methods.
400         $enroldata = array();
401         $instances = enrol_get_instances($course->id, false);
402         $this->assertCount(2, $instances);
403         foreach ($instances as $instance) {
404             $enroldata[$instance->enrol] = $instance;
405         }
407         $this->assertNotEmpty($enroldata['guest']);
408         $this->assertEquals(ENROL_INSTANCE_DISABLED, $enroldata['guest']->status);
409         $this->assertNotEmpty($enroldata['self']);
410         $this->assertEquals($data['enrolment_2_roleid'], $enroldata['self']->roleid);
411         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['self']->status);
412     }
414     public function test_default_data_saved() {
415         global $DB;
416         $this->resetAfterTest(true);
418         // Create.
419         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
420         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
421         $data = array(
422             'shortname' => 'c1',
423         );
424         $defaultdata = array(
425             'fullname' => 'Fullname',
426             'category' => '1',
427             'visible' => '0',
428             'startdate' => '8 June 1990',
429             'idnumber' => '123abc',
430             'summary' => 'Summary',
431             'format' => 'weeks',
432             'theme' => 'afterburner',
433             'lang' => 'en',
434             'newsitems' => '7',
435             'showgrades' => '0',
436             'showreports' => '1',
437             'legacyfiles' => '1',
438             'maxbytes' => '1234',
439             'groupmode' => '2',
440             'groupmodeforce' => '1',
441             'enablecompletion' => '1',
442         );
444         $this->assertFalse($DB->record_exists('course', array('shortname' => 'c1')));
445         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
446         $this->assertTrue($co->prepare());
447         $co->proceed();
448         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
449         $course = $DB->get_record('course', array('shortname' => 'c1'));
450         $ctx = context_course::instance($course->id);
452         $this->assertEquals($defaultdata['fullname'], $course->fullname);
453         $this->assertEquals($defaultdata['category'], $course->category);
454         $this->assertEquals($defaultdata['visible'], $course->visible);
455         $this->assertEquals(mktime(0, 0, 0, 6, 8, 1990), $course->startdate);
456         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
457         $this->assertEquals($defaultdata['summary'], $course->summary);
458         $this->assertEquals($defaultdata['format'], $course->format);
459         $this->assertEquals($defaultdata['theme'], $course->theme);
460         $this->assertEquals($defaultdata['lang'], $course->lang);
461         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
462         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
463         $this->assertEquals($defaultdata['showreports'], $course->showreports);
464         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
465         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
466         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
467         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
468         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
470         // Update.
471         $cat = $this->getDataGenerator()->create_category();
472         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
473         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
474         $data = array(
475             'shortname' => 'c1',
476         );
477         $defaultdata = array(
478             'fullname' => 'Fullname 2',
479             'category' => $cat->id,
480             'visible' => '1',
481             'startdate' => '11 June 1984',
482             'idnumber' => 'changedid',
483             'summary' => 'Summary 2',
484             'format' => 'topics',
485             'theme' => 'clean',
486             'lang' => '',
487             'newsitems' => '2',
488             'showgrades' => '1',
489             'showreports' => '0',
490             'legacyfiles' => '0',
491             'maxbytes' => '1111',
492             'groupmode' => '1',
493             'groupmodeforce' => '0',
494             'enablecompletion' => '0',
495         );
497         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
498         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata);
499         $this->assertTrue($co->prepare());
500         $co->proceed();
501         $this->assertTrue($DB->record_exists('course', array('shortname' => 'c1')));
502         $course = $DB->get_record('course', array('shortname' => 'c1'));
503         $ctx = context_course::instance($course->id);
505         $this->assertEquals($defaultdata['fullname'], $course->fullname);
506         $this->assertEquals($defaultdata['category'], $course->category);
507         $this->assertEquals($defaultdata['visible'], $course->visible);
508         $this->assertEquals(mktime(0, 0, 0, 6, 11, 1984), $course->startdate);
509         $this->assertEquals($defaultdata['idnumber'], $course->idnumber);
510         $this->assertEquals($defaultdata['summary'], $course->summary);
511         $this->assertEquals($defaultdata['format'], $course->format);
512         $this->assertEquals($defaultdata['theme'], $course->theme);
513         $this->assertEquals($defaultdata['lang'], $course->lang);
514         $this->assertEquals($defaultdata['newsitems'], $course->newsitems);
515         $this->assertEquals($defaultdata['showgrades'], $course->showgrades);
516         $this->assertEquals($defaultdata['showreports'], $course->showreports);
517         $this->assertEquals($defaultdata['legacyfiles'], $course->legacyfiles);
518         $this->assertEquals($defaultdata['maxbytes'], $course->maxbytes);
519         $this->assertEquals($defaultdata['groupmode'], $course->groupmode);
520         $this->assertEquals($defaultdata['groupmodeforce'], $course->groupmodeforce);
521         $this->assertEquals($defaultdata['enablecompletion'], $course->enablecompletion);
522     }
524     public function test_rename() {
525         global $DB;
526         $this->resetAfterTest(true);
528         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'c1'));
529         $c2 = $this->getDataGenerator()->create_course(array('shortname' => 'c2'));
531         // Cannot rename when creating.
532         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
533         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
534         $importoptions = array('canrename' => true);
535         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
536         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
537         $this->assertFalse($co->prepare());
538         $this->assertArrayHasKey('courseexistsanduploadnotallowed', $co->get_errors());
540         // Cannot rename when creating.
541         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
542         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
543         $importoptions = array('canrename' => true);
544         $data = array('shortname' => 'c1', 'rename' => 'newshortname', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
545         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
546         $this->assertFalse($co->prepare());
547         $this->assertArrayHasKey('canonlyrenameinupdatemode', $co->get_errors());
549         // Error when not allowed to rename the course.
550         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
551         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
552         $importoptions = array('canrename' => false);
553         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
554         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
555         $this->assertFalse($co->prepare());
556         $this->assertArrayHasKey('courserenamingnotallowed', $co->get_errors());
558         // Can rename when updating.
559         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
560         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
561         $importoptions = array('canrename' => true);
562         $data = array('shortname' => 'c1', 'rename' => 'newshortname');
563         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
564         $this->assertTrue($co->prepare());
565         $co->proceed();
566         $this->assertEquals('newshortname', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
568         // Can rename when updating.
569         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
570         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
571         $importoptions = array('canrename' => true);
572         $data = array('shortname' => 'newshortname', 'rename' => 'newshortname2');
573         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
574         $this->assertTrue($co->prepare());
575         $co->proceed();
576         $this->assertEquals('newshortname2', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
578         // Error when course does not exist.
579         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
580         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
581         $importoptions = array('canrename' => true);
582         $data = array('shortname' => 'DoesNotExist', 'rename' => 'c1', 'category' => 1, 'summary' => 'S', 'fullname' => 'F');
583         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
584         $this->assertFalse($co->prepare());
585         $this->assertArrayHasKey('cannotrenamecoursenotexist', $co->get_errors());
587         // Renaming still updates the other values.
588         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
589         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
590         $importoptions = array('canrename' => true);
591         $data = array('shortname' => 'newshortname2', 'rename' => 'c1', 'fullname' => 'Another fullname!');
592         $defaultdata = array('summary' => 'New summary!');
593         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaultdata, $importoptions);
594         $this->assertTrue($co->prepare());
595         $co->proceed();
596         $this->assertEquals('c1', $DB->get_field_select('course', 'shortname', 'id = :id', array('id' => $c1->id)));
597         $this->assertEquals('New summary!', $DB->get_field_select('course', 'summary', 'id = :id', array('id' => $c1->id)));
598         $this->assertEquals('Another fullname!', $DB->get_field_select('course', 'fullname', 'id = :id', array('id' => $c1->id)));
600         // Renaming with invalid shortname.
601         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
602         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
603         $importoptions = array('canrename' => true);
604         $data = array('shortname' => 'c1', 'rename' => '<span>invalid</span>');
605         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
606         $this->assertFalse($co->prepare());
607         $this->assertArrayHasKey('invalidshortname', $co->get_errors());
609         // Renaming with invalid shortname.
610         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
611         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
612         $importoptions = array('canrename' => true);
613         $data = array('shortname' => 'c1', 'rename' => 'c2');
614         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
615         $this->assertFalse($co->prepare());
616         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
617     }
619     public function test_restore_course() {
620         global $DB;
621         $this->resetAfterTest(true);
622         $this->setAdminUser();
624         $c1 = $this->getDataGenerator()->create_course();
625         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
627         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
628         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
629         $data = array('shortname' => 'A1', 'templatecourse' => $c1->shortname, 'summary' => 'A', 'category' => 1,
630             'fullname' => 'A1');
631         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
632         $this->assertTrue($co->prepare());
633         $co->proceed();
634         $course = $DB->get_record('course', array('shortname' => 'A1'));
635         $modinfo = get_fast_modinfo($course);
636         $found = false;
637         foreach ($modinfo->get_cms() as $cmid => $cm) {
638             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
639                 $found = true;
640                 break;
641             }
642         }
643         $this->assertTrue($found);
645         // Restoring twice from the same course should work.
646         $data = array('shortname' => 'B1', 'templatecourse' => $c1->shortname, 'summary' => 'B', 'category' => 1,
647             'fullname' => 'B1');
648         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
649         $this->assertTrue($co->prepare());
650         $co->proceed();
651         $course = $DB->get_record('course', array('shortname' => 'B1'));
652         $modinfo = get_fast_modinfo($course);
653         $found = false;
654         foreach ($modinfo->get_cms() as $cmid => $cm) {
655             if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
656                 $found = true;
657                 break;
658             }
659         }
660         $this->assertTrue($found);
661     }
663     public function test_restore_file() {
664         global $DB;
665         $this->resetAfterTest(true);
666         $this->setAdminUser();
668         $c1 = $this->getDataGenerator()->create_course();
669         $c1f1 = $this->getDataGenerator()->create_module('forum', array('course' => $c1->id));
671         // Restore from a file, checking that the file takes priority over the templatecourse.
672         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
673         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
674         $data = array('shortname' => 'A1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
675             'summary' => 'A', 'category' => 1, 'fullname' => 'A1', 'templatecourse' => $c1->shortname);
676         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
677         $this->assertTrue($co->prepare());
678         $co->proceed();
679         $course = $DB->get_record('course', array('shortname' => 'A1'));
680         $modinfo = get_fast_modinfo($course);
681         $found = false;
682         foreach ($modinfo->get_cms() as $cmid => $cm) {
683             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
684                 $found = true;
685             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
686                 // We should not find this!
687                 $this->assertTrue(false);
688             }
689         }
690         $this->assertTrue($found);
692         // Restoring twice from the same file should work.
693         $data = array('shortname' => 'B1', 'backupfile' => __DIR__ . '/fixtures/backup.mbz',
694             'summary' => 'B', 'category' => 1, 'fullname' => 'B1');
695         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
696         $this->assertTrue($co->prepare());
697         $co->proceed();
698         $course = $DB->get_record('course', array('shortname' => 'B1'));
699         $modinfo = get_fast_modinfo($course);
700         $found = false;
701         foreach ($modinfo->get_cms() as $cmid => $cm) {
702             if ($cm->modname == 'glossary' && $cm->name == 'Imported Glossary') {
703                 $found = true;
704             } else if ($cm->modname == 'forum' && $cm->name == $c1f1->name) {
705                 // We should not find this!
706                 $this->assertTrue(false);
707             }
708         }
709         $this->assertTrue($found);
710     }
712     public function test_restore_invalid_file() {
713         $this->resetAfterTest();
715         // Restore from a non-existing file should not be allowed.
716         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
717         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
718         $data = array('shortname' => 'A1', 'backupfile' => '/lead/no/where',
719             'category' => 1, 'fullname' => 'A1');
720         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
721         $this->assertFalse($co->prepare());
722         $this->assertArrayHasKey('cannotreadbackupfile', $co->get_errors());
724         // Restore from an invalid file should not be allowed.
725         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
726         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
727         $data = array('shortname' => 'A1', 'backupfile' => __FILE__,
728             'category' => 1, 'fullname' => 'A1');
729         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
731         $this->assertFalse($co->prepare());
732         $this->assertArrayHasKey('invalidbackupfile', $co->get_errors());
734         // Zip packer throws a debugging message, this assertion is only here to prevent
735         // the message from being displayed.
736         $this->assertDebuggingCalled();
737     }
739     public function test_restore_invalid_course() {
740         $this->resetAfterTest();
742         // Restore from an invalid file should not be allowed.
743         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
744         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
745         $data = array('shortname' => 'A1', 'templatecourse' => 'iamnotavalidcourse',
746             'category' => 1, 'fullname' => 'A1');
747         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
748         $this->assertFalse($co->prepare());
749         $this->assertArrayHasKey('coursetorestorefromdoesnotexist', $co->get_errors());
750     }
752     /**
753      * Testing the reset on groups, group members and enrolments.
754      */
755     public function test_reset() {
756         global $DB;
757         $this->resetAfterTest(true);
759         $c1 = $this->getDataGenerator()->create_course();
760         $c1ctx = context_course::instance($c1->id);
761         $studentrole = $DB->get_record('role', array('shortname' => 'student'));
762         $teacherrole = $DB->get_record('role', array('shortname' => 'teacher'));
764         $u1 = $this->getDataGenerator()->create_user();
765         $this->getDataGenerator()->enrol_user($u1->id, $c1->id, $studentrole->id);
766         $this->assertCount(1, get_enrolled_users($c1ctx));
768         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
769         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
770         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
771         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
773         // Wrong mode.
774         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
775         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
776         $data = array('shortname' => 'DoesNotExist', 'reset' => '1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 1);
777         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
778         $this->assertFalse($co->prepare());
779         $this->assertArrayHasKey('canonlyresetcourseinupdatemode', $co->get_errors());
780         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
781         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
782         $this->assertCount(1, get_enrolled_users($c1ctx));
784         // Reset not allowed.
785         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
786         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
787         $data = array('shortname' => $c1->shortname, 'reset' => '1');
788         $importoptions = array('canreset' => false);
789         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
790         $this->assertFalse($co->prepare());
791         $this->assertArrayHasKey('courseresetnotallowed', $co->get_errors());
792         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
793         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
794         $this->assertCount(1, get_enrolled_users($c1ctx));
796         // Reset allowed but not requested.
797         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
798         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
799         $data = array('shortname' => $c1->shortname, 'reset' => '0');
800         $importoptions = array('canreset' => true);
801         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
802         $this->assertTrue($co->prepare());
803         $co->proceed();
804         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
805         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
806         $this->assertCount(1, get_enrolled_users($c1ctx));
808         // Reset passed as a default parameter, should not be taken in account.
809         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
810         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
811         $data = array('shortname' => $c1->shortname);
812         $importoptions = array('canreset' => true);
813         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array('reset' => 1), $importoptions);
814         $this->assertTrue($co->prepare());
815         $co->proceed();
816         $this->assertTrue($DB->record_exists('groups', array('id' => $g1->id)));
817         $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
818         $this->assertCount(1, get_enrolled_users($c1ctx));
820         // Reset executed from data.
821         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
822         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
823         $data = array('shortname' => $c1->shortname, 'reset' => 1);
824         $importoptions = array('canreset' => true);
825         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
826         $this->assertTrue($co->prepare());
827         $co->proceed();
828         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
829         $this->assertFalse($DB->record_exists('groups_members', array('groupid' => $g1->id, 'userid' => $u1->id)));
830         $this->assertCount(0, get_enrolled_users($c1ctx));
832         // Reset executed from import option.
833         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
834         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
835         $data = array('shortname' => $c1->shortname, 'reset' => 0);
836         $importoptions = array('reset' => 1, 'canreset' => true);
837         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
839         $g1 = $this->getDataGenerator()->create_group(array('courseid' => $c1->id));
840         $this->getDataGenerator()->create_group_member(array('groupid' => $g1->id, 'userid' => $u1->id));
841         $this->assertEquals(1, $DB->count_records('groups', array('courseid' => $c1->id)));
842         $this->assertTrue($co->prepare());
843         $co->proceed();
844         $this->assertFalse($DB->record_exists('groups', array('id' => $g1->id)));
845     }
847     public function test_create_bad_category() {
848         global $DB;
849         $this->resetAfterTest(true);
851         // Ensure fails when category cannot be resolved upon creation.
852         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
853         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
854         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => 'Wrong cat');
855         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
856         $this->assertFalse($co->prepare());
857         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
859         // Ensure fails when category is 0 on create.
860         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
861         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
862         $data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '0');
863         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
864         $this->assertFalse($co->prepare());
865         $this->assertArrayHasKey('missingmandatoryfields', $co->get_errors());
867         // Ensure fails when category cannot be resolved upon update.
868         $c1 = $this->getDataGenerator()->create_course();
869         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
870         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
871         $data = array('shortname' => $c1->shortname, 'category' => 'Wrong cat');
872         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
873         $this->assertFalse($co->prepare());
874         $this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
876         // Ensure does not update the category when it is 0.
877         $c1 = $this->getDataGenerator()->create_course();
878         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
879         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
880         $data = array('shortname' => $c1->shortname, 'category' => '0');
881         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
882         $this->assertTrue($co->prepare());
883         $this->assertEmpty($co->get_errors());
884         $this->assertEmpty($co->get_statuses());
885         $co->proceed();
886         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
888         // Ensure does not update the category when it is set to 0 in the defaults.
889         $c1 = $this->getDataGenerator()->create_course();
890         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
891         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
892         $data = array('shortname' => $c1->shortname);
893         $defaults = array('category' => '0');
894         $co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
895         $this->assertTrue($co->prepare());
896         $this->assertEmpty($co->get_errors());
897         $this->assertEmpty($co->get_statuses());
898         $co->proceed();
899         $this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
900     }
902     public function test_enrolment_data() {
903         $this->resetAfterTest(true);
905         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
906         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
907         $data = array('shortname' => 'c1', 'summary' => 'S', 'fullname' => 'FN', 'category' => '1');
908         $data['enrolment_1'] = 'manual';
909         $data['enrolment_1_role'] = 'teacher';
910         $data['enrolment_1_startdate'] = '2nd July 2013';
911         $data['enrolment_1_enddate'] = '2nd August 2013';
912         $data['enrolment_1_enrolperiod'] = '10 days';
913         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
914         $this->assertTrue($co->prepare());
915         $co->proceed();
917         // Enrolment methods.
918         $enroldata = array();
919         $instances = enrol_get_instances($co->get_id(), false);
920         foreach ($instances as $instance) {
921             $enroldata[$instance->enrol] = $instance;
922         }
924         $this->assertNotEmpty($enroldata['manual']);
925         $this->assertEquals(ENROL_INSTANCE_ENABLED, $enroldata['manual']->status);
926         $this->assertEquals(strtotime($data['enrolment_1_startdate']), $enroldata['manual']->enrolstartdate);
927         $this->assertEquals(strtotime('1970-01-01 GMT + ' . $data['enrolment_1_enrolperiod']), $enroldata['manual']->enrolperiod);
928         $this->assertEquals(strtotime('12th July 2013'), $enroldata['manual']->enrolenddate);
929     }
931     public function test_idnumber_problems() {
932         $this->resetAfterTest(true);
934         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'sntaken', 'idnumber' => 'taken'));
935         $c2 = $this->getDataGenerator()->create_course();
937         // Create with existing ID number.
938         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
939         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
940         $data = array('shortname' => 'c2', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '1',
941             'idnumber' => $c1->idnumber);
942         $co = new tool_uploadcourse_course($mode, $updatemode, $data);
943         $this->assertFalse($co->prepare());
944         $this->assertArrayHasKey('idnumberalreadyinuse', $co->get_errors());
946         // Rename to existing ID number.
947         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
948         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
949         $data = array('shortname' => $c2->shortname, 'rename' => 'SN', 'idnumber' => $c1->idnumber);
950         $importoptions = array('canrename' => true);
951         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
952         $this->assertFalse($co->prepare());
953         $this->assertArrayHasKey('cannotrenameidnumberconflict', $co->get_errors());
955         // Incrementing shortname increments idnumber.
956         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
957         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
958         $data = array('shortname' => $c1->shortname, 'idnumber' => $c1->idnumber, 'summary' => 'S', 'fullname' => 'F',
959             'category' => 1);
960         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
961         $this->assertTrue($co->prepare());
962         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
963         $this->assertArrayHasKey('courseidnumberincremented', $co->get_statuses());
964         $data = $co->get_data();
965         $this->assertEquals('sntaken_2', $data['shortname']);
966         $this->assertEquals('taken_2', $data['idnumber']);
968         // Incrementing shortname increments idnumber unless available.
969         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
970         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
971         $data = array('shortname' => $c1->shortname, 'idnumber' => 'nottaken', 'summary' => 'S', 'fullname' => 'F',
972             'category' => 1);
973         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
974         $this->assertTrue($co->prepare());
975         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
976         $this->assertArrayNotHasKey('courseidnumberincremented', $co->get_statuses());
977         $data = $co->get_data();
978         $this->assertEquals('sntaken_2', $data['shortname']);
979         $this->assertEquals('nottaken', $data['idnumber']);
980     }
982     public function test_generate_shortname() {
983         $this->resetAfterTest(true);
985         $c1 = $this->getDataGenerator()->create_course(array('shortname' => 'taken'));
987         // Generate a shortname.
988         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
989         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
990         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1', 'idnumber' => 'IDN');
991         $importoptions = array('shortnametemplate' => '%i');
992         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
993         $this->assertTrue($co->prepare());
994         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
996         // Generate a shortname without a template.
997         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
998         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
999         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1000         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), array());
1001         $this->assertFalse($co->prepare());
1002         $this->assertArrayHasKey('missingshortnamenotemplate', $co->get_errors());
1004         // Generate a shortname in update mode.
1005         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1006         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1007         $data = array('summary' => 'summary', 'fullname' => 'FN', 'category' => '1');
1008         $importoptions = array('shortnametemplate' => '%f');
1009         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1010         $this->assertFalse($co->prepare());
1011         // Commented because we never get here as the course without shortname does not exist.
1012         // $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1014         // Generate a shortname to a course that already exists.
1015         $mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
1016         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1017         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1018         $importoptions = array('shortnametemplate' => '%f');
1019         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1020         $this->assertFalse($co->prepare());
1021         $this->assertArrayHasKey('generatedshortnamealreadyinuse', $co->get_errors());
1023         // Generate a shortname to a course that already exists will be incremented.
1024         $mode = tool_uploadcourse_processor::MODE_CREATE_ALL;
1025         $updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
1026         $data = array('summary' => 'summary', 'fullname' => 'taken', 'category' => '1');
1027         $importoptions = array('shortnametemplate' => '%f');
1028         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1029         $this->assertTrue($co->prepare());
1030         $this->assertArrayHasKey('courseshortnamegenerated', $co->get_statuses());
1031         $this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
1032     }
1034     public function test_mess_with_frontpage() {
1035         global $SITE;
1036         $this->resetAfterTest(true);
1038         // Updating the front page.
1039         $mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
1040         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1041         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1042         $importoptions = array();
1043         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1044         $this->assertFalse($co->prepare());
1045         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1047         // Updating the front page.
1048         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1049         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1050         $data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
1051         $importoptions = array();
1052         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1053         $this->assertFalse($co->prepare());
1054         $this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
1056         // Generating a shortname should not be allowed in update mode, and so we cannot update the front page.
1057         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1058         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1059         $data = array('idnumber' => 'NewIDN', 'fullname' => 'FN', 'category' => 1);
1060         $importoptions = array('shortnametemplate' => $SITE->shortname);
1061         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1062         $this->assertFalse($co->prepare());
1063         $this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
1065         // Renaming to the front page should not be allowed.
1066         $c1 = $this->getDataGenerator()->create_course();
1067         $mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
1068         $updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
1069         $data = array('shortname' => $c1->shortname, 'fullname' => 'FN', 'idnumber' => 'NewIDN', 'rename' => $SITE->shortname);
1070         $importoptions = array('canrename' => true);
1071         $co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
1072         $this->assertFalse($co->prepare());
1073         $this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
1075     }