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 310 and 311] [Versions 39 and 311]

   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_calculatedsimple;
  18  
  19  use question_attempt_step;
  20  use question_classified_response;
  21  use question_state;
  22  
  23  defined('MOODLE_INTERNAL') || die();
  24  
  25  global $CFG;
  26  require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
  27  
  28  
  29  /**
  30   * Unit tests for qtype_calculatedsimple_definition.
  31   *
  32   * @package    qtype_calculatedsimple
  33   * @copyright  2011 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class question_test extends \advanced_testcase {
  37      public function test_is_complete_response() {
  38          $question = \test_question_maker::make_question('calculatedsimple');
  39  
  40          $this->assertFalse($question->is_complete_response(array()));
  41          $this->assertTrue($question->is_complete_response(array('answer' => '0')));
  42          $this->assertTrue($question->is_complete_response(array('answer' => 0)));
  43          $this->assertFalse($question->is_complete_response(array('answer' => 'test')));
  44      }
  45  
  46      public function test_is_gradable_response() {
  47          $question = \test_question_maker::make_question('calculatedsimple');
  48  
  49          $this->assertFalse($question->is_gradable_response(array()));
  50          $this->assertTrue($question->is_gradable_response(array('answer' => '0')));
  51          $this->assertTrue($question->is_gradable_response(array('answer' => 0)));
  52          $this->assertTrue($question->is_gradable_response(array('answer' => 'test')));
  53      }
  54  
  55      public function test_grading() {
  56          $question = \test_question_maker::make_question('calculatedsimple');
  57          $question->start_attempt(new question_attempt_step(), 1);
  58          $values = $question->vs->get_values();
  59  
  60          $this->assertEquals(array(0, question_state::$gradedwrong),
  61                  $question->grade_response(array('answer' => $values['a'] - $values['b'])));
  62          $this->assertEquals(array(1, question_state::$gradedright),
  63                  $question->grade_response(array('answer' => $values['a'] + $values['b'])));
  64      }
  65  
  66      public function test_get_correct_response() {
  67          $question = \test_question_maker::make_question('calculatedsimple');
  68          $question->start_attempt(new question_attempt_step(), 1);
  69          $values = $question->vs->get_values();
  70  
  71          $this->assertEquals(array('answer' => $values['a'] + $values['b']),
  72                  $question->get_correct_response());
  73      }
  74  
  75      public function test_get_question_summary() {
  76          $question = \test_question_maker::make_question('calculatedsimple');
  77          $question->start_attempt(new question_attempt_step(), 1);
  78          $values = $question->vs->get_values();
  79  
  80          $qsummary = $question->get_question_summary();
  81          $this->assertEquals('What is ' . $values['a'] . ' + ' . $values['b'] . '?', $qsummary);
  82      }
  83  
  84      public function test_summarise_response() {
  85          $question = \test_question_maker::make_question('calculatedsimple');
  86          $question->start_attempt(new question_attempt_step(), 1);
  87          $values = $question->vs->get_values();
  88  
  89          $this->assertEquals('3.1', $question->summarise_response(array('answer' => '3.1')));
  90      }
  91  
  92      public function test_classify_response() {
  93          $question = \test_question_maker::make_question('calculatedsimple');
  94          $question->start_attempt(new question_attempt_step(), 1);
  95          $values = $question->vs->get_values();
  96  
  97          $this->assertEquals(array(
  98                  new question_classified_response(13, $values['a'] + $values['b'], 1.0)),
  99                  $question->classify_response(array('answer' => $values['a'] + $values['b'])));
 100          $this->assertEquals(array(
 101                  new question_classified_response(14, $values['a'] - $values['b'], 0.0)),
 102                  $question->classify_response(array('answer' => $values['a'] - $values['b'])));
 103          $this->assertEquals(array(
 104                  new question_classified_response(17, 7 * $values['a'], 0.0)),
 105                  $question->classify_response(array('answer' => 7 * $values['a'])));
 106          $this->assertEquals(array(
 107                  question_classified_response::no_response()),
 108                  $question->classify_response(array('answer' => '')));
 109      }
 110  }