on-demand release 2.9beta+
[moodle.git] / question / type / tests / questionbase_test.php
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/>.
17 /**
18  * Unit tests for the question definition base classes.
19  *
20  * @package    moodlecore
21  * @subpackage questiontypes
22  * @copyright  2008 The Open University
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
30 require_once($CFG->dirroot . '/question/type/questiontypebase.php');
33 /**
34  * Unit tests for the question definition base classes.
35  *
36  * @copyright  2008 The Open University
37  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38  */
39 class test_response_answer_comparer implements question_response_answer_comparer {
40     protected $answers = array();
42     public function __construct($answers) {
43         $this->answers = $answers;
44     }
46     public function get_answers() {
47         return $this->answers;
48     }
50     public function compare_response_with_answer(array $response, question_answer $answer) {
51         return $response['answer'] == $answer->answer;
52     }
53 }
55 /**
56  * Tests for {@link question_first_matching_answer_grading_strategy}.
57  *
58  * @copyright  2008 The Open University
59  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
60  */
61 class question_first_matching_answer_grading_strategy_test extends advanced_testcase {
62     protected function setUp() {
63     }
65     protected function tearDown() {
66     }
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()));
72     }
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')));
79     }
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')));
87     }
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')));
95     }
96 }
99 /**
100  * Test for question_hint and subclasses.
101  *
102  * @copyright  2010 The Open University
103  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
104  */
105 class question_hint_test extends advanced_testcase {
106     public function test_basic() {
107         $row = new stdClass();
108         $row->id = 123;
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);
115     }
117     public function test_with_parts() {
118         $row = new stdClass();
119         $row->id = 123;
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);
131     }