2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for the question_first_matching_answer_grading_strategy class.
20 * @package core_question
21 * @copyright 2008 The Open University
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot . '/question/type/questiontypebase.php');
32 * Helper used by the testcases in this file.
34 * @copyright 2008 The Open University
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class test_response_answer_comparer implements question_response_answer_comparer {
38 protected $answers = array();
40 public function __construct($answers) {
41 $this->answers = $answers;
44 public function get_answers() {
45 return $this->answers;
48 public function compare_response_with_answer(array $response, question_answer $answer) {
49 return $response['answer'] == $answer->answer;
55 * Tests for {@link question_first_matching_answer_grading_strategy}.
57 * @copyright 2008 The Open University
58 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
60 class question_first_matching_answer_grading_strategy_testcase extends advanced_testcase {
61 protected function setUp() {
64 protected function tearDown() {
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()));
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')));
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')));
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')));