Skip to content

Commit

Permalink
MDL-66024 tool_uploadcourse: validate shortname/fullname field length.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulholden committed Dec 5, 2019
1 parent 800563e commit 714343c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
12 changes: 12 additions & 0 deletions admin/tool/uploadcourse/classes/course.php
Expand Up @@ -412,6 +412,12 @@ public function prepare() {
$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();
Expand Down Expand Up @@ -479,6 +485,12 @@ public function prepare() {
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) {

Expand Down
2 changes: 2 additions & 0 deletions admin/tool/uploadcourse/lang/en/tool_uploadcourse.php
Expand Up @@ -93,6 +93,8 @@
$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';
Expand Down
31 changes: 31 additions & 0 deletions admin/tool/uploadcourse/tests/course_test.php
Expand Up @@ -82,6 +82,37 @@ public function test_invalid_shortname() {
$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;
Expand Down

0 comments on commit 714343c

Please sign in to comment.