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   * Privacy provider tests.
  19   *
  20   * @package    core_portfolio
  21   * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_portfolio\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_portfolio\privacy\provider;
  29  use core_privacy\local\request\approved_userlist;
  30  
  31  /**
  32   * Privacy provider tests class.
  33   *
  34   * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class provider_test extends \core_privacy\tests\provider_testcase {
  38  
  39      protected function create_portfolio_data($plugin, $name, $user, $preference, $value) {
  40          global $DB;
  41          $portfolioinstance = (object) [
  42              'plugin' => $plugin,
  43              'name' => $name,
  44              'visible' => 1
  45          ];
  46          $portfolioinstance->id = $DB->insert_record('portfolio_instance', $portfolioinstance);
  47          $userinstance = (object) [
  48              'instance' => $portfolioinstance->id,
  49              'userid' => $user->id,
  50              'name' => $preference,
  51              'value' => $value
  52          ];
  53          $DB->insert_record('portfolio_instance_user', $userinstance);
  54  
  55          $DB->insert_record('portfolio_log', [
  56              'portfolio' => $portfolioinstance->id,
  57              'userid' => $user->id,
  58              'caller_class' => 'forum_portfolio_caller',
  59              'caller_component' => 'mod_forum',
  60              'time' => time(),
  61          ]);
  62  
  63          $DB->insert_record('portfolio_log', [
  64              'portfolio' => $portfolioinstance->id,
  65              'userid' => $user->id,
  66              'caller_class' => 'workshop_portfolio_caller',
  67              'caller_component' => 'mod_workshop',
  68              'time' => time(),
  69          ]);
  70      }
  71  
  72      /**
  73       *  Verify that a collection of metadata is returned for this component and that it just returns the righ types for 'portfolio'.
  74       */
  75      public function test_get_metadata() {
  76          $collection = new \core_privacy\local\metadata\collection('core_portfolio');
  77          $collection = provider::get_metadata($collection);
  78          $this->assertNotEmpty($collection);
  79          $items = $collection->get_collection();
  80          $this->assertEquals(4, count($items));
  81          $this->assertInstanceOf(\core_privacy\local\metadata\types\database_table::class, $items[0]);
  82          $this->assertInstanceOf(\core_privacy\local\metadata\types\database_table::class, $items[1]);
  83          $this->assertInstanceOf(\core_privacy\local\metadata\types\database_table::class, $items[2]);
  84          $this->assertInstanceOf(\core_privacy\local\metadata\types\plugintype_link::class, $items[3]);
  85      }
  86  
  87      /**
  88       * Test that the export for a user id returns a user context.
  89       */
  90      public function test_get_contexts_for_userid() {
  91          $this->resetAfterTest();
  92          $user = $this->getDataGenerator()->create_user();
  93          $context = \context_user::instance($user->id);
  94          $this->create_portfolio_data('googledocs', 'Google Docs', $user, 'visible', 1);
  95          $contextlist = provider::get_contexts_for_userid($user->id);
  96          $this->assertEquals($context->id, $contextlist->current()->id);
  97      }
  98  
  99      /**
 100       * Test that exporting user data works as expected.
 101       */
 102      public function test_export_user_data() {
 103          $this->resetAfterTest();
 104          $user = $this->getDataGenerator()->create_user();
 105          $context = \context_user::instance($user->id);
 106          $this->create_portfolio_data('googledocs', 'Google Docs', $user, 'visible', 1);
 107          $contextlist = new \core_privacy\local\request\approved_contextlist($user, 'core_portfolio', [$context->id]);
 108          provider::export_user_data($contextlist);
 109          $writer = \core_privacy\local\request\writer::with_context($context);
 110          $portfoliodata = $writer->get_data([get_string('privacy:path', 'portfolio')]);
 111          $this->assertEquals('Google Docs', $portfoliodata->{'Google Docs'}->name);
 112      }
 113  
 114      /**
 115       * Test that deleting only results in the one context being removed.
 116       */
 117      public function test_delete_data_for_all_users_in_context() {
 118          global $DB;
 119  
 120          $this->resetAfterTest();
 121          $user1 = $this->getDataGenerator()->create_user();
 122          $user2 = $this->getDataGenerator()->create_user();
 123          $this->create_portfolio_data('googledocs', 'Google Docs', $user1, 'visible', 1);
 124          $this->create_portfolio_data('onedrive', 'Microsoft onedrive', $user2, 'visible', 1);
 125          // Check a system context sent through.
 126          $systemcontext = \context_system::instance();
 127          provider::delete_data_for_all_users_in_context($systemcontext);
 128          $records = $DB->get_records('portfolio_instance_user');
 129          $this->assertCount(2, $records);
 130          $this->assertCount(4, $DB->get_records('portfolio_log'));
 131          $context = \context_user::instance($user1->id);
 132          provider::delete_data_for_all_users_in_context($context);
 133          $records = $DB->get_records('portfolio_instance_user');
 134          // Only one entry should remain for user 2.
 135          $this->assertCount(1, $records);
 136          $data = array_shift($records);
 137          $this->assertEquals($user2->id, $data->userid);
 138          $this->assertCount(2, $DB->get_records('portfolio_log'));
 139      }
 140  
 141      /**
 142       * Test that deleting only results in one user's data being removed.
 143       */
 144      public function test_delete_data_for_user() {
 145          global $DB;
 146  
 147          $this->resetAfterTest();
 148          $user1 = $this->getDataGenerator()->create_user();
 149          $user2 = $this->getDataGenerator()->create_user();
 150          $this->create_portfolio_data('googledocs', 'Google Docs', $user1, 'visible', 1);
 151          $this->create_portfolio_data('onedrive', 'Microsoft onedrive', $user2, 'visible', 1);
 152  
 153          $records = $DB->get_records('portfolio_instance_user');
 154          $this->assertCount(2, $records);
 155          $this->assertCount(4, $DB->get_records('portfolio_log'));
 156  
 157          $context = \context_user::instance($user1->id);
 158          $contextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_portfolio', [$context->id]);
 159          provider::delete_data_for_user($contextlist);
 160          $records = $DB->get_records('portfolio_instance_user');
 161          // Only one entry should remain for user 2.
 162          $this->assertCount(1, $records);
 163          $data = array_shift($records);
 164          $this->assertEquals($user2->id, $data->userid);
 165          $this->assertCount(2, $DB->get_records('portfolio_log'));
 166      }
 167  
 168      /**
 169       * Test that only users with a user context are fetched.
 170       */
 171      public function test_get_users_in_context() {
 172          $this->resetAfterTest();
 173  
 174          $component = 'core_portfolio';
 175          // Create a user.
 176          $user = $this->getDataGenerator()->create_user();
 177          $usercontext = \context_user::instance($user->id);
 178          // The list of users should not return anything yet (related data still haven't been created).
 179          $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
 180          provider::get_users_in_context($userlist);
 181          $this->assertCount(0, $userlist);
 182  
 183          // Create portfolio data for user.
 184          $this->create_portfolio_data('googledocs', 'Google Docs', $user,
 185              'visible', 1);
 186  
 187          // The list of users for user context should return the user.
 188          provider::get_users_in_context($userlist);
 189          $this->assertCount(1, $userlist);
 190          $expected = [$user->id];
 191          $actual = $userlist->get_userids();
 192          $this->assertEquals($expected, $actual);
 193  
 194          // The list of users for system context should not return any users.
 195          $systemcontext = \context_system::instance();
 196          $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
 197          provider::get_users_in_context($userlist);
 198          $this->assertCount(0, $userlist);
 199      }
 200  
 201      /**
 202       * Test that data for users in approved userlist is deleted.
 203       */
 204      public function test_delete_data_for_users() {
 205          $this->resetAfterTest();
 206  
 207          $component = 'core_portfolio';
 208          // Create user1.
 209          $user1 = $this->getDataGenerator()->create_user();
 210          $usercontext1 = \context_user::instance($user1->id);
 211          // Create user1.
 212          $user2 = $this->getDataGenerator()->create_user();
 213          $usercontext2 = \context_user::instance($user2->id);
 214  
 215          // Create portfolio data for user1 and user2.
 216          $this->create_portfolio_data('googledocs', 'Google Docs', $user1,
 217              'visible', 1);
 218          $this->create_portfolio_data('onedrive', 'Microsoft onedrive', $user2,
 219              'visible', 1);
 220  
 221          // The list of users for usercontext1 should return user1.
 222          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 223          provider::get_users_in_context($userlist1);
 224          $this->assertCount(1, $userlist1);
 225          $expected = [$user1->id];
 226          $actual = $userlist1->get_userids();
 227          $this->assertEquals($expected, $actual);
 228          // The list of users for usercontext2 should return user2.
 229          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 230          provider::get_users_in_context($userlist2);
 231          $this->assertCount(1, $userlist2);
 232          $expected = [$user2->id];
 233          $actual = $userlist2->get_userids();
 234          $this->assertEquals($expected, $actual);
 235  
 236          // Add userlist1 to the approved user list.
 237          $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
 238          // Delete user data using delete_data_for_user for usercontext1.
 239          provider::delete_data_for_users($approvedlist);
 240          // Re-fetch users in usercontext1 - The user list should now be empty.
 241          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 242          provider::get_users_in_context($userlist1);
 243          $this->assertCount(0, $userlist1);
 244          // Re-fetch users in usercontext2 - The user list should not be empty (user2).
 245          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 246          provider::get_users_in_context($userlist2);
 247          $this->assertCount(1, $userlist2);
 248  
 249          // User data should be only removed in the user context.
 250          $systemcontext = \context_system::instance();
 251          // Add userlist2 to the approved user list in the system context.
 252          $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
 253          // Delete user1 data using delete_data_for_user.
 254          provider::delete_data_for_users($approvedlist);
 255          // Re-fetch users in usercontext2 - The user list should not be empty (user2).
 256          $userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
 257          provider::get_users_in_context($userlist1);
 258          $this->assertCount(1, $userlist1);
 259      }
 260  }