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_shortanswer; 18 19 use qtype_shortanswer; 20 use qtype_shortanswer_edit_form; 21 use question_possible_response; 22 23 defined('MOODLE_INTERNAL') || die(); 24 25 global $CFG; 26 require_once($CFG->dirroot . '/question/type/shortanswer/questiontype.php'); 27 require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); 28 require_once($CFG->dirroot . '/question/type/edit_question_form.php'); 29 require_once($CFG->dirroot . '/question/type/shortanswer/edit_shortanswer_form.php'); 30 31 /** 32 * Unit tests for the shortanswer question type class. 33 * 34 * @package qtype_shortanswer 35 * @copyright 2007 The Open University 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 * 38 * @covers \question_type 39 * @covers \qtype_shortanswer 40 */ 41 class question_type_test extends \advanced_testcase { 42 protected $qtype; 43 44 protected function setUp(): void { 45 $this->qtype = new qtype_shortanswer(); 46 } 47 48 protected function tearDown(): void { 49 $this->qtype = null; 50 } 51 52 protected function get_test_question_data() { 53 return \test_question_maker::get_question_data('shortanswer'); 54 } 55 56 public function test_name() { 57 $this->assertEquals($this->qtype->name(), 'shortanswer'); 58 } 59 60 public function test_can_analyse_responses() { 61 $this->assertTrue($this->qtype->can_analyse_responses()); 62 } 63 64 public function test_get_random_guess_score() { 65 $q = \test_question_maker::get_question_data('shortanswer'); 66 $q->options->answers[15]->fraction = 0.1; 67 $this->assertEquals(0.1, $this->qtype->get_random_guess_score($q)); 68 } 69 70 public function test_get_possible_responses() { 71 $q = \test_question_maker::get_question_data('shortanswer'); 72 73 $this->assertEquals(array( 74 $q->id => array( 75 13 => new question_possible_response('frog', 1), 76 14 => new question_possible_response('toad', 0.8), 77 15 => new question_possible_response('*', 0), 78 null => question_possible_response::no_response() 79 ), 80 ), $this->qtype->get_possible_responses($q)); 81 } 82 83 public function test_get_possible_responses_no_star() { 84 $q = \test_question_maker::get_question_data('shortanswer', 'frogonly'); 85 86 $this->assertEquals(array( 87 $q->id => array( 88 13 => new question_possible_response('frog', 1), 89 0 => new question_possible_response(get_string('didnotmatchanyanswer', 'question'), 0), 90 null => question_possible_response::no_response() 91 ), 92 ), $this->qtype->get_possible_responses($q)); 93 } 94 95 public function test_question_saving_frogtoad() { 96 $this->resetAfterTest(true); 97 $this->setAdminUser(); 98 99 $questiondata = \test_question_maker::get_question_data('shortanswer'); 100 $formdata = \test_question_maker::get_question_form_data('shortanswer'); 101 102 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 103 $cat = $generator->create_question_category(array()); 104 105 $formdata->category = "{$cat->id},{$cat->contextid}"; 106 qtype_shortanswer_edit_form::mock_submit((array)$formdata); 107 108 $form = \qtype_shortanswer_test_helper::get_question_editing_form($cat, $questiondata); 109 110 $this->assertTrue($form->is_validated()); 111 112 $fromform = $form->get_data(); 113 114 // Create a new question version with the form submission. 115 unset($questiondata->id); 116 $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); 117 $actualquestionsdata = question_load_questions([$returnedfromsave->id], 'qbe.idnumber'); 118 $actualquestiondata = end($actualquestionsdata); 119 120 foreach ($questiondata as $property => $value) { 121 if (!in_array($property, array('id', 'timemodified', 'timecreated', 'options'))) { 122 $this->assertEquals($value, $actualquestiondata->$property); 123 } 124 } 125 126 foreach ($questiondata->options as $optionname => $value) { 127 if ($optionname != 'answers') { 128 $this->assertEquals($value, $actualquestiondata->options->$optionname); 129 } 130 } 131 132 foreach ($questiondata->options->answers as $answer) { 133 $actualanswer = array_shift($actualquestiondata->options->answers); 134 foreach ($answer as $ansproperty => $ansvalue) { 135 // This question does not use 'answerformat', will ignore it. 136 if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) { 137 $this->assertEquals($ansvalue, $actualanswer->$ansproperty); 138 } 139 } 140 } 141 } 142 143 public function test_question_saving_trims_answers() { 144 $this->resetAfterTest(true); 145 $this->setAdminUser(); 146 147 $questiondata = \test_question_maker::get_question_data('shortanswer'); 148 $formdata = \test_question_maker::get_question_form_data('shortanswer'); 149 150 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 151 $cat = $generator->create_question_category(array()); 152 153 $formdata->category = "{$cat->id},{$cat->contextid}"; 154 $formdata->answer[0] = ' frog '; 155 qtype_shortanswer_edit_form::mock_submit((array)$formdata); 156 157 $form = \qtype_shortanswer_test_helper::get_question_editing_form($cat, $questiondata); 158 159 $this->assertTrue($form->is_validated()); 160 161 $fromform = $form->get_data(); 162 163 $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); 164 $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); 165 $actualquestiondata = end($actualquestionsdata); 166 167 $firstsavedanswer = reset($questiondata->options->answers); 168 $this->assertEquals('frog', $firstsavedanswer->answer); 169 } 170 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body