Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   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 test for the mnetservice_enrol implementation of the privacy API.
  19   *
  20   * @package    mnetservice_enrol
  21   * @category   test
  22   * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use mnetservice_enrol\privacy\provider;
  29  use core_privacy\local\request\approved_contextlist;
  30  use core_privacy\local\request\writer;
  31  use core_privacy\local\request\transform;
  32  use core_privacy\tests\provider_testcase;
  33  use core_privacy\local\request\approved_userlist;
  34  
  35  /**
  36   * Privacy test for the mnetservice_enrol.
  37   *
  38   * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class mnetservice_enrol_privacy_testcase extends provider_testcase {
  42  
  43      /** @var stdClass the mnet host we are using to test. */
  44      protected $mnethost;
  45  
  46      /** @var stdClass the mnet service enrolment to test. */
  47      protected $enrolment;
  48  
  49      /**
  50       * Test set up.
  51       *
  52       * This is executed before running any test in this file.
  53       */
  54      public function setUp(): void {
  55          global $DB;
  56  
  57          // Add a mnet host.
  58          $this->mnethost = new stdClass();
  59          $this->mnethost->name = 'A mnet host';
  60          $this->mnethost->public_key = 'A random public key!';
  61          $this->mnethost->id = $DB->insert_record('mnet_host', $this->mnethost);
  62      }
  63  
  64      /**
  65       * Check that a user context is returned if there is any user data for this user.
  66       */
  67      public function test_get_contexts_for_userid() {
  68          $this->resetAfterTest();
  69          $user = $this->getDataGenerator()->create_user();
  70          $this->assertEmpty(provider::get_contexts_for_userid($user->id));
  71  
  72          // Create a test MNet service enrol enrolments.
  73          $remotecourseid = 101;
  74          $this->insert_mnetservice_enrol_courses($remotecourseid);
  75          $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid);
  76  
  77          $contextlist = provider::get_contexts_for_userid($user->id);
  78          // Check that we only get back two context.
  79          $this->assertCount(1, $contextlist);
  80          // Check that the contexts are returned are the expected.
  81          $usercontext = \context_user::instance($user->id);
  82          $this->assertEquals($usercontext->id, $contextlist->get_contextids()[0]);
  83      }
  84  
  85      /**
  86       * Test that user data is exported correctly.
  87       */
  88      public function test_export_user_data() {
  89          global $DB;
  90  
  91          $this->resetAfterTest();
  92          $user = $this->getDataGenerator()->create_user();
  93          $this->assertEmpty(provider::get_contexts_for_userid($user->id));
  94  
  95          // Create a test MNet service enrol enrolments.
  96          $remotecourseid = 101;
  97          $this->insert_mnetservice_enrol_courses($remotecourseid);
  98          $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid);
  99  
 100          $subcontexts = [
 101              get_string('privacy:metadata:mnetservice_enrol_enrolments', 'mnetservice_enrol')
 102          ];
 103          $usercontext = \context_user::instance($user->id);
 104          $writer = writer::with_context($usercontext);
 105          $this->assertFalse($writer->has_any_data());
 106          $approvedlist = new approved_contextlist($user, 'mnetservice_enrol', [$usercontext->id]);
 107          provider::export_user_data($approvedlist);
 108          $data = $writer->get_data($subcontexts);
 109          $this->assertCount(1, (array)$data);
 110          $this->assertEquals($this->mnethost->name, reset($data)->host);
 111          $remotecoursename = $DB->get_field('mnetservice_enrol_courses', 'fullname',
 112              array('remoteid' => $this->enrolment->remotecourseid));
 113          $this->assertEquals($remotecoursename, reset($data)->remotecourseid);
 114          $this->assertEquals($this->enrolment->rolename, reset($data)->rolename);
 115          $this->assertEquals($this->enrolment->enroltype, reset($data)->enroltype);
 116          $this->assertEquals(transform::datetime($this->enrolment->enroltime), reset($data)->enroltime);
 117      }
 118  
 119      /**
 120       * Test deleting all user data for a specific context.
 121       */
 122      public function test_delete_data_for_all_users_in_context() {
 123          global $DB;
 124  
 125          $this->resetAfterTest();
 126          $user = $this->getDataGenerator()->create_user();
 127          $user2 = $this->getDataGenerator()->create_user();
 128          $user3 = $this->getDataGenerator()->create_user();
 129          $this->assertEmpty(provider::get_contexts_for_userid($user->id));
 130  
 131          // Create a test MNet service enrol enrolments.
 132          $remotecourseid = 101;
 133          $this->insert_mnetservice_enrol_courses($remotecourseid);
 134          $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid);
 135          $this->insert_mnetservice_enrol_enrolments($user2->id, $remotecourseid);
 136          $this->insert_mnetservice_enrol_enrolments($user3->id, $remotecourseid);
 137          $usercontext = \context_user::instance($user->id);
 138          // Get all user enrolments.
 139          $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array());
 140          $this->assertCount(3, $userenrolments);
 141          // Get all user enrolments match with user.
 142          $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array('userid' => $user->id));
 143          $this->assertCount(1, $userenrolments);
 144          // Delete everything for the first user context.
 145          provider::delete_data_for_all_users_in_context($usercontext);
 146          // Get all user enrolments match with user.
 147          $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', ['userid' => $user->id]);
 148          $this->assertCount(0, $userenrolments);
 149          // Get all user enrolments.
 150          $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array());
 151          $this->assertCount(2, $userenrolments);
 152      }
 153  
 154      /**
 155       * This should work identical to the above test.
 156       */
 157      public function test_delete_data_for_user() {
 158          global $DB;
 159  
 160          $this->resetAfterTest();
 161          $user = $this->getDataGenerator()->create_user();
 162          $user2 = $this->getDataGenerator()->create_user();
 163          $user3 = $this->getDataGenerator()->create_user();
 164          $this->assertEmpty(provider::get_contexts_for_userid($user->id));
 165  
 166          $remotecourseid = 101;
 167          $this->insert_mnetservice_enrol_courses($remotecourseid);
 168          $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid);
 169          $this->insert_mnetservice_enrol_enrolments($user2->id, $remotecourseid);
 170          $this->insert_mnetservice_enrol_enrolments($user3->id, $remotecourseid);
 171          $remotecourseid2 = 102;
 172          $this->insert_mnetservice_enrol_courses($remotecourseid2);
 173          $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid2);
 174  
 175          $usercontext = \context_user::instance($user->id);
 176          // Get all user enrolments.
 177          $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array());
 178          $this->assertCount(4, $userenrolments);
 179          // Get all user enrolments match with user.
 180          $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array('userid' => $user->id));
 181          $this->assertCount(2, $userenrolments);
 182          // Delete everything for the first user.
 183          $approvedlist = new approved_contextlist($user, 'mnetservice_enrol', [$usercontext->id]);
 184          provider::delete_data_for_user($approvedlist);
 185          // Get all user enrolments match with user.
 186          $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', ['userid' => $user->id]);
 187          $this->assertCount(0, $userenrolments);
 188          // Get all user enrolments accounts.
 189          $userenrolments = $DB->get_records('mnetservice_enrol_enrolments', array());
 190          $this->assertCount(2, $userenrolments);
 191      }
 192  
 193      /**
 194       * Test that only users within a course context are fetched.
 195       */
 196      public function test_get_users_in_context() {
 197          $this->resetAfterTest();
 198  
 199          $component = 'mnetservice_enrol';
 200  
 201          // Create a user.
 202          $user = $this->getDataGenerator()->create_user();
 203          $usercontext = context_user::instance($user->id);
 204  
 205          // Create user2.
 206          $user2 = $this->getDataGenerator()->create_user();
 207  
 208          $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
 209          provider::get_users_in_context($userlist);
 210          $this->assertCount(0, $userlist);
 211  
 212          // Create a test MNet service enrol enrolments.
 213          $remotecourseid = 101;
 214          $this->insert_mnetservice_enrol_courses($remotecourseid);
 215          $this->insert_mnetservice_enrol_enrolments($user->id, $remotecourseid);
 216          $this->insert_mnetservice_enrol_enrolments($user2->id, $remotecourseid);
 217  
 218          // The list of users within the user context should contain only user.
 219          provider::get_users_in_context($userlist);
 220          $this->assertCount(1, $userlist);
 221          $this->assertFalse(in_array($user2->id, $userlist->get_userids()));
 222          $this->assertTrue(in_array($user->id, $userlist->get_userids()));
 223  
 224          // The list of users within the system context should be empty.
 225          $systemcontext = context_system::instance();
 226          $userlist2 = new \core_privacy\local\request\userlist($systemcontext, $component);
 227          provider::get_users_in_context($userlist2);
 228          $this->assertCount(0, $userlist2);
 229      }
 230  
 231      /**
 232       * Test that data for users in approved userlist is deleted.
 233       */
 234      public function test_delete_data_for_users() {
 235          $this->resetAfterTest();
 236  
 237          $component = 'mnetservice_enrol';
 238  
 239          // Create user1.
 240          $user1 = $this->getDataGenerator()->create_user();
 241          $usercontext1 = context_user::instance($user1->id);
 242          // Create user2.
 243          $user2 = $this->getDataGenerator()->create_user();
 244          $usercontext2 = context_user::instance($user2->id);
 245  
 246          // Create a test MNet service enrol enrolments.
 247          $remotecourseid = 101;
 248          $this->insert_mnetservice_enrol_courses($remotecourseid);
 249          $this->insert_mnetservice_enrol_enrolments($user1->id, $remotecourseid);
 250          $this->insert_mnetservice_enrol_enrolments($user2->id, $remotecourseid);
 251  
 252          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 253          provider::get_users_in_context($userlist1);
 254          $this->assertCount(1, $userlist1);
 255          $expected = [$user1->id];
 256          $actual = $userlist1->get_userids();
 257          $this->assertEquals($expected, $actual);
 258  
 259          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 260          provider::get_users_in_context($userlist2);
 261          $this->assertCount(1, $userlist2);
 262          $expected = [$user2->id];
 263          $actual = $userlist2->get_userids();
 264          $this->assertEquals($expected, $actual);
 265  
 266          // Convert $userlist1 into an approved_contextlist.
 267          $approvedlist1 = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
 268          // Delete using delete_data_for_user.
 269          provider::delete_data_for_users($approvedlist1);
 270  
 271          // Re-fetch users in usercontext1.
 272          $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
 273          provider::get_users_in_context($userlist1);
 274          // The user data in usercontext1 should be deleted.
 275          $this->assertCount(0, $userlist1);
 276  
 277          // Re-fetch users in usercontext2.
 278          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 279          provider::get_users_in_context($userlist2);
 280          // The user data in usercontext2 should be still present.
 281          $this->assertCount(1, $userlist2);
 282  
 283          // Convert $userlist2 into an approved_contextlist in the system context.
 284          $systemcontext = context_system::instance();
 285          $approvedlist3 = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
 286          // Delete using delete_data_for_user.
 287          provider::delete_data_for_users($approvedlist3);
 288          // Re-fetch users in usercontext2.
 289          $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
 290          provider::get_users_in_context($userlist2);
 291          // The user data in systemcontext should not be deleted.
 292          $this->assertCount(1, $userlist2);
 293      }
 294  
 295      /**
 296       * Help function to create a simulation of MNet service enrol.
 297       * Create a Dummy Enrol into mnetservice_enrol_enrolments.
 298       *
 299       * @param  int $userid  Userid.
 300       * @param  int $remotecourseid  Remotecourseid.
 301       */
 302      protected function insert_mnetservice_enrol_enrolments($userid, $remotecourseid) {
 303          global $DB;
 304  
 305          // Create a test MNet service enrol enrolments.
 306          $this->enrolment                  = new stdclass();
 307          $this->enrolment->hostid          = $this->mnethost->id;
 308          $this->enrolment->userid          = $userid;
 309          $this->enrolment->remotecourseid  = $remotecourseid;
 310          $this->enrolment->rolename        = 'student';
 311          $this->enrolment->enroltime       = time();
 312          $this->enrolment->enroltype       = 'mnet';
 313          $DB->insert_record('mnetservice_enrol_enrolments', $this->enrolment);
 314      }
 315  
 316      /**
 317       * Help function to create a simualtion of MNet service enrol.
 318       * Create a Dummy Course into mnetservice_enrol_courses.
 319       * Important: The real course is on the host.
 320       *
 321       * @param  int    $remoteid  Remote courseid.
 322       */
 323      protected function insert_mnetservice_enrol_courses($remoteid) {
 324          global $DB;
 325  
 326          // Create a Dummy Remote Course to test.
 327          $course                 = new stdclass();
 328          $course->hostid         = $this->mnethost->id;
 329          $course->remoteid       = $remoteid;
 330          $course->categoryid     = 1;
 331          $course->categoryname   = 'Miscellaneous';
 332          $course->sortorder      = 10001;
 333          $course->fullname       = 'Test Remote Course '.$remoteid;
 334          $course->shortname      = 'testremotecourse '.$remoteid;
 335          $course->idnumber       = 'IdnumberRemote '.$remoteid;
 336          $course->summary        = 'TestSummaryRemote '.$remoteid;
 337          $course->summaryformat  = FORMAT_MOODLE;
 338          $course->startdate      = time();
 339          $course->roleid         = 5;
 340          $course->rolename       = 'student';
 341          $DB->insert_record('mnetservice_enrol_courses', $course);
 342      }
 343  }