Differences Between: [Versions 310 and 311] [Versions 39 and 311]
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 * Events tests. 19 * 20 * @package core_question 21 * @copyright 2019 the Open University 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 29 require_once($CFG->dirroot . '/question/editlib.php'); 30 require_once($CFG->dirroot . '/question/category_class.php'); 31 32 class core_question_category_class_testcase extends advanced_testcase { 33 34 /** 35 * @var question_category_object used in the tests. 36 */ 37 protected $qcobject; 38 39 /** 40 * @var context a context to use. 41 */ 42 protected $context; 43 44 /** 45 * @var stdClass top category in context. 46 */ 47 protected $topcat; 48 49 protected function setUp(): void { 50 parent::setUp(); 51 self::setAdminUser(); 52 $this->resetAfterTest(); 53 $this->context = context_course::instance(SITEID); 54 $contexts = new question_edit_contexts($this->context); 55 $this->topcat = question_get_top_category($this->context->id, true); 56 $this->qcobject = new question_category_object(null, 57 new moodle_url('/question/category.php', ['courseid' => SITEID]), 58 $contexts->having_one_edit_tab_cap('categories'), 0, null, 0, 59 $contexts->having_cap('moodle/question:add')); 60 } 61 62 /** 63 * Test creating a category. 64 */ 65 public function test_add_category_no_idnumber() { 66 global $DB; 67 68 $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid, 69 'New category', '', true, FORMAT_HTML, ''); // No idnumber passed as '' to match form data. 70 71 $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST); 72 $this->assertSame('New category', $newcat->name); 73 $this->assertNull($newcat->idnumber); 74 } 75 76 /** 77 * Test creating a category with a tricky idnumber. 78 */ 79 public function test_add_category_set_idnumber_0() { 80 global $DB; 81 82 $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid, 83 'New category', '', true, FORMAT_HTML, '0'); 84 85 $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST); 86 $this->assertSame('New category', $newcat->name); 87 $this->assertSame('0', $newcat->idnumber); 88 } 89 90 /** 91 * Trying to add a category with duplicate idnumber blanks it. 92 * (In reality, this would probably get caught by form validation.) 93 */ 94 public function test_add_category_try_to_set_duplicate_idnumber() { 95 global $DB; 96 97 $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid, 98 'Existing category', '', true, FORMAT_HTML, 'frog'); 99 100 $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid, 101 'New category', '', true, FORMAT_HTML, 'frog'); 102 103 $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST); 104 $this->assertSame('New category', $newcat->name); 105 $this->assertNull($newcat->idnumber); 106 } 107 108 /** 109 * Test updating a category. 110 */ 111 public function test_update_category() { 112 global $DB; 113 114 $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid, 115 'Old name', 'Description', true, FORMAT_HTML, 'frog'); 116 117 $this->qcobject->update_category($id, $this->topcat->id . ',' . $this->topcat->contextid, 118 'New name', 'New description', FORMAT_HTML, '0', false); 119 120 $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST); 121 $this->assertSame('New name', $newcat->name); 122 $this->assertSame('0', $newcat->idnumber); 123 } 124 125 /** 126 * Test updating a category to remove the idnumber. 127 */ 128 public function test_update_category_removing_idnumber() { 129 global $DB; 130 131 $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid, 132 'Old name', 'Description', true, FORMAT_HTML, 'frog'); 133 134 $this->qcobject->update_category($id, $this->topcat->id . ',' . $this->topcat->contextid, 135 'New name', 'New description', FORMAT_HTML, '', false); 136 137 $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST); 138 $this->assertSame('New name', $newcat->name); 139 $this->assertNull($newcat->idnumber); 140 } 141 142 /** 143 * Test updating a category without changing the idnumber. 144 */ 145 public function test_update_category_dont_change_idnumber() { 146 global $DB; 147 148 $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid, 149 'Old name', 'Description', true, FORMAT_HTML, 'frog'); 150 151 $this->qcobject->update_category($id, $this->topcat->id . ',' . $this->topcat->contextid, 152 'New name', 'New description', FORMAT_HTML, 'frog', false); 153 154 $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST); 155 $this->assertSame('New name', $newcat->name); 156 $this->assertSame('frog', $newcat->idnumber); 157 } 158 159 /** 160 * Trying to update a category so its idnumber duplicates idnumber blanks it. 161 * (In reality, this would probably get caught by form validation.) 162 */ 163 public function test_update_category_try_to_set_duplicate_idnumber() { 164 global $DB; 165 166 $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid, 167 'Existing category', '', true, FORMAT_HTML, 'toad'); 168 $id = $this->qcobject->add_category($this->topcat->id . ',' . $this->topcat->contextid, 169 'old name', '', true, FORMAT_HTML, 'frog'); 170 171 $this->qcobject->update_category($id, $this->topcat->id . ',' . $this->topcat->contextid, 172 'New name', '', FORMAT_HTML, 'toad', false); 173 174 $newcat = $DB->get_record('question_categories', ['id' => $id], '*', MUST_EXIST); 175 $this->assertSame('New name', $newcat->name); 176 $this->assertNull($newcat->idnumber); 177 } 178 179 /** 180 * Test that get_real_question_ids_in_category() returns question id 181 * of a shortanswer question in a category. 182 * 183 * @covers ::get_real_question_ids_in_category 184 */ 185 public function test_get_real_question_ids_in_category_shortanswer() { 186 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 187 $categoryid = $this->topcat->id; 188 189 // Short answer question is made of one question. 190 $shortanswer = $generator->create_question('shortanswer', null, ['category' => $categoryid]); 191 $questionids = $this->qcobject->get_real_question_ids_in_category($categoryid); 192 $this->assertCount(1, $questionids); 193 $this->assertContains($shortanswer->id, $questionids); 194 } 195 196 /** 197 * Test that get_real_question_ids_in_category() returns question id 198 * of a multianswer question in a category. 199 * 200 * @covers ::get_real_question_ids_in_category 201 */ 202 public function test_get_real_question_ids_in_category_multianswer() { 203 global $DB; 204 $countq = $DB->count_records('question'); 205 206 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 207 $categoryid = $this->topcat->id; 208 209 // Multi answer question is made of one parent and two child questions. 210 $multianswer = $generator->create_question('multianswer', null, ['category' => $categoryid]); 211 $questionids = $this->qcobject->get_real_question_ids_in_category($categoryid); 212 $this->assertCount(1, $questionids); 213 $this->assertContains($multianswer->id, $questionids); 214 $this->assertEquals(3, $DB->count_records('question') - $countq); 215 } 216 217 /** 218 * Test that get_real_question_ids_in_category() returns question id 219 * of a multianswer question in a category even if their child questions are 220 * linked to a category that doesn't exist. 221 * 222 * @covers ::get_real_question_ids_in_category 223 */ 224 public function test_get_real_question_ids_in_category_multianswer_bad_data() { 225 global $DB; 226 $countq = $DB->count_records('question'); 227 228 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 229 $categoryid = $this->topcat->id; 230 231 // Multi answer question is made of one parent and two child questions. 232 $multianswer = $generator->create_question('multianswer', null, ['category' => $categoryid]); 233 234 // Update category id for child questions to a category that doesn't exist. 235 $DB->set_field_select('question', 'category', 123456, 'id <> :id', ['id' => $multianswer->id]); 236 237 $questionids = $this->qcobject->get_real_question_ids_in_category($categoryid); 238 $this->assertCount(1, $questionids); 239 $this->assertContains($multianswer->id, $questionids); 240 $this->assertEquals(3, $DB->count_records('question') - $countq); 241 } 242 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body