Differences Between: [Versions 311 and 402] [Versions 311 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 * Unit tests for privacy. 19 * 20 * @package core_analytics 21 * @copyright 2018 David MonllaĆ³ {@link http://www.davidmonllao.com} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 namespace core_analytics\privacy; 25 26 use core_analytics\privacy\provider; 27 use core_privacy\local\request\transform; 28 use core_privacy\local\request\writer; 29 use core_privacy\local\request\approved_contextlist; 30 use core_privacy\local\request\approved_userlist; 31 32 defined('MOODLE_INTERNAL') || die(); 33 34 require_once (__DIR__ . '/../fixtures/test_indicator_max.php'); 35 require_once (__DIR__ . '/../fixtures/test_indicator_min.php'); 36 require_once (__DIR__ . '/../fixtures/test_target_site_users.php'); 37 require_once (__DIR__ . '/../fixtures/test_target_course_users.php'); 38 39 /** 40 * Unit tests for privacy. 41 * 42 * @package core_analytics 43 * @copyright 2018 David MonllaĆ³ {@link http://www.davidmonllao.com} 44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 45 */ 46 class provider_test extends \core_privacy\tests\provider_testcase { 47 48 public function setUp(): void { 49 50 $this->resetAfterTest(true); 51 $this->setAdminUser(); 52 53 $timesplittingid = '\core\analytics\time_splitting\single_range'; 54 $target = \core_analytics\manager::get_target('test_target_site_users'); 55 $indicators = array('test_indicator_max'); 56 foreach ($indicators as $key => $indicator) { 57 $indicators[$key] = \core_analytics\manager::get_indicator($indicator); 58 } 59 $this->model1 = \core_analytics\model::create($target, $indicators, $timesplittingid); 60 $this->modelobj1 = $this->model1->get_model_obj(); 61 62 $target = \core_analytics\manager::get_target('test_target_course_users'); 63 $indicators = array('test_indicator_min'); 64 foreach ($indicators as $key => $indicator) { 65 $indicators[$key] = \core_analytics\manager::get_indicator($indicator); 66 } 67 $this->model2 = \core_analytics\model::create($target, $indicators, $timesplittingid); 68 $this->modelobj2 = $this->model1->get_model_obj(); 69 70 $this->u1 = $this->getDataGenerator()->create_user(['firstname' => 'a111111111111', 'lastname' => 'a']); 71 $this->u2 = $this->getDataGenerator()->create_user(['firstname' => 'a222222222222', 'lastname' => 'a']); 72 $this->u3 = $this->getDataGenerator()->create_user(['firstname' => 'b333333333333', 'lastname' => 'b']); 73 $this->u4 = $this->getDataGenerator()->create_user(['firstname' => 'b444444444444', 'lastname' => 'b']); 74 $this->u5 = $this->getdatagenerator()->create_user(['firstname' => 'a555555555555', 'lastname' => 'a']); 75 $this->u6 = $this->getdatagenerator()->create_user(['firstname' => 'a666666666666', 'lastname' => 'a']); 76 $this->u7 = $this->getdatagenerator()->create_user(['firstname' => 'b777777777777', 'lastname' => 'b']); 77 $this->u8 = $this->getDataGenerator()->create_user(['firstname' => 'b888888888888', 'lastname' => 'b']); 78 79 $this->c1 = $this->getDataGenerator()->create_course(['visible' => false]); 80 $this->c2 = $this->getDataGenerator()->create_course(); 81 82 $this->getDataGenerator()->enrol_user($this->u1->id, $this->c1->id, 'student'); 83 $this->getDataGenerator()->enrol_user($this->u2->id, $this->c1->id, 'student'); 84 $this->getDataGenerator()->enrol_user($this->u3->id, $this->c1->id, 'student'); 85 $this->getDataGenerator()->enrol_user($this->u4->id, $this->c1->id, 'student'); 86 $this->getDataGenerator()->enrol_user($this->u5->id, $this->c1->id, 'student'); 87 $this->getDataGenerator()->enrol_user($this->u6->id, $this->c1->id, 'student'); 88 $this->getDataGenerator()->enrol_user($this->u7->id, $this->c1->id, 'student'); 89 $this->getDataGenerator()->enrol_user($this->u8->id, $this->c1->id, 'student'); 90 $this->getDataGenerator()->enrol_user($this->u1->id, $this->c2->id, 'student'); 91 $this->getDataGenerator()->enrol_user($this->u2->id, $this->c2->id, 'student'); 92 $this->getDataGenerator()->enrol_user($this->u3->id, $this->c2->id, 'student'); 93 $this->getDataGenerator()->enrol_user($this->u4->id, $this->c2->id, 'student'); 94 $this->getDataGenerator()->enrol_user($this->u5->id, $this->c2->id, 'student'); 95 $this->getDataGenerator()->enrol_user($this->u6->id, $this->c2->id, 'student'); 96 $this->getDataGenerator()->enrol_user($this->u7->id, $this->c2->id, 'student'); 97 $this->getDataGenerator()->enrol_user($this->u8->id, $this->c2->id, 'student'); 98 99 $this->setAdminUser(); 100 101 $this->model1->enable(); 102 $this->model1->train(); 103 $this->model1->predict(); 104 $this->model2->enable(); 105 $this->model2->train(); 106 $this->model2->predict(); 107 108 list($total, $predictions) = $this->model2->get_predictions(\context_course::instance($this->c1->id)); 109 110 $this->setUser($this->u3); 111 $prediction = reset($predictions); 112 $prediction->action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $this->model2->get_target()); 113 114 $this->setAdminUser(); 115 } 116 117 /** 118 * Test fetching users within a context. 119 */ 120 public function test_get_users_in_context() { 121 global $CFG; 122 123 $component = 'core_analytics'; 124 $course1context = \context_course::instance($this->c1->id); 125 $course2context = \context_course::instance($this->c2->id); 126 $systemcontext = \context_system::instance(); 127 $expected = [$this->u1->id, $this->u2->id, $this->u3->id, $this->u4->id, $this->u5->id, $this->u6->id, 128 $this->u7->id, $this->u8->id]; 129 130 // Check users exist in the relevant contexts. 131 $userlist = new \core_privacy\local\request\userlist($course1context, $component); 132 provider::get_users_in_context($userlist); 133 $actual = $userlist->get_userids(); 134 sort($actual); 135 $this->assertEquals($expected, $actual); 136 137 $userlist = new \core_privacy\local\request\userlist($course2context, $component); 138 provider::get_users_in_context($userlist); 139 $actual = $userlist->get_userids(); 140 sort($actual); 141 $this->assertEquals($expected, $actual); 142 143 // System context will also find guest and admin user, add to expected before testing. 144 $expected = array_merge($expected, [$CFG->siteguest, get_admin()->id]); 145 sort($expected); 146 147 $userlist = new \core_privacy\local\request\userlist($systemcontext, $component); 148 provider::get_users_in_context($userlist); 149 $actual = $userlist->get_userids(); 150 sort($actual); 151 $this->assertEquals($expected, $actual); 152 } 153 154 /** 155 * Test delete a context. 156 * 157 * @return null 158 */ 159 public function test_delete_context_data() { 160 global $DB; 161 162 // We have 4 predictions for model1 and 8 predictions for model2. 163 $this->assertEquals(12, $DB->count_records('analytics_predictions')); 164 $this->assertEquals(26, $DB->count_records('analytics_indicator_calc')); 165 166 // We have 1 prediction action. 167 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions')); 168 169 $coursecontext = \context_course::instance($this->c1->id); 170 171 // Delete the course that was used for prediction. 172 provider::delete_data_for_all_users_in_context($coursecontext); 173 174 // The course1 predictions are deleted. 175 $this->assertEquals(8, $DB->count_records('analytics_predictions')); 176 177 // Calculations related to that context are deleted. 178 $this->assertEmpty($DB->count_records('analytics_indicator_calc', ['contextid' => $coursecontext->id])); 179 180 // The deleted context prediction actions are deleted as well. 181 $this->assertEquals(0, $DB->count_records('analytics_prediction_actions')); 182 } 183 184 /** 185 * Test delete a user. 186 * 187 * @return null 188 */ 189 public function test_delete_user_data() { 190 global $DB; 191 192 $usercontexts = provider::get_contexts_for_userid($this->u3->id); 193 $contextlist = new \core_privacy\local\request\approved_contextlist($this->u3, 'core_analytics', 194 $usercontexts->get_contextids()); 195 provider::delete_data_for_user($contextlist); 196 197 // The site level prediction for u3 was deleted. 198 $this->assertEquals(9, $DB->count_records('analytics_predictions')); 199 $this->assertEquals(0, $DB->count_records('analytics_prediction_actions')); 200 201 $usercontexts = provider::get_contexts_for_userid($this->u1->id); 202 $contextlist = new \core_privacy\local\request\approved_contextlist($this->u1, 'core_analytics', 203 $usercontexts->get_contextids()); 204 provider::delete_data_for_user($contextlist); 205 // We have nothing for u1. 206 $this->assertEquals(9, $DB->count_records('analytics_predictions')); 207 208 $usercontexts = provider::get_contexts_for_userid($this->u4->id); 209 $contextlist = new \core_privacy\local\request\approved_contextlist($this->u4, 'core_analytics', 210 $usercontexts->get_contextids()); 211 provider::delete_data_for_user($contextlist); 212 $this->assertEquals(6, $DB->count_records('analytics_predictions')); 213 } 214 215 /** 216 * Test deleting multiple users in a context. 217 */ 218 public function test_delete_data_for_users() { 219 global $DB; 220 221 $component = 'core_analytics'; 222 $course1context = \context_course::instance($this->c1->id); 223 $course2context = \context_course::instance($this->c2->id); 224 $systemcontext = \context_system::instance(); 225 226 // Ensure all records exist in expected contexts. 227 $expectedcontexts = [$course1context->id, $course2context->id, $systemcontext->id]; 228 sort($expectedcontexts); 229 230 $actualcontexts = [ 231 $this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(), 232 $this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(), 233 $this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(), 234 $this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(), 235 $this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(), 236 $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(), 237 $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(), 238 $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(), 239 ]; 240 241 foreach ($actualcontexts as $userid => $unused) { 242 sort($actualcontexts[$userid]); 243 $this->assertEquals($expectedcontexts, $actualcontexts[$userid]); 244 } 245 246 // Test initial record counts are as expected. 247 $this->assertEquals(12, $DB->count_records('analytics_predictions')); 248 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions')); 249 $this->assertEquals(26, $DB->count_records('analytics_indicator_calc')); 250 251 // Delete u1 and u3 from system context. 252 $approveduserids = [$this->u1->id, $this->u3->id]; 253 $approvedlist = new approved_userlist($systemcontext, $component, $approveduserids); 254 provider::delete_data_for_users($approvedlist); 255 256 // Ensure u1 and u3 system context data deleted only. 257 $expectedcontexts = [ 258 $this->u1->id => [$course1context->id, $course2context->id], 259 $this->u2->id => [$systemcontext->id, $course1context->id, $course2context->id], 260 $this->u3->id => [$course1context->id, $course2context->id], 261 $this->u4->id => [$systemcontext->id, $course1context->id, $course2context->id], 262 $this->u5->id => [$systemcontext->id, $course1context->id, $course2context->id], 263 $this->u6->id => [$systemcontext->id, $course1context->id, $course2context->id], 264 $this->u7->id => [$systemcontext->id, $course1context->id, $course2context->id], 265 $this->u8->id => [$systemcontext->id, $course1context->id, $course2context->id], 266 ]; 267 268 $actualcontexts = [ 269 $this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(), 270 $this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(), 271 $this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(), 272 $this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(), 273 $this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(), 274 $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(), 275 $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(), 276 $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(), 277 ]; 278 279 foreach ($actualcontexts as $userid => $unused) { 280 sort($expectedcontexts[$userid]); 281 sort($actualcontexts[$userid]); 282 $this->assertEquals($expectedcontexts[$userid], $actualcontexts[$userid]); 283 } 284 285 // Test expected number of records have been deleted. 286 $this->assertEquals(11, $DB->count_records('analytics_predictions')); 287 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions')); 288 $this->assertEquals(24, $DB->count_records('analytics_indicator_calc')); 289 290 // Delete for all 8 users in course 2 context. 291 $approveduserids = [$this->u1->id, $this->u2->id, $this->u3->id, $this->u4->id, $this->u5->id, $this->u6->id, 292 $this->u7->id, $this->u8->id]; 293 $approvedlist = new approved_userlist($course2context, $component, $approveduserids); 294 provider::delete_data_for_users($approvedlist); 295 296 // Ensure all course 2 context data deleted for all 4 users. 297 $expectedcontexts = [ 298 $this->u1->id => [$course1context->id], 299 $this->u2->id => [$systemcontext->id, $course1context->id], 300 $this->u3->id => [$course1context->id], 301 $this->u4->id => [$systemcontext->id, $course1context->id], 302 $this->u5->id => [$systemcontext->id, $course1context->id], 303 $this->u6->id => [$systemcontext->id, $course1context->id], 304 $this->u7->id => [$systemcontext->id, $course1context->id], 305 $this->u8->id => [$systemcontext->id, $course1context->id], 306 ]; 307 308 $actualcontexts = [ 309 $this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(), 310 $this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(), 311 $this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(), 312 $this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(), 313 $this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(), 314 $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(), 315 $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(), 316 $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(), 317 ]; 318 319 foreach ($actualcontexts as $userid => $unused) { 320 sort($actualcontexts[$userid]); 321 sort($expectedcontexts[$userid]); 322 $this->assertEquals($expectedcontexts[$userid], $actualcontexts[$userid]); 323 } 324 325 // Test expected number of records have been deleted. 326 $this->assertEquals(7, $DB->count_records('analytics_predictions')); 327 $this->assertEquals(1, $DB->count_records('analytics_prediction_actions')); 328 $this->assertEquals(16, $DB->count_records('analytics_indicator_calc')); 329 330 $approveduserids = [$this->u3->id]; 331 $approvedlist = new approved_userlist($course1context, $component, $approveduserids); 332 provider::delete_data_for_users($approvedlist); 333 334 // Ensure all course 1 context data deleted for u3. 335 $expectedcontexts = [ 336 $this->u1->id => [$course1context->id], 337 $this->u2->id => [$systemcontext->id, $course1context->id], 338 $this->u3->id => [], 339 $this->u4->id => [$systemcontext->id, $course1context->id], 340 $this->u5->id => [$systemcontext->id, $course1context->id], 341 $this->u6->id => [$systemcontext->id, $course1context->id], 342 $this->u7->id => [$systemcontext->id, $course1context->id], 343 $this->u8->id => [$systemcontext->id, $course1context->id], 344 ]; 345 346 $actualcontexts = [ 347 $this->u1->id => provider::get_contexts_for_userid($this->u1->id)->get_contextids(), 348 $this->u2->id => provider::get_contexts_for_userid($this->u2->id)->get_contextids(), 349 $this->u3->id => provider::get_contexts_for_userid($this->u3->id)->get_contextids(), 350 $this->u4->id => provider::get_contexts_for_userid($this->u4->id)->get_contextids(), 351 $this->u5->id => provider::get_contexts_for_userid($this->u5->id)->get_contextids(), 352 $this->u6->id => provider::get_contexts_for_userid($this->u6->id)->get_contextids(), 353 $this->u7->id => provider::get_contexts_for_userid($this->u7->id)->get_contextids(), 354 $this->u8->id => provider::get_contexts_for_userid($this->u8->id)->get_contextids(), 355 ]; 356 foreach ($actualcontexts as $userid => $unused) { 357 sort($actualcontexts[$userid]); 358 sort($expectedcontexts[$userid]); 359 $this->assertEquals($expectedcontexts[$userid], $actualcontexts[$userid]); 360 } 361 362 // Test expected number of records have been deleted. 363 $this->assertEquals(6, $DB->count_records('analytics_predictions')); 364 $this->assertEquals(0, $DB->count_records('analytics_prediction_actions')); 365 $this->assertEquals(15, $DB->count_records('analytics_indicator_calc')); 366 } 367 368 /** 369 * Test export user data. 370 * 371 * @return null 372 */ 373 public function test_export_data() { 374 global $DB; 375 376 $system = \context_system::instance(); 377 list($total, $predictions) = $this->model1->get_predictions($system); 378 foreach ($predictions as $key => $prediction) { 379 if ($prediction->get_prediction_data()->sampleid !== $this->u3->id) { 380 $otheruserprediction = $prediction; 381 break; 382 } 383 } 384 $this->setUser($this->u3); 385 $otheruserprediction->action_executed(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $this->model1->get_target()); 386 $this->setAdminUser(); 387 388 $this->export_context_data_for_user($this->u3->id, $system, 'core_analytics'); 389 $writer = \core_privacy\local\request\writer::with_context($system); 390 $this->assertTrue($writer->has_any_data()); 391 392 $u3prediction = $DB->get_record('analytics_predictions', ['contextid' => $system->id, 'sampleid' => $this->u3->id]); 393 $data = $writer->get_data([get_string('analytics', 'analytics'), 394 get_string('privacy:metadata:analytics:predictions', 'analytics'), $u3prediction->id]); 395 $this->assertEquals(get_string('adminhelplogs'), $data->target); 396 $this->assertEquals(get_string('coresystem'), $data->context); 397 $this->assertEquals('firstname first char is not A', $data->prediction); 398 399 $u3calculation = $DB->get_record('analytics_indicator_calc', ['contextid' => $system->id, 'sampleid' => $this->u3->id]); 400 $data = $writer->get_data([get_string('analytics', 'analytics'), 401 get_string('privacy:metadata:analytics:indicatorcalc', 'analytics'), $u3calculation->id]); 402 $this->assertEquals('Allow stealth activities', $data->indicator); 403 $this->assertEquals(get_string('coresystem'), $data->context); 404 $this->assertEquals(get_string('yes'), $data->calculation); 405 406 $sql = "SELECT apa.id FROM {analytics_prediction_actions} apa 407 JOIN {analytics_predictions} ap ON ap.id = apa.predictionid 408 WHERE ap.contextid = :contextid AND apa.userid = :userid AND ap.modelid = :modelid"; 409 $params = ['contextid' => $system->id, 'userid' => $this->u3->id, 'modelid' => $this->model1->get_id()]; 410 $u3action = $DB->get_record_sql($sql, $params); 411 $data = $writer->get_data([get_string('analytics', 'analytics'), 412 get_string('privacy:metadata:analytics:predictionactions', 'analytics'), $u3action->id]); 413 $this->assertEquals(get_string('adminhelplogs'), $data->target); 414 $this->assertEquals(get_string('coresystem'), $data->context); 415 $this->assertEquals(\core_analytics\prediction::ACTION_INCORRECTLY_FLAGGED, $data->action); 416 417 } 418 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body