MDL-37506 question code: incorrect preg_quote uses.
[moodle.git] / question / type / match / 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 * Unit tests for the matching question definition classes.
19 *
20 * @package qtype
21 * @subpackage match
22 * @copyright 2009 The Open University
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30require_once($CFG->dirroot . '/question/engine/tests/helpers.php');
31
32
33/**
34 * Unit tests for the matching question definition class.
35 *
36 * @copyright 2009 The Open University
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class qtype_match_question_test extends advanced_testcase {
40
41 public function test_get_expected_data() {
42 $question = test_question_maker::make_a_matching_question();
43 $question->start_attempt(new question_attempt_step(), 1);
44
45 $this->assertEquals(array('sub0' => PARAM_INT, 'sub1' => PARAM_INT,
46 'sub2' => PARAM_INT, 'sub3' => PARAM_INT), $question->get_expected_data());
47 }
48
49 public function test_is_complete_response() {
50 $question = test_question_maker::make_a_matching_question();
51 $question->start_attempt(new question_attempt_step(), 1);
52
53 $this->assertFalse($question->is_complete_response(array()));
54 $this->assertFalse($question->is_complete_response(
55 array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '0')));
56 $this->assertFalse($question->is_complete_response(array('sub1' => '1')));
57 $this->assertTrue($question->is_complete_response(
58 array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '1')));
59 }
60
61 public function test_is_gradable_response() {
62 $question = test_question_maker::make_a_matching_question();
63 $question->start_attempt(new question_attempt_step(), 1);
64
65 $this->assertFalse($question->is_gradable_response(array()));
66 $this->assertFalse($question->is_gradable_response(
67 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
68 $this->assertTrue($question->is_gradable_response(
69 array('sub0' => '1', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
70 $this->assertTrue($question->is_gradable_response(array('sub1' => '1')));
71 $this->assertTrue($question->is_gradable_response(
72 array('sub0' => '1', 'sub1' => '1', 'sub2' => '3', 'sub3' => '1')));
73 }
74
75 public function test_is_same_response() {
76 $question = test_question_maker::make_a_matching_question();
77 $question->start_attempt(new question_attempt_step(), 1);
78
79 $this->assertTrue($question->is_same_response(
80 array(),
81 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
82
83 $this->assertTrue($question->is_same_response(
84 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'),
85 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0')));
86
87 $this->assertFalse($question->is_same_response(
88 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'),
89 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
90
91 $this->assertTrue($question->is_same_response(
92 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
93 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
94
95 $this->assertFalse($question->is_same_response(
96 array('sub0' => '2', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'),
97 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1')));
98 }
99
100 public function test_grading() {
101 $question = test_question_maker::make_a_matching_question();
102 $question->shufflestems = false;
103 $question->start_attempt(new question_attempt_step(), 1);
104
105 $choiceorder = $question->get_choice_order();
106 $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
107
108 $this->assertEquals(array(1, question_state::$gradedright),
109 $question->grade_response(array('sub0' => $orderforchoice[1],
110 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2],
111 'sub3' => $orderforchoice[1])));
112 $this->assertEquals(array(0.25, question_state::$gradedpartial),
113 $question->grade_response(array('sub0' => $orderforchoice[1])));
114 $this->assertEquals(array(0, question_state::$gradedwrong),
115 $question->grade_response(array('sub0' => $orderforchoice[2],
116 'sub1' => $orderforchoice[3], 'sub2' => $orderforchoice[1],
117 'sub3' => $orderforchoice[2])));
118 }
119
120 public function test_get_correct_response() {
121 $question = test_question_maker::make_a_matching_question();
122 $question->shufflestems = false;
123 $question->start_attempt(new question_attempt_step(), 1);
124
125 $choiceorder = $question->get_choice_order();
126 $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
127
128 $this->assertEquals(array('sub0' => $orderforchoice[1], 'sub1' => $orderforchoice[2],
129 'sub2' => $orderforchoice[2], 'sub3' => $orderforchoice[1]),
130 $question->get_correct_response());
131 }
132
133 public function test_get_question_summary() {
134 $match = test_question_maker::make_a_matching_question();
135 $match->start_attempt(new question_attempt_step(), 1);
136 $qsummary = $match->get_question_summary();
03cefcc9 137 $this->assertRegExp('/' . preg_quote($match->questiontext, '/') . '/', $qsummary);
603bd001 138 foreach ($match->stems as $stem) {
03cefcc9 139 $this->assertRegExp('/' . preg_quote($stem, '/') . '/', $qsummary);
603bd001
PS
140 }
141 foreach ($match->choices as $choice) {
03cefcc9 142 $this->assertRegExp('/' . preg_quote($choice, '/') . '/', $qsummary);
603bd001
PS
143 }
144 }
145
146 public function test_summarise_response() {
147 $match = test_question_maker::make_a_matching_question();
148 $match->shufflestems = false;
149 $match->start_attempt(new question_attempt_step(), 1);
150
151 $summary = $match->summarise_response(array('sub0' => 2, 'sub1' => 1));
152
153 $this->assertRegExp('/Dog -> \w+; Frog -> \w+/', $summary);
154 }
155
156 public function test_classify_response() {
157 $match = test_question_maker::make_a_matching_question();
158 $match->shufflestems = false;
159 $match->start_attempt(new question_attempt_step(), 1);
160
161 $choiceorder = $match->get_choice_order();
162 $orderforchoice = array_combine(array_values($choiceorder), array_keys($choiceorder));
163 $choices = array(0 => get_string('choose') . '...');
164 foreach ($choiceorder as $key => $choice) {
165 $choices[$key] = $match->choices[$choice];
166 }
167
168 $this->assertEquals(array(
169 1 => new question_classified_response(2, 'Amphibian', 0),
170 2 => new question_classified_response(3, 'Insect', 0),
171 3 => question_classified_response::no_response(),
172 4 => question_classified_response::no_response(),
173 ), $match->classify_response(array('sub0' => $orderforchoice[2],
174 'sub1' => $orderforchoice[3], 'sub2' => 0, 'sub3' => 0)));
175 $this->assertEquals(array(
176 1 => new question_classified_response(1, 'Mammal', 0.25),
177 2 => new question_classified_response(2, 'Amphibian', 0.25),
178 3 => new question_classified_response(2, 'Amphibian', 0.25),
179 4 => new question_classified_response(1, 'Mammal', 0.25),
180 ), $match->classify_response(array('sub0' => $orderforchoice[1],
181 'sub1' => $orderforchoice[2], 'sub2' => $orderforchoice[2],
182 'sub3' => $orderforchoice[1])));
183 }
184}