Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [Versions 401 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   * Privacy test for the authentication mnet
  18   *
  19   * @package    auth_mnet
  20   * @category   test
  21   * @copyright  2018 Victor Deniz <victor@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace auth_mnet\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use auth_mnet\privacy\provider;
  29  use core_privacy\local\request\approved_contextlist;
  30  use core_privacy\local\request\writer;
  31  use core_privacy\tests\provider_testcase;
  32  use core_privacy\local\request\transform;
  33  use core_privacy\local\request\approved_userlist;
  34  
  35  /**
  36   * Privacy test for the authentication mnet
  37   *
  38   * @package    auth_mnet
  39   * @category   test
  40   * @copyright  2018 Victor Deniz <victor@moodle.com>
  41   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  42   */
  43  class provider_test extends provider_testcase {
  44      /**
  45       * Set up method.
  46       */
  47      public function setUp(): void {
  48          $this->resetAfterTest();
  49          $this->setAdminUser();
  50      }
  51  
  52      /**
  53       * Check that a user context is returned if there is any user data for this user.
  54       */
  55      public function test_get_contexts_for_userid() {
  56          global $DB;
  57  
  58          $user = $this->getDataGenerator()->create_user(['auth' => 'mnet']);
  59          $this->assertEmpty(provider::get_contexts_for_userid($user->id));
  60  
  61          // Insert mnet_log record.
  62          $logrecord = new \stdClass();
  63          $logrecord->hostid = '';
  64          $logrecord->remoteid = 65;
  65          $logrecord->time = time();
  66          $logrecord->userid = $user->id;
  67  
  68          $DB->insert_record('mnet_log', $logrecord);
  69  
  70          $contextlist = provider::get_contexts_for_userid($user->id);
  71  
  72          // Check that we only get back one context.
  73          $this->assertCount(1, $contextlist);
  74  
  75          // Check that a context is returned is the expected.
  76          $usercontext = \context_user::instance($user->id);
  77          $this->assertEquals($usercontext->id, $contextlist->get_contextids()[0]);
  78      }
  79  
  80      /**
  81       * Test that user data is exported correctly.
  82       */
  83      public function test_export_user_data() {
  84          global $DB;
  85  
  86          $user = $this->getDataGenerator()->create_user(['auth' => 'mnet']);
  87  
  88          // Insert mnet_host record.
  89          $hostrecord = new \stdClass();
  90          $hostrecord->wwwroot = 'https://external.moodle.com';
  91          $hostrecord->name = 'External Moodle';
  92          $hostrecord->public_key = '-----BEGIN CERTIFICATE-----';
  93  
  94          $hostid = $DB->insert_record('mnet_host', $hostrecord);
  95  
  96          // Insert mnet_log record.
  97          $logrecord = new \stdClass();
  98          $logrecord->hostid = $hostid;
  99          $logrecord->remoteid = 65;
 100          $logrecord->time = time();
 101          $logrecord->userid = $user->id;
 102          $logrecord->course = 3;
 103          $logrecord->coursename = 'test course';
 104  
 105          $DB->insert_record('mnet_log', $logrecord);
 106  
 107          $usercontext = \context_user::instance($user->id);
 108  
 109          /** @var \core_privacy\tests\request\content_writer $writer */
 110          $writer = writer::with_context($usercontext);
 111          $this->assertFalse($writer->has_any_data());
 112          $approvedlist = new approved_contextlist($user, 'auth_mnet', [$usercontext->id]);
 113          provider::export_user_data($approvedlist);
 114  
 115          $data = (array)$writer->get_data([get_string('pluginname', 'auth_mnet'), $hostrecord->name, $logrecord->coursename]);
 116  
 117          $this->assertEquals($logrecord->remoteid, reset($data)->remoteid);
 118          $this->assertEquals(transform::datetime($logrecord->time),  reset($data)->time);
 119      }
 120  
 121      /**
 122       * Test deleting all user data for a specific context.
 123       */
 124      public function test_delete_data_for_all_users_in_context() {
 125          global $DB;
 126  
 127          $user1 = $this->getDataGenerator()->create_user(['auth' => 'mnet']);
 128  
 129          // Insert mnet_log record.
 130          $logrecord1 = new \stdClass();
 131          $logrecord1->hostid = '';
 132          $logrecord1->remoteid = 65;
 133          $logrecord1->time = time();
 134          $logrecord1->userid = $user1->id;
 135  
 136          $DB->insert_record('mnet_log', $logrecord1);
 137  
 138          $user1context = \context_user::instance($user1->id);
 139  
 140          $user2 = $this->getDataGenerator()->create_user(['auth' => 'mnet']);
 141  
 142          // Insert mnet_log record.
 143          $logrecord2 = new \stdClass();
 144          $logrecord2->hostid = '';
 145          $logrecord2->remoteid = 65;
 146          $logrecord2->time = time();
 147          $logrecord2->userid = $user2->id;
 148  
 149          $DB->insert_record('mnet_log', $logrecord2);
 150  
 151          // Get all mnet log records.
 152          $mnetlogrecords = $DB->get_records('mnet_log', array());
 153          // There should be two.
 154          $this->assertCount(2, $mnetlogrecords);
 155  
 156          // Delete everything for the first user context.
 157          provider::delete_data_for_all_users_in_context($user1context);
 158  
 159          // Get all user1 mnet log records.
 160          $mnetlogrecords = $DB->get_records('mnet_log', ['userid' => $user1->id]);
 161          $this->assertCount(0, $mnetlogrecords);
 162  
 163          // Get all mnet log records.
 164          $mnetlogrecords = $DB->get_records('mnet_log', array());
 165          // There should be one (user2).
 166          $this->assertCount(1, $mnetlogrecords);
 167      }
 168  
 169      /**
 170       * This should work identical to the above test.
 171       */
 172      public function test_delete_data_for_user() {
 173          global $DB;
 174  
 175          $user1 = $this->getDataGenerator()->create_user(['auth' => 'mnet']);
 176  
 177          // Insert mnet_log record.
 178          $logrecord1 = new \stdClass();
 179          $logrecord1->hostid = '';
 180          $logrecord1->remoteid = 65;
 181          $logrecord1->time = time();
 182          $logrecord1->userid = $user1->id;
 183  
 184          $DB->insert_record('mnet_log', $logrecord1);
 185  
 186          $user1context = \context_user::instance($user1->id);
 187  
 188          $user2 = $this->getDataGenerator()->create_user(['auth' => 'mnet']);
 189  
 190          // Insert mnet_log record.
 191          $logrecord2 = new \stdClass();
 192          $logrecord2->hostid = '';
 193          $logrecord2->remoteid = 65;
 194          $logrecord2->time = time();
 195          $logrecord2->userid = $user2->id;
 196  
 197          $DB->insert_record('mnet_log', $logrecord2);
 198  
 199          // Get all mnet log records.
 200          $mnetlogrecords = $DB->get_records('mnet_log', array());
 201          // There should be two.
 202          $this->assertCount(2, $mnetlogrecords);
 203  
 204          // Delete everything for the first user.
 205          $approvedlist = new approved_contextlist($user1, 'auth_mnet', [$user1context->id]);
 206          provider::delete_data_for_user($approvedlist);
 207  
 208          // Get all user1 mnet log records.
 209          $mnetlogrecords = $DB->get_records('mnet_log', ['userid' => $user1->id]);
 210          $this->assertCount(0, $mnetlogrecords);
 211  
 212          // Get all mnet log records.
 213          $mnetlogrecords = $DB->get_records('mnet_log', array());
 214          // There should be one (user2).
 215          $this->assertCount(1, $mnetlogrecords);
 216      }
 217  
 218      /**
 219       * Test that only users with a user context are fetched.
 220       */
 221      public function test_get_users_in_context() {
 222          global $DB;
 223  
 224          $this->resetAfterTest();
 225  
 226          $component = 'auth_mnet';
 227          // Create a user.
 228          $user = $this->getDataGenerator()->create_user(['auth' => 'mnet']);
 229          $usercontext = \context_user::instance($user->id);
 230  
 231          // The list of users should not return anything yet (related data still haven't been created).
 232          $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
 233          provider::get_users_in_context($userlist);
 234          $this->assertCount(0, $userlist);
 235  
 236          // Insert mnet_log record.
 237          $logrecord = new \stdClass();
 238          $logrecord->hostid = '';
 239          $logrecord->remoteid = 65;
 240          $logrecord->time = time();
 241          $logrecord->userid = $user->id;
 242          $DB->insert_record('mnet_log', $logrecord);
 243  
 244          // The list of users for user context should return the user.
 245          provider::get_users_in_context($userlist);
 246          $this->assertCount(1, $userlist);
 247          $expected = [$user->id];
 248          $actual = $userlist->get_userids();
 249          $this->assertEquals($expected, $actual);
 250  
 251          // The list of users for system context should not return any users.
 252          $systemcontext = \context_system::instance();
 253          $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
 254          provider::get_users_in_context($userlist);
 255          $this->assertCount(0, $userlist);
 256      }
 257  
 258      /**
 259       * Test that data for users in approved userlist is deleted.
 260       */
 261      public function test_delete_data_for_users() {
 262          global $DB;
 263  
 264          $this->resetAfterTest();
 265  
 266          $component = 'auth_mnet';
 267          // Create user1.
 268          $user1 = $this->getDataGenerator()->create_user(['auth' => 'mnet']);
 269          $usercontext1 = \context_user::instance($user1->id);
 270          // Create user2.
 271          $user2 = $this->getDataGenerator()->create_user(['auth' => 'mnet']);
 272          $usercontext2 = \context_user::instance($user2->id);
 273  
 274          // Insert mnet_log record.
 275          $logrecord1 = new \stdClass();
 276          $logrecord1->hostid = '';
 277          $logrecord1->remoteid = 65;
 278          $logrecord1->time = time();
 279          $logrecord1->userid = $user1->id;
 280          $DB->insert_record('mnet_log', $logrecord1);
 281  
 282          // Insert mnet_log record.
 283          $logrecord2 = new \stdClass();
 284          $logrecord2->hostid = '';
 285          $logrecord2->remoteid = 65;
 286          $logrecord2->time = time();
 287          $logrecord2->userid = $user2->id;
 288          $DB->insert_record('mnet_log', $logrecord2);
 289  
 290          // The list of users for usercontext1 should return user1.
 291          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 292          provider::get_users_in_context($userlist1);
 293          $this->assertCount(1, $userlist1);
 294          $expected = [$user1->id];
 295          $actual = $userlist1->get_userids();
 296          $this->assertEquals($expected, $actual);
 297  
 298          // The list of users for usercontext2 should return user2.
 299          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 300          provider::get_users_in_context($userlist2);
 301          $this->assertCount(1, $userlist2);
 302          $expected = [$user2->id];
 303          $actual = $userlist2->get_userids();
 304          $this->assertEquals($expected, $actual);
 305  
 306          // Add userlist1 to the approved user list.
 307          $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
 308  
 309          // Delete user data using delete_data_for_user for usercontext1.
 310          provider::delete_data_for_users($approvedlist);
 311  
 312          // Re-fetch users in usercontext1 - The user list should now be empty.
 313          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 314          provider::get_users_in_context($userlist1);
 315          $this->assertCount(0, $userlist1);
 316          // Re-fetch users in usercontext2 - The user list should not be empty (user2).
 317          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 318          provider::get_users_in_context($userlist2);
 319          $this->assertCount(1, $userlist2);
 320  
 321          // User data should be only removed in the user context.
 322          $systemcontext = \context_system::instance();
 323          // Add userlist2 to the approved user list in the system context.
 324          $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
 325          // Delete user1 data using delete_data_for_user.
 326          provider::delete_data_for_users($approvedlist);
 327          // Re-fetch users in usercontext2 - The user list should not be empty (user2).
 328          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 329          provider::get_users_in_context($userlist2);
 330          $this->assertCount(1, $userlist2);
 331      }
 332  }