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 tests for core_tag. 19 * 20 * @package core_comment 21 * @category test 22 * @copyright 2018 Zig Tan <zig@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 namespace core_tag\privacy; 26 27 defined('MOODLE_INTERNAL') || die(); 28 global $CFG; 29 30 require_once($CFG->dirroot . '/tag/lib.php'); 31 32 use core_privacy\tests\provider_testcase; 33 use core_privacy\local\request\writer; 34 use core_tag\privacy\provider; 35 use core_privacy\local\request\approved_userlist; 36 37 /** 38 * Unit tests for tag/classes/privacy/policy 39 * 40 * @copyright 2018 Zig Tan <zig@moodle.com> 41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 42 */ 43 class provider_test extends provider_testcase { 44 45 /** 46 * Check the exporting of tags for a user id in a context. 47 */ 48 public function test_export_tags() { 49 global $DB; 50 51 $this->resetAfterTest(true); 52 53 // Create a user to perform tagging. 54 $user = $this->getDataGenerator()->create_user(); 55 $this->setUser($user); 56 57 // Create a course to tag. 58 $course = $this->getDataGenerator()->create_course(); 59 $context = \context_course::instance($course->id); 60 $subcontext = []; 61 62 // Create three dummy tags and tag instances. 63 $dummytags = [ 'Tag 1', 'Tag 2', 'Tag 3' ]; 64 \core_tag_tag::set_item_tags('core_course', 'course', $course->id, \context_course::instance($course->id), 65 $dummytags, $user->id); 66 67 // Get the tag instances that should have been created. 68 $taginstances = $DB->get_records('tag_instance', array('itemtype' => 'course', 'itemid' => $course->id)); 69 $this->assertCount(count($dummytags), $taginstances); 70 71 // Check tag instances match the component and context. 72 foreach ($taginstances as $taginstance) { 73 $this->assertEquals('core_course', $taginstance->component); 74 $this->assertEquals(\context_course::instance($course->id)->id, $taginstance->contextid); 75 } 76 77 // Retrieve tags only for this user. 78 provider::export_item_tags($user->id, $context, $subcontext, 'core_course', 'course', $course->id, true); 79 80 $writer = writer::with_context($context); 81 $this->assertTrue($writer->has_any_data()); 82 83 $exportedtags = $writer->get_related_data($subcontext, 'tags'); 84 $this->assertCount(count($dummytags), $exportedtags); 85 86 // Check the exported tag's rawname is found in the initial dummy tags. 87 foreach ($exportedtags as $exportedtag) { 88 $this->assertContains($exportedtag, $dummytags); 89 } 90 } 91 92 /** 93 * Test method delete_item_tags(). 94 */ 95 public function test_delete_item_tags() { 96 global $DB; 97 98 $this->resetAfterTest(true); 99 100 // Create a course to tag. 101 $course1 = $this->getDataGenerator()->create_course(); 102 $context1 = \context_course::instance($course1->id); 103 $course2 = $this->getDataGenerator()->create_course(); 104 $context2 = \context_course::instance($course2->id); 105 106 // Tag courses. 107 \core_tag_tag::set_item_tags('core_course', 'course', $course1->id, $context1, ['Tag 1', 'Tag 2', 'Tag 3']); 108 \core_tag_tag::set_item_tags('core_course', 'course', $course2->id, $context2, ['Tag 1', 'Tag 2']); 109 110 $expectedtagcount = $DB->count_records('tag_instance'); 111 // Delete tags for course1. 112 provider::delete_item_tags($context1, 'core_course', 'course'); 113 $expectedtagcount -= 3; 114 $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); 115 116 // Delete tags for course2. Use wrong itemid. 117 provider::delete_item_tags($context2, 'core_course', 'course', $course1->id); 118 $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); 119 120 // Use correct itemid. 121 provider::delete_item_tags($context2, 'core_course', 'course', $course2->id); 122 $expectedtagcount -= 2; 123 $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); 124 } 125 126 /** 127 * Test method delete_item_tags() with userid. 128 */ 129 public function test_delete_item_tags_with_userid() { 130 global $DB; 131 132 $this->resetAfterTest(true); 133 // Create a course to tag. 134 $course = $this->getDataGenerator()->create_course(); 135 $context = \context_course::instance($course->id); 136 137 // Create a user to perform tagging. 138 $user = $this->getDataGenerator()->create_user(); 139 $this->setUser($user); 140 141 // Tag courses. 142 \core_tag_tag::set_item_tags('core_course', 'course', $course->id, $context, ['Tag 1', 'Tag 2'], $user->id); 143 $expectedtagcount = $DB->count_records('tag_instance'); 144 145 // Delete tags for course. Use wrong userid. 146 provider::delete_item_tags($context, 'core_course', 'course', null, 1); 147 $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); 148 149 $expectedtagcount -= 2; 150 // Delete tags for course. Use correct userid. 151 provider::delete_item_tags($context, 'core_course', 'course', null, $user->id); 152 $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); 153 } 154 155 /** 156 * Test method delete_item_tags_select(). 157 */ 158 public function test_delete_item_tags_select() { 159 global $DB; 160 161 $this->resetAfterTest(true); 162 163 // Create a course to tag. 164 $course1 = $this->getDataGenerator()->create_course(); 165 $context1 = \context_course::instance($course1->id); 166 $course2 = $this->getDataGenerator()->create_course(); 167 $context2 = \context_course::instance($course2->id); 168 169 // Tag courses. 170 \core_tag_tag::set_item_tags('core_course', 'course', $course1->id, $context1, ['Tag 1', 'Tag 2', 'Tag 3']); 171 \core_tag_tag::set_item_tags('core_course', 'course', $course2->id, $context2, ['Tag 1', 'Tag 2']); 172 173 $expectedtagcount = $DB->count_records('tag_instance'); 174 // Delete tags for course1. 175 list($sql, $params) = $DB->get_in_or_equal([$course1->id, $course2->id], SQL_PARAMS_NAMED); 176 provider::delete_item_tags_select($context1, 'core_course', 'course', $sql, $params); 177 $expectedtagcount -= 3; 178 $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); 179 180 // Delete tags for course2. 181 provider::delete_item_tags_select($context2, 'core_course', 'course', $sql, $params); 182 $expectedtagcount -= 2; 183 $this->assertEquals($expectedtagcount, $DB->count_records('tag_instance')); 184 } 185 186 protected function set_up_tags() { 187 global $CFG; 188 require_once($CFG->dirroot.'/user/editlib.php'); 189 190 $this->resetAfterTest(true); 191 192 $user1 = $this->getDataGenerator()->create_user(); 193 $user2 = $this->getDataGenerator()->create_user(); 194 195 $this->setUser($user1); 196 useredit_update_interests($user1, ['Birdwatching', 'Computers']); 197 198 $this->setUser($user2); 199 useredit_update_interests($user2, ['computers']); 200 201 $this->setAdminUser(); 202 203 $tag = \core_tag_tag::get_by_name(0, 'computers', '*'); 204 $tag->update(['description' => '<img src="@@PLUGINFILE@@/computer.jpg">']); 205 get_file_storage()->create_file_from_string([ 206 'contextid' => \context_system::instance()->id, 207 'component' => 'tag', 208 'filearea' => 'description', 209 'itemid' => $tag->id, 210 'filepath' => '/', 211 'filename' => 'computer.jpg' 212 ], "jpg:image"); 213 214 return [$user1, $user2]; 215 } 216 217 public function test_export_item_tags() { 218 list($user1, $user2) = $this->set_up_tags(); 219 $this->assertEquals([\context_system::instance()->id], 220 provider::get_contexts_for_userid($user1->id)->get_contextids()); 221 $this->assertEmpty(provider::get_contexts_for_userid($user2->id)->get_contextids()); 222 } 223 224 public function test_delete_data_for_user() { 225 global $DB; 226 list($user1, $user2) = $this->set_up_tags(); 227 $context = \context_system::instance(); 228 $this->assertEquals(2, $DB->count_records('tag', [])); 229 $this->assertEquals(0, $DB->count_records('tag', ['userid' => 0])); 230 provider::delete_data_for_user(new \core_privacy\local\request\approved_contextlist($user2, 'core_tag', [$context->id])); 231 $this->assertEquals(2, $DB->count_records('tag', [])); 232 $this->assertEquals(0, $DB->count_records('tag', ['userid' => 0])); 233 provider::delete_data_for_user(new \core_privacy\local\request\approved_contextlist($user1, 'core_tag', [$context->id])); 234 $this->assertEquals(2, $DB->count_records('tag', [])); 235 $this->assertEquals(2, $DB->count_records('tag', ['userid' => 0])); 236 } 237 238 public function test_delete_data_for_all_users_in_context() { 239 global $DB; 240 $course = $this->getDataGenerator()->create_course(); 241 list($user1, $user2) = $this->set_up_tags(); 242 $this->assertEquals(2, $DB->count_records('tag', [])); 243 $this->assertEquals(3, $DB->count_records('tag_instance', [])); 244 provider::delete_data_for_all_users_in_context(\context_course::instance($course->id)); 245 $this->assertEquals(2, $DB->count_records('tag', [])); 246 $this->assertEquals(3, $DB->count_records('tag_instance', [])); 247 provider::delete_data_for_all_users_in_context(\context_system::instance()); 248 $this->assertEquals(0, $DB->count_records('tag', [])); 249 $this->assertEquals(0, $DB->count_records('tag_instance', [])); 250 } 251 252 public function test_export_data_for_user() { 253 global $DB; 254 list($user1, $user2) = $this->set_up_tags(); 255 $context = \context_system::instance(); 256 provider::export_user_data(new \core_privacy\local\request\approved_contextlist($user2, 'core_tag', [$context->id])); 257 $this->assertFalse(writer::with_context($context)->has_any_data()); 258 259 $tagids = array_values(array_map(function($tag) { 260 return $tag->id; 261 }, \core_tag_tag::get_by_name_bulk(\core_tag_collection::get_default(), ['Birdwatching', 'Computers']))); 262 263 provider::export_user_data(new \core_privacy\local\request\approved_contextlist($user1, 'core_tag', [$context->id])); 264 $writer = writer::with_context($context); 265 266 $data = $writer->get_data(['Tags', $tagids[0]]); 267 $files = $writer->get_files(['Tags', $tagids[0]]); 268 $this->assertEquals('Birdwatching', $data->rawname); 269 $this->assertEmpty($files); 270 271 $data = $writer->get_data(['Tags', $tagids[1]]); 272 $files = $writer->get_files(['Tags', $tagids[1]]); 273 $this->assertEquals('Computers', $data->rawname); 274 $this->assertEquals(['computer.jpg'], array_keys($files)); 275 } 276 277 /** 278 * Test that only users within a system context are fetched. 279 */ 280 public function test_get_users_in_context() { 281 $component = 'core_tag'; 282 283 $user1 = $this->set_up_tags()[0]; 284 $systemcontext = \context_system::instance(); 285 286 $userlist1 = new \core_privacy\local\request\userlist($systemcontext, $component); 287 provider::get_users_in_context($userlist1); 288 $this->assertCount(1, $userlist1); 289 $expected = [$user1->id]; 290 $actual = $userlist1->get_userids(); 291 $this->assertEquals($expected, $actual); 292 293 // The list of users within the a context other than system context should be empty. 294 $usercontext1 = \context_user::instance($user1->id); 295 $userlist2 = new \core_privacy\local\request\userlist($usercontext1, $component); 296 provider::get_users_in_context($userlist2); 297 $this->assertCount(0, $userlist2); 298 } 299 300 /** 301 * Test that data for users in approved userlist is deleted. 302 */ 303 public function test_delete_data_for_users() { 304 $component = 'core_tag'; 305 306 list($user1, $user2) = $this->set_up_tags(); 307 $usercontext1 = \context_user::instance($user1->id); 308 $user3 = $this->getDataGenerator()->create_user(); 309 $systemcontext = \context_system::instance(); 310 311 $this->setUser($user2); 312 useredit_update_interests($user2, ['basketball']); 313 314 $this->setUser($user3); 315 useredit_update_interests($user3, ['soccer']); 316 317 $userlist = new \core_privacy\local\request\userlist($systemcontext, $component); 318 provider::get_users_in_context($userlist); 319 $this->assertCount(3, $userlist); 320 $this->assertTrue(in_array($user1->id, $userlist->get_userids())); 321 $this->assertTrue(in_array($user2->id, $userlist->get_userids())); 322 $this->assertTrue(in_array($user3->id, $userlist->get_userids())); 323 324 // Data should not be deleted in contexts other than system context. 325 // Convert $userlist into an approved_contextlist. 326 $approvedlist = new approved_userlist($usercontext1, $component, $userlist->get_userids()); 327 // Delete using delete_data_for_user. 328 provider::delete_data_for_users($approvedlist); 329 // Re-fetch users in systemcontext. 330 $userlist = new \core_privacy\local\request\userlist($systemcontext, $component); 331 provider::get_users_in_context($userlist); 332 // The user data in systemcontext should not be deleted. 333 $this->assertCount(3, $userlist); 334 335 // Add user1 and user2 into an approved_contextlist. 336 $approvedlist = new approved_userlist($systemcontext, $component, [$user1->id, $user2->id]); 337 // Delete using delete_data_for_user. 338 provider::delete_data_for_users($approvedlist); 339 // Re-fetch users in systemcontext. 340 $userlist = new \core_privacy\local\request\userlist($systemcontext, $component); 341 provider::get_users_in_context($userlist); 342 // The approved user data in systemcontext should be deleted. 343 // The user list should return user3. 344 $this->assertCount(1, $userlist); 345 $this->assertTrue(in_array($user3->id, $userlist->get_userids())); 346 } 347 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body