From f85f002b377726b38cdf2ec673828ee346b7e33d Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Fri, 15 Dec 2017 15:53:11 +1300 Subject: [PATCH 1/2] MDL-58888 mod_choice: Verify sorting when testing multiple selections * Improve test_choice_get_my_response() to verify the sorting of the responses returned by choice_get_my_response(). * Fix correct usage of choice_get_my_response(). --- mod/choice/tests/lib_test.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mod/choice/tests/lib_test.php b/mod/choice/tests/lib_test.php index 9f10a7436cc..950ea178e76 100644 --- a/mod/choice/tests/lib_test.php +++ b/mod/choice/tests/lib_test.php @@ -175,31 +175,32 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase { // Setup test data. $course = $this->getDataGenerator()->create_course(); $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); - $context = context_module::instance($choice->cmid); $cm = get_coursemodule_from_instance('choice', $choice->id); $choicewithoptions = choice_get_choice($choice->id); $optionids = array_keys($choicewithoptions->option); choice_user_submit_response($optionids[0], $choice, $USER->id, $course, $cm); - $responses = choice_get_my_response($choice, $course, $cm, $context); + $responses = choice_get_my_response($choice); $this->assertCount(1, $responses); $response = array_shift($responses); $this->assertEquals($optionids[0], $response->optionid); // Multiple responses. $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, 'allowmultiple' => 1)); - $context = context_module::instance($choice->cmid); $cm = get_coursemodule_from_instance('choice', $choice->id); $choicewithoptions = choice_get_choice($choice->id); $optionids = array_keys($choicewithoptions->option); - choice_user_submit_response($optionids, $choice, $USER->id, $course, $cm); - $responses = choice_get_my_response($choice, $course, $cm, $context); + // Submit a response with the options reversed. + $selections = $optionids; + rsort($selections); + choice_user_submit_response($selections, $choice, $USER->id, $course, $cm); + $responses = choice_get_my_response($choice); $this->assertCount(count($optionids), $responses); foreach ($responses as $resp) { - $this->assertContains($resp->optionid, $optionids); + $this->assertEquals(array_shift($optionids), $resp->optionid); } } From 4ab37af07098af9d8f304e3eec2abc7f2cdc2562 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Fri, 15 Dec 2017 15:55:48 +1300 Subject: [PATCH 2/2] MDL-58888 mod_choice: Sort choice_get_my_response() results by optionid --- mod/choice/lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mod/choice/lib.php b/mod/choice/lib.php index cebacfa79aa..4e6eef07ffa 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -1014,7 +1014,7 @@ function choice_print_overview($courses, &$htmlarray) { */ function choice_get_my_response($choice) { global $DB, $USER; - return $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id)); + return $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id), 'optionid'); }