Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Unit tests for the multiple choice, multi-response question definition classes.
  19   *
  20   * @package   qtype_multichoice
  21   * @copyright 2009 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  29  
  30  
  31  /**
  32   * Unit tests for the multiple choice, multi-response question definition class.
  33   *
  34   * @copyright 2009 The Open University
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class qtype_multichoice_multi_question_test extends advanced_testcase {
  38  
  39      public function test_get_expected_data() {
  40          $question = test_question_maker::make_a_multichoice_multi_question();
  41          $question->start_attempt(new question_attempt_step(), 1);
  42  
  43          $this->assertEquals(array('choice0' => PARAM_BOOL, 'choice1' => PARAM_BOOL,
  44                  'choice2' => PARAM_BOOL, 'choice3' => PARAM_BOOL), $question->get_expected_data());
  45      }
  46  
  47      public function test_is_complete_response() {
  48          $question = test_question_maker::make_a_multichoice_multi_question();
  49          $question->start_attempt(new question_attempt_step(), 1);
  50  
  51          $this->assertFalse($question->is_complete_response(array()));
  52          $this->assertFalse($question->is_complete_response(
  53                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  54          $this->assertTrue($question->is_complete_response(array('choice1' => '1')));
  55          $this->assertTrue($question->is_complete_response(
  56                  array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
  57      }
  58  
  59      public function test_is_gradable_response() {
  60          $question = test_question_maker::make_a_multichoice_multi_question();
  61          $question->start_attempt(new question_attempt_step(), 1);
  62  
  63          $this->assertFalse($question->is_gradable_response(array()));
  64          $this->assertFalse($question->is_gradable_response(
  65                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  66          $this->assertTrue($question->is_gradable_response(array('choice1' => '1')));
  67          $this->assertTrue($question->is_gradable_response(
  68                  array('choice0' => '1', 'choice1' => '1', 'choice2' => '1', 'choice3' => '1')));
  69      }
  70  
  71      public function test_is_same_response() {
  72          $question = test_question_maker::make_a_multichoice_multi_question();
  73          $question->start_attempt(new question_attempt_step(), 1);
  74  
  75          $this->assertTrue($question->is_same_response(
  76                  array(),
  77                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  78  
  79          $this->assertTrue($question->is_same_response(
  80                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0'),
  81                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  82  
  83          $this->assertFalse($question->is_same_response(
  84                  array('choice0' => '0', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0'),
  85                  array('choice0' => '1', 'choice1' => '0', 'choice2' => '0', 'choice3' => '0')));
  86  
  87          $this->assertTrue($question->is_same_response(
  88                  array('choice0' => '1', 'choice1' => '0', 'choice2' => '1', 'choice3' => '0'),
  89                  array('choice0' => '1', 'choice1' => '0', 'choice2' => '1', 'choice3' => '0')));
  90      }
  91  
  92      public function test_grading() {
  93          $question = test_question_maker::make_a_multichoice_multi_question();
  94          $question->start_attempt(new question_attempt_step(), 1);
  95  
  96          $this->assertEquals(array(1, question_state::$gradedright),
  97                  $question->grade_response($question->prepare_simulated_post_data(array('A' => 1, 'C' => 1))));
  98          $this->assertEquals(array(0.5, question_state::$gradedpartial),
  99                  $question->grade_response($question->prepare_simulated_post_data(array('A' => 1))));
 100          $this->assertEquals(array(0, question_state::$gradedwrong),
 101                  $question->grade_response($question->prepare_simulated_post_data(array('A' => 1, 'B' => 1, 'C' => 1))));
 102          $this->assertEquals(array(0, question_state::$gradedwrong),
 103                  $question->grade_response($question->prepare_simulated_post_data(array('B' => 1))));
 104      }
 105  
 106      public function test_get_correct_response() {
 107          $question = test_question_maker::make_a_multichoice_multi_question();
 108          $question->start_attempt(new question_attempt_step(), 1);
 109  
 110          $this->assertEquals($question->prepare_simulated_post_data(array('A' => 1, 'C' => 1)), $question->get_correct_response());
 111      }
 112  
 113      public function test_get_question_summary() {
 114          $mc = test_question_maker::make_a_multichoice_single_question();
 115          $mc->start_attempt(new question_attempt_step(), 1);
 116  
 117          $qsummary = $mc->get_question_summary();
 118  
 119          $this->assertRegExp('/' . preg_quote($mc->questiontext, '/') . '/', $qsummary);
 120          foreach ($mc->answers as $answer) {
 121              $this->assertRegExp('/' . preg_quote($answer->answer, '/') . '/', $qsummary);
 122          }
 123      }
 124  
 125      public function test_summarise_response() {
 126          $mc = test_question_maker::make_a_multichoice_multi_question();
 127          $mc->shuffleanswers = false;
 128          $mc->start_attempt(new question_attempt_step(), 1);
 129  
 130          $summary = $mc->summarise_response($mc->prepare_simulated_post_data(array('B' => 1, 'C' => 1)),
 131                  test_question_maker::get_a_qa($mc));
 132  
 133          $this->assertEquals('B; C', $summary);
 134      }
 135  
 136      public function test_summarise_response_clearchoice() {
 137          $mc = test_question_maker::make_a_multichoice_multi_question();
 138          $mc->shuffleanswers = false;
 139          $mc->start_attempt(new question_attempt_step(), 1);
 140  
 141          $summary = $mc->summarise_response($mc->prepare_simulated_post_data(array('clearchoice' => -1)),
 142              test_question_maker::get_a_qa($mc));
 143  
 144          $this->assertNull($summary);
 145      }
 146  
 147      public function test_classify_response() {
 148          $mc = test_question_maker::make_a_multichoice_multi_question();
 149          $mc->start_attempt(new question_attempt_step(), 1);
 150  
 151          $this->assertEquals(array(
 152                      13 => new question_classified_response(13, 'A', 0.5),
 153                      14 => new question_classified_response(14, 'B', -1.0),
 154                  ), $mc->classify_response($mc->prepare_simulated_post_data(array('A' => 1, 'B' => 1))));
 155  
 156          $this->assertEquals(array(), $mc->classify_response(array()));
 157      }
 158  
 159      public function test_prepare_simulated_post_data() {
 160          $mc = test_question_maker::make_a_multichoice_multi_question();
 161          $mc->start_attempt(new question_attempt_step(), 1);
 162          $correctanswers = array(
 163              array(),
 164              array('A' => 1),
 165              array('B' => 1, 'D' => 0),
 166              array('A' => 0, 'B' => 0, 'C' => 0, 'D' => 0),
 167              array('A' => 1, 'B' => 0, 'C' => 1, 'D' => 0),
 168              array('A' => 1, 'B' => 0, 'C' => 1, 'D' => 1),
 169              array('A' => 1, 'B' => 1, 'C' => 1, 'D' => 1)
 170          );
 171          foreach ($correctanswers as $correctanswer) {
 172              $postdata = $mc->prepare_simulated_post_data($correctanswer);
 173              $simulatedreponse = $mc->get_student_response_values_for_simulation($postdata);
 174              $this->assertEquals($correctanswer, $simulatedreponse, '', 0, 10, true);
 175          }
 176      }
 177  
 178  }