See Release Notes
Long Term Support Release
Differences Between: [Versions 400 and 401]
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 enrol_lti\local\ltiadvantage\repository; 18 19 use enrol_lti\helper; 20 21 /** 22 * Tests for published_resource_repository objects. 23 * 24 * @package enrol_lti 25 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com> 26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 27 * @coversDefaultClass \enrol_lti\local\ltiadvantage\repository\published_resource_repository 28 */ 29 class published_resource_repository_test extends \advanced_testcase { 30 /** 31 * Get a list of published resources for testing. 32 * 33 * @return array a list of relevant test data; users courses and mods. 34 */ 35 protected function generate_published_resources() { 36 // Create a course and publish it. 37 $generator = $this->getDataGenerator(); 38 $course = $generator->create_course(); 39 $course2 = $generator->create_course(); 40 $user = $generator->create_and_enrol($course, 'editingteacher'); 41 $user2 = $generator->create_and_enrol($course2, 'editingteacher'); 42 $user3 = $generator->create_user(); 43 $this->setAdminUser(); 44 $mod = $generator->create_module('assign', ['course' => $course->id]); 45 $mod2 = $generator->create_module('resource', ['course' => $course2->id]); 46 $mod3 = $generator->create_module('assign', ['course' => $course->id, 'grade' => 0]); 47 $mod4 = $generator->create_module('workshop', ['course' => $course->id]); // Had multiple grade items. 48 $courseresourcedata = (object) [ 49 'courseid' => $course->id, 50 'membersyncmode' => 0, 51 'membersync' => 0, 52 'ltiversion' => 'LTI-1p3' 53 ]; 54 $moduleresourcedata = (object) [ 55 'courseid' => $course->id, 56 'cmid' => $mod->cmid, 57 'membersyncmode' => 1, 58 'membersync' => 1, 59 'ltiversion' => 'LTI-1p3' 60 ]; 61 $module2resourcedata = (object) [ 62 'courseid' => $course2->id, 63 'cmid' => $mod2->cmid, 64 'membersyncmode' => 1, 65 'membersync' => 1, 66 'ltiversion' => 'LTI-1p3' 67 ]; 68 $module3resourcedata = (object) [ 69 'courseid' => $course->id, 70 'cmid' => $mod3->cmid, 71 'membersyncmode' => 1, 72 'membersync' => 1, 73 'ltiversion' => 'LTI-1p3' 74 ]; 75 $module4resourcedata = (object) [ 76 'courseid' => $course->id, 77 'cmid' => $mod4->cmid, 78 'membersyncmode' => 1, 79 'membersync' => 1, 80 'ltiversion' => 'LTI-1p3' 81 ]; 82 $coursetool = $generator->create_lti_tool($courseresourcedata); 83 $coursetool = helper::get_lti_tool($coursetool->id); 84 $modtool = $generator->create_lti_tool($moduleresourcedata); 85 $modtool = helper::get_lti_tool($modtool->id); 86 $mod2tool = $generator->create_lti_tool($module2resourcedata); 87 $mod2tool = helper::get_lti_tool($mod2tool->id); 88 $mod3tool = $generator->create_lti_tool($module3resourcedata); 89 $mod3tool = helper::get_lti_tool($mod3tool->id); 90 $mod4tool = $generator->create_lti_tool($module4resourcedata); 91 $mod4tool = helper::get_lti_tool($mod4tool->id); 92 return [$user, $user2, $user3, $course, $course2, $mod, $mod2, $mod3, $mod4, $coursetool, $modtool, $mod2tool, 93 $mod3tool, $mod4tool]; 94 } 95 96 /** 97 * Test finding published resources for a given user. 98 * 99 * @covers ::find_all_for_user 100 */ 101 public function test_find_all_for_user() { 102 $this->resetAfterTest(); 103 [$user, $user2, $user3, $course, $course2, $mod, $mod2, $mod3, $mod4] = $this->generate_published_resources(); 104 105 $resourcerepo = new published_resource_repository(); 106 107 $resources = $resourcerepo->find_all_for_user($user->id); 108 $this->assertCount(4, $resources); 109 usort($resources, function($a, $b) { 110 return strcmp($a->get_contextid(), $b->get_contextid()); 111 }); 112 $this->assertEquals($resources[0]->get_contextid(), \context_course::instance($course->id)->id); 113 $this->assertEquals($resources[1]->get_contextid(), \context_module::instance($mod->cmid)->id); 114 $this->assertEquals($resources[2]->get_contextid(), \context_module::instance($mod3->cmid)->id); 115 $this->assertEquals($resources[3]->get_contextid(), \context_module::instance($mod4->cmid)->id); 116 $this->assertTrue($resources[0]->supports_grades()); 117 $this->assertTrue($resources[1]->supports_grades()); 118 $this->assertFalse($resources[2]->supports_grades()); 119 $this->assertFalse($resources[3]->supports_grades()); // Multiple grade items isn't supported in content selection. 120 121 $resources = $resourcerepo->find_all_for_user($user2->id); 122 $this->assertCount(1, $resources); 123 $this->assertEquals($resources[0]->get_contextid(), \context_module::instance($mod2->cmid)->id); 124 $this->assertFalse($resources[0]->supports_grades()); 125 126 $this->assertEmpty($resourcerepo->find_all_for_user($user3->id)); 127 } 128 129 /** 130 * Test finding a subset of published resources, by id, for a user. 131 * 132 * @covers ::find_all_by_ids_for_user 133 */ 134 public function test_find_all_by_ids_for_user() { 135 $this->resetAfterTest(); 136 [$user, $user2, $user3, $course, $course2, $mod, $mod2, $mod3, $mod4, $coursetool, $tool, $tool2, $tool3, $tool4] = 137 $this->generate_published_resources(); 138 139 $resourcerepo = new published_resource_repository(); 140 141 $resources = $resourcerepo->find_all_by_ids_for_user([$tool->id, $tool2->id, $tool3->id, $tool4->id], $user->id); 142 $this->assertCount(3, $resources); 143 usort($resources, function ($a, $b) { 144 return strcmp($a->get_contextid(), $b->get_contextid()); 145 }); 146 $this->assertEquals($resources[0]->get_contextid(), \context_module::instance($mod->cmid)->id); 147 $this->assertEquals($resources[1]->get_contextid(), \context_module::instance($mod3->cmid)->id); 148 $this->assertEquals($resources[2]->get_contextid(), \context_module::instance($mod4->cmid)->id); 149 $this->assertTrue($resources[0]->supports_grades()); 150 $this->assertFalse($resources[1]->supports_grades()); 151 $this->assertFalse($resources[2]->supports_grades()); // Multiple grade items isn't supported in content selection. 152 153 $resources = $resourcerepo->find_all_by_ids_for_user([$tool->id, $tool2->id, $tool3->id], $user2->id); 154 $this->assertCount(1, $resources); 155 $this->assertEquals($resources[0]->get_contextid(), \context_module::instance($mod2->cmid)->id); 156 $this->assertFalse($resources[0]->supports_grades()); 157 158 $this->assertEmpty($resourcerepo->find_all_by_ids_for_user([$tool->id], $user2->id)); 159 $this->assertEmpty($resourcerepo->find_all_by_ids_for_user([], $user2->id)); 160 } 161 162 /** 163 * Test finding published resources for different roles having different capabilities at the course level. 164 * 165 * @covers ::find_all_for_user 166 */ 167 public function test_find_all_for_user_no_permissions() { 168 $this->resetAfterTest(); 169 global $DB; 170 [$user, $user2, $user3, $course, $course2, $mod, $mod2, $mod3, $mod4, $coursetool, $modtool, $mod2tool] 171 = $this->generate_published_resources(); 172 173 // Grant the user permissions as an editing teacher in a specific module within the course, 174 // as if the user had launched into the module via LTI enrolment, with an instructor role of 'editingteacher'. 175 $modaccessonlyuser = $this->getDataGenerator()->create_user(); 176 helper::enrol_user($modtool, $modaccessonlyuser->id); // Enrolment only, no role. 177 $editingteacherrole = $DB->get_record('role', array('shortname' => 'editingteacher')); 178 role_assign($editingteacherrole->id, $modaccessonlyuser->id, $modtool->contextid); 179 180 // Verify that without a course role of editing teacher (not module role), the published content isn't visible. 181 $resourcerepo = new published_resource_repository(); 182 $resources = $resourcerepo->find_all_for_user($modaccessonlyuser->id); 183 $this->assertCount(0, $resources); 184 185 // Now, give the user a course role of 'editingteacher' and confirm they can see the published content. 186 $this->getDataGenerator()->enrol_user($modaccessonlyuser->id, $course->id, 'editingteacher'); 187 $resources = $resourcerepo->find_all_for_user($modaccessonlyuser->id); 188 $this->assertCount(4, $resources); 189 190 // Check other course level roles without the capability, e.g. 'teacher'. 191 role_unassign($editingteacherrole->id, $modaccessonlyuser->id, \context_course::instance($course->id)->id); 192 $this->getDataGenerator()->enrol_user($modaccessonlyuser->id, $course->id, 'teacher'); 193 $resources = $resourcerepo->find_all_for_user($modaccessonlyuser->id); 194 $this->assertCount(0, $resources); 195 } 196 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body