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