Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [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_calculated;
  18  
  19  use qtype_calculated;
  20  use qtype_numerical;
  21  use question_bank;
  22  use question_possible_response;
  23  
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  global $CFG;
  27  require_once($CFG->dirroot . '/question/type/calculated/questiontype.php');
  28  require_once($CFG->dirroot . '/question/type/calculated/tests/helper.php');
  29  
  30  
  31  /**
  32   * Unit tests for question/type/calculated/questiontype.php.
  33   *
  34   * @package    qtype_calculated
  35   * @copyright  2012 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_calculated
  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_calculated();
  47      }
  48  
  49      protected function tearDown(): void {
  50          $this->qtype = null;
  51      }
  52  
  53      public function test_name() {
  54          $this->assertEquals($this->qtype->name(), 'calculated');
  55      }
  56  
  57      public function test_can_analyse_responses() {
  58          $this->assertTrue($this->qtype->can_analyse_responses());
  59      }
  60  
  61      public function test_get_random_guess_score() {
  62          $q = \test_question_maker::get_question_data('calculated');
  63          $q->options->answers[17]->fraction = 0.1;
  64          $this->assertEquals(0.1, $this->qtype->get_random_guess_score($q));
  65      }
  66  
  67      public function test_load_question() {
  68          $this->resetAfterTest();
  69  
  70          $syscontext = \context_system::instance();
  71          /** @var core_question_generator $generator */
  72          $generator = $this->getDataGenerator()->get_plugin_generator('core_question');
  73          $category = $generator->create_question_category(['contextid' => $syscontext->id]);
  74  
  75          $fromform = \test_question_maker::get_question_form_data('calculated');
  76          $fromform->category = $category->id . ',' . $syscontext->id;
  77  
  78          $question = new \stdClass();
  79          $question->category = $category->id;
  80          $question->qtype = 'calculated';
  81          $question->createdby = 0;
  82  
  83          $this->qtype->save_question($question, $fromform);
  84          $questiondata = question_bank::load_question_data($question->id);
  85  
  86          $this->assertEquals(['id', 'category', 'parent', 'name', 'questiontext', 'questiontextformat',
  87                  'generalfeedback', 'generalfeedbackformat', 'defaultmark', 'penalty', 'qtype',
  88                  'length', 'stamp', 'timecreated', 'timemodified', 'createdby', 'modifiedby', 'idnumber', 'contextid',
  89                  'status', 'versionid', 'version', 'questionbankentryid', 'categoryobject', 'options', 'hints'],
  90                  array_keys(get_object_vars($questiondata)));
  91          $this->assertEquals($category->id, $questiondata->category);
  92          $this->assertEquals(0, $questiondata->parent);
  93          $this->assertEquals($fromform->name, $questiondata->name);
  94          $this->assertEquals($fromform->questiontext, $questiondata->questiontext);
  95          $this->assertEquals($fromform->questiontextformat, $questiondata->questiontextformat);
  96          $this->assertEquals('', $questiondata->generalfeedback);
  97          $this->assertEquals(0, $questiondata->generalfeedbackformat);
  98          $this->assertEquals($fromform->defaultmark, $questiondata->defaultmark);
  99          $this->assertEquals(0, $questiondata->penalty);
 100          $this->assertEquals('calculated', $questiondata->qtype);
 101          $this->assertEquals(1, $questiondata->length);
 102          $this->assertEquals(\core_question\local\bank\question_version_status::QUESTION_STATUS_READY, $questiondata->status);
 103          $this->assertEquals($question->createdby, $questiondata->createdby);
 104          $this->assertEquals($question->createdby, $questiondata->modifiedby);
 105          $this->assertEquals('', $questiondata->idnumber);
 106          $this->assertEquals($syscontext->id, $questiondata->contextid);
 107          $this->assertEquals([], $questiondata->hints);
 108  
 109          // Options.
 110          $this->assertEquals($questiondata->id, $questiondata->options->question);
 111          $this->assertEquals([], $questiondata->options->units);
 112          $this->assertEquals(qtype_numerical::UNITNONE, $questiondata->options->showunits);
 113          $this->assertEquals(0, $questiondata->options->unitgradingtype); // Unit role is none, so this is 0.
 114          $this->assertEquals($fromform->unitpenalty, $questiondata->options->unitpenalty);
 115          $this->assertEquals($fromform->unitsleft, $questiondata->options->unitsleft);
 116  
 117          // Build the expected answer base.
 118          $answerbase = [
 119              'question' => $questiondata->id,
 120              'answerformat' => 0,
 121          ];
 122          $expectedanswers = [];
 123          foreach ($fromform->answer as $key => $value) {
 124              $answer = $answerbase + [
 125                  'answer' => $fromform->answer[$key],
 126                  'fraction' => (float)$fromform->fraction[$key],
 127                  'tolerance' => $fromform->tolerance[$key],
 128                  'tolerancetype' => $fromform->tolerancetype[$key],
 129                  'correctanswerlength' => $fromform->correctanswerlength[$key],
 130                  'correctanswerformat' => $fromform->correctanswerformat[$key],
 131                  'feedback' => $fromform->feedback[$key]['text'],
 132                  'feedbackformat' => $fromform->feedback[$key]['format'],
 133              ];
 134              $expectedanswers[] = (object)$answer;
 135          }
 136          // Need to get rid of ids.
 137          $gotanswers = array_map(function($answer) {
 138                  unset($answer->id);
 139                  return $answer;
 140          }, $questiondata->options->answers);
 141          // Compare answers.
 142          $this->assertEquals($expectedanswers, array_values($gotanswers));
 143      }
 144  
 145      protected function get_possible_response($ans, $tolerance, $type) {
 146          $a = new \stdClass();
 147          $a->answer = $ans;
 148          $a->tolerance = $tolerance;
 149          $a->tolerancetype = get_string($type, 'qtype_numerical');
 150          return get_string('answerwithtolerance', 'qtype_calculated', $a);
 151      }
 152  
 153      public function test_get_possible_responses() {
 154          $q = \test_question_maker::get_question_data('calculated');
 155  
 156          $this->assertEquals(array(
 157              $q->id => array(
 158                  13 => new question_possible_response(
 159                          $this->get_possible_response('{a} + {b}', 0.001, 'nominal'), 1.0),
 160                  14 => new question_possible_response(
 161                          $this->get_possible_response('{a} - {b}', 0.001, 'nominal'), 0.0),
 162                  17 => new question_possible_response('*', 0.0),
 163                  null => question_possible_response::no_response()
 164              ),
 165          ), $this->qtype->get_possible_responses($q));
 166      }
 167  
 168      public function test_get_possible_responses_no_star() {
 169          $q = \test_question_maker::get_question_data('calculated');
 170          unset($q->options->answers[17]);
 171  
 172          $this->assertEquals(array(
 173              $q->id => array(
 174                  13 => new question_possible_response(
 175                          $this->get_possible_response('{a} + {b}', 0.001, 'nominal'), 1),
 176                  14 => new question_possible_response(
 177                          $this->get_possible_response('{a} - {b}', 0.001, 'nominal'), 0),
 178                  0  => new question_possible_response(
 179                          get_string('didnotmatchanyanswer', 'question'), 0),
 180                  null => question_possible_response::no_response()
 181              ),
 182          ), $this->qtype->get_possible_responses($q));
 183      }
 184  
 185      public function test_get_short_question_name() {
 186          $this->resetAfterTest();
 187  
 188          // Enable multilang filter to on content and heading.
 189          filter_set_global_state('multilang', TEXTFILTER_ON);
 190          filter_set_applies_to_strings('multilang', 1);
 191          $filtermanager = \filter_manager::instance();
 192          $filtermanager->reset_caches();
 193  
 194          $context = \context_system::instance();
 195  
 196          $longmultilangquestionname = "<span lang=\"en\" class=\"multilang\">Lorem ipsum dolor sit amet, consetetur sadipscing elitr</span><span lang=\"fr\" class=\"multilang\">Lorem ipsum dolor sit amet, consetetur sadipscing elitr</span>";
 197          $shortmultilangquestionname = "<span lang=\"en\" class=\"multilang\">Lorem ipsum</span><span lang=\"fr\" class=\"multilang\">Lorem ipsum</span>";
 198          $longquestionname = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr";
 199          $shortquestionname = "Lorem ipsum";
 200          $this->assertEquals("Lorem ipsum dolor...", $this->qtype->get_short_question_name($longmultilangquestionname, 20));
 201          $this->assertEquals("Lorem ipsum", $this->qtype->get_short_question_name($shortmultilangquestionname, 20));
 202          $this->assertEquals("Lorem ipsum dolor...", $this->qtype->get_short_question_name($longquestionname, 20));
 203          $this->assertEquals("Lorem ipsum", $this->qtype->get_short_question_name($shortquestionname, 20));
 204      }
 205  
 206      public function test_placehodler_regex() {
 207          preg_match_all(qtype_calculated::PLACEHODLER_REGEX, '= {={a} + {b}}', $matches);
 208          $this->assertEquals([['{a}', '{b}'], ['a', 'b']], $matches);
 209      }
 210  
 211      public function test_formulas_in_text_regex() {
 212          preg_match_all(qtype_calculated::FORMULAS_IN_TEXT_REGEX, '= {={a} + {b}}', $matches);
 213          $this->assertEquals([['{={a} + {b}}'], ['{a} + {b}']], $matches);
 214      }
 215  
 216      public function test_find_dataset_names() {
 217          $this->assertEquals([], $this->qtype->find_dataset_names('Frog.'));
 218  
 219          $this->assertEquals(['a' => 'a', 'b' => 'b'],
 220                  $this->qtype->find_dataset_names('= {={a} + {b}}'));
 221  
 222          $this->assertEquals(['a' => 'a', 'b' => 'b'],
 223                  $this->qtype->find_dataset_names('What is {a} plus {b}? (Hint, it is not {={a}*{b}}.)'));
 224  
 225          $this->assertEquals(['a' => 'a', 'b' => 'b', 'c' => 'c'],
 226                  $this->qtype->find_dataset_names('
 227                          <p>If called with $a = {a} and $b = {b}, what does this PHP function return?</p>
 228                          <pre>
 229                          /**
 230                           * What does this do?
 231                           */
 232                          function mystery($a, $b) {
 233                              return {c}*$a + $b;
 234                          }
 235                          </pre>
 236                          '));
 237      }
 238  
 239      public function test_calculate_answer_nan_inf() {
 240          $answer = qtype_calculated_calculate_answer('acos(1.1)', [], 0.1, 1, 2, 2);
 241          $this->assertIsObject($answer);
 242          $this->assertNan($answer->answer);
 243  
 244          $answer = qtype_calculated_calculate_answer('log(0.0)', [], 0.1, 1, 2, 2);
 245          $this->assertIsObject($answer);
 246          $this->assertInfinite($answer->answer); // Actually -INF.
 247  
 248          // Dividing by zero is hard to test, so get +INF another way.
 249          $answer = qtype_calculated_calculate_answer('abs(log(0.0))', [], 0.1, 1, 2, 2);
 250          $this->assertIsObject($answer);
 251          $this->assertInfinite($answer->answer);
 252      }
 253  }