Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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