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