MDL-66024 tool_uploadcourse: validate shortname/fullname field length.
This commit is contained in:
@@ -412,6 +412,12 @@ class tool_uploadcourse_course {
|
||||
$this->error('invalidshortname', new lang_string('invalidshortname', 'tool_uploadcourse'));
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure we don't overflow the maximum length of the shortname field.
|
||||
if (core_text::strlen($this->shortname) > 255) {
|
||||
$this->error('invalidshortnametoolong', new lang_string('invalidshortnametoolong', 'tool_uploadcourse', 255));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
$exists = $this->exists();
|
||||
@@ -479,6 +485,12 @@ class tool_uploadcourse_course {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Ensure we don't overflow the maximum length of the fullname field.
|
||||
if (!empty($coursedata['fullname']) && core_text::strlen($coursedata['fullname']) > 254) {
|
||||
$this->error('invalidfullnametoolong', new lang_string('invalidfullnametoolong', 'tool_uploadcourse', 254));
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the course does not exist, or will be forced created.
|
||||
if (!$exists || $mode === tool_uploadcourse_processor::MODE_CREATE_ALL) {
|
||||
|
||||
|
||||
@@ -93,6 +93,8 @@ $string['invalideupdatemode'] = 'Invalid update mode selected';
|
||||
$string['invalidvisibilitymode'] = 'Invalid visibility mode given';
|
||||
$string['invalidroles'] = 'Invalid role names: {$a}';
|
||||
$string['invalidshortname'] = 'Invalid shortname';
|
||||
$string['invalidfullnametoolong'] = 'The fullname field is limited to {$a} characters';
|
||||
$string['invalidshortnametoolong'] = 'The shortname field is limited to {$a} characters';
|
||||
$string['missingmandatoryfields'] = 'Missing value for mandatory fields: {$a}';
|
||||
$string['missingshortnamenotemplate'] = 'Missing shortname and shortname template not set';
|
||||
$string['mode'] = 'Upload mode';
|
||||
|
||||
@@ -82,6 +82,37 @@ class tool_uploadcourse_course_testcase extends advanced_testcase {
|
||||
$this->assertArrayHasKey('invalidshortname', $co->get_errors());
|
||||
}
|
||||
|
||||
public function test_invalid_shortname_too_long() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
|
||||
$updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
|
||||
|
||||
$upload = new tool_uploadcourse_course($mode, $updatemode, [
|
||||
'category' => 1,
|
||||
'fullname' => 'New course',
|
||||
'shortname' => str_repeat('X', 2000),
|
||||
]);
|
||||
|
||||
$this->assertFalse($upload->prepare());
|
||||
$this->assertArrayHasKey('invalidshortnametoolong', $upload->get_errors());
|
||||
}
|
||||
|
||||
public function test_invalid_fullname_too_long() {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
|
||||
$updatemode = tool_uploadcourse_processor::UPDATE_NOTHING;
|
||||
|
||||
$upload = new tool_uploadcourse_course($mode, $updatemode, [
|
||||
'category' => 1,
|
||||
'fullname' => str_repeat('X', 2000),
|
||||
]);
|
||||
|
||||
$this->assertFalse($upload->prepare());
|
||||
$this->assertArrayHasKey('invalidfullnametoolong', $upload->get_errors());
|
||||
}
|
||||
|
||||
public function test_invalid_visibility() {
|
||||
$this->resetAfterTest(true);
|
||||
$mode = tool_uploadcourse_processor::MODE_CREATE_NEW;
|
||||
|
||||
Reference in New Issue
Block a user