Merge branch 'MDL-70148-310' of git://github.com/andrewnicols/moodle into MOODLE_310_...
[moodle.git] / question / type / calculatedmulti / tests / question_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/>.
18 /**
19  * Unit tests for the calculated multiple-choice question definition class.
20  *
21  * @package    qtype
22  * @subpackage calculatedmulti
23  * @copyright  2011 The Open University
24  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25  */
28 defined('MOODLE_INTERNAL') || die();
30 global $CFG;
31 require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
34 /**
35  * Unit tests for qtype_calculatedmulti_definition.
36  *
37  * @copyright  2011 The Open University
38  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39  */
40 class qtype_calculatedmulti_question_test extends advanced_testcase {
41     public function test_is_complete_response() {
42         $question = test_question_maker::make_question('calculated');
44         $this->assertFalse($question->is_complete_response(array()));
45         $this->assertTrue($question->is_complete_response(array('answer' => '0')));
46         $this->assertTrue($question->is_complete_response(array('answer' => 0)));
47         $this->assertFalse($question->is_complete_response(array('answer' => 'test')));
48     }
50     public function test_is_gradable_response() {
51         $question = test_question_maker::make_question('calculated');
53         $this->assertFalse($question->is_gradable_response(array()));
54         $this->assertTrue($question->is_gradable_response(array('answer' => '0')));
55         $this->assertTrue($question->is_gradable_response(array('answer' => 0)));
56         $this->assertTrue($question->is_gradable_response(array('answer' => 'test')));
57     }
59     public function test_grading() {
60         $question = test_question_maker::make_question('calculated');
61         $question->start_attempt(new question_attempt_step(), 1);
62         $values = $question->vs->get_values();
64         $this->assertEquals(array(0, question_state::$gradedwrong),
65                 $question->grade_response(array('answer' => $values['a'] - $values['b'])));
66         $this->assertEquals(array(1, question_state::$gradedright),
67                 $question->grade_response(array('answer' => $values['a'] + $values['b'])));
68     }
70     public function test_get_correct_response() {
71         $question = test_question_maker::make_question('calculated');
72         $question->start_attempt(new question_attempt_step(), 1);
73         $values = $question->vs->get_values();
75         $this->assertEquals(array('answer' => $values['a'] + $values['b']),
76                 $question->get_correct_response());
77     }
79     public function test_get_question_summary() {
80         $question = test_question_maker::make_question('calculated');
81         $question->start_attempt(new question_attempt_step(), 1);
82         $values = $question->vs->get_values();
84         $qsummary = $question->get_question_summary();
85         $this->assertEquals('What is ' . $values['a'] . ' + ' . $values['b'] . '?', $qsummary);
86     }
88     public function test_summarise_response() {
89         $question = test_question_maker::make_question('calculated');
90         $question->start_attempt(new question_attempt_step(), 1);
91         $values = $question->vs->get_values();
93         $this->assertEquals('3.1', $question->summarise_response(array('answer' => '3.1')));
94     }
96     public function test_classify_response() {
97         $question = test_question_maker::make_question('calculated');
98         $question->start_attempt(new question_attempt_step(), 1);
99         $values = $question->vs->get_values();
101         $this->assertEquals(array(
102                 new question_classified_response(13, $values['a'] + $values['b'], 1.0)),
103                 $question->classify_response(array('answer' => $values['a'] + $values['b'])));
104         $this->assertEquals(array(
105                 new question_classified_response(14, $values['a'] - $values['b'], 0.0)),
106                 $question->classify_response(array('answer' => $values['a'] - $values['b'])));
107         $this->assertEquals(array(
108                 new question_classified_response(17, 7 * $values['a'], 0.0)),
109                 $question->classify_response(array('answer' => 7 * $values['a'])));
110         $this->assertEquals(array(
111                 question_classified_response::no_response()),
112                 $question->classify_response(array('answer' => '')));
113     }