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.

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   * Base class for unit tests for core_rss.
  18   *
  19   * @package    core_rss
  20   * @copyright  2018 Sara Arjona <sara@moodle.com>
  21   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22   */
  23  namespace core_rss\privacy;
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use core_privacy\tests\provider_testcase;
  28  use core_rss\privacy\provider;
  29  use core_privacy\local\request\writer;
  30  use core_privacy\local\request\approved_contextlist;
  31  use core_privacy\local\request\approved_userlist;
  32  
  33  /**
  34   * Unit tests for rss\classes\privacy\provider.php
  35   *
  36   * @copyright  2018 Sara Arjona <sara@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class provider_test extends provider_testcase {
  40  
  41      /**
  42       * Basic setup for these tests.
  43       */
  44      public function setUp(): void {
  45          $this->resetAfterTest(true);
  46      }
  47  
  48      /**
  49       * Test getting the context for the user ID related to this plugin.
  50       */
  51      public function test_get_contexts_for_userid() {
  52          // Create user and RSS user keys.
  53          $user = $this->getDataGenerator()->create_user();
  54          $context = \context_user::instance($user->id);
  55          $key = get_user_key('rss', $user->id);
  56  
  57          $contextlist = provider::get_contexts_for_userid($user->id);
  58          $this->assertEquals($context->id, $contextlist->current()->id);
  59      }
  60  
  61      /**
  62       * Test that data is exported correctly for this plugin.
  63       */
  64      public function test_export_user_data() {
  65          global $DB;
  66  
  67          // Create user and RSS user keys.
  68          $user = $this->getDataGenerator()->create_user();
  69          $context = \context_user::instance($user->id);
  70          $keyvalue = get_user_key('rss', $user->id);
  71          $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
  72  
  73          // Validate exported data.
  74          $this->setUser($user);
  75          $writer = writer::with_context($context);
  76          $this->assertFalse($writer->has_any_data());
  77          $this->export_context_data_for_user($user->id, $context, 'core_rss');
  78          $userkeydata = $writer->get_related_data([], 'userkeys');
  79          $this->assertCount(1, $userkeydata->keys);
  80          $this->assertEquals($key->script, reset($userkeydata->keys)->script);
  81      }
  82  
  83      /**
  84       * Test for provider::delete_data_for_all_users_in_context().
  85       */
  86      public function test_delete_data_for_all_users_in_context() {
  87          global $DB;
  88  
  89          // Create user and RSS user keys.
  90          $user = $this->getDataGenerator()->create_user();
  91          $context = \context_user::instance($user->id);
  92          $keyvalue = get_user_key('rss', $user->id);
  93          $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
  94  
  95          // Before deletion, we should have 1 user_private_key.
  96          $count = $DB->count_records('user_private_key', ['script' => 'rss']);
  97          $this->assertEquals(1, $count);
  98  
  99          // Delete data.
 100          provider::delete_data_for_all_users_in_context($context);
 101  
 102          // After deletion, the user_private_key entries should have been deleted.
 103          $count = $DB->count_records('user_private_key', ['script' => 'rss']);
 104          $this->assertEquals(0, $count);
 105      }
 106  
 107      /**
 108       * Test for provider::delete_data_for_user().
 109       */
 110      public function test_delete_data_for_user() {
 111          global $DB;
 112  
 113          // Create user and RSS user keys.
 114          $user = $this->getDataGenerator()->create_user();
 115          $context = \context_user::instance($user->id);
 116          $keyvalue = get_user_key('rss', $user->id);
 117          $key = $DB->get_record('user_private_key', ['value' => $keyvalue]);
 118  
 119          // Before deletion, we should have 1 user_private_key.
 120          $count = $DB->count_records('user_private_key', ['script' => 'rss']);
 121          $this->assertEquals(1, $count);
 122  
 123          // Delete data.
 124          $contextlist = provider::get_contexts_for_userid($user->id);
 125          $approvedcontextlist = new approved_contextlist($user, 'rss', $contextlist->get_contextids());
 126          provider::delete_data_for_user($approvedcontextlist);
 127  
 128          // After deletion, the user_private_key entries should have been deleted.
 129          $count = $DB->count_records('user_private_key', ['script' => 'rss']);
 130          $this->assertEquals(0, $count);
 131      }
 132  
 133      /**
 134       * Test that only users with a user context are fetched.
 135       */
 136      public function test_get_users_in_context() {
 137          $component = 'core_rss';
 138          // Create a user.
 139          $user = $this->getDataGenerator()->create_user();
 140  
 141          $usercontext = \context_user::instance($user->id);
 142          $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
 143          // The list of users should not return anything yet (related data still haven't been created).
 144          provider::get_users_in_context($userlist);
 145          $this->assertCount(0, $userlist);
 146          // Create private access key for user.
 147          get_user_key('rss', $user->id);
 148  
 149          // The list of users for user context should return the user.
 150          provider::get_users_in_context($userlist);
 151          $this->assertCount(1, $userlist);
 152          $expected = [$user->id];
 153          $actual = $userlist->get_userids();
 154          $this->assertEquals($expected, $actual);
 155  
 156          // The list of users for system context should not return any users.
 157          $systemcontext = \context_system::instance();
 158          $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
 159          provider::get_users_in_context($userlist);
 160          $this->assertCount(0, $userlist);
 161      }
 162  
 163      /**
 164       * Test that data for users in approved userlist is deleted.
 165       */
 166      public function test_delete_data_for_users() {
 167          $component = 'core_rss';
 168          // Create a user1.
 169          $user1 = $this->getDataGenerator()->create_user();
 170          $usercontext1 = \context_user::instance($user1->id);
 171          // Create list of users with a related user data in usercontext1.
 172          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 173  
 174          // Create a user1.
 175          $user2 = $this->getDataGenerator()->create_user();
 176          $usercontext2 = \context_user::instance($user2->id);
 177          // Create list of users with a related user data in usercontext2.
 178          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 179  
 180          // Create private access key for user1.
 181          get_user_key('rss', $user1->id);
 182          // Create private access key for user2.
 183          get_user_key('rss', $user2->id);
 184  
 185          // Ensure the user list for usercontext1 contains user1.
 186          provider::get_users_in_context($userlist1);
 187          $this->assertCount(1, $userlist1);
 188          // Ensure the user list for usercontext2 contains user2.
 189          provider::get_users_in_context($userlist2);
 190          $this->assertCount(1, $userlist2);
 191  
 192          // Convert $userlist1 into an approved_contextlist.
 193          $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
 194  
 195          // Delete using delete_data_for_user.
 196          provider::delete_data_for_users($approvedlist);
 197  
 198          // Re-fetch users in usercontext1 - The user list should now be empty.
 199          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 200          provider::get_users_in_context($userlist1);
 201          $this->assertCount(0, $userlist1);
 202          // Re-fetch users in usercontext2 - The user list should not be empty (user2).
 203          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 204          provider::get_users_in_context($userlist2);
 205          $this->assertCount(1, $userlist2);
 206  
 207          // User data should be only removed in the user context.
 208          $systemcontext = \context_system::instance();
 209          // Add userlist2 to the approved user list in the system context.
 210          $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
 211          // Delete user1 data using delete_data_for_user.
 212          provider::delete_data_for_users($approvedlist);
 213          // Re-fetch users in usercontext2 - The user list should not be empty (user2).
 214          $userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
 215          provider::get_users_in_context($userlist1);
 216          $this->assertCount(1, $userlist1);
 217      }
 218  }