Skip to content

Commit

Permalink
MDL-71242 core_course: Test the validation of the sort value
Browse files Browse the repository at this point in the history
Adds new unit test, test_course_get_recent_courses_sort_validation(),
which is reponsible for testing the validation of the sort value in
course_get_recent_courses().
  • Loading branch information
Mihail Geshoski authored and stronk7 committed Jul 8, 2021
1 parent 069cc71 commit 084ff0c
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions course/tests/courselib_test.php
Expand Up @@ -5484,6 +5484,57 @@ public function test_course_get_recent_courses() {
$this->assertArrayNotHasKey($courses[0]->id, $result);
}

/**
* Test the validation of the sort value in course_get_recent_courses().
*
* @dataProvider course_get_recent_courses_sort_validation_provider
* @param string $sort The sort value
* @param string $expectedexceptionmsg The expected exception message
*/
public function test_course_get_recent_courses_sort_validation(string $sort, string $expectedexceptionmsg) {
$this->resetAfterTest();

$user = $this->getDataGenerator()->create_user();

if (!empty($expectedexceptionmsg)) {
$this->expectException('invalid_parameter_exception');
$this->expectExceptionMessage($expectedexceptionmsg);
}
course_get_recent_courses($user->id, 0, 0, $sort);
}

/**
* Data provider for test_course_get_recent_courses_sort_validation().
*
* @return array
*/
function course_get_recent_courses_sort_validation_provider() {
return [
'Invalid sort format (SQL injection attempt)' =>
[
'shortname DESC LIMIT 1--',
'Invalid structure of the sort parameter, allowed structure: fieldname [ASC|DESC].',
],
'Sort uses \'sort by\' field that does not exist' =>
[
'shortname DESC, xyz ASC',
'Invalid field in the sort parameter, allowed fields: id, idnumber, summary, summaryformat, ' .
'startdate, enddate, category, shortname, fullname, timeaccess, component, visible, ' .
'showactivitydates, showcompletionconditions.',
],
'Sort uses invalid value for the sorting direction' =>
[
'shortname xyz, lastaccess',
'Invalid sort direction in the sort parameter, allowed values: asc, desc.',
],
'Valid sort format' =>
[
'shortname asc, timeaccess',
''
]
];
}

/**
* Test the course_get_recent_courses function.
*/
Expand Down

0 comments on commit 084ff0c

Please sign in to comment.