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 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 shortanswer question type class.
  19   *
  20   * @package    qtype_shortanswer
  21   * @copyright  2013 The Open University
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once($CFG->dirroot . '/question/type/shortanswer/questiontype.php');
  30  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  31  require_once($CFG->dirroot . '/question/type/edit_question_form.php');
  32  require_once($CFG->dirroot . '/question/type/shortanswer/edit_shortanswer_form.php');
  33  
  34  /**
  35   * Unit tests for the shortanswer question type class.
  36   *
  37   * @copyright  2007 The Open University
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class qtype_shortanswer_test extends advanced_testcase {
  41      public static $includecoverage = array(
  42          'question/type/questiontypebase.php',
  43          'question/type/shortanswer/questiontype.php',
  44      );
  45  
  46      protected $qtype;
  47  
  48      protected function setUp(): void {
  49          $this->qtype = new qtype_shortanswer();
  50      }
  51  
  52      protected function tearDown(): void {
  53          $this->qtype = null;
  54      }
  55  
  56      protected function get_test_question_data() {
  57          return test_question_maker::get_question_data('shortanswer');
  58      }
  59  
  60      public function test_name() {
  61          $this->assertEquals($this->qtype->name(), 'shortanswer');
  62      }
  63  
  64      public function test_can_analyse_responses() {
  65          $this->assertTrue($this->qtype->can_analyse_responses());
  66      }
  67  
  68      public function test_get_random_guess_score() {
  69          $q = test_question_maker::get_question_data('shortanswer');
  70          $q->options->answers[15]->fraction = 0.1;
  71          $this->assertEquals(0.1, $this->qtype->get_random_guess_score($q));
  72      }
  73  
  74      public function test_get_possible_responses() {
  75          $q = test_question_maker::get_question_data('shortanswer');
  76  
  77          $this->assertEquals(array(
  78              $q->id => array(
  79                  13 => new question_possible_response('frog', 1),
  80                  14 => new question_possible_response('toad', 0.8),
  81                  15 => new question_possible_response('*', 0),
  82                  null => question_possible_response::no_response()
  83              ),
  84          ), $this->qtype->get_possible_responses($q));
  85      }
  86  
  87      public function test_get_possible_responses_no_star() {
  88          $q = test_question_maker::get_question_data('shortanswer', 'frogonly');
  89  
  90          $this->assertEquals(array(
  91              $q->id => array(
  92                  13 => new question_possible_response('frog', 1),
  93                  0 => new question_possible_response(get_string('didnotmatchanyanswer', 'question'), 0),
  94                  null => question_possible_response::no_response()
  95              ),
  96          ), $this->qtype->get_possible_responses($q));
  97      }
  98  
  99      public function test_question_saving_frogtoad() {
 100          $this->resetAfterTest(true);
 101          $this->setAdminUser();
 102  
 103          $questiondata = test_question_maker::get_question_data('shortanswer');
 104          $formdata = test_question_maker::get_question_form_data('shortanswer');
 105  
 106          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 107          $cat = $generator->create_question_category(array());
 108  
 109          $formdata->category = "{$cat->id},{$cat->contextid}";
 110          qtype_shortanswer_edit_form::mock_submit((array)$formdata);
 111  
 112          $form = qtype_shortanswer_test_helper::get_question_editing_form($cat, $questiondata);
 113  
 114          $this->assertTrue($form->is_validated());
 115  
 116          $fromform = $form->get_data();
 117  
 118          $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
 119          $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
 120          $actualquestiondata = end($actualquestionsdata);
 121  
 122          foreach ($questiondata as $property => $value) {
 123              if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options'))) {
 124                  $this->assertEquals($value, $actualquestiondata->$property);
 125              }
 126          }
 127  
 128          foreach ($questiondata->options as $optionname => $value) {
 129              if ($optionname != 'answers') {
 130                  $this->assertEquals($value, $actualquestiondata->options->$optionname);
 131              }
 132          }
 133  
 134          foreach ($questiondata->options->answers as $answer) {
 135              $actualanswer = array_shift($actualquestiondata->options->answers);
 136              foreach ($answer as $ansproperty => $ansvalue) {
 137                  // This question does not use 'answerformat', will ignore it.
 138                  if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
 139                      $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
 140                  }
 141              }
 142          }
 143      }
 144  
 145      public function test_question_saving_trims_answers() {
 146          $this->resetAfterTest(true);
 147          $this->setAdminUser();
 148  
 149          $questiondata = test_question_maker::get_question_data('shortanswer');
 150          $formdata = test_question_maker::get_question_form_data('shortanswer');
 151  
 152          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 153          $cat = $generator->create_question_category(array());
 154  
 155          $formdata->category = "{$cat->id},{$cat->contextid}";
 156          $formdata->answer[0] = '   frog   ';
 157          qtype_shortanswer_edit_form::mock_submit((array)$formdata);
 158  
 159          $form = qtype_shortanswer_test_helper::get_question_editing_form($cat, $questiondata);
 160  
 161          $this->assertTrue($form->is_validated());
 162  
 163          $fromform = $form->get_data();
 164  
 165          $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
 166          $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
 167          $actualquestiondata = end($actualquestionsdata);
 168  
 169          $firstsavedanswer = reset($questiondata->options->answers);
 170          $this->assertEquals('frog', $firstsavedanswer->answer);
 171      }
 172  }