See Release Notes
Long Term Support Release
Differences Between: [Versions 311 and 401]
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->status = \core_question\local\bank\question_version_status::QUESTION_STATUS_READY; 69 $q->version = 1; 70 $q->versionid = 0; 71 $q->questionbankentryid = 0; 72 $q->idnumber = null; 73 $q->timecreated = time(); 74 $q->timemodified = time(); 75 $q->createdby = $USER->id; 76 $q->modifiedby = $USER->id; 77 78 $q->options = new \stdClass(); 79 $q->options->shuffleanswers = false; 80 \test_question_maker::set_standard_combined_feedback_fields($q->options); 81 82 $q->options->subquestions = array( 83 14 => (object) array( 84 'id' => 14, 85 'questiontext' => 'frog', 86 'questiontextformat' => FORMAT_HTML, 87 'answertext' => 'amphibian'), 88 15 => (object) array( 89 'id' => 15, 90 'questiontext' => 'cat', 91 'questiontextformat' => FORMAT_HTML, 92 'answertext' => 'mammal'), 93 16 => (object) array( 94 'id' => 16, 95 'questiontext' => 'newt', 96 'questiontextformat' => FORMAT_HTML, 97 'answertext' => 'amphibian'), 98 17 => (object) array( 99 'id' => 17, 100 'questiontext' => '', 101 'questiontextformat' => FORMAT_HTML, 102 'answertext' => 'insect'), 103 ); 104 105 return $q; 106 } 107 108 public function test_name() { 109 $this->assertEquals($this->qtype->name(), 'match'); 110 } 111 112 public function test_can_analyse_responses() { 113 $this->assertTrue($this->qtype->can_analyse_responses()); 114 } 115 116 public function test_make_question_instance() { 117 $questiondata = \test_question_maker::get_question_data('match', 'trickynums'); 118 $question = question_bank::make_question($questiondata); 119 $this->assertEquals($questiondata->name, $question->name); 120 $this->assertEquals($questiondata->questiontext, $question->questiontext); 121 $this->assertEquals($questiondata->questiontextformat, $question->questiontextformat); 122 $this->assertEquals($questiondata->generalfeedback, $question->generalfeedback); 123 $this->assertEquals($questiondata->generalfeedbackformat, $question->generalfeedbackformat); 124 $this->assertInstanceOf('qtype_match', $question->qtype); 125 $this->assertEquals($questiondata->options->shuffleanswers, $question->shufflestems); 126 127 $this->assertEquals( 128 [14 => 'System.out.println(0);', 15 => 'System.out.println(0.0);'], 129 $question->stems); 130 131 $this->assertEquals([14 => '0', 15 => '0.0', 16 => 'NULL'], $question->choices); 132 133 $this->assertEquals([14 => 14, 15 => 15], $question->right); 134 } 135 136 public function test_get_random_guess_score() { 137 $q = $this->get_test_question_data(); 138 $this->assertEqualsWithDelta(0.3333333, $this->qtype->get_random_guess_score($q), 0.0000001); 139 } 140 141 public function test_get_possible_responses() { 142 $q = $this->get_test_question_data(); 143 144 $this->assertEquals(array( 145 14 => array( 146 14 => new question_possible_response('frog: amphibian', 1/3), 147 15 => new question_possible_response('frog: mammal', 0), 148 17 => new question_possible_response('frog: insect', 0), 149 null => question_possible_response::no_response()), 150 15 => array( 151 14 => new question_possible_response('cat: amphibian', 0), 152 15 => new question_possible_response('cat: mammal', 1/3), 153 17 => new question_possible_response('cat: insect', 0), 154 null => question_possible_response::no_response()), 155 16 => array( 156 14 => new question_possible_response('newt: amphibian', 1/3), 157 15 => new question_possible_response('newt: mammal', 0), 158 17 => new question_possible_response('newt: insect', 0), 159 null => question_possible_response::no_response()), 160 ), $this->qtype->get_possible_responses($q)); 161 } 162 163 164 public function test_question_saving_foursubq() { 165 $this->resetAfterTest(true); 166 $this->setAdminUser(); 167 168 $questiondata = \test_question_maker::get_question_data('match'); 169 $formdata = \test_question_maker::get_question_form_data('match'); 170 171 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 172 $cat = $generator->create_question_category(array()); 173 174 $formdata->category = "{$cat->id},{$cat->contextid}"; 175 176 qtype_match_edit_form::mock_submit((array)$formdata); 177 178 $form = \qtype_match_test_helper::get_question_editing_form($cat, $questiondata); 179 $this->assertTrue($form->is_validated()); 180 181 $fromform = $form->get_data(); 182 183 // Create a new question version with the form submission. 184 unset($questiondata->id); 185 $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); 186 $actualquestionsdata = question_load_questions([$returnedfromsave->id], 'qbe.idnumber'); 187 $actualquestiondata = end($actualquestionsdata); 188 189 foreach ($questiondata as $property => $value) { 190 if (!in_array($property, ['id', 'timemodified', 'timecreated', 'options', 'stamp', 191 'versionid', 'questionbankentryid'])) { 192 if (!empty($actualquestiondata)) { 193 $this->assertEquals($value, $actualquestiondata->$property); 194 } 195 } 196 } 197 198 foreach ($questiondata->options as $optionname => $value) { 199 if ($optionname != 'subquestions') { 200 $this->assertEquals($value, $actualquestiondata->options->$optionname); 201 } 202 } 203 204 $this->assertObjectHasAttribute('subquestions', $actualquestiondata->options); 205 206 $subqpropstoignore = array('id'); 207 foreach ($questiondata->options->subquestions as $subq) { 208 $actualsubq = array_shift($actualquestiondata->options->subquestions); 209 foreach ($subq as $subqproperty => $subqvalue) { 210 if (!in_array($subqproperty, $subqpropstoignore)) { 211 $this->assertEquals($subqvalue, $actualsubq->$subqproperty); 212 } 213 } 214 } 215 } 216 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body