Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [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  /**
  18   * Base class for unit tests for enrol_meta.
  19   *
  20   * @package    enrol_meta
  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 enrol_meta\privacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_privacy\local\request\writer;
  30  use core_privacy\local\request\approved_contextlist;
  31  use enrol_meta\privacy\provider;
  32  
  33  /**
  34   * Unit tests for the enrol_meta implementation of the privacy API.
  35   *
  36   * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class provider_test extends \core_privacy\tests\provider_testcase {
  40  
  41      /**
  42       * Enable enrol_meta plugin.
  43       */
  44      protected function enable_plugin() {
  45          $enabled = enrol_get_plugins(true);
  46          $enabled['meta'] = true;
  47          $enabled = array_keys($enabled);
  48          set_config('enrol_plugins_enabled', implode(',', $enabled));
  49      }
  50  
  51      /**
  52       * Test getting the context for the user ID related to this plugin.
  53       */
  54      public function test_get_contexts_for_userid() {
  55          global $DB;
  56  
  57          $this->resetAfterTest();
  58          $metaplugin = enrol_get_plugin('meta');
  59          $user1 = $this->getDataGenerator()->create_user();
  60          $course1 = $this->getDataGenerator()->create_course();
  61          $course2 = $this->getDataGenerator()->create_course();
  62          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
  63          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  64  
  65          $this->enable_plugin();
  66          $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
  67          $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $studentrole->id);
  68          // Check if user1 is enrolled into course1 in group 1.
  69          $this->assertTrue(groups_is_member($group1->id, $user1->id));
  70          $this->assertTrue($DB->record_exists('groups_members',
  71              array(
  72                  'groupid' => $group1->id,
  73                  'userid' => $user1->id,
  74                  'component' => 'enrol_meta'
  75              )
  76          ));
  77          // Check context course fro provider to user1.
  78          $context = \context_course::instance($course1->id);
  79          $contextlist = provider::get_contexts_for_userid($user1->id);
  80          $this->assertEquals($context->id, $contextlist->current()->id);
  81      }
  82  
  83      /**
  84       * Test that user data is exported correctly.
  85       */
  86      public function test_export_user_data() {
  87          global $DB;
  88  
  89          $this->resetAfterTest();
  90          $metaplugin = enrol_get_plugin('meta');
  91          $user1 = $this->getDataGenerator()->create_user();
  92          $course1 = $this->getDataGenerator()->create_course();
  93          $course2 = $this->getDataGenerator()->create_course();
  94          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
  95          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  96  
  97          $this->enable_plugin();
  98          $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
  99          $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $studentrole->id);
 100  
 101          $this->setUser($user1);
 102          $contextlist = provider::get_contexts_for_userid($user1->id);
 103          $approvedcontextlist = new approved_contextlist($user1, 'enrol_meta', $contextlist->get_contextids());
 104          provider::export_user_data($approvedcontextlist);
 105          foreach ($contextlist as $context) {
 106              $writer = writer::with_context($context);
 107              $data = $writer->get_data([
 108                  get_string('pluginname', 'enrol_meta'),
 109                  get_string('groups', 'core_group')
 110              ]);
 111              $this->assertTrue($writer->has_any_data());
 112              if ($context->contextlevel == CONTEXT_COURSE) {
 113                  $exportedgroups = $data->groups;
 114                  // User1 only belongs to group1 via enrol_meta.
 115                  $this->assertCount(1, $exportedgroups);
 116                  $exportedgroup = reset($exportedgroups);
 117                  $this->assertEquals($group1->name, $exportedgroup->name);
 118              }
 119          }
 120      }
 121  
 122      /**
 123       * Test for provider::delete_data_for_all_users_in_context().
 124       */
 125      public function test_delete_data_for_all_users_in_context() {
 126          global $DB;
 127  
 128          $this->resetAfterTest();
 129          $metaplugin = enrol_get_plugin('meta');
 130          $user1 = $this->getDataGenerator()->create_user();
 131          $user2 = $this->getDataGenerator()->create_user();
 132          $user3 = $this->getDataGenerator()->create_user();
 133          $course1 = $this->getDataGenerator()->create_course();
 134          $course2 = $this->getDataGenerator()->create_course();
 135          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 136          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 137  
 138          $this->enable_plugin();
 139          $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
 140          $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $studentrole->id);
 141          $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $studentrole->id);
 142          $this->getDataGenerator()->enrol_user($user3->id, $course2->id, $studentrole->id);
 143          $this->assertEquals(
 144                  3,
 145                  $DB->count_records_sql("SELECT COUNT(gm.id)
 146                                        FROM {groups_members} gm
 147                                        JOIN {groups} g ON gm.groupid = g.id
 148                                       WHERE g.courseid = ?", [$course1->id])
 149          );
 150  
 151          $coursecontext1 = \context_course::instance($course1->id);
 152          provider::delete_data_for_all_users_in_context($coursecontext1);
 153          $this->assertEquals(
 154              0,
 155              $DB->count_records_sql("SELECT COUNT(gm.id)
 156                                        FROM {groups_members} gm
 157                                        JOIN {groups} g ON gm.groupid = g.id
 158                                       WHERE g.courseid = ?", [$course1->id])
 159          );
 160      }
 161  
 162      /**
 163       * Test for provider::delete_data_for_user().
 164       */
 165      public function test_delete_data_for_user() {
 166          global $DB;
 167  
 168          $this->resetAfterTest();
 169          $metaplugin = enrol_get_plugin('meta');
 170          $user1 = $this->getDataGenerator()->create_user();
 171          $user2 = $this->getDataGenerator()->create_user();
 172          $user3 = $this->getDataGenerator()->create_user();
 173          $course1 = $this->getDataGenerator()->create_course();
 174          $course2 = $this->getDataGenerator()->create_course();
 175          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 176          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 177  
 178          $this->enable_plugin();
 179          $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
 180          $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $studentrole->id);
 181          $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $studentrole->id);
 182          $this->getDataGenerator()->enrol_user($user3->id, $course2->id, $studentrole->id);
 183  
 184          $this->assertEquals(
 185                  3,
 186                  $DB->count_records_sql("SELECT COUNT(gm.id)
 187                                        FROM {groups_members} gm
 188                                        JOIN {groups} g ON gm.groupid = g.id
 189                                       WHERE g.courseid = ?", [$course1->id])
 190          );
 191  
 192          $this->setUser($user1);
 193          $coursecontext1 = \context_course::instance($course1->id);
 194          $coursecontext2 = \context_course::instance($course2->id);
 195          $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'enrol_meta',
 196                  [$coursecontext1->id]);
 197          provider::delete_data_for_user($approvedcontextlist);
 198          // Check we have 2 users in groups because we are deleted user1.
 199          $this->assertEquals(
 200                  2,
 201                  $DB->count_records_sql("SELECT COUNT(gm.id)
 202                                            FROM {groups_members} gm
 203                                            JOIN {groups} g ON gm.groupid = g.id
 204                                           WHERE g.courseid = ?", [$course1->id])
 205          );
 206      }
 207  
 208      /**
 209       * Test for provider::delete_data_for_users().
 210       */
 211      public function test_delete_data_for_users() {
 212          global $DB;
 213  
 214          $this->resetAfterTest();
 215  
 216          $metaplugin = enrol_get_plugin('meta');
 217  
 218          $user1 = $this->getDataGenerator()->create_user();
 219          $user2 = $this->getDataGenerator()->create_user();
 220          $user3 = $this->getDataGenerator()->create_user();
 221  
 222          $course1 = $this->getDataGenerator()->create_course();
 223          $course2 = $this->getDataGenerator()->create_course();
 224  
 225          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 226  
 227          $this->enable_plugin();
 228          $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
 229  
 230          $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'student');
 231          $this->getDataGenerator()->enrol_user($user2->id, $course2->id, 'student');
 232          $this->getDataGenerator()->enrol_user($user3->id, $course2->id, 'student');
 233  
 234          $this->assertEquals(
 235                  3,
 236                  $DB->count_records_sql("SELECT COUNT(gm.id)
 237                                        FROM {groups_members} gm
 238                                        JOIN {groups} g ON gm.groupid = g.id
 239                                       WHERE g.courseid = ?", [$course1->id])
 240          );
 241  
 242          $coursecontext1 = \context_course::instance($course1->id);
 243  
 244          $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_meta',
 245                  [$user1->id, $user2->id]);
 246          provider::delete_data_for_users($approveduserlist);
 247  
 248          // Check we have 1 user in groups because we have deleted user1 and user2.
 249          $this->assertEquals(
 250                  1,
 251                  $DB->count_records_sql("SELECT COUNT(gm.id)
 252                                            FROM {groups_members} gm
 253                                            JOIN {groups} g ON gm.groupid = g.id
 254                                           WHERE g.courseid = ?", [$course1->id])
 255          );
 256      }
 257  
 258      /**
 259       * Test for provider::get_users_in_context().
 260       */
 261      public function test_get_users_in_context() {
 262          global $DB;
 263  
 264          $this->resetAfterTest();
 265  
 266          $metaplugin = enrol_get_plugin('meta');
 267  
 268          $course1 = $this->getDataGenerator()->create_course();
 269          $course2 = $this->getDataGenerator()->create_course();
 270  
 271          $user1 = $this->getDataGenerator()->create_user();
 272          $user2 = $this->getDataGenerator()->create_user();
 273          $user3 = $this->getDataGenerator()->create_user();
 274  
 275          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 276  
 277          $this->enable_plugin();
 278          $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
 279  
 280          $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'student');
 281          $this->getDataGenerator()->enrol_user($user2->id, $course2->id, 'student');
 282  
 283          // Check if user1 is enrolled into course1 in group 1.
 284          $this->assertTrue(groups_is_member($group1->id, $user1->id));
 285          $this->assertTrue($DB->record_exists('groups_members',
 286                  array(
 287                      'groupid' => $group1->id,
 288                      'userid' => $user1->id,
 289                      'component' => 'enrol_meta'
 290                  )
 291          ));
 292  
 293          $context = \context_course::instance($course1->id);
 294  
 295          $userlist = new \core_privacy\local\request\userlist($context, 'enrol_meta');
 296          \enrol_meta\privacy\provider::get_users_in_context($userlist);
 297  
 298          $this->assertEqualsCanonicalizing(
 299                  [$user1->id, $user2->id],
 300                  $userlist->get_userids());
 301      }
 302  }