Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  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  defined('MOODLE_INTERNAL') || die();
  26  
  27  global $CFG;
  28  require_once($CFG->dirroot . '/question/type/questiontypebase.php');
  29  
  30  
  31  /**
  32   * Helper used by the testcases in this file.
  33   *
  34   * @copyright  2008 The Open University
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class test_response_answer_comparer implements question_response_answer_comparer {
  38      protected $answers = array();
  39  
  40      public function __construct($answers) {
  41          $this->answers = $answers;
  42      }
  43  
  44      public function get_answers() {
  45          return $this->answers;
  46      }
  47  
  48      public function compare_response_with_answer(array $response, question_answer $answer) {
  49          return $response['answer'] == $answer->answer;
  50      }
  51  }
  52  
  53  
  54  /**
  55   * Tests for {@link question_first_matching_answer_grading_strategy}.
  56   *
  57   * @copyright  2008 The Open University
  58   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  59   */
  60  class question_first_matching_answer_grading_strategy_testcase extends advanced_testcase {
  61      protected function setUp() {
  62      }
  63  
  64      protected function tearDown() {
  65      }
  66  
  67      public function test_no_answers_gives_null() {
  68          $question = new test_response_answer_comparer(array());
  69          $strategy = new question_first_matching_answer_grading_strategy($question);
  70          $this->assertNull($strategy->grade(array()));
  71      }
  72  
  73      public function test_matching_answer_returned1() {
  74          $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
  75          $question = new test_response_answer_comparer(array($answer));
  76          $strategy = new question_first_matching_answer_grading_strategy($question);
  77          $this->assertSame($answer, $strategy->grade(array('answer' => 'frog')));
  78      }
  79  
  80      public function test_matching_answer_returned2() {
  81          $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
  82          $answer2 = new question_answer(0, 'frog', 0.5, '', FORMAT_HTML);
  83          $question = new test_response_answer_comparer(array($answer, $answer2));
  84          $strategy = new question_first_matching_answer_grading_strategy($question);
  85          $this->assertSame($answer, $strategy->grade(array('answer' => 'frog')));
  86      }
  87  
  88      public function test_no_matching_answer_gives_null() {
  89          $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
  90          $answer2 = new question_answer(0, 'frog', 0.5, '', FORMAT_HTML);
  91          $question = new test_response_answer_comparer(array($answer, $answer2));
  92          $strategy = new question_first_matching_answer_grading_strategy($question);
  93          $this->assertNull($strategy->grade(array('answer' => 'toad')));
  94      }
  95  }