MDL-13114 tool_uploadcourse: ID number errors handling

This commit is contained in:
Frederic Massart
2013-07-15 10:02:55 +08:00
parent 96aaf65481
commit f54c49ccf0
2 changed files with 35 additions and 0 deletions
@@ -374,6 +374,7 @@ class tool_uploadcourse_course {
* @return bool false is any error occured.
*/
public function prepare() {
global $DB;
$this->prepared = true;
// Validate the shortname.
@@ -536,6 +537,14 @@ class tool_uploadcourse_course {
}
}
// If the course does not exist, ensure that the ID number is not taken.
if (!$exists && isset($coursedata['idnumber'])) {
if ($DB->count_records_select('course', 'idnumber = :idn', array('idn' => $coursedata['idnumber'])) > 0) {
$this->error('idnumberalreadyinuse', new lang_string('idnumberalreadyinuse', 'tool_uploadcourse'));
return false;
}
}
// Ultimate check mode vs. existence.
switch ($mode) {
case tool_uploadcourse_processor::MODE_CREATE_NEW:
@@ -779,4 +779,30 @@ class tool_uploadcourse_course_testcase extends advanced_testcase {
$this->assertEquals(strtotime('1970-01-01 GMT + ' . $data['enrolment_1_enrolperiod']), $enroldata['manual']->enrolperiod);
$this->assertEquals(strtotime('12th July 2013'), $enroldata['manual']->enrolenddate);
}
public function test_idnumber_problems() {
$this->resetAfterTest(true);
$c1 = $this->getDataGenerator()->create_course(array('idnumber' => 'Taken'));
$c2 = $this->getDataGenerator()->create_course();
// Create with existing ID number.
$mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
$updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
$data = array('shortname' => 'c2', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '1',
'idnumber' => $c1->idnumber);
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
$this->assertFalse($co->prepare());
$this->assertArrayHasKey('idnumberalreadyinuse', $co->get_errors());
// Rename to existing ID number.
$mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
$updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
$data = array('shortname' => $c2->shortname, 'rename' => 'SN', 'idnumber' => $c1->idnumber);
$importoptions = array('canrename' => true);
$co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
$this->assertFalse($co->prepare());
$this->assertArrayHasKey('cannotrenameidnumberconflict', $co->get_errors());
}
}