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 401] [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   * Privacy test for the core_enrol implementation of the privacy API.
  18   *
  19   * @package    core_enrol
  20   * @category   test
  21   * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace core_enrol\privacy;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_enrol\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 core_enrol.
  37   *
  38   * @package    core_enrol
  39   * @category   test
  40   * @copyright  2018 Carlos Escobedo <carlos@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       * Check that a course context is returned if there is any user data for this user.
  46       */
  47      public function test_get_contexts_for_userid() {
  48          $this->resetAfterTest();
  49          $user1 = $this->getDataGenerator()->create_user();
  50          $course1 = $this->getDataGenerator()->create_course();
  51          $this->assertEmpty(provider::get_contexts_for_userid($user1->id));
  52          // Enrol user into courses and check contextlist.
  53          $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'manual');
  54          $contextlist = provider::get_contexts_for_userid($user1->id);
  55          // Check that we only get back two context.
  56          $this->assertCount(1, $contextlist);
  57          // Check that the context is returned is the expected.
  58          $coursecontext1 = \context_course::instance($course1->id);
  59          $this->assertEquals($coursecontext1->id, $contextlist->get_contextids()[0]);
  60      }
  61      /**
  62       * Test that user data is exported correctly.
  63       */
  64      public function test_export_user_data() {
  65          global $DB;
  66  
  67          $this->resetAfterTest();
  68          $user1 = $this->getDataGenerator()->create_user();
  69          $course1 = $this->getDataGenerator()->create_course();
  70          $course2 = $this->getDataGenerator()->create_course();
  71          $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'manual');
  72          $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'self');
  73          $this->getDataGenerator()->enrol_user($user1->id, $course2->id,  null, 'manual');
  74          $subcontexts = [
  75              get_string('privacy:metadata:user_enrolments', 'core_enrol')
  76          ];
  77          $coursecontext1 = \context_course::instance($course1->id);
  78          $coursecontext2 = \context_course::instance($course2->id);
  79          $this->setUser($user1);
  80          $writer = writer::with_context($coursecontext1);
  81          $this->assertFalse($writer->has_any_data());
  82          $this->export_context_data_for_user($user1->id, $coursecontext1, 'core_enrol');
  83          $data = $writer->get_related_data($subcontexts);
  84          $this->assertCount(2, (array)$data);
  85  
  86          $sql = "SELECT ue.id,
  87                         ue.status,
  88                         ue.timestart,
  89                         ue.timeend,
  90                         ue.timecreated,
  91                         ue.timemodified
  92                    FROM {user_enrolments} ue
  93                    JOIN {enrol} e
  94                      ON e.id = ue.enrolid
  95                     AND e.courseid = :courseid
  96                   WHERE ue.userid = :userid";
  97          $enrolmentcouse2 = $DB->get_record_sql($sql, array('userid' => $user1->id, 'courseid' => $course2->id));
  98          writer::reset();
  99          $writer = writer::with_context($coursecontext2);
 100          $this->export_context_data_for_user($user1->id, $coursecontext2, 'core_enrol');
 101          $data = $writer->get_related_data($subcontexts, 'manual');
 102          $this->assertEquals($enrolmentcouse2->status, reset($data)->status);
 103          $this->assertEquals(transform::datetime($enrolmentcouse2->timestart), reset($data)->timestart);
 104          $this->assertEquals(transform::datetime($enrolmentcouse2->timeend), reset($data)->timeend);
 105          $this->assertEquals(transform::datetime($enrolmentcouse2->timecreated), reset($data)->timecreated);
 106          $this->assertEquals(transform::datetime($enrolmentcouse2->timemodified), reset($data)->timemodified);
 107      }
 108      /**
 109       * Test deleting all user data for a specific context.
 110       */
 111      public function test_delete_data_for_all_users_in_context() {
 112          global $DB;
 113  
 114          $this->resetAfterTest();
 115          $user1 = $this->getDataGenerator()->create_user();
 116          $user2 = $this->getDataGenerator()->create_user();
 117          $user3 = $this->getDataGenerator()->create_user();
 118          $course1 = $this->getDataGenerator()->create_course();
 119          $course2 = $this->getDataGenerator()->create_course();
 120          $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'manual');
 121          $this->getDataGenerator()->enrol_user($user2->id, $course1->id,  null, 'manual');
 122          $this->getDataGenerator()->enrol_user($user3->id, $course1->id,  null, 'manual');
 123          $this->getDataGenerator()->enrol_user($user1->id, $course2->id,  null, 'manual');
 124          $this->getDataGenerator()->enrol_user($user2->id, $course2->id,  null, 'manual');
 125          // Get all user enrolments.
 126          $userenrolments = $DB->get_records('user_enrolments', array());
 127          $this->assertCount(5, $userenrolments);
 128          // Get all user enrolments match with course1.
 129          $sql = "SELECT ue.id
 130                    FROM {user_enrolments} ue
 131                    JOIN {enrol} e
 132                      ON e.id = ue.enrolid
 133                     AND e.courseid = :courseid";
 134          $userenrolments = $DB->get_records_sql($sql, array('courseid' => $course1->id));
 135          $this->assertCount(3, $userenrolments);
 136          // Delete everything for the first course context.
 137          $coursecontext1 = \context_course::instance($course1->id);
 138          provider::delete_data_for_all_users_in_context($coursecontext1);
 139          // Get all user enrolments match with this course contest.
 140          $userenrolments = $DB->get_records_sql($sql, array('courseid' => $course1->id));
 141          $this->assertCount(0, $userenrolments);
 142          // Get all user enrolments.
 143          $userenrolments = $DB->get_records('user_enrolments', array());
 144          $this->assertCount(2, $userenrolments);
 145      }
 146      /**
 147       * This should work identical to the above test.
 148       */
 149      public function test_delete_data_for_user() {
 150          global $DB;
 151  
 152          $this->resetAfterTest();
 153          $user1 = $this->getDataGenerator()->create_user();
 154          $user2 = $this->getDataGenerator()->create_user();
 155          $user3 = $this->getDataGenerator()->create_user();
 156          $course1 = $this->getDataGenerator()->create_course();
 157          $course2 = $this->getDataGenerator()->create_course();
 158          $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'manual');
 159          $this->getDataGenerator()->enrol_user($user2->id, $course1->id,  null, 'manual');
 160          $this->getDataGenerator()->enrol_user($user3->id, $course1->id,  null, 'manual');
 161          $this->getDataGenerator()->enrol_user($user1->id, $course2->id,  null, 'manual');
 162  
 163          // Get all user enrolments.
 164          $userenrolments = $DB->get_records('user_enrolments', array());
 165          $this->assertCount(4, $userenrolments);
 166          // Get all user enrolments match with user1.
 167          $userenrolments = $DB->get_records('user_enrolments', array('userid' => $user1->id));
 168          $this->assertCount(2, $userenrolments);
 169          // Delete everything for the user1 in the context course 1.
 170          $coursecontext1 = \context_course::instance($course1->id);
 171          $approvedlist = new approved_contextlist($user1, 'core_enrol', [$coursecontext1->id]);
 172          provider::delete_data_for_user($approvedlist);
 173          // Get all user enrolments match with user.
 174          $userenrolments = $DB->get_records('user_enrolments', ['userid' => $user1->id]);
 175          $this->assertCount(1, $userenrolments);
 176          // Get all user enrolments accounts.
 177          $userenrolments = $DB->get_records('user_enrolments', array());
 178          $this->assertCount(3, $userenrolments);
 179      }
 180  
 181      /**
 182       * Test that only users within a course context are fetched.
 183       */
 184      public function test_get_users_in_context() {
 185          $this->resetAfterTest();
 186  
 187          $component = 'core_enrol';
 188  
 189          $user = $this->getDataGenerator()->create_user();
 190          $usercontext = \context_user::instance($user->id);
 191          $course = $this->getDataGenerator()->create_course();
 192          $coursecontext = \context_course::instance($course->id);
 193  
 194          $userlist1 = new \core_privacy\local\request\userlist($coursecontext, $component);
 195          provider::get_users_in_context($userlist1);
 196          $this->assertCount(0, $userlist1);
 197  
 198          // Enrol user into course.
 199          $this->getDataGenerator()->enrol_user($user->id, $course->id,  null, 'manual');
 200  
 201          // The list of users within the course context should contain user.
 202          provider::get_users_in_context($userlist1);
 203          $this->assertCount(1, $userlist1);
 204          $expected = [$user->id];
 205          $actual = $userlist1->get_userids();
 206          $this->assertEquals($expected, $actual);
 207  
 208          // The list of users within the user context should be empty.
 209          $userlist2 = new \core_privacy\local\request\userlist($usercontext, $component);
 210          provider::get_users_in_context($userlist2);
 211          $this->assertCount(0, $userlist2);
 212      }
 213  
 214      /**
 215       * Test that data for users in approved userlist is deleted.
 216       */
 217      public function test_delete_data_for_users() {
 218          $this->resetAfterTest();
 219  
 220          $component = 'core_enrol';
 221  
 222          $user1 = $this->getDataGenerator()->create_user();
 223          $user2 = $this->getDataGenerator()->create_user();
 224          $user3 = $this->getDataGenerator()->create_user();
 225          $course1 = $this->getDataGenerator()->create_course();
 226          $course2 = $this->getDataGenerator()->create_course();
 227          $coursecontext1 = \context_course::instance($course1->id);
 228          $coursecontext2 = \context_course::instance($course2->id);
 229          $systemcontext = \context_system::instance();
 230  
 231          // Enrol user1 into course1.
 232          $this->getDataGenerator()->enrol_user($user1->id, $course1->id,  null, 'manual');
 233          // Enrol user2 into course1.
 234          $this->getDataGenerator()->enrol_user($user2->id, $course1->id,  null, 'manual');
 235          // Enrol user3 into course2.
 236          $this->getDataGenerator()->enrol_user($user3->id, $course2->id,  null, 'manual');
 237  
 238          $userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
 239          provider::get_users_in_context($userlist1);
 240          $this->assertCount(2, $userlist1);
 241  
 242          $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
 243          provider::get_users_in_context($userlist2);
 244          $this->assertCount(1, $userlist2);
 245  
 246          // Convert $userlist1 into an approved_contextlist.
 247          $approvedlist1 = new approved_userlist($coursecontext1, $component, $userlist1->get_userids());
 248          // Delete using delete_data_for_user.
 249          provider::delete_data_for_users($approvedlist1);
 250          // Re-fetch users in coursecontext1.
 251          $userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
 252          provider::get_users_in_context($userlist1);
 253          // The user data in coursecontext1 should be deleted.
 254          $this->assertCount(0, $userlist1);
 255  
 256          // Re-fetch users in coursecontext2.
 257          $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
 258          provider::get_users_in_context($userlist2);
 259          // The user data in coursecontext2 should be still present.
 260          $this->assertCount(1, $userlist2);
 261  
 262          // Convert $userlist2 into an approved_contextlist in the system context.
 263          $approvedlist2 = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
 264          // Delete using delete_data_for_user.
 265          provider::delete_data_for_users($approvedlist2);
 266          // Re-fetch users in coursecontext1.
 267          $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
 268          provider::get_users_in_context($userlist2);
 269          // The user data in systemcontext should not be deleted.
 270          $this->assertCount(1, $userlist2);
 271      }
 272  }