Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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_match 21 * @copyright 2009 The Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); 30 31 32 /** 33 * Unit tests for the matching question definition class. 34 * 35 * @copyright 2009 The Open University 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class qtype_match_question_test extends advanced_testcase { 39 40 public function test_get_expected_data() { 41 $question = test_question_maker::make_question('match'); 42 $question->start_attempt(new question_attempt_step(), 1); 43 44 $this->assertEquals(array('sub0' => PARAM_INT, 'sub1' => PARAM_INT, 45 'sub2' => PARAM_INT, 'sub3' => PARAM_INT), $question->get_expected_data()); 46 } 47 48 public function test_is_complete_response() { 49 $question = test_question_maker::make_question('match'); 50 $question->start_attempt(new question_attempt_step(), 1); 51 52 $this->assertFalse($question->is_complete_response(array())); 53 $this->assertFalse($question->is_complete_response( 54 array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '0'))); 55 $this->assertFalse($question->is_complete_response(array('sub1' => '1'))); 56 $this->assertTrue($question->is_complete_response( 57 array('sub0' => '1', 'sub1' => '1', 'sub2' => '1', 'sub3' => '1'))); 58 } 59 60 public function test_is_gradable_response() { 61 $question = test_question_maker::make_question('match'); 62 $question->start_attempt(new question_attempt_step(), 1); 63 64 $this->assertFalse($question->is_gradable_response(array())); 65 $this->assertFalse($question->is_gradable_response( 66 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'))); 67 $this->assertTrue($question->is_gradable_response( 68 array('sub0' => '1', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'))); 69 $this->assertTrue($question->is_gradable_response(array('sub1' => '1'))); 70 $this->assertTrue($question->is_gradable_response( 71 array('sub0' => '1', 'sub1' => '1', 'sub2' => '3', 'sub3' => '1'))); 72 } 73 74 public function test_is_same_response() { 75 $question = test_question_maker::make_question('match'); 76 $question->start_attempt(new question_attempt_step(), 1); 77 78 $this->assertTrue($question->is_same_response( 79 array(), 80 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'))); 81 82 $this->assertTrue($question->is_same_response( 83 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'), 84 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'))); 85 86 $this->assertFalse($question->is_same_response( 87 array('sub0' => '0', 'sub1' => '0', 'sub2' => '0', 'sub3' => '0'), 88 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'))); 89 90 $this->assertTrue($question->is_same_response( 91 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'), 92 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'))); 93 94 $this->assertFalse($question->is_same_response( 95 array('sub0' => '2', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'), 96 array('sub0' => '1', 'sub1' => '2', 'sub2' => '3', 'sub3' => '1'))); 97 } 98 99 public function test_grading() { 100 $question = test_question_maker::make_question('match'); 101 $question->start_attempt(new question_attempt_step(), 1); 102 103 $correctresponse = $question->prepare_simulated_post_data( 104 array('Dog' => 'Mammal', 105 'Frog' => 'Amphibian', 106 'Toad' => 'Amphibian', 107 'Cat' => 'Mammal')); 108 $this->assertEquals(array(1, question_state::$gradedright), $question->grade_response($correctresponse)); 109 110 $partialresponse = $question->prepare_simulated_post_data(array('Dog' => 'Mammal')); 111 $this->assertEquals(array(0.25, question_state::$gradedpartial), $question->grade_response($partialresponse)); 112 113 $partiallycorrectresponse = $question->prepare_simulated_post_data( 114 array('Dog' => 'Mammal', 115 'Frog' => 'Insect', 116 'Toad' => 'Insect', 117 'Cat' => 'Amphibian')); 118 $this->assertEquals(array(0.25, question_state::$gradedpartial), $question->grade_response($partiallycorrectresponse)); 119 120 $wrongresponse = $question->prepare_simulated_post_data( 121 array('Dog' => 'Amphibian', 122 'Frog' => 'Insect', 123 'Toad' => 'Insect', 124 'Cat' => 'Amphibian')); 125 $this->assertEquals(array(0, question_state::$gradedwrong), $question->grade_response($wrongresponse)); 126 } 127 128 public function test_get_correct_response() { 129 $question = test_question_maker::make_question('match'); 130 $question->start_attempt(new question_attempt_step(), 1); 131 132 $correct = $question->prepare_simulated_post_data(array('Dog' => 'Mammal', 133 'Frog' => 'Amphibian', 134 'Toad' => 'Amphibian', 135 'Cat' => 'Mammal')); 136 $this->assertEquals($correct, $question->get_correct_response()); 137 } 138 139 public function test_get_question_summary() { 140 $match = test_question_maker::make_question('match'); 141 $match->start_attempt(new question_attempt_step(), 1); 142 $qsummary = $match->get_question_summary(); 143 $this->assertRegExp('/' . preg_quote($match->questiontext, '/') . '/', $qsummary); 144 foreach ($match->stems as $stem) { 145 $this->assertRegExp('/' . preg_quote($stem, '/') . '/', $qsummary); 146 } 147 foreach ($match->choices as $choice) { 148 $this->assertRegExp('/' . preg_quote($choice, '/') . '/', $qsummary); 149 } 150 } 151 152 public function test_summarise_response() { 153 $match = test_question_maker::make_question('match'); 154 $match->start_attempt(new question_attempt_step(), 1); 155 156 $summary = $match->summarise_response($match->prepare_simulated_post_data(array('Dog' => 'Amphibian', 'Frog' => 'Mammal'))); 157 158 $this->assertRegExp('/Dog -> Amphibian/', $summary); 159 $this->assertRegExp('/Frog -> Mammal/', $summary); 160 } 161 162 public function test_classify_response() { 163 $match = test_question_maker::make_question('match'); 164 $match->start_attempt(new question_attempt_step(), 1); 165 166 $response = $match->prepare_simulated_post_data(array('Dog' => 'Amphibian', 'Frog' => 'Insect', 'Toad' => '', 'Cat' => '')); 167 $this->assertEquals(array( 168 1 => new question_classified_response(2, 'Amphibian', 0), 169 2 => new question_classified_response(3, 'Insect', 0), 170 3 => question_classified_response::no_response(), 171 4 => question_classified_response::no_response(), 172 ), $match->classify_response($response)); 173 174 $response = $match->prepare_simulated_post_data(array('Dog' => 'Mammal', 'Frog' => 'Amphibian', 175 'Toad' => 'Amphibian', 'Cat' => 'Mammal')); 176 $this->assertEquals(array( 177 1 => new question_classified_response(1, 'Mammal', 0.25), 178 2 => new question_classified_response(2, 'Amphibian', 0.25), 179 3 => new question_classified_response(2, 'Amphibian', 0.25), 180 4 => new question_classified_response(1, 'Mammal', 0.25), 181 ), $match->classify_response($response)); 182 } 183 184 public function test_classify_response_choice_deleted_after_attempt() { 185 $match = test_question_maker::make_question('match'); 186 $firststep = new question_attempt_step(); 187 188 $match->start_attempt($firststep, 1); 189 $response = $match->prepare_simulated_post_data(array( 190 'Dog' => 'Amphibian', 'Frog' => 'Insect', 'Toad' => '', 'Cat' => 'Mammal')); 191 192 $match = test_question_maker::make_question('match'); 193 unset($match->stems[4]); 194 unset($match->stemsformat[4]); 195 unset($match->right[4]); 196 $match->apply_attempt_state($firststep); 197 198 $this->assertEquals(array( 199 1 => new question_classified_response(2, 'Amphibian', 0), 200 2 => new question_classified_response(3, 'Insect', 0), 201 3 => question_classified_response::no_response(), 202 ), $match->classify_response($response)); 203 } 204 205 public function test_classify_response_choice_added_after_attempt() { 206 $match = test_question_maker::make_question('match'); 207 $firststep = new question_attempt_step(); 208 209 $match->start_attempt($firststep, 1); 210 $response = $match->prepare_simulated_post_data(array( 211 'Dog' => 'Amphibian', 'Frog' => 'Insect', 'Toad' => '', 'Cat' => 'Mammal')); 212 213 $match = test_question_maker::make_question('match'); 214 $match->stems[5] = "Snake"; 215 $match->stemsformat[5] = FORMAT_HTML; 216 $match->choices[5] = "Reptile"; 217 $match->right[5] = 5; 218 $match->apply_attempt_state($firststep); 219 220 $this->assertEquals(array( 221 1 => new question_classified_response(2, 'Amphibian', 0), 222 2 => new question_classified_response(3, 'Insect', 0), 223 3 => question_classified_response::no_response(), 224 4 => new question_classified_response(1, 'Mammal', 0.20), 225 ), $match->classify_response($response)); 226 } 227 228 public function test_prepare_simulated_post_data() { 229 $m = test_question_maker::make_question('match'); 230 $m->start_attempt(new question_attempt_step(), 1); 231 $postdata = $m->prepare_simulated_post_data(array('Dog' => 'Mammal', 'Frog' => 'Amphibian', 232 'Toad' => 'Amphibian', 'Cat' => 'Mammal')); 233 $this->assertEquals(array(4, 4), $m->get_num_parts_right($postdata)); 234 } 235 236 /** 237 * test_get_question_definition_for_external_rendering 238 */ 239 public function test_get_question_definition_for_external_rendering() { 240 $question = test_question_maker::make_question('match'); 241 $question->start_attempt(new question_attempt_step(), 1); 242 $qa = test_question_maker::get_a_qa($question); 243 $displayoptions = new question_display_options(); 244 245 $options = $question->get_question_definition_for_external_rendering($qa, $displayoptions); 246 $this->assertEquals(1, $options['shufflestems']); 247 } 248 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body