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.
   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 tests for enrol_flatfile.
  19   *
  20   * @package    enrol_flatfile
  21   * @category   test
  22   * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace enrol_flatfile\privacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_privacy\local\metadata\collection;
  30  use core_privacy\tests\provider_testcase;
  31  use core_privacy\local\request\approved_contextlist;
  32  use core_privacy\local\request\writer;
  33  use enrol_flatfile\privacy\provider;
  34  
  35  /**
  36   * Privacy tests for enrol_flatfile.
  37   *
  38   * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class provider_test extends provider_testcase {
  42  
  43      /** @var \stdClass $user1 a test user.*/
  44      protected $user1;
  45  
  46      /** @var \stdClass $user2 a test user.*/
  47      protected $user2;
  48  
  49      /** @var \stdClass $user3 a test user.*/
  50      protected $user3;
  51  
  52      /** @var \stdClass $user4 a test user.*/
  53      protected $user4;
  54  
  55      /** @var \context $coursecontext1 a course context.*/
  56      protected $coursecontext1;
  57  
  58      /** @var \context $coursecontext2 a course context.*/
  59      protected $coursecontext2;
  60  
  61      /** @var \context $coursecontext3 a course context.*/
  62      protected $coursecontext3;
  63  
  64      /**
  65       * Called before every test.
  66       */
  67      public function setUp(): void {
  68          $this->resetAfterTest(true);
  69      }
  70  
  71      /**
  72       * Verify that get_metadata returns the database table mapping.
  73       */
  74      public function test_get_metadata() {
  75          $collection = new collection('enrol_flatfile');
  76          $collection = provider::get_metadata($collection);
  77          $collectiondata = $collection->get_collection();
  78          $this->assertNotEmpty($collectiondata);
  79          $this->assertInstanceOf(\core_privacy\local\metadata\types\database_table::class, $collectiondata[0]);
  80      }
  81  
  82      /**
  83       * Verify that the relevant course contexts are returned for users with pending enrolment records.
  84       */
  85      public function test_get_contexts_for_user() {
  86          global $DB;
  87          // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
  88          $this->create_future_enrolments();
  89  
  90          $this->assertEquals(5, $DB->count_records('enrol_flatfile'));
  91  
  92          // We expect to see 2 entries for user1, in course1 and course3.
  93          $contextlist = provider::get_contexts_for_userid($this->user1->id);
  94          $this->assertEquals(2, $contextlist->count());
  95          $contextids = $contextlist->get_contextids();
  96          $this->assertContainsEquals($this->coursecontext1->id, $contextids);
  97          $this->assertContainsEquals($this->coursecontext3->id, $contextids);
  98  
  99          // And 1 for user2 on course2.
 100          $contextlist = provider::get_contexts_for_userid($this->user2->id);
 101          $this->assertEquals(1, $contextlist->count());
 102          $contextids = $contextlist->get_contextids();
 103          $this->assertContainsEquals($this->coursecontext2->id, $contextids);
 104      }
 105  
 106      /**
 107       * Verify the export includes any future enrolment records for the user.
 108       */
 109      public function test_export_user_data() {
 110          // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
 111          $this->create_future_enrolments();
 112  
 113          // Get contexts containing user data.
 114          $contextlist = provider::get_contexts_for_userid($this->user1->id);
 115          $this->assertEquals(2, $contextlist->count());
 116  
 117          $approvedcontextlist = new approved_contextlist(
 118              $this->user1,
 119              'enrol_flatfile',
 120              $contextlist->get_contextids()
 121          );
 122  
 123          // Export for the approved contexts.
 124          provider::export_user_data($approvedcontextlist);
 125  
 126          // Verify we see one future course enrolment in course1, and one in course3.
 127          $subcontext = \core_enrol\privacy\provider::get_subcontext([get_string('pluginname', 'enrol_flatfile')]);
 128  
 129          $writer = writer::with_context($this->coursecontext1);
 130          $this->assertNotEmpty($writer->get_data($subcontext));
 131  
 132          $writer = writer::with_context($this->coursecontext3);
 133          $this->assertNotEmpty($writer->get_data($subcontext));
 134  
 135          // Verify we have nothing in course 2 for this user.
 136          $writer = writer::with_context($this->coursecontext2);
 137          $this->assertEmpty($writer->get_data($subcontext));
 138      }
 139  
 140      /**
 141       * Verify export will limit any future enrolment records to only those contextids provided.
 142       */
 143      public function test_export_user_data_restricted_context_subset() {
 144          // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
 145          $this->create_future_enrolments();
 146  
 147          // Now, limit the export scope to just course1's context and verify only that data is seen in any export.
 148          $subsetapprovedcontextlist = new approved_contextlist(
 149              $this->user1,
 150              'enrol_flatfile',
 151              [$this->coursecontext1->id]
 152          );
 153  
 154          // Export for the approved contexts.
 155          provider::export_user_data($subsetapprovedcontextlist);
 156  
 157          // Verify we see one future course enrolment in course1 only.
 158          $subcontext = \core_enrol\privacy\provider::get_subcontext([get_string('pluginname', 'enrol_flatfile')]);
 159  
 160          $writer = writer::with_context($this->coursecontext1);
 161          $this->assertNotEmpty($writer->get_data($subcontext));
 162  
 163          // And nothing in the course3 context.
 164          $writer = writer::with_context($this->coursecontext3);
 165          $this->assertEmpty($writer->get_data($subcontext));
 166      }
 167  
 168      /**
 169       * Verify that records can be deleted by context.
 170       */
 171      public function test_delete_data_for_all_users_in_context() {
 172          global $DB;
 173          // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
 174          $this->create_future_enrolments();
 175  
 176          // Verify we have 3 future enrolments for course 1.
 177          $this->assertEquals(3, $DB->count_records('enrol_flatfile', ['courseid' => $this->coursecontext1->instanceid]));
 178  
 179          // Now, run delete by context and confirm that all records are removed.
 180          provider::delete_data_for_all_users_in_context($this->coursecontext1);
 181          $this->assertEquals(0, $DB->count_records('enrol_flatfile', ['courseid' => $this->coursecontext1->instanceid]));
 182      }
 183  
 184      public function test_delete_data_for_user() {
 185          global $DB;
 186          // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
 187          $this->create_future_enrolments();
 188  
 189          // Verify we have 2 future enrolments for course 1 and course 3.
 190          $contextlist = provider::get_contexts_for_userid($this->user1->id);
 191          $this->assertEquals(2, $contextlist->count());
 192          $contextids = $contextlist->get_contextids();
 193          $this->assertContainsEquals($this->coursecontext1->id, $contextids);
 194          $this->assertContainsEquals($this->coursecontext3->id, $contextids);
 195  
 196          $approvedcontextlist = new approved_contextlist(
 197              $this->user1,
 198              'enrol_flatfile',
 199              $contextids
 200          );
 201  
 202          // Now, run delete for user and confirm that both records are removed.
 203          provider::delete_data_for_user($approvedcontextlist);
 204          $contextlist = provider::get_contexts_for_userid($this->user1->id);
 205          $this->assertEquals(0, $contextlist->count());
 206          $this->assertEquals(0, $DB->count_records('enrol_flatfile', ['userid' => $this->user1->id]));
 207      }
 208  
 209      /**
 210       * Test for provider::get_users_in_context().
 211       */
 212      public function test_get_users_in_context() {
 213          global $DB;
 214          // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
 215          $this->create_future_enrolments();
 216  
 217          $this->assertEquals(5, $DB->count_records('enrol_flatfile'));
 218  
 219          // We expect to see 3 entries for course1, and that's user1, user3 and user4.
 220          $userlist = new \core_privacy\local\request\userlist($this->coursecontext1, 'enrol_flatfile');
 221          provider::get_users_in_context($userlist);
 222          $this->assertEqualsCanonicalizing(
 223                  [$this->user1->id, $this->user3->id, $this->user4->id],
 224                  $userlist->get_userids());
 225  
 226          // And 1 for course2 which is for user2.
 227          $userlist = new \core_privacy\local\request\userlist($this->coursecontext2, 'enrol_flatfile');
 228          provider::get_users_in_context($userlist);
 229          $this->assertEquals([$this->user2->id], $userlist->get_userids());
 230  
 231          // And 1 for course3 which is for user1 again.
 232          $userlist = new \core_privacy\local\request\userlist($this->coursecontext3, 'enrol_flatfile');
 233          provider::get_users_in_context($userlist);
 234          $this->assertEquals([$this->user1->id], $userlist->get_userids());
 235      }
 236  
 237      /**
 238       * Test for provider::delete_data_for_users().
 239       */
 240      public function test_delete_data_for_users() {
 241          global $DB;
 242  
 243          // Create, via flatfile syncing, the future enrolments entries in the enrol_flatfile table.
 244          $this->create_future_enrolments();
 245  
 246          // Verify we have 3 future enrolment for user 1, user 3 and user 4.
 247          $userlist = new \core_privacy\local\request\userlist($this->coursecontext1, 'enrol_flatfile');
 248          provider::get_users_in_context($userlist);
 249          $this->assertEqualsCanonicalizing(
 250                  [$this->user1->id, $this->user3->id, $this->user4->id],
 251                  $userlist->get_userids());
 252  
 253          $approveduserlist = new \core_privacy\local\request\approved_userlist($this->coursecontext1, 'enrol_flatfile',
 254                  [$this->user1->id, $this->user3->id]);
 255  
 256          // Now, run delete for user and confirm that the record is removed.
 257          provider::delete_data_for_users($approveduserlist);
 258          $userlist = new \core_privacy\local\request\userlist($this->coursecontext1, 'enrol_flatfile');
 259          provider::get_users_in_context($userlist);
 260          $this->assertEquals([$this->user4->id], $userlist->get_userids());
 261          $this->assertEquals(
 262                  [$this->user4->id],
 263                  $DB->get_fieldset_select('enrol_flatfile', 'userid', 'courseid = ?', [$this->coursecontext1->instanceid])
 264          );
 265      }
 266  
 267      /**
 268       * Helper to sync a file and create the enrol_flatfile DB entries, for use with the get, export and delete tests.
 269       */
 270      protected function create_future_enrolments() {
 271          global $CFG;
 272          $this->user1 = $this->getDataGenerator()->create_user(['idnumber' => 'u1']);
 273          $this->user2 = $this->getDataGenerator()->create_user(['idnumber' => 'u2']);
 274          $this->user3 = $this->getDataGenerator()->create_user(['idnumber' => 'u3']);
 275          $this->user4 = $this->getDataGenerator()->create_user(['idnumber' => 'u4']);
 276  
 277          $course1 = $this->getDataGenerator()->create_course(['idnumber' => 'c1']);
 278          $course2 = $this->getDataGenerator()->create_course(['idnumber' => 'c2']);
 279          $course3 = $this->getDataGenerator()->create_course(['idnumber' => 'c3']);
 280          $this->coursecontext1 = \context_course::instance($course1->id);
 281          $this->coursecontext2 = \context_course::instance($course2->id);
 282          $this->coursecontext3 = \context_course::instance($course3->id);
 283  
 284          $now = time();
 285          $future = $now + 60 * 60 * 5;
 286          $farfuture = $now + 60 * 60 * 24 * 5;
 287  
 288          $file = "$CFG->dataroot/enrol.txt";
 289          $data = "add,student,u1,c1,$future,0
 290                   add,student,u2,c2,$future,0
 291                   add,student,u3,c1,$future,0
 292                   add,student,u4,c1,$future,0
 293                   add,student,u1,c3,$future,$farfuture";
 294          file_put_contents($file, $data);
 295  
 296          $trace = new \null_progress_trace();
 297          $this->enable_plugin();
 298          $flatfileplugin = enrol_get_plugin('flatfile');
 299          $flatfileplugin->set_config('location', $file);
 300          $flatfileplugin->sync($trace);
 301      }
 302  
 303      /**
 304       * Enables the flatfile plugin for testing.
 305       */
 306      protected function enable_plugin() {
 307          $enabled = enrol_get_plugins(true);
 308          $enabled['flatfile'] = true;
 309          $enabled = array_keys($enabled);
 310          set_config('enrol_plugins_enabled', implode(',', $enabled));
 311      }
 312  }