See Release Notes
Long Term Support Release
Differences Between: [Versions 401 and 402] [Versions 401 and 403]
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 * Privacy provider tests. 19 * 20 * @package enrol_lti 21 * @copyright 2018 Mark Nelson <markn@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 namespace enrol_lti\privacy; 25 26 use enrol_lti\privacy\provider; 27 28 defined('MOODLE_INTERNAL') || die(); 29 30 /** 31 * Privacy provider tests class. 32 * 33 * @package enrol_lti 34 * @copyright 2018 Mark Nelson <markn@moodle.com> 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class provider_test extends \core_privacy\tests\provider_testcase { 38 39 /** 40 * @var stdClass The user 41 */ 42 protected $user = null; 43 44 /** 45 * @var stdClass Another user 46 */ 47 protected $anotheruser = null; 48 49 /** 50 * @var stdClass The course 51 */ 52 protected $course = null; 53 54 /** 55 * @var stdClass The activity 56 */ 57 protected $activity = null; 58 59 /** 60 * Basic setup for these tests. 61 */ 62 public function setUp(): void { 63 $this->resetAfterTest(); 64 65 $this->course = $this->getDataGenerator()->create_course(); 66 $this->user = $this->getDataGenerator()->create_user(); 67 $this->activity = $this->getDataGenerator()->create_module('forum', ['course' => $this->course->id]); 68 69 // Get the course and activity contexts. 70 $coursecontext = \context_course::instance($this->course->id); 71 $cmcontext = \context_module::instance($this->activity->cmid); 72 73 // Create LTI tools in different contexts. 74 $this->create_lti_users($coursecontext, $this->user->id); 75 $this->create_lti_users($coursecontext, $this->user->id); 76 $this->create_lti_users($cmcontext, $this->user->id); 77 78 // Create another LTI user. 79 $this->anotheruser = $this->getDataGenerator()->create_user(); 80 $this->create_lti_users($coursecontext, $this->anotheruser->id); 81 } 82 83 /** 84 * Test getting the context for the user ID related to this plugin. 85 */ 86 public function test_get_contexts_for_userid() { 87 $contextlist = provider::get_contexts_for_userid($this->user->id); 88 89 $this->assertCount(2, $contextlist); 90 91 $coursectx = \context_course::instance($this->course->id); 92 $activityctx = \context_module::instance($this->activity->cmid); 93 $expectedids = [$coursectx->id, $activityctx->id]; 94 95 $actualids = $contextlist->get_contextids(); 96 $this->assertEqualsCanonicalizing($expectedids, $actualids); 97 } 98 99 /** 100 * Test for provider::export_user_data(). 101 */ 102 public function test_export_for_context() { 103 $coursecontext = \context_course::instance($this->course->id); 104 $cmcontext = \context_module::instance($this->activity->cmid); 105 106 // Export all of the data for the course context. 107 $this->export_context_data_for_user($this->user->id, $coursecontext, 'enrol_lti'); 108 $writer = \core_privacy\local\request\writer::with_context($coursecontext); 109 $this->assertTrue($writer->has_any_data()); 110 111 $data = (array) $writer->get_data(['enrol_lti_users']); 112 $this->assertCount(2, $data); 113 foreach ($data as $ltiuser) { 114 $this->assertArrayHasKey('lastgrade', $ltiuser); 115 $this->assertArrayHasKey('timecreated', $ltiuser); 116 $this->assertArrayHasKey('timemodified', $ltiuser); 117 } 118 119 // Export all of the data for the activity context. 120 $this->export_context_data_for_user($this->user->id, $cmcontext, 'enrol_lti'); 121 $writer = \core_privacy\local\request\writer::with_context($cmcontext); 122 $this->assertTrue($writer->has_any_data()); 123 124 $data = (array) $writer->get_data(['enrol_lti_users']); 125 $this->assertCount(1, $data); 126 foreach ($data as $ltiuser) { 127 $this->assertArrayHasKey('lastgrade', $ltiuser); 128 $this->assertArrayHasKey('timecreated', $ltiuser); 129 $this->assertArrayHasKey('timemodified', $ltiuser); 130 } 131 } 132 133 /** 134 * Test for provider::delete_data_for_all_users_in_context(). 135 */ 136 public function test_delete_data_for_all_users_in_context() { 137 global $DB; 138 139 $count = $DB->count_records('enrol_lti_users'); 140 $this->assertEquals(4, $count); 141 142 // Delete data based on context. 143 $coursecontext = \context_course::instance($this->course->id); 144 provider::delete_data_for_all_users_in_context($coursecontext); 145 146 $ltiusers = $DB->get_records('enrol_lti_users'); 147 $this->assertCount(1, $ltiusers); 148 149 $ltiuser = reset($ltiusers); 150 $this->assertEquals($ltiuser->userid, $this->user->id); 151 } 152 153 /** 154 * Test for provider::delete_data_for_user(). 155 */ 156 public function test_delete_data_for_user() { 157 global $DB; 158 159 $cmcontext = \context_module::instance($this->activity->cmid); 160 $coursecontext = \context_course::instance($this->course->id); 161 162 $count = $DB->count_records('enrol_lti_users'); 163 $this->assertEquals(4, $count); 164 165 $contextlist = new \core_privacy\local\request\approved_contextlist($this->user, 'enrol_lti', 166 [\context_system::instance()->id, $coursecontext->id, $cmcontext->id]); 167 provider::delete_data_for_user($contextlist); 168 169 $ltiusers = $DB->get_records('enrol_lti_users'); 170 $this->assertCount(1, $ltiusers); 171 172 $ltiuser = reset($ltiusers); 173 $this->assertNotEquals($ltiuser->userid, $this->user->id); 174 } 175 176 /** 177 * Creates a LTI user given the provided context 178 * 179 * @param context $context 180 * @param int $userid 181 */ 182 private function create_lti_users(\context $context, int $userid) { 183 global $DB; 184 185 // Create a tool. 186 $ltitool = (object) [ 187 'enrolid' => 5, 188 'contextid' => $context->id, 189 'roleinstructor' => 5, 190 'rolelearner' => 5, 191 'timecreated' => time(), 192 'timemodified' => time() + DAYSECS 193 ]; 194 $toolid = $DB->insert_record('enrol_lti_tools', $ltitool); 195 196 // Create a user. 197 $ltiuser = (object) [ 198 'userid' => $userid, 199 'toolid' => $toolid, 200 'lastgrade' => 50, 201 'lastaccess' => time() + DAYSECS, 202 'timecreated' => time() 203 ]; 204 $DB->insert_record('enrol_lti_users', $ltiuser); 205 } 206 207 /** 208 * Test for provider::get_users_in_context() when the context is a course. 209 */ 210 public function test_get_users_in_context_course() { 211 $coursecontext = \context_course::instance($this->course->id); 212 $userlist = new \core_privacy\local\request\userlist($coursecontext, 'enrol_paypal'); 213 provider::get_users_in_context($userlist); 214 215 $this->assertEqualsCanonicalizing( 216 [$this->user->id, $this->anotheruser->id], 217 $userlist->get_userids()); 218 } 219 220 /** 221 * Test for provider::get_users_in_context() when the context is an activity. 222 */ 223 public function test_get_users_in_context_activity() { 224 $activityctx = \context_module::instance($this->activity->cmid); 225 $userlist = new \core_privacy\local\request\userlist($activityctx, 'enrol_paypal'); 226 provider::get_users_in_context($userlist); 227 228 $this->assertEquals( 229 [$this->user->id], 230 $userlist->get_userids()); 231 } 232 233 /** 234 * Test for provider::delete_data_for_users() when the context is a course. 235 */ 236 public function test_delete_data_for_users_course() { 237 global $DB; 238 239 $coursecontext = \context_course::instance($this->course->id); 240 241 $count = $DB->count_records('enrol_lti_users'); 242 $this->assertEquals(4, $count); 243 244 $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext, 'enrol_paypal', 245 [$this->user->id]); 246 provider::delete_data_for_users($approveduserlist); 247 248 $ltiusers = $DB->get_records('enrol_lti_users'); 249 $this->assertCount(2, $ltiusers); 250 251 foreach ($ltiusers as $ltiuser) { 252 $leftover = false; 253 if ($ltiuser->userid == $this->user->id) { 254 $contextid = $DB->get_field('enrol_lti_tools', 'contextid', ['id' => $ltiuser->toolid]); 255 if ($contextid == $coursecontext->id) { 256 $leftover = true; 257 } 258 } 259 } 260 $this->assertFalse($leftover); 261 } 262 263 /** 264 * Test for provider::delete_data_for_users() when the context is an activity. 265 */ 266 public function test_delete_data_for_users_activity() { 267 global $DB; 268 269 $cmcontext = \context_module::instance($this->activity->cmid); 270 271 $count = $DB->count_records('enrol_lti_users'); 272 $this->assertEquals(4, $count); 273 274 $approveduserlist = new \core_privacy\local\request\approved_userlist($cmcontext, 'enrol_paypal', 275 [$this->user->id]); 276 provider::delete_data_for_users($approveduserlist); 277 278 $ltiusers = $DB->get_records('enrol_lti_users'); 279 $this->assertCount(3, $ltiusers); 280 281 foreach ($ltiusers as $ltiuser) { 282 $leftover = false; 283 if ($ltiuser->userid == $this->user->id) { 284 $contextid = $DB->get_field('enrol_lti_tools', 'contextid', ['id' => $ltiuser->toolid]); 285 if ($contextid == $cmcontext->id) { 286 $leftover = true; 287 } 288 } 289 } 290 $this->assertFalse($leftover); 291 } 292 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body