|
| 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_encoded; |
| 18 | + |
| 19 | +/** |
| 20 | + * Helper unit tests. |
| 21 | + * |
| 22 | + * @package tool_encoded |
| 23 | + * @author Benjamin Walker (benjaminwalker@catalyst-au.net) |
| 24 | + * @copyright 2024 Catalyst IT |
| 25 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 26 | + * @coversDefaultClass \tool_encoded\helper |
| 27 | + */ |
| 28 | +class helper_test extends \advanced_testcase { |
| 29 | + /** |
| 30 | + * Set up before each test. |
| 31 | + */ |
| 32 | + protected function setUp(): void { |
| 33 | + parent::setUp(); |
| 34 | + $this->resetAfterTest(); |
| 35 | + $this->setAdminUser(); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Tests getting instance id from a base64 record |
| 40 | + * |
| 41 | + * @covers ::get_instance_id |
| 42 | + */ |
| 43 | + public function test_get_instance_id(): void { |
| 44 | + $course = $this->getDataGenerator()->create_course(); |
| 45 | + $user = $this->getDataGenerator()->create_user(); |
| 46 | + $this->getDataGenerator()->enrol_user($user->id, $course->id); |
| 47 | + |
| 48 | + // Test course mapping. |
| 49 | + $section = course_create_section($course); |
| 50 | + $record = (object) [ |
| 51 | + 'report_table' => 'course_sections', |
| 52 | + 'report_column' => 'summary', |
| 53 | + 'native_id' => $section->id, |
| 54 | + ]; |
| 55 | + $instanceid = helper::get_instance_id($record); |
| 56 | + $this->assertEquals($course->id, $instanceid); |
| 57 | + |
| 58 | + // Logic for direct module tables like 'page' load the cmid directly and should not use the helper. |
| 59 | + |
| 60 | + // Test course module mapping with simple lookups. |
| 61 | + $book = $this->getDataGenerator()->create_module('book', ['course' => $course->id]); |
| 62 | + $bookgenerator = $this->getDataGenerator()->get_plugin_generator('mod_book'); |
| 63 | + $chapter = $bookgenerator->create_chapter(['bookid' => $book->id]); |
| 64 | + |
| 65 | + $record = (object) [ |
| 66 | + 'report_table' => 'book_chapters', |
| 67 | + 'report_column' => 'content', |
| 68 | + 'native_id' => $chapter->id, |
| 69 | + ]; |
| 70 | + $instanceid = helper::get_instance_id($record); |
| 71 | + $this->assertEquals($book->cmid, $instanceid); |
| 72 | + |
| 73 | + // Test course module mapping for forum posts. |
| 74 | + $forum = $this->getDataGenerator()->create_module('forum', ['course' => $course->id]); |
| 75 | + $forumgenerator = $this->getDataGenerator()->get_plugin_generator('mod_forum'); |
| 76 | + $properties = (object) [ |
| 77 | + 'course' => $course->id, |
| 78 | + 'forum' => $forum->id, |
| 79 | + 'userid' => $user->id, |
| 80 | + ]; |
| 81 | + $discussion = $forumgenerator->create_discussion($properties); |
| 82 | + $properties->discussion = $discussion->id; |
| 83 | + $post = $forumgenerator->create_post($properties); |
| 84 | + |
| 85 | + $record = (object) [ |
| 86 | + 'report_table' => 'forum_posts', |
| 87 | + 'report_column' => 'message', |
| 88 | + 'native_id' => $post->id, |
| 89 | + ]; |
| 90 | + $instanceid = helper::get_instance_id($record); |
| 91 | + $this->assertEquals($forum->cmid, $instanceid); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Tests getting variable context from a base64 record |
| 96 | + * |
| 97 | + * @covers ::get_variable_context |
| 98 | + */ |
| 99 | + public function test_get_variable_context(): void { |
| 100 | + global $DB; |
| 101 | + |
| 102 | + $course = $this->getDataGenerator()->create_course(); |
| 103 | + $user = $this->getDataGenerator()->create_user(); |
| 104 | + $this->getDataGenerator()->enrol_user($user->id, $course->id); |
| 105 | + |
| 106 | + // Test mapping of question table to system context. |
| 107 | + $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question'); |
| 108 | + $category = $questiongenerator->create_question_category(); |
| 109 | + $question = $questiongenerator->create_question('multichoice', null, ['category' => $category->id]); |
| 110 | + |
| 111 | + $record = (object) [ |
| 112 | + 'report_table' => 'question', |
| 113 | + 'report_column' => 'questiontext', |
| 114 | + 'native_id' => $question->id, |
| 115 | + ]; |
| 116 | + |
| 117 | + $mapping = helper::get_mapping($record); |
| 118 | + $context = helper::get_variable_context($record, $mapping); |
| 119 | + $systemcontext = \context_system::instance(); |
| 120 | + $this->assertEquals($systemcontext->contextlevel, $context->contextlevel); |
| 121 | + $this->assertEquals($systemcontext->instanceid, $context->instanceid); |
| 122 | + |
| 123 | + // Test mapping of question table to course context. |
| 124 | + $coursecontext = \context_course::instance($course->id); |
| 125 | + $category = $questiongenerator->create_question_category(['contextid' => $coursecontext->id]); |
| 126 | + $question = $questiongenerator->create_question('multichoice', null, ['category' => $category->id]); |
| 127 | + |
| 128 | + $record = (object) [ |
| 129 | + 'report_table' => 'question', |
| 130 | + 'report_column' => 'questiontext', |
| 131 | + 'native_id' => $question->id, |
| 132 | + ]; |
| 133 | + |
| 134 | + $mapping = helper::get_mapping($record); |
| 135 | + $context = helper::get_variable_context($record, $mapping); |
| 136 | + $this->assertEquals($coursecontext->contextlevel, $context->contextlevel); |
| 137 | + $this->assertEquals($coursecontext->instanceid, $context->instanceid); |
| 138 | + |
| 139 | + // Test mapping of question type tables to course context. |
| 140 | + $category = $questiongenerator->create_question_category(['contextid' => $coursecontext->id]); |
| 141 | + $question = $questiongenerator->create_question('match', null, ['category' => $category->id]); |
| 142 | + |
| 143 | + $subquestionid = $DB->get_field('qtype_match_subquestions', 'id', ['questionid' => $question->id], IGNORE_MULTIPLE); |
| 144 | + $record = (object) [ |
| 145 | + 'report_table' => 'qtype_match_subquestions', |
| 146 | + 'report_column' => 'questiontext', |
| 147 | + 'native_id' => $subquestionid, |
| 148 | + ]; |
| 149 | + |
| 150 | + $mapping = helper::get_mapping($record); |
| 151 | + $context = helper::get_variable_context($record, $mapping); |
| 152 | + $this->assertEquals($coursecontext->contextlevel, $context->contextlevel); |
| 153 | + $this->assertEquals($coursecontext->instanceid, $context->instanceid); |
| 154 | + |
| 155 | + // Test mapping of grade grades to module context. |
| 156 | + $assign = $this->getDataGenerator()->create_module('assign', [ |
| 157 | + 'course' => $course->id, |
| 158 | + 'assignfeedback_comments_enabled' => 1, |
| 159 | + ]); |
| 160 | + |
| 161 | + $gradeitem = $this->getDataGenerator()->create_grade_item([ |
| 162 | + 'courseid' => $course->id, |
| 163 | + 'itemtype' => 'mod', |
| 164 | + 'itemmodule' => 'assign', |
| 165 | + 'iteminstance' => $assign->id, |
| 166 | + ]); |
| 167 | + |
| 168 | + $gradegrade = $this->getDataGenerator()->create_grade_grade([ |
| 169 | + 'itemid' => $gradeitem->id, |
| 170 | + 'userid' => $user->id, |
| 171 | + 'assignfeedbackcomments_editor' => ['text' => 'Comment', 'format' => FORMAT_MOODLE], |
| 172 | + ]); |
| 173 | + |
| 174 | + $record = (object) [ |
| 175 | + 'report_table' => 'grade_grades', |
| 176 | + 'report_column' => 'feedback', |
| 177 | + 'native_id' => $gradegrade->id, |
| 178 | + ]; |
| 179 | + |
| 180 | + $mapping = helper::get_mapping($record); |
| 181 | + $context = helper::get_variable_context($record, $mapping); |
| 182 | + $modulecontext = \context_module::instance($assign->cmid); |
| 183 | + $this->assertEquals($modulecontext->contextlevel, $context->contextlevel); |
| 184 | + $this->assertEquals($modulecontext->instanceid, $context->instanceid); |
| 185 | + } |
| 186 | +} |
0 commit comments