Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Base class for unit tests for core_cohort.
  19   *
  20   * @package    core_files
  21   * @category   test
  22   * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace core_files\privacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_files\privacy\provider;
  30  use core_privacy\local\request\approved_contextlist;
  31  use core_privacy\local\request\writer;
  32  use core_privacy\tests\provider_testcase;
  33  use core_privacy\local\request\approved_userlist;
  34  
  35  /**
  36   * Unit tests for files\classes\privacy\provider.php
  37   *
  38   * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class provider_test extends provider_testcase {
  42  
  43      /**
  44       * Test getting the context for the user ID related to this plugin.
  45       */
  46      public function test_get_contexts_for_userid() {
  47          $this->resetAfterTest();
  48  
  49          // Create a user.
  50          $user = $this->getDataGenerator()->create_user();
  51          $userctx = \context_user::instance($user->id);
  52  
  53          create_user_key('core_files', $user->id);
  54  
  55          $contextlist = provider::get_contexts_for_userid($user->id);
  56          $this->assertCount(1, (array) $contextlist->get_contextids());
  57          $this->assertContainsEquals($userctx->id, $contextlist->get_contextids());
  58      }
  59  
  60      /**
  61       * Test that data is exported correctly for this plugin.
  62       */
  63      public function test_export_user_data() {
  64          global $DB;
  65  
  66          $this->resetAfterTest();
  67  
  68          // Create a user.
  69          $user = $this->getDataGenerator()->create_user();
  70          $usercontext = \context_user::instance($user->id);
  71  
  72          $keyvalue = get_user_key('core_files', $user->id);
  73          $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
  74  
  75          // Validate exported data.
  76          $this->setUser($user);
  77          $writer = writer::with_context($usercontext);
  78          $this->assertFalse($writer->has_any_data());
  79          $this->export_context_data_for_user($user->id, $usercontext, 'core_files');
  80          $subcontext = [
  81              get_string('files')
  82          ];
  83          $userkeydata = $writer->get_related_data($subcontext, 'userkeys');
  84          $this->assertCount(1, $userkeydata->keys);
  85          $this->assertEquals($key->script, reset($userkeydata->keys)->script);
  86      }
  87  
  88      /**
  89       * Test for provider::delete_data_for_all_users_in_context().
  90       */
  91      public function test_delete_data_for_all_users_in_context() {
  92          global $DB;
  93  
  94          $this->resetAfterTest();
  95  
  96          // Create a user.
  97          $user = $this->getDataGenerator()->create_user();
  98          $usercontext = \context_user::instance($user->id);
  99  
 100          create_user_key('core_files', $user->id);
 101  
 102          // Before deletion, we should have 1 user_private_key.
 103          $count = $DB->count_records('user_private_key', ['script' => 'core_files']);
 104          $this->assertEquals(1, $count);
 105  
 106          // Delete data.
 107          provider::delete_data_for_all_users_in_context($usercontext);
 108  
 109          // After deletion, the user_private_key entries should have been deleted.
 110          $count = $DB->count_records('user_private_key', ['script' => 'core_files']);
 111          $this->assertEquals(0, $count);
 112      }
 113  
 114      /**
 115       * Test for provider::delete_data_for_user().
 116       */
 117      public function test_delete_data_for_user() {
 118          global $DB;
 119  
 120          $this->resetAfterTest();
 121  
 122          // Create a user.
 123          $user = $this->getDataGenerator()->create_user();
 124  
 125          create_user_key('core_files', $user->id);
 126  
 127          // Before deletion, we should have 1 user_private_key.
 128          $count = $DB->count_records('user_private_key', ['script' => 'core_files']);
 129          $this->assertEquals(1, $count);
 130  
 131          // Delete data.
 132          $contextlist = provider::get_contexts_for_userid($user->id);
 133          $approvedcontextlist = new approved_contextlist($user, 'core_files', $contextlist->get_contextids());
 134  
 135          provider::delete_data_for_user($approvedcontextlist);
 136          // After deletion, the user_private_key entries should have been deleted.
 137          $count = $DB->count_records('user_private_key', ['script' => 'core_files']);
 138          $this->assertEquals(0, $count);
 139      }
 140  
 141      /**
 142       * Test that only users within a course context are fetched.
 143       */
 144      public function test_get_users_in_context() {
 145          $this->resetAfterTest();
 146  
 147          $component = 'core_files';
 148  
 149          // Create a user.
 150          $user = $this->getDataGenerator()->create_user();
 151          $userctx = \context_user::instance($user->id);
 152  
 153          $userlist = new \core_privacy\local\request\userlist($userctx, $component);
 154          provider::get_users_in_context($userlist);
 155          $this->assertCount(0, $userlist);
 156  
 157          create_user_key('core_files', $user->id);
 158  
 159          // The list of users within the userctx context should contain user.
 160          provider::get_users_in_context($userlist);
 161          $this->assertCount(1, $userlist);
 162          $expected = [$user->id];
 163          $actual = $userlist->get_userids();
 164          $this->assertEquals($expected, $actual);
 165  
 166          // The list of users within contexts different than user should be empty.
 167          $systemctx = \context_system::instance();
 168          $userlist = new \core_privacy\local\request\userlist($systemctx, $component);
 169          provider::get_users_in_context($userlist);
 170          $this->assertCount(0, $userlist);
 171      }
 172  
 173      /**
 174       * Test that data for users in approved userlist is deleted.
 175       */
 176      public function test_delete_data_for_users() {
 177          $this->resetAfterTest();
 178  
 179          $component = 'core_files';
 180  
 181          // Create user1.
 182          $user1 = $this->getDataGenerator()->create_user();
 183          $userctx1 = \context_user::instance($user1->id);
 184          // Create user2.
 185          $user2 = $this->getDataGenerator()->create_user();
 186          $userctx2 = \context_user::instance($user2->id);
 187  
 188          create_user_key('core_files', $user1->id);
 189          create_user_key('core_files', $user2->id);
 190  
 191          $userlist1 = new \core_privacy\local\request\userlist($userctx1, $component);
 192          provider::get_users_in_context($userlist1);
 193          $this->assertCount(1, $userlist1);
 194  
 195          $userlist2 = new \core_privacy\local\request\userlist($userctx2, $component);
 196          provider::get_users_in_context($userlist2);
 197          $this->assertCount(1, $userlist2);
 198  
 199          // Convert $userlist1 into an approved_contextlist.
 200          $approvedlist1 = new approved_userlist($userctx1, $component, $userlist1->get_userids());
 201          // Delete using delete_data_for_user.
 202          provider::delete_data_for_users($approvedlist1);
 203  
 204          // Re-fetch users in userctx1.
 205          $userlist1 = new \core_privacy\local\request\userlist($userctx1, $component);
 206          provider::get_users_in_context($userlist1);
 207          // The user data in coursecategoryctx should be deleted.
 208          $this->assertCount(0, $userlist1);
 209  
 210          // Re-fetch users in userctx2.
 211          $userlist2 = new \core_privacy\local\request\userlist($userctx2, $component);
 212          provider::get_users_in_context($userlist2);
 213          // The user data in userctx2 should be still present.
 214          $this->assertCount(1, $userlist2);
 215  
 216          // Convert $userlist2 into an approved_contextlist in the system context.
 217          $systemctx = \context_system::instance();
 218          $approvedlist3 = new approved_userlist($systemctx, $component, $userlist2->get_userids());
 219          // Delete using delete_data_for_user.
 220          provider::delete_data_for_users($approvedlist3);
 221          // Re-fetch users in userctx2.
 222          $userlist3 = new \core_privacy\local\request\userlist($userctx2, $component);
 223          provider::get_users_in_context($userlist3);
 224          // The user data in userctx2 should not be deleted.
 225          $this->assertCount(1, $userlist3);
 226      }
 227  }