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