diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index c7de2be192f..aa8233b86bd 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -5484,6 +5484,57 @@ class core_course_courselib_testcase extends advanced_testcase { $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. */