See Release Notes
Long Term Support Release
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 tool_brickfield; 18 19 defined('MOODLE_INTERNAL') || die(); 20 21 global $CFG; 22 require_once($CFG->dirroot . '/admin/tool/brickfield/tests/area_test_base.php'); 23 24 /** 25 * Class tool_brickfield_area_testcase 26 * 27 * @package tool_brickfield 28 * @copyright 2020 onward: Brickfield Education Labs, https://www.brickfield.ie 29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 30 */ 31 class area_test extends area_test_base { 32 /** 33 * Test for the area assign intro 34 */ 35 public function test_assign() { 36 $this->resetAfterTest(); 37 $course = $this->getDataGenerator()->create_course(); 38 $assign1 = $this->getDataGenerator()->create_module('assign', array( 39 'course' => $course->id, 'name' => 'Test!', 'intro' => '<p>Here we go</p>', 40 'introformat' => FORMAT_HTML)); 41 list($course1, $cm1) = get_course_and_cm_from_instance($assign1->id, 'assign'); 42 $assign2 = $this->getDataGenerator()->create_module('assign', array( 43 'course' => SITEID, 'name' => 'Test2!', 'intro' => 'Something', 44 'introformat' => FORMAT_MOODLE)); 45 list($course2, $cm2) = get_course_and_cm_from_instance($assign2->id, 'assign'); 46 47 $c = new \tool_brickfield\local\areas\mod_assign\intro(); 48 $this->assertEquals('mod_assign', $c->get_component()); 49 $this->assertEquals('assign', $c->get_tablename()); 50 $resultsrs = $c->find_course_areas($course1->id); 51 $resultsrs2 = $c->find_course_areas($course2->id); 52 // Set up a results array from the recordset for easier testing. 53 $results = array_merge($this->array_from_recordset($resultsrs), $this->array_from_recordset($resultsrs2)); 54 $this->assertEquals([ 55 (object)[ 56 'type' => area_base::TYPE_FIELD, 57 'contextid' => \context_module::instance($cm1->id)->id, 58 'component' => $c->get_component(), 59 'tablename' => $c->get_tablename(), 60 'fieldorarea' => $c->get_fieldname(), 61 'itemid' => $assign1->id, 62 'cmid' => $cm1->id, 63 'courseid' => $course1->id, 64 'content' => $assign1->intro, 65 ], 66 (object)[ 67 'type' => area_base::TYPE_FIELD, 68 'contextid' => \context_module::instance($cm2->id)->id, 69 'component' => $c->get_component(), 70 'tablename' => $c->get_tablename(), 71 'fieldorarea' => $c->get_fieldname(), 72 'itemid' => $assign2->id, 73 'cmid' => $cm2->id, 74 'courseid' => $course2->id, 75 'content' => $assign2->intro, 76 ] 77 ], $results); 78 79 // Emulate the course_module_updated event. 80 $event = \core\event\course_module_updated::create_from_cm($cm1); 81 $relevantresultsrs = $c->find_relevant_areas($event); 82 // Set up a relevantresults array from the recordset for easier testing. 83 $relevantresults = $this->array_from_recordset($relevantresultsrs); 84 $this->assertEquals([$results[0]], $relevantresults); 85 } 86 87 /** 88 * Test for the area questiontext 89 */ 90 public function test_questiontext() { 91 $this->resetAfterTest(); 92 /** @var \core_question_generator $generator */ 93 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 94 $component = 'core_question'; 95 96 list($category1, $course1, $qcat1, $questions1) = $generator->setup_course_and_questions('course'); 97 list($category2, $course2, $qcat2, $questions2) = $generator->setup_course_and_questions('category'); 98 list($category3, $course3, $qcat3, $questions3) = $generator->setup_course_and_questions('system'); 99 100 $c = new \tool_brickfield\local\areas\core_question\questiontext(); 101 // Set up results arrays from the recordset for easier testing. 102 $course1areas = $this->array_from_recordset($c->find_course_areas($course1->id)); 103 $course2areas = $c->find_course_areas($course2->id); 104 $course3areas = $c->find_course_areas($course3->id); 105 $sysareas = $this->array_from_recordset($c->find_system_areas()); 106 107 // Assert the core_question area exists for the individual question's context, courseid and categoryid. 108 $this->assert_area_in_array( 109 $course1areas, 110 $component, 111 \context_course::instance($course1->id)->id, 112 $questions1[0]->id, 113 $course1->id, 114 null 115 ); 116 $this->assert_area_in_array( 117 $sysareas, 118 $component, 119 \context_coursecat::instance($category2->id)->id, 120 $questions2[0]->id, 121 SITEID, 122 $category2->id 123 ); 124 $this->assert_area_in_array( 125 $sysareas, 126 $component, 127 \context_system::instance()->id, 128 $questions3[0]->id, 129 SITEID, 130 null 131 ); 132 133 // Emulate the question_created event. 134 $event = \core\event\question_created::create_from_question_instance($questions1[1], 135 \context_course::instance($course1->id)); 136 $relevantresults = $this->array_from_recordset($c->find_relevant_areas($event)); 137 $this->assert_area_in_array( 138 $course1areas, 139 $component, 140 \context_course::instance($relevantresults[0]->courseid)->id, 141 $relevantresults[0]->itemid, 142 $relevantresults[0]->courseid, 143 $relevantresults[0]->categoryid 144 ); 145 } 146 147 /** 148 * test for the area questionanswers 149 */ 150 public function test_questionanswers() { 151 global $DB; 152 153 $this->resetAfterTest(); 154 /** @var \core_question_generator $generator */ 155 $generator = $this->getDataGenerator()->get_plugin_generator('core_question'); 156 $course = $this->getDataGenerator()->create_course(); 157 $cat = $generator->create_question_category(['contextid' => \context_course::instance($course->id)->id]); 158 $question1 = $generator->create_question('multichoice', null, 159 ['name' => 'Example multichoice question', 'category' => $cat->id]); 160 $question2 = $generator->create_question('numerical', null, 161 ['name' => 'Example numerical question', 'category' => $cat->id]); 162 163 $dbanswers = $DB->get_records('question_answers', [] , 'id'); 164 $this->assertNotEmpty(count($dbanswers)); 165 166 $c = new \tool_brickfield\local\areas\core_question\questionanswers(); 167 $resultsrs = $c->find_course_areas($course->id); 168 $results = $this->array_from_recordset($resultsrs); 169 170 // There will be the same number of results as the number of records in the question_answers table. 171 $this->assertEquals(count($dbanswers), count($results)); 172 173 // Emulate the question_updated event. 174 $event = \core\event\question_updated::create_from_question_instance($question1, 175 \context_course::instance($course->id)); 176 $relevantresultsrs = $c->find_relevant_areas($event); 177 // Set up a relevantresults array from the recordset for easier testing. 178 $relevantresults = $this->array_from_recordset($relevantresultsrs); 179 180 $dbanswers = array_values($DB->get_records('question_answers', ['question' => $question1->id], 'id')); 181 $this->assertEquals(count($dbanswers), count($relevantresults)); 182 foreach ($dbanswers as $i => $dbanswer) { 183 $relevantresult = $relevantresults[$i]; 184 $this->assertEquals($dbanswer->answer, $relevantresult->content); 185 $this->assertEquals('question', $relevantresult->reftable); 186 $this->assertEquals($question1->id, $relevantresult->refid); 187 $this->assertEquals($dbanswer->id, $relevantresult->itemid); 188 } 189 } 190 191 /** 192 * Test for the areas choice intro and choice options 193 */ 194 public function test_choice() { 195 global $DB; 196 $this->resetAfterTest(); 197 $course = $this->getDataGenerator()->create_course(); 198 $choice1 = $this->getDataGenerator()->create_module('choice', [ 199 'course' => $course->id, 'option' => ['fried rice', 'spring rolls', 'sweet and sour pork'] 200 ]); 201 list($course1, $cm1) = get_course_and_cm_from_instance($choice1->id, 'choice'); 202 $choice2 = $this->getDataGenerator()->create_module('choice', [ 203 'course' => $course->id, 'option' => ['blue', 'red'] 204 ]); 205 list($course2, $cm2) = get_course_and_cm_from_instance($choice2->id, 'choice'); 206 207 // Testing the choice intro. 208 $c = new \tool_brickfield\local\areas\mod_choice\intro(); 209 $resultsrs = $c->find_course_areas($course->id); 210 // Set up a results array from the recordset for easier testing. 211 $results = $this->array_from_recordset($resultsrs); 212 213 $this->assertCount(2, $results); 214 $this->assertEquals($cm1->id, $results[0]->cmid); 215 $this->assertEquals($choice2->id, $results[1]->itemid); 216 217 // Emulate the course_module_created event. 218 $event = \core\event\course_module_created::create_from_cm($cm1); 219 $relevantresultsrs = $c->find_relevant_areas($event); 220 $relevantresults = $this->array_from_recordset($relevantresultsrs); 221 $this->assertEquals([$results[0]], $relevantresults); 222 223 // Testing the choice options. 224 $c = new \tool_brickfield\local\areas\mod_choice\option(); 225 $resultsrs = $c->find_course_areas($course->id); 226 // Set up a results array from the recordset for easier testing. 227 $results = $this->array_from_recordset($resultsrs); 228 229 $this->assertCount(5, $results); 230 $this->assertEquals($cm2->id, $results[3]->cmid); 231 $this->assertEquals('choice_options', $results[3]->tablename); 232 $this->assertEquals('choice', $results[3]->reftable); 233 $this->assertEquals($choice2->id, $results[3]->refid); 234 $options3 = $DB->get_records_menu('choice_options', ['choiceid' => $choice2->id], 'id', 'text,id'); 235 $this->assertEquals($options3['blue'], $results[3]->itemid); 236 $this->assertEquals('blue', $results[3]->content); 237 238 // Emulate the course_module_updated event. 239 $event = \core\event\course_module_updated::create_from_cm($cm2); 240 $relevantresultsrs = $c->find_relevant_areas($event); 241 $relevantresults = $this->array_from_recordset($relevantresultsrs); 242 $this->assertEquals([$results[3], $results[4]], $relevantresults); 243 } 244 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body