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