Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]

   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, single 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, single 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_single_question_test extends advanced_testcase {
  38  
  39      public function test_get_expected_data() {
  40          $question = test_question_maker::make_a_multichoice_single_question();
  41          $this->assertEquals(array('answer' => PARAM_INT), $question->get_expected_data());
  42      }
  43  
  44      public function test_is_complete_response() {
  45          $question = test_question_maker::make_a_multichoice_single_question();
  46  
  47          $this->assertFalse($question->is_complete_response(array()));
  48          $this->assertTrue($question->is_complete_response(array('answer' => '0')));
  49          $this->assertTrue($question->is_complete_response(array('answer' => '2')));
  50          $this->assertFalse($question->is_complete_response(array('answer' => '-1')));
  51          $this->assertFalse($question->is_complete_response(array('answer' => -1)));
  52      }
  53  
  54      public function test_is_gradable_response() {
  55          $question = test_question_maker::make_a_multichoice_single_question();
  56  
  57          $this->assertFalse($question->is_gradable_response(array()));
  58          $this->assertTrue($question->is_gradable_response(array('answer' => '0')));
  59          $this->assertTrue($question->is_gradable_response(array('answer' => '2')));
  60          $this->assertFalse($question->is_gradable_response(array('answer' => '-1')));
  61      }
  62  
  63      public function test_is_same_response() {
  64          $question = test_question_maker::make_a_multichoice_single_question();
  65          $question->start_attempt(new question_attempt_step(), 1);
  66  
  67          $this->assertTrue($question->is_same_response(
  68                  array(),
  69                  array()));
  70  
  71          $this->assertFalse($question->is_same_response(
  72                  array(),
  73                  array('answer' => '0')));
  74  
  75          $this->assertTrue($question->is_same_response(
  76                  array('answer' => '0'),
  77                  array('answer' => '0')));
  78  
  79          $this->assertFalse($question->is_same_response(
  80                  array('answer' => '0'),
  81                  array('answer' => '1')));
  82  
  83          $this->assertTrue($question->is_same_response(
  84                  array('answer' => '2'),
  85                  array('answer' => '2')));
  86  
  87          $this->assertFalse($question->is_same_response(
  88                  array('answer' => '0'),
  89                  array('answer' => '-1')));
  90  
  91          $this->assertFalse($question->is_same_response(
  92                  array('answer' => '-1'),
  93                  array('answer' => '0')));
  94  
  95          $this->assertTrue($question->is_same_response(
  96                  array('answer' => '-1'),
  97                  array('answer' => '-1')));
  98  
  99          $this->assertTrue($question->is_same_response(
 100                  array(),
 101                  array('answer' => '-1')));
 102      }
 103  
 104      public function test_grading() {
 105          $question = test_question_maker::make_a_multichoice_single_question();
 106          $question->start_attempt(new question_attempt_step(), 1);
 107  
 108          $this->assertEquals(array(1, question_state::$gradedright),
 109                  $question->grade_response($question->prepare_simulated_post_data(array('answer' => 'A'))));
 110          $this->assertEquals(array(-0.3333333, question_state::$gradedwrong),
 111                  $question->grade_response($question->prepare_simulated_post_data(array('answer' => 'B'))));
 112          $this->assertEquals(array(-0.3333333, question_state::$gradedwrong),
 113                  $question->grade_response($question->prepare_simulated_post_data(array('answer' => 'C'))));
 114      }
 115  
 116      public function test_grading_rounding_three_right() {
 117          question_bank::load_question_definition_classes('multichoice');
 118          $mc = new qtype_multichoice_multi_question();
 119          test_question_maker::initialise_a_question($mc);
 120          $mc->name = 'Odd numbers';
 121          $mc->questiontext = 'Which are the odd numbers?';
 122          $mc->generalfeedback = '1, 3 and 5 are the odd numbers.';
 123          $mc->qtype = question_bank::get_qtype('multichoice');
 124  
 125          $mc->answernumbering = 'abc';
 126          $mc->showstandardinstruction = 0;
 127  
 128          test_question_maker::set_standard_combined_feedback_fields($mc);
 129  
 130          $mc->answers = array(
 131              11 => new question_answer(11, '1', 0.3333333, '', FORMAT_HTML),
 132              12 => new question_answer(12, '2', -1, '', FORMAT_HTML),
 133              13 => new question_answer(13, '3', 0.3333333, '', FORMAT_HTML),
 134              14 => new question_answer(14, '4', -1, '', FORMAT_HTML),
 135              15 => new question_answer(15, '5', 0.3333333, '', FORMAT_HTML),
 136              16 => new question_answer(16, '6', -1, '', FORMAT_HTML),
 137          );
 138  
 139          $mc->start_attempt(new question_attempt_step(), 1);
 140  
 141          list($grade, $state) = $mc->grade_response($mc->prepare_simulated_post_data(array('1' => '1', '3' => '1', '5' => '1')));
 142          $this->assertEqualsWithDelta(1, $grade, 0.000001);
 143          $this->assertEquals(question_state::$gradedright, $state);
 144      }
 145  
 146      public function test_get_correct_response() {
 147          $question = test_question_maker::make_a_multichoice_single_question();
 148          $question->start_attempt(new question_attempt_step(), 1);
 149  
 150          $this->assertEquals($question->prepare_simulated_post_data(array('answer' => 'A')), $question->get_correct_response());
 151      }
 152  
 153      public function test_summarise_response() {
 154          $mc = test_question_maker::make_a_multichoice_single_question();
 155          $mc->start_attempt(new question_attempt_step(), 1);
 156  
 157          $summary = $mc->summarise_response($mc->prepare_simulated_post_data(array('answer' => 'A')),
 158                                              test_question_maker::get_a_qa($mc));
 159  
 160          $this->assertEquals('A', $summary);
 161  
 162          $this->assertNull($mc->summarise_response(array(), test_question_maker::get_a_qa($mc)));
 163          $this->assertNull($mc->summarise_response(array('answer' => '-1'), test_question_maker::get_a_qa($mc)));
 164      }
 165  
 166      public function test_classify_response() {
 167          $mc = test_question_maker::make_a_multichoice_single_question();
 168          $mc->start_attempt(new question_attempt_step(), 1);
 169  
 170          $this->assertEquals(array($mc->id => new question_classified_response(14, 'B', -0.3333333)),
 171                              $mc->classify_response($mc->prepare_simulated_post_data(array('answer' => 'B'))));
 172  
 173          $this->assertEquals(array(
 174                  $mc->id => question_classified_response::no_response(),
 175          ), $mc->classify_response(array()));
 176  
 177          $this->assertEquals(array(
 178                  $mc->id => question_classified_response::no_response(),
 179          ), $mc->classify_response(array('answer' => '-1')));
 180      }
 181  
 182      public function test_make_html_inline() {
 183          $mc = test_question_maker::make_a_multichoice_single_question();
 184          $this->assertEquals('Frog', $mc->make_html_inline('<p>Frog</p>'));
 185          $this->assertEquals('Frog<br />Toad', $mc->make_html_inline("<p>Frog</p>\n<p>Toad</p>"));
 186          $this->assertEquals('<img src="http://example.com/pic.png" alt="Graph" />',
 187                  $mc->make_html_inline(
 188                      '<p><img src="http://example.com/pic.png" alt="Graph" /></p>'));
 189          $this->assertEquals("Frog<br />XXX <img src='http://example.com/pic.png' alt='Graph' />",
 190                  $mc->make_html_inline(" <p> Frog </p> \n\r
 191                      <p> XXX <img src='http://example.com/pic.png' alt='Graph' /> </p> "));
 192          $this->assertEquals('Frog', $mc->make_html_inline('<p>Frog</p><p></p>'));
 193          $this->assertEquals('Frog<br />†', $mc->make_html_inline('<p>Frog</p><p>†</p>'));
 194      }
 195  
 196      public function test_simulated_post_data() {
 197          $mc = test_question_maker::make_a_multichoice_single_question();
 198          $mc->shuffleanswers = false;
 199          $mc->answers[13]->answer = '<p>A</p>';
 200          $mc->answers[14]->answer = '<p>B</p>';
 201          $mc->answers[15]->answer = '<p>C</p>';
 202          $mc->start_attempt(new question_attempt_step(), 1);
 203  
 204          $originalresponse = array('answer' => 1);
 205  
 206          $simulated = $mc->get_student_response_values_for_simulation($originalresponse);
 207          $this->assertEquals(array('answer' => 'B'), $simulated);
 208  
 209          $reconstucted = $mc->prepare_simulated_post_data($simulated);
 210          $this->assertEquals($originalresponse, $reconstucted);
 211      }
 212  }