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  /**
  18   * Unit tests for the question_first_matching_answer_grading_strategy class.
  19   *
  20   * @package   core_question
  21   * @copyright 2008 The Open University
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_question;
  26  
  27  use question_answer;
  28  use question_first_matching_answer_grading_strategy;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  global $CFG;
  33  require_once($CFG->dirroot . '/question/type/questiontypebase.php');
  34  
  35  /**
  36   * Helper used by the testcases in this file.
  37   *
  38   * @copyright  2008 The Open University
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class test_response_answer_comparer implements \question_response_answer_comparer {
  42      protected $answers = array();
  43  
  44      public function __construct($answers) {
  45          $this->answers = $answers;
  46      }
  47  
  48      public function get_answers() {
  49          return $this->answers;
  50      }
  51  
  52      public function compare_response_with_answer(array $response, question_answer $answer) {
  53          return $response['answer'] == $answer->answer;
  54      }
  55  }
  56  
  57  /**
  58   * Tests for {@link question_first_matching_answer_grading_strategy}.
  59   *
  60   * @package    core_question
  61   * @copyright  2008 The Open University
  62   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  63   */
  64  class question_first_matching_answer_grading_strategy_test extends \advanced_testcase {
  65      protected function setUp(): void {
  66      }
  67  
  68      protected function tearDown(): void {
  69      }
  70  
  71      public function test_no_answers_gives_null() {
  72          $question = new test_response_answer_comparer(array());
  73          $strategy = new question_first_matching_answer_grading_strategy($question);
  74          $this->assertNull($strategy->grade(array()));
  75      }
  76  
  77      public function test_matching_answer_returned1() {
  78          $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
  79          $question = new test_response_answer_comparer(array($answer));
  80          $strategy = new question_first_matching_answer_grading_strategy($question);
  81          $this->assertSame($answer, $strategy->grade(array('answer' => 'frog')));
  82      }
  83  
  84      public function test_matching_answer_returned2() {
  85          $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
  86          $answer2 = new question_answer(0, 'frog', 0.5, '', FORMAT_HTML);
  87          $question = new test_response_answer_comparer(array($answer, $answer2));
  88          $strategy = new question_first_matching_answer_grading_strategy($question);
  89          $this->assertSame($answer, $strategy->grade(array('answer' => 'frog')));
  90      }
  91  
  92      public function test_no_matching_answer_gives_null() {
  93          $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
  94          $answer2 = new question_answer(0, 'frog', 0.5, '', FORMAT_HTML);
  95          $question = new test_response_answer_comparer(array($answer, $answer2));
  96          $strategy = new question_first_matching_answer_grading_strategy($question);
  97          $this->assertNull($strategy->grade(array('answer' => 'toad')));
  98      }
  99  }