Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

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