Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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_match; 18 19 use qtype_match; 20 use qtype_match_edit_form; 21 use question_bank; 22 use question_possible_response; 23 24 defined('MOODLE_INTERNAL') || die(); 25 26 global $CFG; 27 require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); 28 require_once($CFG->dirroot . '/question/type/match/questiontype.php'); 29 require_once($CFG->dirroot . '/question/type/edit_question_form.php'); 30 require_once($CFG->dirroot . '/question/type/match/edit_match_form.php'); 31 32 33 /** 34 * Unit tests for the matching question definition class. 35 * 36 * @package qtype_match 37 * @copyright 2009 The Open University 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class question_type_test extends \advanced_testcase { 41 /** @var qtype_match instance of the question type class to test. */ 42 protected $qtype; 43 44 protected function setUp(): void { 45 $this->qtype = new qtype_match(); 46 } 47 48 protected function tearDown(): void { 49 $this->qtype = null; 50 } 51 52 protected function get_test_question_data() { 53 global $USER; 54 $q = new \stdClass(); 55 $q->id = 0; 56 $q->name = 'Matching question'; 57 $q->category = 0; 58 $q->contextid = 0; 59 $q->parent = 0; 60 $q->questiontext = 'Classify the animals.'; 61 $q->questiontextformat = FORMAT_HTML; 62 $q->generalfeedback = 'General feedback.'; 63 $q->generalfeedbackformat = FORMAT_HTML; 64 $q->defaultmark = 1; 65 $q->penalty = 0.3333333; 66 $q->length = 1; 67 $q->stamp = make_unique_id_code(); 68 $q->version = make_unique_id_code(); 69 $q->hidden = 0; 70 $q->idnumber = null; 71 $q->timecreated = time(); 72 $q->timemodified = time(); 73 $q->createdby = $USER->id; 74 $q->modifiedby = $USER->id; 75 76 $q->options = new \stdClass(); 77 $q->options->shuffleanswers = false; 78 \test_question_maker::set_standard_combined_feedback_fields($q->options); 79 80 $q->options->subquestions = array( 81 14 => (object) array( 82 'id' => 14, 83 'questiontext' => 'frog', 84 'questiontextformat' => FORMAT_HTML, 85 'answertext' => 'amphibian'), 86 15 => (object) array( 87 'id' => 15, 88 'questiontext' => 'cat', 89 'questiontextformat' => FORMAT_HTML, 90 'answertext' => 'mammal'), 91 16 => (object) array( 92 'id' => 16, 93 'questiontext' => 'newt', 94 'questiontextformat' => FORMAT_HTML, 95 'answertext' => 'amphibian'), 96 17 => (object) array( 97 'id' => 17, 98 'questiontext' => '', 99 'questiontextformat' => FORMAT_HTML, 100 'answertext' => 'insect'), 101 ); 102 103 return $q; 104 } 105 106 public function test_name() { 107 $this->assertEquals($this->qtype->name(), 'match'); 108 } 109 110 public function test_can_analyse_responses() { 111 $this->assertTrue($this->qtype->can_analyse_responses()); 112 } 113 114 public function test_make_question_instance() { 115 $questiondata = \test_question_maker::get_question_data('match', 'trickynums'); 116 $question = question_bank::make_question($questiondata); 117 $this->assertEquals($questiondata->name, $question->name); 118 $this->assertEquals($questiondata->questiontext, $question->questiontext); 119 $this->assertEquals($questiondata->questiontextformat, $question->questiontextformat); 120 $this->assertEquals($questiondata->generalfeedback, $question->generalfeedback); 121 $this->assertEquals($questiondata->generalfeedbackformat, $question->generalfeedbackformat); 122 $this->assertInstanceOf('qtype_match', $question->qtype); 123 $this->assertEquals($questiondata->options->shuffleanswers, $question->shufflestems); 124 125 $this->assertEquals( 126 [14 => 'System.out.println(0);', 15 => 'System.out.println(0.0);'], 127 $question->stems); 128 129 $this->assertEquals([14 => '0', 15 => '0.0', 16 => 'NULL'], $question->choices); 130 131 $this->assertEquals([14 => 14, 15 => 15], $question->right); 132 } 133 134 public function test_get_random_guess_score() { 135 $q = $this->get_test_question_data(); 136 $this->assertEqualsWithDelta(0.3333333, $this->qtype->get_random_guess_score($q), 0.0000001); 137 } 138 139 public function test_get_possible_responses() { 140 $q = $this->get_test_question_data(); 141 142 $this->assertEquals(array( 143 14 => array( 144 14 => new question_possible_response('frog: amphibian', 1/3), 145 15 => new question_possible_response('frog: mammal', 0), 146 17 => new question_possible_response('frog: insect', 0), 147 null => question_possible_response::no_response()), 148 15 => array( 149 14 => new question_possible_response('cat: amphibian', 0), 150 15 => new question_possible_response('cat: mammal', 1/3), 151 17 => new question_possible_response('cat: insect', 0), 152 null => question_possible_response::no_response()), 153 16 => array( 154 14 => new question_possible_response('newt: amphibian', 1/3), 155 15 => new question_possible_response('newt: mammal', 0), 156 17 => new question_possible_response('newt: insect', 0), 157 null => question_possible_response::no_response()), 158 ), $this->qtype->get_possible_responses($q)); 159 } 160 161 162 public function test_question_saving_foursubq() { 163 $this->resetAfterTest(true); 164 $this->setAdminUser(); 165 166 $questiondata = \test_question_maker::get_question_data('match'); 167 $formdata = \test_question_maker::get_question_form_data('match'); 168 169 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 170 $cat = $generator->create_question_category(array()); 171 172 $formdata->category = "{$cat->id},{$cat->contextid}"; 173 174 qtype_match_edit_form::mock_submit((array)$formdata); 175 176 $form = \qtype_match_test_helper::get_question_editing_form($cat, $questiondata); 177 $this->assertTrue($form->is_validated()); 178 179 $fromform = $form->get_data(); 180 181 $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); 182 $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); 183 $actualquestiondata = end($actualquestionsdata); 184 185 foreach ($questiondata as $property => $value) { 186 if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options', 'stamp'))) { 187 $this->assertEquals($value, $actualquestiondata->$property); 188 } 189 } 190 191 foreach ($questiondata->options as $optionname => $value) { 192 if ($optionname != 'subquestions') { 193 $this->assertEquals($value, $actualquestiondata->options->$optionname); 194 } 195 } 196 197 $this->assertObjectHasAttribute('subquestions', $actualquestiondata->options); 198 199 $subqpropstoignore = array('id'); 200 foreach ($questiondata->options->subquestions as $subq) { 201 $actualsubq = array_shift($actualquestiondata->options->subquestions); 202 foreach ($subq as $subqproperty => $subqvalue) { 203 if (!in_array($subqproperty, $subqpropstoignore)) { 204 $this->assertEquals($subqvalue, $actualsubq->$subqproperty); 205 } 206 } 207 } 208 } 209 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body