Skip to content

Commit

Permalink
MDL-58888 mod_choice: Verify sorting when testing multiple selections
Browse files Browse the repository at this point in the history
* 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().
  • Loading branch information
junpataleta committed Dec 15, 2017
1 parent fb20acf commit a687077
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions mod/choice/tests/lib_test.php
Expand Up @@ -175,31 +175,32 @@ public function test_choice_get_my_response() {
// 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);
}
}

Expand Down

0 comments on commit a687077

Please sign in to comment.