Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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   * Unit tests for the repository_onedrive implementation of the privacy API.
  18   *
  19   * @package    repository_onedrive
  20   * @category   test
  21   * @copyright  2018 Zig Tan <zig@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace repository_onedrive\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  use core_privacy\local\metadata\collection;
  28  use core_privacy\local\request\writer;
  29  use core_privacy\local\request\approved_contextlist;
  30  use core_privacy\local\request\approved_userlist;
  31  use repository_onedrive\privacy\provider;
  32  /**
  33   * Unit tests for the repository_onedrive implementation of the privacy API.
  34   *
  35   * @copyright  2018 Zig Tan <zig@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class provider_test extends \core_privacy\tests\provider_testcase {
  39  
  40      /**
  41       * Overriding setUp() function to always reset after tests.
  42       */
  43      public function setUp(): void {
  44          $this->resetAfterTest(true);
  45      }
  46  
  47      /**
  48       * Test for provider::get_contexts_for_userid().
  49       */
  50      public function test_get_contexts_for_userid() {
  51          global $DB;
  52  
  53          // Test setup.
  54          $user = $this->getDataGenerator()->create_user();
  55          $this->setUser($user);
  56  
  57          // Add two repository_onedrive_access records for the User.
  58          $access = (object)[
  59              'usermodified' => $user->id,
  60              'itemid' => 'Some onedrive access item data',
  61              'permissionid' => 'Some onedrive access permission data',
  62              'timecreated' => date('u'),
  63              'timemodified' => date('u'),
  64          ];
  65          $DB->insert_record('repository_onedrive_access', $access);
  66          $access = (object)[
  67              'usermodified' => $user->id,
  68              'itemid' => 'Another onedrive access item data',
  69              'permissionid' => 'Another onedrive access permission data',
  70              'timecreated' => date('u'),
  71              'timemodified' => date('u'),
  72          ];
  73          $DB->insert_record('repository_onedrive_access', $access);
  74  
  75          // Test there are two repository_onedrive_access records for the User.
  76          $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user->id]);
  77          $this->assertCount(2, $access);
  78  
  79          // Test the User's retrieved contextlist contains only one context.
  80          $contextlist = provider::get_contexts_for_userid($user->id);
  81          $contexts = $contextlist->get_contexts();
  82          $this->assertCount(1, $contexts);
  83  
  84          // Test the User's contexts equal the User's own context.
  85          $context = reset($contexts);
  86          $this->assertEquals(CONTEXT_USER, $context->contextlevel);
  87          $this->assertEquals($user->id, $context->instanceid);
  88      }
  89  
  90      /**
  91       * Test for provider::test_get_users_in_context().
  92       */
  93      public function test_get_users_in_context() {
  94          global $DB;
  95          $component = 'repository_onedrive';
  96  
  97          // Test setup.
  98          $user1 = $this->getDataGenerator()->create_user();
  99          $user2 = $this->getDataGenerator()->create_user();
 100          $u1id = $user1->id;
 101          $u2id = $user2->id;
 102  
 103          // Add a repository_onedrive_access records for each user.
 104          $this->setUser($user1);
 105          $access = (object)[
 106              'usermodified' => $u1id,
 107              'itemid' => 'Some onedrive access item data',
 108              'permissionid' => 'Some onedrive access permission data',
 109              'timecreated' => date('u'),
 110              'timemodified' => date('u'),
 111          ];
 112          $DB->insert_record('repository_onedrive_access', $access);
 113  
 114          $this->setUser($user2);
 115          $access = (object)[
 116              'usermodified' => $u2id,
 117              'itemid' => 'Another onedrive access item data',
 118              'permissionid' => 'Another onedrive access permission data',
 119              'timecreated' => date('u'),
 120              'timemodified' => date('u'),
 121          ];
 122          $DB->insert_record('repository_onedrive_access', $access);
 123  
 124          // Fetch the context of each user's access record.
 125          $contextlist = provider::get_contexts_for_userid($u1id);
 126          $u1contexts = $contextlist->get_contexts();
 127          $this->assertCount(1, $u1contexts);
 128  
 129          $contextlist = provider::get_contexts_for_userid($u2id);
 130          $u2contexts = $contextlist->get_contexts();
 131          $this->assertCount(1, $u2contexts);
 132  
 133          $contexts = [
 134              $u1id => $u1contexts[0],
 135              $u2id => $u2contexts[0],
 136          ];
 137  
 138          // Test context 1 only contains user 1.
 139          $userlist = new \core_privacy\local\request\userlist($contexts[$u1id], $component);
 140          provider::get_users_in_context($userlist);
 141  
 142          $this->assertCount(1, $userlist);
 143          $actual = $userlist->get_userids();
 144          $this->assertEquals([$u1id], $actual);
 145  
 146          // Test context 2 only contains user 2.
 147          $userlist = new \core_privacy\local\request\userlist($contexts[$u2id], $component);
 148          provider::get_users_in_context($userlist);
 149  
 150          $this->assertCount(1, $userlist);
 151          $actual = $userlist->get_userids();
 152          $this->assertEquals([$u2id], $actual);
 153  
 154          // Test the contexts match the users' contexts.
 155          $this->assertEquals($u1id, $contexts[$u1id]->instanceid);
 156          $this->assertEquals($u2id, $contexts[$u2id]->instanceid);
 157      }
 158  
 159      /**
 160       * Test for provider::export_user_data().
 161       */
 162      public function test_export_user_data() {
 163          global $DB;
 164  
 165          // Test setup.
 166          $user = $this->getDataGenerator()->create_user();
 167          $this->setUser($user);
 168  
 169          // Add two repository_onedrive_access records for the User.
 170          $access = (object)[
 171              'usermodified' => $user->id,
 172              'itemid' => 'Some onedrive access item data',
 173              'permissionid' => 'Some onedrive access permission data',
 174              'timecreated' => date('u'),
 175              'timemodified' => date('u'),
 176          ];
 177          $DB->insert_record('repository_onedrive_access', $access);
 178          $access = (object)[
 179              'usermodified' => $user->id,
 180              'itemid' => 'Another onedrive access item data',
 181              'permissionid' => 'Another onedrive access permission data',
 182              'timecreated' => date('u'),
 183              'timemodified' => date('u'),
 184          ];
 185          $DB->insert_record('repository_onedrive_access', $access);
 186  
 187          // Test there are two repository_onedrive_access records for the User.
 188          $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user->id]);
 189          $this->assertCount(2, $access);
 190  
 191          // Test the User's retrieved contextlist contains only one context.
 192          $contextlist = provider::get_contexts_for_userid($user->id);
 193          $contexts = $contextlist->get_contexts();
 194          $this->assertCount(1, $contexts);
 195  
 196          // Test the User's contexts equal the User's own context.
 197          $context = reset($contexts);
 198          $this->assertEquals(CONTEXT_USER, $context->contextlevel);
 199          $this->assertEquals($user->id, $context->instanceid);
 200          $approvedcontextlist = new approved_contextlist($user, 'repository_onedrive', $contextlist->get_contextids());
 201  
 202          // Retrieve repository_onedrive_access data only for this user.
 203          provider::export_user_data($approvedcontextlist);
 204  
 205          // Test the repository_onedrive_access data is exported at the User context level.
 206          $user = $approvedcontextlist->get_user();
 207          $contextuser = \context_user::instance($user->id);
 208          $writer = writer::with_context($contextuser);
 209          $this->assertTrue($writer->has_any_data());
 210      }
 211  
 212      /**
 213       * Test for provider::delete_data_for_all_users_in_context().
 214       */
 215      public function test_delete_data_for_all_users_in_context() {
 216          global $DB;
 217  
 218          // Test setup.
 219          $user = $this->getDataGenerator()->create_user();
 220          $this->setUser($user);
 221  
 222          // Add two repository_onedrive_access records for the User.
 223          $access = (object)[
 224              'usermodified' => $user->id,
 225              'itemid' => 'Some onedrive access item data',
 226              'permissionid' => 'Some onedrive access permission data',
 227              'timecreated' => date('u'),
 228              'timemodified' => date('u'),
 229          ];
 230          $DB->insert_record('repository_onedrive_access', $access);
 231          $access = (object)[
 232              'usermodified' => $user->id,
 233              'itemid' => 'Another onedrive access item data',
 234              'permissionid' => 'Another onedrive access permission data',
 235              'timecreated' => date('u'),
 236              'timemodified' => date('u'),
 237          ];
 238          $DB->insert_record('repository_onedrive_access', $access);
 239  
 240          // Test there are two repository_onedrive_access records for the User.
 241          $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user->id]);
 242          $this->assertCount(2, $access);
 243  
 244          // Test the User's retrieved contextlist contains only one context.
 245          $contextlist = provider::get_contexts_for_userid($user->id);
 246          $contexts = $contextlist->get_contexts();
 247          $this->assertCount(1, $contexts);
 248  
 249          // Test the User's contexts equal the User's own context.
 250          $context = reset($contexts);
 251          $this->assertEquals(CONTEXT_USER, $context->contextlevel);
 252          $this->assertEquals($user->id, $context->instanceid);
 253  
 254          // Test delete all users content by context.
 255          provider::delete_data_for_all_users_in_context($context);
 256          $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user->id]);
 257          $this->assertCount(0, $access);
 258      }
 259  
 260      /**
 261       * Test for provider::delete_data_for_user().
 262       */
 263      public function test_delete_data_for_user() {
 264          global $DB;
 265  
 266          // Test setup.
 267          $user1 = $this->getDataGenerator()->create_user();
 268          $user2 = $this->getDataGenerator()->create_user();
 269          $this->setUser($user1);
 270  
 271          // Add 3 repository_onedrive_accesss records for User 1.
 272          $noaccess = 3;
 273          for ($a = 0; $a < $noaccess; $a++) {
 274              $access = (object)[
 275                  'usermodified' => $user1->id,
 276                  'itemid' => 'Some onedrive access item data - ' . $a,
 277                  'permissionid' => 'Some onedrive access permission data - ' . $a,
 278                  'timecreated' => date('u'),
 279                  'timemodified' => date('u'),
 280              ];
 281              $DB->insert_record('repository_onedrive_access', $access);
 282          }
 283          // Add 1 repository_onedrive_accesss record for User 2.
 284          $access = (object)[
 285              'usermodified' => $user2->id,
 286              'itemid' => 'Some onedrive access item data',
 287              'permissionid' => 'Some onedrive access permission data',
 288              'timecreated' => date('u'),
 289              'timemodified' => date('u'),
 290          ];
 291          $DB->insert_record('repository_onedrive_access', $access);
 292  
 293          // Test the created repository_onedrive records for User 1 equals test number of access specified.
 294          $communities = $DB->get_records('repository_onedrive_access', ['usermodified' => $user1->id]);
 295          $this->assertCount($noaccess, $communities);
 296  
 297          // Test the created repository_onedrive records for User 2 equals 1.
 298          $communities = $DB->get_records('repository_onedrive_access', ['usermodified' => $user2->id]);
 299          $this->assertCount(1, $communities);
 300  
 301          // Test the deletion of repository_onedrive_access records for User 1 results in zero records.
 302          $contextlist = provider::get_contexts_for_userid($user1->id);
 303          $contexts = $contextlist->get_contexts();
 304          $this->assertCount(1, $contexts);
 305  
 306          // Test the User's contexts equal the User's own context.
 307          $context = reset($contexts);
 308          $this->assertEquals(CONTEXT_USER, $context->contextlevel);
 309          $this->assertEquals($user1->id, $context->instanceid);
 310  
 311          $approvedcontextlist = new approved_contextlist($user1, 'repository_onedrive', $contextlist->get_contextids());
 312          provider::delete_data_for_user($approvedcontextlist);
 313          $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user1->id]);
 314          $this->assertCount(0, $access);
 315  
 316          // Test that User 2's single repository_onedrive_access record still exists.
 317          $contextlist = provider::get_contexts_for_userid($user2->id);
 318          $contexts = $contextlist->get_contexts();
 319          $this->assertCount(1, $contexts);
 320  
 321          // Test the User's contexts equal the User's own context.
 322          $context = reset($contexts);
 323          $this->assertEquals(CONTEXT_USER, $context->contextlevel);
 324          $this->assertEquals($user2->id, $context->instanceid);
 325          $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user2->id]);
 326          $this->assertCount(1, $access);
 327      }
 328  
 329      /**
 330       * Test for provider::delete_data_for_users().
 331       */
 332      public function test_delete_data_for_users() {
 333          global $DB;
 334          $component = 'repository_onedrive';
 335  
 336          // Test setup.
 337          $user1 = $this->getDataGenerator()->create_user();
 338          $user2 = $this->getDataGenerator()->create_user();
 339          $this->setUser($user1);
 340  
 341          // Add 3 repository_onedrive_accesss records for User 1.
 342          $noaccess = 3;
 343          for ($a = 0; $a < $noaccess; $a++) {
 344              $access = (object)[
 345                  'usermodified' => $user1->id,
 346                  'itemid' => 'Some onedrive access item data for user 1 - ' . $a,
 347                  'permissionid' => 'Some onedrive access permission data - ' . $a,
 348                  'timecreated' => date('u'),
 349                  'timemodified' => date('u'),
 350              ];
 351              $DB->insert_record('repository_onedrive_access', $access);
 352          }
 353          // Add 1 repository_onedrive_accesss record for User 2.
 354          $access = (object)[
 355              'usermodified' => $user2->id,
 356              'itemid' => 'Some onedrive access item data for user 2',
 357              'permissionid' => 'Some onedrive access permission data',
 358              'timecreated' => date('u'),
 359              'timemodified' => date('u'),
 360          ];
 361          $DB->insert_record('repository_onedrive_access', $access);
 362  
 363          // Test the created repository_onedrive records for User 1 equals test number of access specified.
 364          $communities = $DB->get_records('repository_onedrive_access', ['usermodified' => $user1->id]);
 365          $this->assertCount($noaccess, $communities);
 366  
 367          // Test the created repository_onedrive records for User 2 equals 1.
 368          $communities = $DB->get_records('repository_onedrive_access', ['usermodified' => $user2->id]);
 369          $this->assertCount(1, $communities);
 370  
 371          // Fetch the context of each user's access record.
 372          $contextlist = provider::get_contexts_for_userid($user1->id);
 373          $u1contexts = $contextlist->get_contexts();
 374  
 375          // Test the deletion of context 1 results in deletion of user 1's records only.
 376          $approveduserids = [$user1->id, $user2->id];
 377          $approvedlist = new approved_userlist($u1contexts[0], $component, $approveduserids);
 378          provider::delete_data_for_users($approvedlist);
 379  
 380          $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user1->id]);
 381          $this->assertCount(0, $access);
 382  
 383          // Ensure user 2's record still exists.
 384          $access = $DB->get_records('repository_onedrive_access', ['usermodified' => $user2->id]);
 385          $this->assertCount(1, $access);
 386      }
 387  }