[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
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 multianswer question definition class. 19 * 20 * @package qtype 21 * @subpackage multianswer 22 * @copyright 2011 The Open University 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 27 defined('MOODLE_INTERNAL') || die(); 28 29 global $CFG; 30 require_once($CFG->dirroot . '/question/engine/tests/helpers.php'); 31 require_once($CFG->dirroot . '/question/type/multianswer/questiontype.php'); 32 require_once($CFG->dirroot . '/question/type/edit_question_form.php'); 33 require_once($CFG->dirroot . '/question/type/multianswer/edit_multianswer_form.php'); 34 35 36 /** 37 * Unit tests for the multianswer question definition class. 38 * 39 * @copyright 2011 The Open University 40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 41 */ 42 class qtype_multianswer_test extends advanced_testcase { 43 /** @var qtype_multianswer instance of the question type class to test. */ 44 protected $qtype; 45 46 protected function setUp() { 47 $this->qtype = new qtype_multianswer(); 48 } 49 50 protected function tearDown() { 51 $this->qtype = null; 52 } 53 54 protected function get_test_question_data() { 55 global $USER; 56 $q = new stdClass(); 57 $q->id = 0; 58 $q->name = 'Simple multianswer'; 59 $q->category = 0; 60 $q->contextid = 0; 61 $q->parent = 0; 62 $q->questiontext = 63 'Complete this opening line of verse: "The {#1} and the {#2} went to sea".'; 64 $q->questiontextformat = FORMAT_HTML; 65 $q->generalfeedback = 'Generalfeedback: It\'s from "The Owl and the Pussy-cat" by Lear: ' . 66 '"The owl and the pussycat went to see'; 67 $q->generalfeedbackformat = FORMAT_HTML; 68 $q->defaultmark = 2; 69 $q->penalty = 0.3333333; 70 $q->length = 1; 71 $q->stamp = make_unique_id_code(); 72 $q->version = make_unique_id_code(); 73 $q->hidden = 0; 74 $q->timecreated = time(); 75 $q->timemodified = time(); 76 $q->createdby = $USER->id; 77 $q->modifiedby = $USER->id; 78 79 $sadata = new stdClass(); 80 $sadata->id = 1; 81 $sadata->qtype = 'shortanswer'; 82 $sadata->defaultmark = 1; 83 $sadata->options->usecase = true; 84 $sadata->options->answers[1] = (object) array('answer' => 'Bow-wow', 'fraction' => 0); 85 $sadata->options->answers[2] = (object) array('answer' => 'Wiggly worm', 'fraction' => 0); 86 $sadata->options->answers[3] = (object) array('answer' => 'Pussy-cat', 'fraction' => 1); 87 88 $mcdata = new stdClass(); 89 $mcdata->id = 1; 90 $mcdata->qtype = 'multichoice'; 91 $mcdata->defaultmark = 1; 92 $mcdata->options->single = true; 93 $mcdata->options->answers[1] = (object) array('answer' => 'Dog', 'fraction' => 0); 94 $mcdata->options->answers[2] = (object) array('answer' => 'Owl', 'fraction' => 1); 95 $mcdata->options->answers[3] = (object) array('answer' => '*', 'fraction' => 0); 96 97 $q->options->questions = array( 98 1 => $sadata, 99 2 => $mcdata, 100 ); 101 102 return $q; 103 } 104 105 public function test_name() { 106 $this->assertEquals($this->qtype->name(), 'multianswer'); 107 } 108 109 public function test_can_analyse_responses() { 110 $this->assertFalse($this->qtype->can_analyse_responses()); 111 } 112 113 public function test_get_random_guess_score() { 114 $q = test_question_maker::get_question_data('multianswer', 'twosubq'); 115 $this->assertEquals(0.1666667, $this->qtype->get_random_guess_score($q), '', 0.0000001); 116 } 117 118 public function test_question_saving_twosubq() { 119 $this->resetAfterTest(true); 120 $this->setAdminUser(); 121 122 $questiondata = test_question_maker::get_question_data('multianswer'); 123 $formdata = test_question_maker::get_question_form_data('multianswer'); 124 125 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 126 $cat = $generator->create_question_category(array()); 127 128 $formdata->category = "{$cat->id},{$cat->contextid}"; 129 qtype_multianswer_edit_form::mock_submit((array)$formdata); 130 131 $form = qtype_multianswer_test_helper::get_question_editing_form($cat, $questiondata); 132 133 $this->assertTrue($form->is_validated()); 134 135 $fromform = $form->get_data(); 136 137 $returnedfromsave = $this->qtype->save_question($questiondata, $fromform); 138 $actualquestionsdata = question_load_questions(array($returnedfromsave->id)); 139 $actualquestiondata = end($actualquestionsdata); 140 141 foreach ($questiondata as $property => $value) { 142 if (!in_array($property, array('id', 'version', 'timemodified', 'timecreated', 'options', 'hints', 'stamp'))) { 143 $this->assertAttributeEquals($value, $property, $actualquestiondata); 144 } 145 } 146 147 foreach ($questiondata->options as $optionname => $value) { 148 if ($optionname != 'questions') { 149 $this->assertAttributeEquals($value, $optionname, $actualquestiondata->options); 150 } 151 } 152 153 foreach ($questiondata->hints as $hint) { 154 $actualhint = array_shift($actualquestiondata->hints); 155 foreach ($hint as $property => $value) { 156 if (!in_array($property, array('id', 'questionid', 'options'))) { 157 $this->assertAttributeEquals($value, $property, $actualhint); 158 } 159 } 160 } 161 162 $this->assertObjectHasAttribute('questions', $actualquestiondata->options); 163 164 $subqpropstoignore = 165 array('id', 'category', 'parent', 'contextid', 'question', 'options', 'stamp', 'version', 'timemodified', 166 'timecreated'); 167 foreach ($questiondata->options->questions as $subqno => $subq) { 168 $actualsubq = $actualquestiondata->options->questions[$subqno]; 169 foreach ($subq as $subqproperty => $subqvalue) { 170 if (!in_array($subqproperty, $subqpropstoignore)) { 171 $this->assertAttributeEquals($subqvalue, $subqproperty, $actualsubq); 172 } 173 } 174 foreach ($subq->options as $optionname => $value) { 175 if (!in_array($optionname, array('answers'))) { 176 $this->assertAttributeEquals($value, $optionname, $actualsubq->options); 177 } 178 } 179 foreach ($subq->options->answers as $answer) { 180 $actualanswer = array_shift($actualsubq->options->answers); 181 foreach ($answer as $ansproperty => $ansvalue) { 182 // These questions do not use 'answerformat', will ignore it. 183 if (!in_array($ansproperty, array('id', 'question', 'answerformat'))) { 184 $this->assertAttributeEquals($ansvalue, $ansproperty, $actualanswer); 185 } 186 } 187 } 188 } 189 } 190 /** 191 * Verify that the multiplechoice variants parameters are correctly interpreted from 192 * the question text 193 * 194 * 195 */ 196 public function test_questiontext_extraction_of_multiplechoice_subquestions_variants() { 197 $questiontext = array(); 198 $questiontext['format'] = FORMAT_HTML; 199 $questiontext['itemid'] = ''; 200 $questiontext['text'] = '<p>Match the following cities with the correct state:</p> 201 <ul> 202 <li>1 San Francisco:{1:MULTICHOICE:=California#OK~Arizona#Wrong}</li> 203 <li>2 Tucson:{1:MC:%0%California#Wrong~=Arizona#OK}</li> 204 <li>3 Los Angeles:{1:MULTICHOICE_S:=California#OK~Arizona#Wrong}</li> 205 <li>4 Phoenix:{1:MCS:%0%California#Wrong~=Arizona#OK}</li> 206 <li>5 San Francisco:{1:MULTICHOICE_H:=California#OK~Arizona#Wrong}</li> 207 <li>6 Tucson:{1:MCH:%0%California#Wrong~=Arizona#OK}</li> 208 <li>7 Los Angeles:{1:MULTICHOICE_HS:=California#OK~Arizona#Wrong}</li> 209 <li>8 Phoenix:{1:MCHS:%0%California#Wrong~=Arizona#OK}</li> 210 <li>9 San Francisco:{1:MULTICHOICE_V:=California#OK~Arizona#Wrong}</li> 211 <li>10 Tucson:{1:MCV:%0%California#Wrong~=Arizona#OK}</li> 212 <li>11 Los Angeles:{1:MULTICHOICE_VS:=California#OK~Arizona#Wrong}</li> 213 <li>12 Phoenix:{1:MCVS:%0%California#Wrong~=Arizona#OK}</li> 214 </ul>'; 215 216 $q = qtype_multianswer_extract_question($questiontext); 217 foreach ($q->options->questions as $key => $sub) { 218 $this->assertSame($sub->qtype, 'multichoice'); 219 if ($key == 1 || $key == 2 || $key == 5 || $key == 6 || $key == 9 || $key == 10) { 220 $this->assertSame($sub->shuffleanswers, 0); 221 } else { 222 $this->assertSame($sub->shuffleanswers, 1); 223 } 224 if ($key == 1 || $key == 2 || $key == 3 || $key == 4) { 225 $this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_DROPDOWN); 226 } else if ($key == 5 || $key == 6 || $key == 7 || $key == 8) { 227 $this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_HORIZONTAL); 228 } else if ($key == 9 || $key == 10 || $key == 11 || $key == 12) { 229 $this->assertSame($sub->layout, qtype_multichoice_base::LAYOUT_VERTICAL); 230 } 231 } 232 } 233 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |