MDL-51569 mod_choice: Fix use of hardcoded ids
authorDan Poltawski <dan@moodle.com>
Fri, 6 Nov 2015 08:09:47 +0000 (08:09 +0000)
committerDan Poltawski <dan@moodle.com>
Fri, 6 Nov 2015 08:09:47 +0000 (08:09 +0000)
(Previous approach of making up ids longer works now we are validating
them..)

mod/choice/tests/events_test.php

index 90ce356..71bb599 100644 (file)
@@ -65,12 +65,14 @@ class mod_choice_events_testcase extends advanced_testcase {
      * Test to ensure that event data is being stored correctly.
      */
     public function test_answer_submitted() {
+        global $DB;
         // Generate user data.
         $user = $this->getDataGenerator()->create_user();
 
+        $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
         // Redirect event.
         $sink = $this->redirectEvents();
-        choice_user_submit_response(3, $this->choice, $user->id, $this->course, $this->cm);
+        choice_user_submit_response($optionids[3], $this->choice, $user->id, $this->course, $this->cm);
         $events = $sink->get_events();
 
         // Data checking.
@@ -79,7 +81,7 @@ class mod_choice_events_testcase extends advanced_testcase {
         $this->assertEquals($user->id, $events[0]->userid);
         $this->assertEquals(context_module::instance($this->choice->cmid), $events[0]->get_context());
         $this->assertEquals($this->choice->id, $events[0]->other['choiceid']);
-        $this->assertEquals(array(3), $events[0]->other['optionid']);
+        $this->assertEquals(array($optionids[3]), $events[0]->other['optionid']);
         $expected = array($this->course->id, "choice", "choose", 'view.php?id=' . $this->cm->id, $this->choice->id, $this->cm->id);
         $this->assertEventLegacyLogData($expected, $events[0]);
         $this->assertEventContextNotUsed($events[0]);
@@ -101,9 +103,12 @@ class mod_choice_events_testcase extends advanced_testcase {
         $cm = $DB->get_record('course_modules', array('id' => $choice->cmid));
         $context = context_module::instance($choice->cmid);
 
+        $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $choice->id)));
+        $submittedoptionids = array($optionids[1], $optionids[3]);
+
         // Redirect event.
         $sink = $this->redirectEvents();
-        choice_user_submit_response(array(1, 3), $choice, $user->id, $this->course, $cm);
+        choice_user_submit_response($submittedoptionids, $choice, $user->id, $this->course, $cm);
         $events = $sink->get_events();
 
         // Data checking.
@@ -112,7 +117,7 @@ class mod_choice_events_testcase extends advanced_testcase {
         $this->assertEquals($user->id, $events[0]->userid);
         $this->assertEquals(context_module::instance($choice->cmid), $events[0]->get_context());
         $this->assertEquals($choice->id, $events[0]->other['choiceid']);
-        $this->assertEquals(array(1, 3), $events[0]->other['optionid']);
+        $this->assertEquals($submittedoptionids, $events[0]->other['optionid']);
         $expected = array($this->course->id, "choice", "choose", 'view.php?id=' . $cm->id, $choice->id, $cm->id);
         $this->assertEventLegacyLogData($expected, $events[0]);
         $this->assertEventContextNotUsed($events[0]);
@@ -144,16 +149,19 @@ class mod_choice_events_testcase extends advanced_testcase {
      * Test to ensure that event data is being stored correctly.
      */
     public function test_answer_updated() {
+        global $DB;
         // Generate user data.
         $user = $this->getDataGenerator()->create_user();
 
+        $optionids = array_keys($DB->get_records('choice_options', array('choiceid' => $this->choice->id)));
+
         // Create the first answer.
-        choice_user_submit_response(2, $this->choice, $user->id, $this->course, $this->cm);
+        choice_user_submit_response($optionids[2], $this->choice, $user->id, $this->course, $this->cm);
 
         // Redirect event.
         $sink = $this->redirectEvents();
         // Now choose a different answer.
-        choice_user_submit_response(3, $this->choice, $user->id, $this->course, $this->cm);
+        choice_user_submit_response($optionids[3], $this->choice, $user->id, $this->course, $this->cm);
 
         $events = $sink->get_events();
 
@@ -163,7 +171,7 @@ class mod_choice_events_testcase extends advanced_testcase {
         $this->assertEquals($user->id, $events[0]->userid);
         $this->assertEquals(context_module::instance($this->choice->cmid), $events[0]->get_context());
         $this->assertEquals($this->choice->id, $events[0]->other['choiceid']);
-        $this->assertEquals(3, $events[0]->other['optionid']);
+        $this->assertEquals($optionids[3], $events[0]->other['optionid']);
         $expected = array($this->course->id, "choice", "choose again", 'view.php?id=' . $this->cm->id,
                 $this->choice->id, $this->cm->id);
         $this->assertEventLegacyLogData($expected, $events[0]);