Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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 test for the event monitor
  19   *
  20   * @package    tool_monitor
  21   * @category   test
  22   * @copyright  2018 Adrian Greeve <adriangreeve.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace tool_monitor\privacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use tool_monitor\privacy\provider;
  30  use core_privacy\local\request\approved_contextlist;
  31  use core_privacy\local\request\approved_userlist;
  32  use core_privacy\tests\provider_testcase;
  33  
  34  /**
  35   * Privacy test for the event monitor
  36   *
  37   * @package    tool_monitor
  38   * @category   test
  39   * @copyright  2018 Adrian Greeve <adriangreeve.com>
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class provider_test extends provider_testcase {
  43  
  44      /**
  45       * Set up method.
  46       */
  47      public function setUp(): void {
  48          $this->resetAfterTest();
  49          // Enable monitor.
  50          set_config('enablemonitor', 1, 'tool_monitor');
  51      }
  52  
  53      /**
  54       * Assign a capability to $USER
  55       * The function creates a student $USER if $USER->id is empty
  56       *
  57       * @param string $capability capability name
  58       * @param int $contextid
  59       * @param int $roleid
  60       * @return int the role id - mainly returned for creation, so calling function can reuse it
  61       */
  62      public static function assign_user_capability($capability, $contextid, $roleid = null) {
  63          global $USER;
  64  
  65          // Create a new student $USER if $USER doesn't exist.
  66          if (empty($USER->id)) {
  67              $user  = self::getDataGenerator()->create_user();
  68              self::setUser($user);
  69          }
  70  
  71          if (empty($roleid)) {
  72              $roleid = create_role('Dummy role', 'dummyrole', 'dummy role description');
  73          }
  74  
  75          assign_capability($capability, CAP_ALLOW, $roleid, $contextid);
  76  
  77          role_assign($roleid, $USER->id, $contextid);
  78  
  79          accesslib_clear_all_caches_for_unit_testing();
  80  
  81          return $roleid;
  82      }
  83  
  84      /**
  85       * Test that a collection with data is returned when calling this function.
  86       */
  87      public function test_get_metadata() {
  88          $collection = new \core_privacy\local\metadata\collection('tool_monitor');
  89          $collection = provider::get_metadata($collection);
  90          $this->assertNotEmpty($collection);
  91      }
  92  
  93      /**
  94       * Check that a user context is returned if there is any user data for this user.
  95       */
  96      public function test_get_contexts_for_userid() {
  97          $user = $this->getDataGenerator()->create_user();
  98          $user2 = $this->getDataGenerator()->create_user();
  99          $usercontext = \context_user::instance($user->id);
 100          $usercontext2 = \context_user::instance($user2->id);
 101          $this->assertEmpty(provider::get_contexts_for_userid($user->id));
 102          $this->assertEmpty(provider::get_contexts_for_userid($user2->id));
 103  
 104          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 105  
 106          // Create a rule with this user.
 107          $this->setUser($user);
 108          $rule = $monitorgenerator->create_rule();
 109          $contextlist = provider::get_contexts_for_userid($user->id);
 110  
 111          // Check that we only get back one context.
 112          $this->assertCount(1, $contextlist);
 113  
 114          // Check that a context is returned for just creating a rule.
 115          $this->assertEquals($usercontext->id, $contextlist->get_contextids()[0]);
 116  
 117          $this->setUser($user2);
 118  
 119          $record = new \stdClass();
 120          $record->courseid = 0;
 121          $record->userid = $user2->id;
 122          $record->ruleid = $rule->id;
 123  
 124          $subscription = $monitorgenerator->create_subscription($record);
 125          $contextlist = provider::get_contexts_for_userid($user2->id);
 126  
 127          // Check that we only get back one context.
 128          $this->assertCount(1, $contextlist);
 129  
 130          // Check that a context is returned for just subscribing to a rule.
 131          $this->assertEquals($usercontext2->id, $contextlist->get_contextids()[0]);
 132      }
 133  
 134      /**
 135       * Check that the correct userlist is returned if there is any user data for this context.
 136       */
 137      public function test_get_users_in_context() {
 138          $component = 'tool_monitor';
 139          $user = $this->getDataGenerator()->create_user();
 140          $user2 = $this->getDataGenerator()->create_user();
 141          $usercontext = \context_user::instance($user->id);
 142          $usercontext2 = \context_user::instance($user2->id);
 143  
 144          $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
 145          provider::get_users_in_context($userlist);
 146          $this->assertEmpty($userlist);
 147  
 148          $userlist = new \core_privacy\local\request\userlist($usercontext2, $component);
 149          provider::get_users_in_context($userlist);
 150          $this->assertEmpty($userlist);
 151  
 152          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 153  
 154          // Create a rule with user.
 155          $this->setUser($user);
 156          $rule = $monitorgenerator->create_rule();
 157          $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
 158          provider::get_users_in_context($userlist);
 159  
 160          // Check that we only get back user.
 161          $userids = $userlist->get_userids();
 162          $this->assertCount(1, $userlist);
 163          $this->assertEquals($user->id, $userids[0]);
 164  
 165          // Create a subscription with user2.
 166          $this->setUser($user2);
 167  
 168          $record = new \stdClass();
 169          $record->courseid = 0;
 170          $record->userid = $user2->id;
 171          $record->ruleid = $rule->id;
 172  
 173          $subscription = $monitorgenerator->create_subscription($record);
 174          $userlist = new \core_privacy\local\request\userlist($usercontext2, $component);
 175          provider::get_users_in_context($userlist);
 176  
 177          // Check that user2 is returned for just subscribing to a rule.
 178          $userids = $userlist->get_userids();
 179          $this->assertCount(1, $userlist);
 180          $this->assertEquals($user2->id, $userids[0]);
 181      }
 182  
 183      /**
 184       * Test that user data is exported correctly.
 185       */
 186      public function test_export_user_data() {
 187          $user = $this->getDataGenerator()->create_user();
 188          $usercontext = \context_user::instance($user->id);
 189          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 190  
 191          $this->setUser($user);
 192          $rulerecord = (object)['name' => 'privacy rule'];
 193          $rule = $monitorgenerator->create_rule($rulerecord);
 194  
 195          $secondrulerecord = (object)['name' => 'privacy rule2'];
 196          $rule2 = $monitorgenerator->create_rule($secondrulerecord);
 197  
 198          $subscription = (object)['ruleid' => $rule->id, 'userid' => $user->id];
 199          $subscription = $monitorgenerator->create_subscription($subscription);
 200  
 201          $writer = \core_privacy\local\request\writer::with_context($usercontext);
 202          $this->assertFalse($writer->has_any_data());
 203  
 204          $approvedlist = new approved_contextlist($user, 'tool_monitor', [$usercontext->id]);
 205          provider::export_user_data($approvedlist);
 206  
 207          // Check that the rules created by this user are exported.
 208          $this->assertEquals($rulerecord->name, $writer->get_data([get_string('privacy:createdrules', 'tool_monitor'),
 209                  $rulerecord->name . '_' . $rule->id])->name);
 210          $this->assertEquals($secondrulerecord->name, $writer->get_data([get_string('privacy:createdrules', 'tool_monitor'),
 211                  $secondrulerecord->name . '_' . $rule2->id])->name);
 212  
 213          // Check that the subscriptions for this user are also exported.
 214          $this->assertEquals($rulerecord->name, $writer->get_data([get_string('privacy:subscriptions', 'tool_monitor'),
 215                  $rulerecord->name . '_' . $subscription->id, 'Site' , 'All events'])->name);
 216      }
 217  
 218      /**
 219       * Test deleting all user data for a specific context.
 220       */
 221      public function test_delete_data_for_all_users_in_context() {
 222          global $DB;
 223  
 224          $user = $this->getDataGenerator()->create_user();
 225          $user2 = $this->getDataGenerator()->create_user();
 226          $usercontext = \context_user::instance($user->id);
 227          $usercontext2 = \context_user::instance($user2->id);
 228          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 229  
 230          $this->setUser($user);
 231          // Need to give user one the ability to manage rules.
 232          $this->assign_user_capability('tool/monitor:managerules', \context_system::instance());
 233  
 234          $rulerecord = (object)['name' => 'privacy rule'];
 235          $rule = $monitorgenerator->create_rule($rulerecord);
 236  
 237          $secondrulerecord = (object)['name' => 'privacy rule2'];
 238          $rule2 = $monitorgenerator->create_rule($secondrulerecord);
 239  
 240          $subscription = (object)['ruleid' => $rule->id, 'userid' => $user->id];
 241          $subscription = $monitorgenerator->create_subscription($subscription);
 242  
 243          // Have user 2 subscribe to the second rule created by user 1.
 244          $subscription2 = (object)['ruleid' => $rule2->id, 'userid' => $user2->id];
 245          $subscription2 = $monitorgenerator->create_subscription($subscription2);
 246  
 247          $this->setUser($user2);
 248          $thirdrulerecord = (object)['name' => 'privacy rule for second user'];
 249          $rule3 = $monitorgenerator->create_rule($thirdrulerecord);
 250  
 251          $subscription3 = (object)['ruleid' => $rule3->id, 'userid' => $user2->id];
 252          $subscription3 = $monitorgenerator->create_subscription($subscription3);
 253  
 254          // Try a different context first.
 255          provider::delete_data_for_all_users_in_context(\context_system::instance());
 256  
 257          // Get all of the monitor rules.
 258          $dbrules = $DB->get_records('tool_monitor_rules');
 259  
 260          // All of the rules should still be present.
 261          $this->assertCount(3, $dbrules);
 262          $this->assertEquals($user->id, $dbrules[$rule->id]->userid);
 263          $this->assertEquals($user->id, $dbrules[$rule2->id]->userid);
 264          $this->assertEquals($user2->id, $dbrules[$rule3->id]->userid);
 265  
 266          // Delete everything for the first user context.
 267          provider::delete_data_for_all_users_in_context($usercontext);
 268  
 269          // Get all of the monitor rules.
 270          $dbrules = $DB->get_records('tool_monitor_rules');
 271  
 272          // Only the rules for user 1 that does not have any more subscriptions should be deleted (the first rule).
 273          $this->assertCount(2, $dbrules);
 274          $this->assertEquals($user->id, $dbrules[$rule2->id]->userid);
 275          $this->assertEquals($user2->id, $dbrules[$rule3->id]->userid);
 276  
 277          // Get all of the monitor subscriptions.
 278          $dbsubs = $DB->get_records('tool_monitor_subscriptions');
 279          // There should be two subscriptions left, both for user 2.
 280          $this->assertCount(2, $dbsubs);
 281          $this->assertEquals($user2->id, $dbsubs[$subscription2->id]->userid);
 282          $this->assertEquals($user2->id, $dbsubs[$subscription3->id]->userid);
 283      }
 284  
 285      /**
 286       * This should work identical to the above test.
 287       */
 288      public function test_delete_data_for_user() {
 289          global $DB;
 290  
 291          $user = $this->getDataGenerator()->create_user();
 292          $user2 = $this->getDataGenerator()->create_user();
 293          $usercontext = \context_user::instance($user->id);
 294          $usercontext2 = \context_user::instance($user2->id);
 295          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 296  
 297          $this->setUser($user);
 298          // Need to give user one the ability to manage rules.
 299          $this->assign_user_capability('tool/monitor:managerules', \context_system::instance());
 300  
 301          $rulerecord = (object)['name' => 'privacy rule'];
 302          $rule = $monitorgenerator->create_rule($rulerecord);
 303  
 304          $secondrulerecord = (object)['name' => 'privacy rule2'];
 305          $rule2 = $monitorgenerator->create_rule($secondrulerecord);
 306  
 307          $subscription = (object)['ruleid' => $rule->id, 'userid' => $user->id];
 308          $subscription = $monitorgenerator->create_subscription($subscription);
 309  
 310          // Have user 2 subscribe to the second rule created by user 1.
 311          $subscription2 = (object)['ruleid' => $rule2->id, 'userid' => $user2->id];
 312          $subscription2 = $monitorgenerator->create_subscription($subscription2);
 313  
 314          $this->setUser($user2);
 315          $thirdrulerecord = (object)['name' => 'privacy rule for second user'];
 316          $rule3 = $monitorgenerator->create_rule($thirdrulerecord);
 317  
 318          $subscription3 = (object)['ruleid' => $rule3->id, 'userid' => $user2->id];
 319          $subscription3 = $monitorgenerator->create_subscription($subscription3);
 320  
 321          $approvedlist = new approved_contextlist($user, 'tool_monitor', [$usercontext->id]);
 322  
 323          // Delete everything for the first user.
 324          provider::delete_data_for_user($approvedlist);
 325  
 326          // Get all of the monitor rules.
 327          $dbrules = $DB->get_records('tool_monitor_rules');
 328  
 329          // Only the rules for user 1 that does not have any more subscriptions should be deleted (the first rule).
 330          $this->assertCount(2, $dbrules);
 331          $this->assertEquals($user->id, $dbrules[$rule2->id]->userid);
 332          $this->assertEquals($user2->id, $dbrules[$rule3->id]->userid);
 333  
 334          // Get all of the monitor subscriptions.
 335          $dbsubs = $DB->get_records('tool_monitor_subscriptions');
 336          // There should be two subscriptions left, both for user 2.
 337          $this->assertCount(2, $dbsubs);
 338          $this->assertEquals($user2->id, $dbsubs[$subscription2->id]->userid);
 339          $this->assertEquals($user2->id, $dbsubs[$subscription3->id]->userid);
 340      }
 341  
 342      /**
 343       * Test deleting user data for an approved userlist in a context.
 344       */
 345      public function test_delete_data_for_users() {
 346          global $DB;
 347  
 348          $component = 'tool_monitor';
 349          $user = $this->getDataGenerator()->create_user();
 350          $user2 = $this->getDataGenerator()->create_user();
 351          $usercontext = \context_user::instance($user->id);
 352          $usercontext2 = \context_user::instance($user2->id);
 353          $monitorgenerator = $this->getDataGenerator()->get_plugin_generator('tool_monitor');
 354  
 355          $this->setUser($user);
 356          // Need to give user one the ability to manage rules.
 357          $this->assign_user_capability('tool/monitor:managerules', \context_system::instance());
 358  
 359          $rulerecord = (object)['name' => 'privacy rule'];
 360          $rule = $monitorgenerator->create_rule($rulerecord);
 361  
 362          $secondrulerecord = (object)['name' => 'privacy rule2'];
 363          $rule2 = $monitorgenerator->create_rule($secondrulerecord);
 364  
 365          $subscription = (object)['ruleid' => $rule->id, 'userid' => $user->id];
 366          $subscription = $monitorgenerator->create_subscription($subscription);
 367  
 368          // Have user 2 subscribe to the second rule created by user 1.
 369          $subscription2 = (object)['ruleid' => $rule2->id, 'userid' => $user2->id];
 370          $subscription2 = $monitorgenerator->create_subscription($subscription2);
 371  
 372          $this->setUser($user2);
 373          $thirdrulerecord = (object)['name' => 'privacy rule for second user'];
 374          $rule3 = $monitorgenerator->create_rule($thirdrulerecord);
 375  
 376          $subscription3 = (object)['ruleid' => $rule3->id, 'userid' => $user2->id];
 377          $subscription3 = $monitorgenerator->create_subscription($subscription3);
 378  
 379          // Get all of the monitor rules, ensure all exist.
 380          $dbrules = $DB->get_records('tool_monitor_rules');
 381          $this->assertCount(3, $dbrules);
 382  
 383          // Delete for user2 in first user's context, should have no effect.
 384          $approveduserids = [$user2->id];
 385          $approvedlist = new approved_userlist($usercontext, $component, $approveduserids);
 386          provider::delete_data_for_users($approvedlist);
 387  
 388          $dbrules = $DB->get_records('tool_monitor_rules');
 389          $this->assertCount(3, $dbrules);
 390  
 391          // Delete for user in usercontext.
 392          $approveduserids = [$user->id];
 393          $approvedlist = new approved_userlist($usercontext, $component, $approveduserids);
 394          provider::delete_data_for_users($approvedlist);
 395  
 396          // Only the rules for user 1 that does not have any more subscriptions should be deleted (the first rule).
 397          $dbrules = $DB->get_records('tool_monitor_rules');
 398          $this->assertCount(2, $dbrules);
 399          $this->assertEquals($user->id, $dbrules[$rule2->id]->userid);
 400          $this->assertEquals($user2->id, $dbrules[$rule3->id]->userid);
 401  
 402          // There should be two subscriptions left, both for user 2.
 403          $dbsubs = $DB->get_records('tool_monitor_subscriptions');
 404          $this->assertCount(2, $dbsubs);
 405          $this->assertEquals($user2->id, $dbsubs[$subscription2->id]->userid);
 406          $this->assertEquals($user2->id, $dbsubs[$subscription3->id]->userid);
 407  
 408          // Delete for user2 in context 2.
 409          $approveduserids = [$user2->id];
 410          $approvedlist = new approved_userlist($usercontext2, $component, $approveduserids);
 411          provider::delete_data_for_users($approvedlist);
 412  
 413          // There should be no subscriptions left.
 414          $dbsubs = $DB->get_records('tool_monitor_subscriptions');
 415          $this->assertEmpty($dbsubs);
 416      }
 417  }