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_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 $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); 115 $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); 116 $actualquestiondata = end($actualquestionsdata); 117 118 foreach ($questiondata as $property => $value) { 119 if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options'))) { 120 $this->assertEquals($value, $actualquestiondata->$property); 121 } 122 } 123 124 foreach ($questiondata->options as $optionname => $value) { 125 if ($optionname != 'answers') { 126 $this->assertEquals($value, $actualquestiondata->options->$optionname); 127 } 128 } 129 130 foreach ($questiondata->options->answers as $answer) { 131 $actualanswer = array_shift($actualquestiondata->options->answers); 132 foreach ($answer as $ansproperty => $ansvalue) { 133 // This question does not use 'answerformat', will ignore it. 134 if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) { 135 $this->assertEquals($ansvalue, $actualanswer->$ansproperty); 136 } 137 } 138 } 139 } 140 141 public function test_question_saving_trims_answers() { 142 $this->resetAfterTest(true); 143 $this->setAdminUser(); 144 145 $questiondata = \test_question_maker::get_question_data('shortanswer'); 146 $formdata = \test_question_maker::get_question_form_data('shortanswer'); 147 148 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 149 $cat = $generator->create_question_category(array()); 150 151 $formdata->category = "{$cat->id},{$cat->contextid}"; 152 $formdata->answer[0] = ' frog '; 153 qtype_shortanswer_edit_form::mock_submit((array)$formdata); 154 155 $form = \qtype_shortanswer_test_helper::get_question_editing_form($cat, $questiondata); 156 157 $this->assertTrue($form->is_validated()); 158 159 $fromform = $form->get_data(); 160 161 $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); 162 $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); 163 $actualquestiondata = end($actualquestionsdata); 164 165 $firstsavedanswer = reset($questiondata->options->answers); 166 $this->assertEquals('frog', $firstsavedanswer->answer); 167 } 168 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body