Merge branch 'MDL-70065_310' of https://github.com/timhunt/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / calculatedsimple / tests / question_test.php
CommitLineData
603bd001
PS
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/**
19 * Unit tests for the simple calculated question definition class.
20 *
21 * @package qtype
22 * @subpackage calculatedsimple
23 * @copyright 2011 The Open University
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26
27
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
32
33
34/**
35 * Unit tests for qtype_calculatedsimple_definition.
36 *
37 * @copyright 2011 The Open University
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 */
40class qtype_calculatedsimple_question_test extends advanced_testcase {
41 public function test_is_complete_response() {
42 $question = test_question_maker::make_question('calculatedsimple');
43
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 }
49
50 public function test_is_gradable_response() {
51 $question = test_question_maker::make_question('calculatedsimple');
52
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 }
58
59 public function test_grading() {
60 $question = test_question_maker::make_question('calculatedsimple');
61 $question->start_attempt(new question_attempt_step(), 1);
62 $values = $question->vs->get_values();
63
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 }
69
70 public function test_get_correct_response() {
71 $question = test_question_maker::make_question('calculatedsimple');
72 $question->start_attempt(new question_attempt_step(), 1);
73 $values = $question->vs->get_values();
74
75 $this->assertEquals(array('answer' => $values['a'] + $values['b']),
76 $question->get_correct_response());
77 }
78
79 public function test_get_question_summary() {
80 $question = test_question_maker::make_question('calculatedsimple');
81 $question->start_attempt(new question_attempt_step(), 1);
82 $values = $question->vs->get_values();
83
84 $qsummary = $question->get_question_summary();
85 $this->assertEquals('What is ' . $values['a'] . ' + ' . $values['b'] . '?', $qsummary);
86 }
87
88 public function test_summarise_response() {
89 $question = test_question_maker::make_question('calculatedsimple');
90 $question->start_attempt(new question_attempt_step(), 1);
91 $values = $question->vs->get_values();
92
93 $this->assertEquals('3.1', $question->summarise_response(array('answer' => '3.1')));
94 }
95
96 public function test_classify_response() {
97 $question = test_question_maker::make_question('calculatedsimple');
98 $question->start_attempt(new question_attempt_step(), 1);
99 $values = $question->vs->get_values();
100
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 }
114}