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 definition base classes.
21 * @subpackage questiontypes
22 * @copyright 2008 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->dirroot . '/question/type/questiontypebase.php');
34 * Unit tests for the question definition base classes.
36 * @copyright 2008 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class test_response_answer_comparer implements question_response_answer_comparer {
40 protected $answers = array();
42 public function __construct($answers) {
43 $this->answers = $answers;
46 public function get_answers() {
47 return $this->answers;
50 public function compare_response_with_answer(array $response, question_answer $answer) {
51 return $response['answer'] == $answer->answer;
56 * Tests for {@link question_first_matching_answer_grading_strategy}.
58 * @copyright 2008 The Open University
59 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
61 class question_first_matching_answer_grading_strategy_test extends advanced_testcase {
62 protected function setUp() {
65 protected function tearDown() {
68 public function test_no_answers_gives_null() {
69 $question = new test_response_answer_comparer(array());
70 $strategy = new question_first_matching_answer_grading_strategy($question);
71 $this->assertNull($strategy->grade(array()));
74 public function test_matching_answer_returned1() {
75 $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
76 $question = new test_response_answer_comparer(array($answer));
77 $strategy = new question_first_matching_answer_grading_strategy($question);
78 $this->assertSame($answer, $strategy->grade(array('answer' => 'frog')));
81 public function test_matching_answer_returned2() {
82 $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
83 $answer2 = new question_answer(0, 'frog', 0.5, '', FORMAT_HTML);
84 $question = new test_response_answer_comparer(array($answer, $answer2));
85 $strategy = new question_first_matching_answer_grading_strategy($question);
86 $this->assertSame($answer, $strategy->grade(array('answer' => 'frog')));
89 public function test_no_matching_answer_gives_null() {
90 $answer = new question_answer(0, 'frog', 1, '', FORMAT_HTML);
91 $answer2 = new question_answer(0, 'frog', 0.5, '', FORMAT_HTML);
92 $question = new test_response_answer_comparer(array($answer, $answer2));
93 $strategy = new question_first_matching_answer_grading_strategy($question);
94 $this->assertNull($strategy->grade(array('answer' => 'toad')));
100 * Test for question_hint and subclasses.
102 * @copyright 2010 The Open University
103 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
105 class question_hint_test extends advanced_testcase {
106 public function test_basic() {
107 $row = new stdClass();
109 $row->hint = 'A hint';
110 $row->hintformat = FORMAT_HTML;
111 $hint = question_hint::load_from_record($row);
112 $this->assertEquals($row->id, $hint->id);
113 $this->assertEquals($row->hint, $hint->hint);
114 $this->assertEquals($row->hintformat, $hint->hintformat);
117 public function test_with_parts() {
118 $row = new stdClass();
120 $row->hint = 'A hint';
121 $row->hintformat = FORMAT_HTML;
122 $row->shownumcorrect = 1;
123 $row->clearwrong = 1;
125 $hint = question_hint_with_parts::load_from_record($row);
126 $this->assertEquals($row->id, $hint->id);
127 $this->assertEquals($row->hint, $hint->hint);
128 $this->assertEquals($row->hintformat, $hint->hintformat);
129 $this->assertNotEmpty($hint->shownumcorrect);
130 $this->assertNotEmpty($hint->clearwrong);