Differences Between: [Versions 39 and 310]
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 tests for core_favourites. 19 * 20 * @package core_favourites 21 * @category test 22 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 use \core_privacy\tests\provider_testcase; 29 use \core_favourites\privacy\provider; 30 use \core_privacy\local\request\transform; 31 32 /** 33 * Unit tests for favourites/classes/privacy/provider 34 * 35 * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com> 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class privacy_test extends provider_testcase { 39 40 public function setUp(): void { 41 $this->resetAfterTest(true); 42 } 43 44 /** 45 * Helper to set up some sample users and courses. 46 */ 47 protected function set_up_courses_and_users() { 48 $user1 = self::getDataGenerator()->create_user(); 49 $user1context = \context_user::instance($user1->id); 50 $user2 = self::getDataGenerator()->create_user(); 51 $user2context = \context_user::instance($user2->id); 52 $course1 = self::getDataGenerator()->create_course(); 53 $course2 = self::getDataGenerator()->create_course(); 54 $course1context = context_course::instance($course1->id); 55 $course2context = context_course::instance($course2->id); 56 return [$user1, $user2, $user1context, $user2context, $course1context, $course2context]; 57 } 58 59 /** 60 * Test confirming that contexts of favourited items can be added to the contextlist. 61 */ 62 public function test_add_contexts_for_userid() { 63 list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users(); 64 65 // Favourite 2 courses for user1 and 1 course for user2, all at the site context. 66 $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context); 67 $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context); 68 $systemcontext = context_system::instance(); 69 $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $systemcontext); 70 $ufservice1->create_favourite('core_course', 'courses', $course2context->instanceid, $systemcontext); 71 $ufservice2->create_favourite('core_course', 'courses', $course2context->instanceid, $systemcontext); 72 $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'courses')); 73 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses')); 74 75 // Now, just for variety, let's assume you can favourite a course at user context, and do so for user1. 76 $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $user1context); 77 78 // Now, ask the favourites privacy api to export contexts for favourites of the type we just created, for user1. 79 $contextlist = new \core_privacy\local\request\contextlist(); 80 \core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $user1->id, 'core_course', 'courses'); 81 82 // Verify we have two contexts in the list for user1. 83 $this->assertCount(2, $contextlist->get_contextids()); 84 85 // And verify we only have the system context returned for user2. 86 $contextlist = new \core_privacy\local\request\contextlist(); 87 \core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $user2->id, 'core_course', 'courses'); 88 $this->assertCount(1, $contextlist->get_contextids()); 89 } 90 91 /** 92 * Test deletion of user favourites based on an approved_contextlist and component area. 93 */ 94 public function test_delete_favourites_for_user() { 95 list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users(); 96 97 // Favourite 2 courses for user1 and 1 course for user2, all at the user context. 98 $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context); 99 $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context); 100 $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $user1context); 101 $ufservice1->create_favourite('core_course', 'courses', $course2context->instanceid, $user1context); 102 $ufservice2->create_favourite('core_course', 'courses', $course2context->instanceid, $user2context); 103 $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'courses')); 104 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses')); 105 106 // Now, delete the favourites for user1 only. 107 $approvedcontextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_course', [$user1context->id]); 108 provider::delete_favourites_for_user($approvedcontextlist, 'core_course', 'courses'); 109 110 // Verify that we have no favourite courses for user1 but that the records are in tact for user2. 111 $this->assertCount(0, $ufservice1->find_favourites_by_type('core_course', 'courses')); 112 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses')); 113 } 114 115 public function test_delete_favourites_for_all_users() { 116 list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users(); 117 118 // Favourite 2 course modules for user1 and 1 course module for user2 all in course 1 context. 119 $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context); 120 $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context); 121 $ufservice1->create_favourite('core_course', 'modules', 1, $course1context); 122 $ufservice1->create_favourite('core_course', 'modules', 2, $course1context); 123 $ufservice2->create_favourite('core_course', 'modules', 3, $course1context); 124 125 // Now, favourite a different course module for user2 in course 2. 126 $ufservice2->create_favourite('core_course', 'modules', 5, $course2context); 127 128 $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'modules')); 129 $this->assertCount(2, $ufservice2->find_favourites_by_type('core_course', 'modules')); 130 131 // Now, delete all course module favourites in the 'course1' context only. 132 provider::delete_favourites_for_all_users($course1context, 'core_course', 'modules'); 133 134 // Verify that only a single favourite for user1 in course 1 remains. 135 $this->assertCount(0, $ufservice1->find_favourites_by_type('core_course', 'modules')); 136 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'modules')); 137 } 138 139 /** 140 * Test confirming that user ID's of favourited items can be added to the userlist. 141 */ 142 public function test_add_userids_for_context() { 143 list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users(); 144 145 // Favourite 2 courses for user1 and 1 course for user2, all at the site context. 146 $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context); 147 $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context); 148 $systemcontext = context_system::instance(); 149 $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $systemcontext); 150 $ufservice1->create_favourite('core_course', 'courses', $course2context->instanceid, $systemcontext); 151 $ufservice2->create_favourite('core_course', 'courses', $course2context->instanceid, $systemcontext); 152 $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'courses')); 153 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses')); 154 155 // Now, just for variety, let's assume you can favourite a course at user context, and do so for user1. 156 $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $user1context); 157 158 // Now, ask the favourites privacy api to export userids for favourites of the type we just created, in the system context. 159 $userlist = new \core_privacy\local\request\userlist($systemcontext, 'core_course'); 160 provider::add_userids_for_context($userlist, 'courses'); 161 // Verify we have two userids in the list for system context. 162 $this->assertCount(2, $userlist->get_userids()); 163 $expected = [ 164 $user1->id, 165 $user2->id 166 ]; 167 $this->assertEqualsCanonicalizing($expected, $userlist->get_userids()); 168 169 // Ask the favourites privacy api to export userids for favourites of the type we just created, in the user1 context. 170 $userlist = new \core_privacy\local\request\userlist($user1context, 'core_course'); 171 provider::add_userids_for_context($userlist, 'courses'); 172 // Verify we have one userid in the list for user1 context. 173 $this->assertCount(1, $userlist->get_userids()); 174 $expected = [$user1->id]; 175 $this->assertEquals($expected, $userlist->get_userids()); 176 177 // Ask the favourites privacy api to export userids for favourites of the type we just created, in the user2 context. 178 $userlist = new \core_privacy\local\request\userlist($user2context, 'core_favourites'); 179 provider::add_userids_for_context($userlist, 'core_course', 'courses'); 180 // Verify we do not have any userids in the list for user2 context. 181 $this->assertCount(0, $userlist->get_userids()); 182 } 183 184 /** 185 * Test deletion of user favourites based on an approved_userlist, component area and item type. 186 */ 187 public function test_delete_favourites_for_userlist() { 188 list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users(); 189 190 // Favourite 2 courses for user1 and 1 course for user2. 191 $systemcontext = context_system::instance(); 192 $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context); 193 $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context); 194 $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $systemcontext); 195 $ufservice1->create_favourite('core_course', 'courses', $course2context->instanceid, $user1context); 196 $ufservice2->create_favourite('core_course', 'courses', $course2context->instanceid, $systemcontext); 197 $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'courses')); 198 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses')); 199 200 // Ask the favourites privacy api to export userids for favourites of the type we just created, in the system context. 201 $userlist1 = new \core_privacy\local\request\userlist($systemcontext, 'core_course'); 202 provider::add_userids_for_context($userlist1, 'courses'); 203 // Verify we have two userids in the list for system context. 204 $this->assertCount(2, $userlist1->get_userids()); 205 206 // Ask the favourites privacy api to export userids for favourites of the type we just created, in the user1 context. 207 $userlist2 = new \core_privacy\local\request\userlist($user1context, 'core_course'); 208 provider::add_userids_for_context($userlist2, 'courses'); 209 // Verify we have one userid in the list for user1 context. 210 $this->assertCount(1, $userlist2->get_userids()); 211 212 // Now, delete the favourites for user1 only in the system context. 213 $approveduserlist = new \core_privacy\local\request\approved_userlist($systemcontext, 'core_course', 214 [$user1->id]); 215 provider::delete_favourites_for_userlist($approveduserlist, 'courses'); 216 // Ensure user1's data was deleted and user2 is still returned for system context. 217 $userlist1 = new \core_privacy\local\request\userlist($systemcontext, 'core_course'); 218 provider::add_userids_for_context($userlist1, 'courses'); 219 $this->assertCount(1, $userlist1->get_userids()); 220 // Verify that user2 is still in the list for system context. 221 $expected = [$user2->id]; 222 $this->assertEquals($expected, $userlist1->get_userids()); 223 // Verify that the data of user1 was not deleted in the user1context. 224 $userlist2 = new \core_privacy\local\request\userlist($user1context, 'core_course'); 225 provider::add_userids_for_context($userlist2, 'courses'); 226 $expected = [$user1->id]; 227 $this->assertEquals($expected, $userlist2->get_userids()); 228 229 // Now, delete the favourites for user2 only in the user1 context. 230 // Make sure favourites are only being deleted in the right context. 231 $approveduserlist = new \core_privacy\local\request\approved_userlist($user1context, 'core_course', 232 [$user2->id]); 233 provider::delete_favourites_for_userlist($approveduserlist, 'courses'); 234 // Verify we have one userid in the list for system context. 235 $userlist2 = new \core_privacy\local\request\userlist($systemcontext, 'core_course'); 236 provider::add_userids_for_context($userlist2, 'courses'); 237 $this->assertCount(1, $userlist2->get_userids()); 238 // Verify that user2 is still in the list for system context. 239 $expected = [$user2->id]; 240 $this->assertEquals($expected, $userlist2->get_userids()); 241 242 // Verify that user1 is still present in the list for user1 context. 243 $userlist3 = new \core_privacy\local\request\userlist($user1context, 'core_course'); 244 provider::add_userids_for_context($userlist3, 'courses'); 245 $this->assertCount(1, $userlist3->get_userids()); 246 // Verify that user1 is still in the list for user1 context. 247 $expected = [$user1->id]; 248 $this->assertEquals($expected, $userlist3->get_userids()); 249 } 250 251 /** 252 * Test fetching the favourites data for a specified user in a specified component, item type and item ID. 253 */ 254 public function test_get_favourites_info_for_user() { 255 list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users(); 256 257 // Favourite 2 courses for user1 and 1 course for user2. 258 $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context); 259 $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context); 260 $coursefavourite1 = $ufservice1->create_favourite('core_course', 'courses', 261 $course1context->instanceid, $course1context); 262 $this->waitForSecond(); 263 $coursefavourite2 = $ufservice1->create_favourite('core_course', 'courses', 264 $course2context->instanceid, $course2context); 265 $this->waitForSecond(); 266 $coursefavourite3 = $ufservice2->create_favourite('core_course', 'courses', 267 $course2context->instanceid, $course2context); 268 $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'courses')); 269 $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses')); 270 271 // Get the favourites info for user1 in the course1 context. 272 $favouriteinfo1 = (object) provider::get_favourites_info_for_user($user1->id, $course1context, 273 'core_course', 'courses', $course1context->instanceid); 274 // Ensure the correct data has been returned. 275 $this->assertEquals(transform::yesno(true), $favouriteinfo1->starred); 276 $this->assertEquals('', $favouriteinfo1->ordering); 277 $this->assertEquals(transform::datetime($coursefavourite1->timecreated), $favouriteinfo1->timecreated); 278 $this->assertEquals(transform::datetime($coursefavourite1->timemodified), $favouriteinfo1->timemodified); 279 280 // Get the favourites info for user1 in the course2 context. 281 $favouriteinfo2 = (object) provider::get_favourites_info_for_user($user1->id, $course2context, 282 'core_course', 'courses', $course2context->instanceid); 283 // Ensure the correct data has been returned. 284 $this->assertEquals(transform::yesno(true), $favouriteinfo2->starred); 285 $this->assertEquals('', $favouriteinfo2->ordering); 286 $this->assertEquals(transform::datetime($coursefavourite2->timecreated), $favouriteinfo2->timecreated); 287 $this->assertEquals(transform::datetime($coursefavourite2->timemodified), $favouriteinfo2->timemodified); 288 289 // Get the favourites info for user2 in the course2 context. 290 $favouriteinfo3 = (object) provider::get_favourites_info_for_user($user2->id, $course2context, 291 'core_course', 'courses', $course2context->instanceid); 292 // Ensure the correct data has been returned. 293 $this->assertEquals(transform::yesno(true), $favouriteinfo3->starred); 294 $this->assertEquals('', $favouriteinfo3->ordering); 295 $this->assertEquals(transform::datetime($coursefavourite3->timecreated), $favouriteinfo3->timecreated); 296 $this->assertEquals(transform::datetime($coursefavourite3->timemodified), $favouriteinfo3->timemodified); 297 298 // Get the favourites info for user2 in the course1 context (user2 has not favourited course1). 299 $favouriteinfo4 = provider::get_favourites_info_for_user($user2->id, $course1context, 300 'core_course', 'courses', $course1context->instanceid); 301 // Ensure that data has not been returned. 302 $this->assertEmpty($favouriteinfo4); 303 } 304 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body