mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
Merge branch 'MDL-40737-master' of git://github.com/FMCorz/moodle
This commit is contained in:
commit
f0655ecb71
@ -399,7 +399,7 @@ class tool_uploadcourse_course {
|
|||||||
* @return bool false is any error occured.
|
* @return bool false is any error occured.
|
||||||
*/
|
*/
|
||||||
public function prepare() {
|
public function prepare() {
|
||||||
global $DB;
|
global $DB, $SITE;
|
||||||
$this->prepared = true;
|
$this->prepared = true;
|
||||||
|
|
||||||
// Validate the shortname.
|
// Validate the shortname.
|
||||||
@ -432,6 +432,12 @@ class tool_uploadcourse_course {
|
|||||||
$this->error('courseexistsanduploadnotallowed',
|
$this->error('courseexistsanduploadnotallowed',
|
||||||
new lang_string('courseexistsanduploadnotallowed', 'tool_uploadcourse'));
|
new lang_string('courseexistsanduploadnotallowed', 'tool_uploadcourse'));
|
||||||
return false;
|
return false;
|
||||||
|
} else if ($this->can_update()) {
|
||||||
|
// We can never allow for any front page changes!
|
||||||
|
if ($this->shortname == $SITE->shortname) {
|
||||||
|
$this->error('cannotupdatefrontpage', new lang_string('cannotupdatefrontpage', 'tool_uploadcourse'));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!$this->can_create()) {
|
if (!$this->can_create()) {
|
||||||
@ -608,6 +614,13 @@ class tool_uploadcourse_course {
|
|||||||
if ($exists) {
|
if ($exists) {
|
||||||
$missingonly = ($updatemode === tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS);
|
$missingonly = ($updatemode === tool_uploadcourse_processor::UPDATE_MISSING_WITH_DATA_OR_DEFAUTLS);
|
||||||
$coursedata = $this->get_final_update_data($coursedata, $usedefaults, $missingonly);
|
$coursedata = $this->get_final_update_data($coursedata, $usedefaults, $missingonly);
|
||||||
|
|
||||||
|
// Make sure we are not trying to mess with the front page, though we should never get here!
|
||||||
|
if ($coursedata['id'] == $SITE->id) {
|
||||||
|
$this->error('cannotupdatefrontpage', new lang_string('cannotupdatefrontpage', 'tool_uploadcourse'));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$this->do = self::DO_UPDATE;
|
$this->do = self::DO_UPDATE;
|
||||||
} else {
|
} else {
|
||||||
$coursedata = $this->get_final_create_data($coursedata);
|
$coursedata = $this->get_final_create_data($coursedata);
|
||||||
|
@ -32,6 +32,7 @@ $string['cannotreadbackupfile'] = 'Cannot read the backup file';
|
|||||||
$string['cannotrenamecoursenotexist'] = 'Cannot rename a course that does not exist';
|
$string['cannotrenamecoursenotexist'] = 'Cannot rename a course that does not exist';
|
||||||
$string['cannotrenameidnumberconflict'] = 'Cannot rename the course, the ID number conflicts with an existing course';
|
$string['cannotrenameidnumberconflict'] = 'Cannot rename the course, the ID number conflicts with an existing course';
|
||||||
$string['cannotrenameshortnamealreadyinuse'] = 'Cannot rename the course, the shortname is already used';
|
$string['cannotrenameshortnamealreadyinuse'] = 'Cannot rename the course, the shortname is already used';
|
||||||
|
$string['cannotupdatefrontpage'] = 'It is forbidden to modify the front page';
|
||||||
$string['canonlyrenameinupdatemode'] = 'Can only rename a course when update is allowed';
|
$string['canonlyrenameinupdatemode'] = 'Can only rename a course when update is allowed';
|
||||||
$string['canonlyresetcourseinupdatemode'] = 'Can only reset a course in update mode';
|
$string['canonlyresetcourseinupdatemode'] = 'Can only reset a course in update mode';
|
||||||
$string['couldnotresolvecatgorybyid'] = 'Could not resolve category by ID';
|
$string['couldnotresolvecatgorybyid'] = 'Could not resolve category by ID';
|
||||||
|
@ -768,6 +768,7 @@ class tool_uploadcourse_course_testcase extends advanced_testcase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function test_create_bad_category() {
|
public function test_create_bad_category() {
|
||||||
|
global $DB;
|
||||||
$this->resetAfterTest(true);
|
$this->resetAfterTest(true);
|
||||||
|
|
||||||
// Ensure fails when category cannot be resolved upon creation.
|
// Ensure fails when category cannot be resolved upon creation.
|
||||||
@ -778,6 +779,14 @@ class tool_uploadcourse_course_testcase extends advanced_testcase {
|
|||||||
$this->assertFalse($co->prepare());
|
$this->assertFalse($co->prepare());
|
||||||
$this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
|
$this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
|
||||||
|
|
||||||
|
// Ensure fails when category is 0 on create.
|
||||||
|
$mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
|
||||||
|
$updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
|
||||||
|
$data = array('shortname' => 'c1', 'summary' => 'summary', 'fullname' => 'FN', 'category' => '0');
|
||||||
|
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
|
||||||
|
$this->assertFalse($co->prepare());
|
||||||
|
$this->assertArrayHasKey('missingmandatoryfields', $co->get_errors());
|
||||||
|
|
||||||
// Ensure fails when category cannot be resolved upon update.
|
// Ensure fails when category cannot be resolved upon update.
|
||||||
$c1 = $this->getDataGenerator()->create_course();
|
$c1 = $this->getDataGenerator()->create_course();
|
||||||
$mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
|
$mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
|
||||||
@ -786,6 +795,31 @@ class tool_uploadcourse_course_testcase extends advanced_testcase {
|
|||||||
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
|
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
|
||||||
$this->assertFalse($co->prepare());
|
$this->assertFalse($co->prepare());
|
||||||
$this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
|
$this->assertArrayHasKey('couldnotresolvecatgorybyid', $co->get_errors());
|
||||||
|
|
||||||
|
// Ensure does not update the category when it is 0.
|
||||||
|
$c1 = $this->getDataGenerator()->create_course();
|
||||||
|
$mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
|
||||||
|
$updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
|
||||||
|
$data = array('shortname' => $c1->shortname, 'category' => '0');
|
||||||
|
$co = new tool_uploadcourse_course($mode, $updatemode, $data);
|
||||||
|
$this->assertTrue($co->prepare());
|
||||||
|
$this->assertEmpty($co->get_errors());
|
||||||
|
$this->assertEmpty($co->get_statuses());
|
||||||
|
$co->proceed();
|
||||||
|
$this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
|
||||||
|
|
||||||
|
// Ensure does not update the category when it is set to 0 in the defaults.
|
||||||
|
$c1 = $this->getDataGenerator()->create_course();
|
||||||
|
$mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
|
||||||
|
$updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_OR_DEFAUTLS;
|
||||||
|
$data = array('shortname' => $c1->shortname);
|
||||||
|
$defaults = array('category' => '0');
|
||||||
|
$co = new tool_uploadcourse_course($mode, $updatemode, $data, $defaults);
|
||||||
|
$this->assertTrue($co->prepare());
|
||||||
|
$this->assertEmpty($co->get_errors());
|
||||||
|
$this->assertEmpty($co->get_statuses());
|
||||||
|
$co->proceed();
|
||||||
|
$this->assertEquals($c1->category, $DB->get_field('course', 'category', array('id' => $c1->id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_enrolment_data() {
|
public function test_enrolment_data() {
|
||||||
@ -920,4 +954,47 @@ class tool_uploadcourse_course_testcase extends advanced_testcase {
|
|||||||
$this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
|
$this->assertArrayHasKey('courseshortnameincremented', $co->get_statuses());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_mess_with_frontpage() {
|
||||||
|
global $SITE;
|
||||||
|
$this->resetAfterTest(true);
|
||||||
|
|
||||||
|
// Updating the front page.
|
||||||
|
$mode = tool_uploadcourse_processor::MODE_UPDATE_ONLY;
|
||||||
|
$updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
|
||||||
|
$data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
|
||||||
|
$importoptions = array();
|
||||||
|
$co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
|
||||||
|
$this->assertFalse($co->prepare());
|
||||||
|
$this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
|
||||||
|
|
||||||
|
// Updating the front page.
|
||||||
|
$mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
|
||||||
|
$updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
|
||||||
|
$data = array('shortname' => $SITE->shortname, 'idnumber' => 'NewIDN');
|
||||||
|
$importoptions = array();
|
||||||
|
$co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
|
||||||
|
$this->assertFalse($co->prepare());
|
||||||
|
$this->assertArrayHasKey('cannotupdatefrontpage', $co->get_errors());
|
||||||
|
|
||||||
|
// Generating a shortname should not be allowed in update mode, and so we cannot update the front page.
|
||||||
|
$mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
|
||||||
|
$updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
|
||||||
|
$data = array('idnumber' => 'NewIDN', 'fullname' => 'FN', 'category' => 1);
|
||||||
|
$importoptions = array('shortnametemplate' => $SITE->shortname);
|
||||||
|
$co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
|
||||||
|
$this->assertFalse($co->prepare());
|
||||||
|
$this->assertArrayHasKey('cannotgenerateshortnameupdatemode', $co->get_errors());
|
||||||
|
|
||||||
|
// Renaming to the front page should not be allowed.
|
||||||
|
$c1 = $this->getDataGenerator()->create_course();
|
||||||
|
$mode = tool_uploadcourse_processor::MODE_CREATE_OR_UPDATE;
|
||||||
|
$updatemode = tool_uploadcourse_processor::UPDATE_ALL_WITH_DATA_ONLY;
|
||||||
|
$data = array('shortname' => $c1->shortname, 'fullname' => 'FN', 'idnumber' => 'NewIDN', 'rename' => $SITE->shortname);
|
||||||
|
$importoptions = array('canrename' => true);
|
||||||
|
$co = new tool_uploadcourse_course($mode, $updatemode, $data, array(), $importoptions);
|
||||||
|
$this->assertFalse($co->prepare());
|
||||||
|
$this->assertArrayHasKey('cannotrenameshortnamealreadyinuse', $co->get_errors());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user