Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403]
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 namespace qtype_multichoice; 18 19 use qtype_multichoice_multi_question; 20 use question_answer; 21 use question_attempt_step; 22 use question_bank; 23 use question_classified_response; 24 use question_state; 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); 30 31 /** 32 * Unit tests for the multiple choice, single response question definition class. 33 * 34 * @package qtype_multichoice 35 * @copyright 2009 The Open University 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 * @covers \qtype_multichoice_single_question 38 */ 39 class question_single_test extends \advanced_testcase { 40 41 public function test_get_expected_data() { 42 $question = \test_question_maker::make_a_multichoice_single_question(); 43 $this->assertEquals(array('answer' => PARAM_INT), $question->get_expected_data()); 44 } 45 46 public function test_is_complete_response() { 47 $question = \test_question_maker::make_a_multichoice_single_question(); 48 49 $this->assertFalse($question->is_complete_response(array())); 50 $this->assertTrue($question->is_complete_response(array('answer' => '0'))); 51 $this->assertTrue($question->is_complete_response(array('answer' => '2'))); 52 $this->assertFalse($question->is_complete_response(array('answer' => '-1'))); 53 $this->assertFalse($question->is_complete_response(array('answer' => -1))); 54 } 55 56 public function test_is_gradable_response() { 57 $question = \test_question_maker::make_a_multichoice_single_question(); 58 59 $this->assertFalse($question->is_gradable_response(array())); 60 $this->assertTrue($question->is_gradable_response(array('answer' => '0'))); 61 $this->assertTrue($question->is_gradable_response(array('answer' => '2'))); 62 $this->assertFalse($question->is_gradable_response(array('answer' => '-1'))); 63 } 64 65 public function test_is_same_response() { 66 $question = \test_question_maker::make_a_multichoice_single_question(); 67 $question->start_attempt(new question_attempt_step(), 1); 68 69 $this->assertTrue($question->is_same_response( 70 array(), 71 array())); 72 73 $this->assertFalse($question->is_same_response( 74 array(), 75 array('answer' => '0'))); 76 77 $this->assertTrue($question->is_same_response( 78 array('answer' => '0'), 79 array('answer' => '0'))); 80 81 $this->assertFalse($question->is_same_response( 82 array('answer' => '0'), 83 array('answer' => '1'))); 84 85 $this->assertTrue($question->is_same_response( 86 array('answer' => '2'), 87 array('answer' => '2'))); 88 89 $this->assertFalse($question->is_same_response( 90 array('answer' => '0'), 91 array('answer' => '-1'))); 92 93 $this->assertFalse($question->is_same_response( 94 array('answer' => '-1'), 95 array('answer' => '0'))); 96 97 $this->assertTrue($question->is_same_response( 98 array('answer' => '-1'), 99 array('answer' => '-1'))); 100 101 $this->assertTrue($question->is_same_response( 102 array(), 103 array('answer' => '-1'))); 104 } 105 106 public function test_grading() { 107 $question = \test_question_maker::make_a_multichoice_single_question(); 108 $question->start_attempt(new question_attempt_step(), 1); 109 110 $this->assertEquals(array(1, question_state::$gradedright), 111 $question->grade_response($question->prepare_simulated_post_data(array('answer' => 'A')))); 112 $this->assertEquals(array(-0.3333333, question_state::$gradedwrong), 113 $question->grade_response($question->prepare_simulated_post_data(array('answer' => 'B')))); 114 $this->assertEquals(array(-0.3333333, question_state::$gradedwrong), 115 $question->grade_response($question->prepare_simulated_post_data(array('answer' => 'C')))); 116 } 117 118 public function test_grading_rounding_three_right() { 119 question_bank::load_question_definition_classes('multichoice'); 120 $mc = new qtype_multichoice_multi_question(); 121 \test_question_maker::initialise_a_question($mc); 122 $mc->name = 'Odd numbers'; 123 $mc->questiontext = 'Which are the odd numbers?'; 124 $mc->generalfeedback = '1, 3 and 5 are the odd numbers.'; 125 $mc->qtype = question_bank::get_qtype('multichoice'); 126 127 $mc->answernumbering = 'abc'; 128 $mc->showstandardinstruction = 0; 129 130 \test_question_maker::set_standard_combined_feedback_fields($mc); 131 132 $mc->answers = array( 133 11 => new question_answer(11, '1', 0.3333333, '', FORMAT_HTML), 134 12 => new question_answer(12, '2', -1, '', FORMAT_HTML), 135 13 => new question_answer(13, '3', 0.3333333, '', FORMAT_HTML), 136 14 => new question_answer(14, '4', -1, '', FORMAT_HTML), 137 15 => new question_answer(15, '5', 0.3333333, '', FORMAT_HTML), 138 16 => new question_answer(16, '6', -1, '', FORMAT_HTML), 139 ); 140 141 $mc->start_attempt(new question_attempt_step(), 1); 142 143 list($grade, $state) = $mc->grade_response($mc->prepare_simulated_post_data(array('1' => '1', '3' => '1', '5' => '1'))); 144 $this->assertEqualsWithDelta(1, $grade, 0.000001); 145 $this->assertEquals(question_state::$gradedright, $state); 146 } 147 148 public function test_get_correct_response() { 149 $question = \test_question_maker::make_a_multichoice_single_question(); 150 $question->start_attempt(new question_attempt_step(), 1); 151 152 $this->assertEquals($question->prepare_simulated_post_data(array('answer' => 'A')), $question->get_correct_response()); 153 } 154 155 public function test_summarise_response() { 156 $mc = \test_question_maker::make_a_multichoice_single_question(); 157 $mc->start_attempt(new question_attempt_step(), 1); 158 159 $summary = $mc->summarise_response($mc->prepare_simulated_post_data(array('answer' => 'A')), 160 \test_question_maker::get_a_qa($mc)); 161 162 $this->assertEquals('A', $summary); 163 164 $this->assertNull($mc->summarise_response([])); 165 $this->assertNull($mc->summarise_response(['answer' => '-1'])); 166 } 167 168 public function test_un_summarise_response() { 169 $mc = \test_question_maker::make_a_multichoice_single_question(); 170 $mc->shuffleanswers = false; 171 $mc->start_attempt(new question_attempt_step(), 1); 172 173 $this->assertEquals(['answer' => '1'], $mc->un_summarise_response('B')); 174 175 $this->assertEquals([], $mc->un_summarise_response('')); 176 } 177 178 public function test_classify_response() { 179 $mc = \test_question_maker::make_a_multichoice_single_question(); 180 $mc->start_attempt(new question_attempt_step(), 1); 181 182 $this->assertEquals(array($mc->id => new question_classified_response(14, 'B', -0.3333333)), 183 $mc->classify_response($mc->prepare_simulated_post_data(array('answer' => 'B')))); 184 185 $this->assertEquals(array( 186 $mc->id => question_classified_response::no_response(), 187 ), $mc->classify_response(array())); 188 189 $this->assertEquals(array( 190 $mc->id => question_classified_response::no_response(), 191 ), $mc->classify_response(array('answer' => '-1'))); 192 } 193 194 public function test_make_html_inline() { 195 $mc = \test_question_maker::make_a_multichoice_single_question(); 196 $this->assertEquals('Frog', $mc->make_html_inline('<p>Frog</p>')); 197 $this->assertEquals('Frog<br />Toad', $mc->make_html_inline("<p>Frog</p>\n<p>Toad</p>")); 198 $this->assertEquals('<img src="http://example.com/pic.png" alt="Graph" />', 199 $mc->make_html_inline( 200 '<p><img src="http://example.com/pic.png" alt="Graph" /></p>')); 201 $this->assertEquals("Frog<br />XXX <img src='http://example.com/pic.png' alt='Graph' />", 202 $mc->make_html_inline(" <p> Frog </p> \n\r 203 <p> XXX <img src='http://example.com/pic.png' alt='Graph' /> </p> ")); 204 $this->assertEquals('Frog', $mc->make_html_inline('<p>Frog</p><p></p>')); 205 $this->assertEquals('Frog<br />†', $mc->make_html_inline('<p>Frog</p><p>†</p>')); 206 } 207 208 public function test_simulated_post_data() { 209 $mc = \test_question_maker::make_a_multichoice_single_question(); 210 $mc->shuffleanswers = false; 211 $mc->answers[13]->answer = '<p>A</p>'; 212 $mc->answers[14]->answer = '<p>B</p>'; 213 $mc->answers[15]->answer = '<p>C</p>'; 214 $mc->start_attempt(new question_attempt_step(), 1); 215 216 $originalresponse = array('answer' => 1); 217 218 $simulated = $mc->get_student_response_values_for_simulation($originalresponse); 219 $this->assertEquals(array('answer' => 'B'), $simulated); 220 221 $reconstucted = $mc->prepare_simulated_post_data($simulated); 222 $this->assertEquals($originalresponse, $reconstucted); 223 } 224 225 public function test_validate_can_regrade_with_other_version_bad() { 226 $mc = \test_question_maker::make_a_multichoice_single_question(); 227 228 $newmc = clone($mc); 229 $newmc->answers = array( 230 23 => new question_answer(13, 'A', 1, 'A is right', FORMAT_HTML), 231 24 => new question_answer(14, 'B', -0.3333333, 'B is wrong', FORMAT_HTML), 232 ); 233 234 $this->assertEquals(get_string('regradeissuenumchoiceschanged', 'qtype_multichoice'), 235 $newmc->validate_can_regrade_with_other_version($mc)); 236 } 237 238 public function test_validate_can_regrade_with_other_version_ok() { 239 $mc = \test_question_maker::make_a_multichoice_single_question(); 240 241 $newmc = clone($mc); 242 $newmc->answers = array( 243 23 => new question_answer(13, 'A', 1, 'A is right', FORMAT_HTML), 244 24 => new question_answer(14, 'B', -0.3333333, 'B is wrong', FORMAT_HTML), 245 25 => new question_answer(15, 'C', -0.3333333, 'C is wrong', FORMAT_HTML), 246 ); 247 248 $this->assertNull($newmc->validate_can_regrade_with_other_version($mc)); 249 } 250 251 public function test_update_attempt_state_date_from_old_version_bad() { 252 $mc = \test_question_maker::make_a_multichoice_single_question(); 253 254 $newmc = clone($mc); 255 $newmc->answers = array( 256 23 => new question_answer(13, 'A', 1, 'A is right', FORMAT_HTML), 257 24 => new question_answer(14, 'B', -0.3333333, 'B is wrong', FORMAT_HTML), 258 ); 259 260 $oldstep = new question_attempt_step(); 261 $oldstep->set_qt_var('_order', '14,13,15'); 262 $this->expectExceptionMessage(get_string('regradeissuenumchoiceschanged', 'qtype_multichoice')); 263 $newmc->update_attempt_state_data_for_new_version($oldstep, $mc); 264 } 265 266 public function test_update_attempt_state_date_from_old_version_ok() { 267 $mc = \test_question_maker::make_a_multichoice_single_question(); 268 269 $newmc = clone($mc); 270 $newmc->answers = array( 271 23 => new question_answer(13, 'A', 1, 'A is right', FORMAT_HTML), 272 24 => new question_answer(14, 'B', -0.3333333, 'B is wrong', FORMAT_HTML), 273 25 => new question_answer(15, 'C', -0.3333333, 'C is wrong', FORMAT_HTML), 274 ); 275 276 $oldstep = new question_attempt_step(); 277 $oldstep->set_qt_var('_order', '14,13,15'); 278 $this->assertEquals(['_order' => '24,23,25'], 279 $newmc->update_attempt_state_data_for_new_version($oldstep, $mc)); 280 } 281 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body