Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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