Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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  namespace qtype_numerical;
  18  
  19  use qtype_numerical;
  20  use qtype_numerical_edit_form;
  21  use question_possible_response;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once($CFG->dirroot . '/question/type/numerical/questiontype.php');
  27  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  28  require_once($CFG->dirroot . '/question/type/edit_question_form.php');
  29  require_once($CFG->dirroot . '/question/type/numerical/edit_numerical_form.php');
  30  
  31  /**
  32   * Unit tests for question/type/numerical/questiontype.php.
  33   *
  34   * @package    qtype_numerical
  35   * @copyright  2006 The Open University
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   *
  38   * @covers \question_type
  39   * @covers \qtype_numerical
  40   */
  41  class question_type_test extends \advanced_testcase {
  42      protected $tolerance = 0.00000001;
  43      protected $qtype;
  44  
  45      protected function setUp(): void {
  46          $this->qtype = new qtype_numerical();
  47      }
  48  
  49      protected function tearDown(): void {
  50          $this->qtype = null;
  51      }
  52  
  53      protected function get_test_question_data() {
  54          $q = new \stdClass;
  55          $q->id = 1;
  56          $q->options = new \stdClass();
  57          $q->options->unitpenalty = 0;
  58          $q->options->answers[13] = (object) array(
  59              'id' => 13,
  60              'answer' => 42,
  61              'fraction' => 1,
  62              'feedback' => 'yes',
  63              'feedbackformat' => FORMAT_MOODLE,
  64              'tolerance' => 0.5
  65          );
  66          $q->options->answers[14] = (object) array(
  67              'id' => 14,
  68              'answer' => '*',
  69              'fraction' => 0.1,
  70              'feedback' => 'no',
  71              'feedbackformat' => FORMAT_MOODLE,
  72              'tolerance' => ''
  73          );
  74  
  75          $q->options->units = array(
  76              (object) array('unit' => 'm', 'multiplier' => 1),
  77              (object) array('unit' => 'cm', 'multiplier' => 0.01)
  78          );
  79  
  80          return $q;
  81      }
  82  
  83      public function test_name() {
  84          $this->assertEquals($this->qtype->name(), 'numerical');
  85      }
  86  
  87      public function test_can_analyse_responses() {
  88          $this->assertTrue($this->qtype->can_analyse_responses());
  89      }
  90  
  91      public function test_get_random_guess_score() {
  92          $q = $this->get_test_question_data();
  93          $this->assertEquals(0.1, $this->qtype->get_random_guess_score($q));
  94      }
  95  
  96      public function test_get_possible_responses() {
  97          $q = $this->get_test_question_data();
  98  
  99          $this->assertEquals(array(
 100              $q->id => array(
 101                  13 => new question_possible_response('42 m (41.5..42.5)', 1),
 102                  14 => new question_possible_response('*', 0.1),
 103                  null => question_possible_response::no_response()
 104              ),
 105          ), $this->qtype->get_possible_responses($q));
 106      }
 107  
 108      public function test_get_possible_responses_no_star() {
 109          $q = $this->get_test_question_data();
 110          unset($q->options->answers[14]);
 111  
 112          $this->assertEquals(array(
 113              $q->id => array(
 114                  13 => new question_possible_response('42 m (41.5..42.5)', 1),
 115                  0 => new question_possible_response(
 116                          get_string('didnotmatchanyanswer', 'question'), 0),
 117                  null => question_possible_response::no_response()
 118              ),
 119          ), $this->qtype->get_possible_responses($q));
 120      }
 121  
 122      public function test_question_saving_pi() {
 123          $this->resetAfterTest(true);
 124          $this->setAdminUser();
 125  
 126          $questiondata = \test_question_maker::get_question_data('numerical');
 127          $formdata = \test_question_maker::get_question_form_data('numerical');
 128  
 129          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
 130          $cat = $generator->create_question_category(array());
 131  
 132          $formdata->category = "{$cat->id},{$cat->contextid}";
 133          qtype_numerical_edit_form::mock_submit((array)$formdata);
 134  
 135          $form = \qtype_numerical_test_helper::get_question_editing_form($cat, $questiondata);
 136  
 137          $this->assertTrue($form->is_validated());
 138  
 139          $fromform = $form->get_data();
 140  
 141          $returnedfromsave = $this->qtype->save_question($questiondata, $fromform);
 142          $actualquestionsdata = question_load_questions(array($returnedfromsave->id));
 143          $actualquestiondata = end($actualquestionsdata);
 144  
 145          foreach ($questiondata as $property => $value) {
 146              if (!in_array($property, array('options'))) {
 147                  $this->assertEquals($value, $actualquestiondata->$property);
 148              }
 149          }
 150  
 151          foreach ($questiondata->options as $optionname => $value) {
 152              if (!in_array($optionname, array('answers'))) {
 153                  $this->assertEquals($value, $actualquestiondata->options->$optionname);
 154              }
 155          }
 156  
 157          foreach ($questiondata->options->answers as $ansindex => $answer) {
 158              $actualanswer = array_shift($actualquestiondata->options->answers);
 159              foreach ($answer as $ansproperty => $ansvalue) {
 160                  // This question does not use 'answerformat', will ignore it.
 161                  if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) {
 162                      $this->assertEquals($ansvalue, $actualanswer->$ansproperty);
 163                  }
 164              }
 165          }
 166      }
 167  
 168      public function test_is_valid_number() {
 169          $this->assertTrue(qtype_numerical::is_valid_number('1,001'));
 170          $this->assertTrue(qtype_numerical::is_valid_number('1.001'));
 171          $this->assertTrue(qtype_numerical::is_valid_number('1'));
 172          $this->assertTrue(qtype_numerical::is_valid_number('1,e8'));
 173          $this->assertFalse(qtype_numerical::is_valid_number('1001 xxx'));
 174          $this->assertTrue(qtype_numerical::is_valid_number('1.e8'));
 175      }
 176  }