Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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              /** @var \core_privacy\tests\request\content_writer $writer */
 107              $writer = writer::with_context($context);
 108              $data = $writer->get_data([
 109                  get_string('pluginname', 'enrol_meta'),
 110                  get_string('groups', 'core_group')
 111              ]);
 112              $this->assertTrue($writer->has_any_data());
 113              if ($context->contextlevel == CONTEXT_COURSE) {
 114                  $exportedgroups = $data->groups;
 115                  // User1 only belongs to group1 via enrol_meta.
 116                  $this->assertCount(1, $exportedgroups);
 117                  $exportedgroup = reset($exportedgroups);
 118                  $this->assertEquals($group1->name, $exportedgroup->name);
 119              }
 120          }
 121      }
 122  
 123      /**
 124       * Test for provider::delete_data_for_all_users_in_context().
 125       */
 126      public function test_delete_data_for_all_users_in_context() {
 127          global $DB;
 128  
 129          $this->resetAfterTest();
 130          $metaplugin = enrol_get_plugin('meta');
 131          $user1 = $this->getDataGenerator()->create_user();
 132          $user2 = $this->getDataGenerator()->create_user();
 133          $user3 = $this->getDataGenerator()->create_user();
 134          $course1 = $this->getDataGenerator()->create_course();
 135          $course2 = $this->getDataGenerator()->create_course();
 136          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 137          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 138  
 139          $this->enable_plugin();
 140          $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
 141          $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $studentrole->id);
 142          $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $studentrole->id);
 143          $this->getDataGenerator()->enrol_user($user3->id, $course2->id, $studentrole->id);
 144          $this->assertEquals(
 145                  3,
 146                  $DB->count_records_sql("SELECT COUNT(gm.id)
 147                                        FROM {groups_members} gm
 148                                        JOIN {groups} g ON gm.groupid = g.id
 149                                       WHERE g.courseid = ?", [$course1->id])
 150          );
 151  
 152          $coursecontext1 = \context_course::instance($course1->id);
 153          provider::delete_data_for_all_users_in_context($coursecontext1);
 154          $this->assertEquals(
 155              0,
 156              $DB->count_records_sql("SELECT COUNT(gm.id)
 157                                        FROM {groups_members} gm
 158                                        JOIN {groups} g ON gm.groupid = g.id
 159                                       WHERE g.courseid = ?", [$course1->id])
 160          );
 161      }
 162  
 163      /**
 164       * Test for provider::delete_data_for_user().
 165       */
 166      public function test_delete_data_for_user() {
 167          global $DB;
 168  
 169          $this->resetAfterTest();
 170          $metaplugin = enrol_get_plugin('meta');
 171          $user1 = $this->getDataGenerator()->create_user();
 172          $user2 = $this->getDataGenerator()->create_user();
 173          $user3 = $this->getDataGenerator()->create_user();
 174          $course1 = $this->getDataGenerator()->create_course();
 175          $course2 = $this->getDataGenerator()->create_course();
 176          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 177          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 178  
 179          $this->enable_plugin();
 180          $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
 181          $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $studentrole->id);
 182          $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $studentrole->id);
 183          $this->getDataGenerator()->enrol_user($user3->id, $course2->id, $studentrole->id);
 184  
 185          $this->assertEquals(
 186                  3,
 187                  $DB->count_records_sql("SELECT COUNT(gm.id)
 188                                        FROM {groups_members} gm
 189                                        JOIN {groups} g ON gm.groupid = g.id
 190                                       WHERE g.courseid = ?", [$course1->id])
 191          );
 192  
 193          $this->setUser($user1);
 194          $coursecontext1 = \context_course::instance($course1->id);
 195          $coursecontext2 = \context_course::instance($course2->id);
 196          $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'enrol_meta',
 197                  [$coursecontext1->id]);
 198          provider::delete_data_for_user($approvedcontextlist);
 199          // Check we have 2 users in groups because we are deleted user1.
 200          $this->assertEquals(
 201                  2,
 202                  $DB->count_records_sql("SELECT COUNT(gm.id)
 203                                            FROM {groups_members} gm
 204                                            JOIN {groups} g ON gm.groupid = g.id
 205                                           WHERE g.courseid = ?", [$course1->id])
 206          );
 207      }
 208  
 209      /**
 210       * Test for provider::delete_data_for_users().
 211       */
 212      public function test_delete_data_for_users() {
 213          global $DB;
 214  
 215          $this->resetAfterTest();
 216  
 217          $metaplugin = enrol_get_plugin('meta');
 218  
 219          $user1 = $this->getDataGenerator()->create_user();
 220          $user2 = $this->getDataGenerator()->create_user();
 221          $user3 = $this->getDataGenerator()->create_user();
 222  
 223          $course1 = $this->getDataGenerator()->create_course();
 224          $course2 = $this->getDataGenerator()->create_course();
 225  
 226          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 227  
 228          $this->enable_plugin();
 229          $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
 230  
 231          $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'student');
 232          $this->getDataGenerator()->enrol_user($user2->id, $course2->id, 'student');
 233          $this->getDataGenerator()->enrol_user($user3->id, $course2->id, 'student');
 234  
 235          $this->assertEquals(
 236                  3,
 237                  $DB->count_records_sql("SELECT COUNT(gm.id)
 238                                        FROM {groups_members} gm
 239                                        JOIN {groups} g ON gm.groupid = g.id
 240                                       WHERE g.courseid = ?", [$course1->id])
 241          );
 242  
 243          $coursecontext1 = \context_course::instance($course1->id);
 244  
 245          $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_meta',
 246                  [$user1->id, $user2->id]);
 247          provider::delete_data_for_users($approveduserlist);
 248  
 249          // Check we have 1 user in groups because we have deleted user1 and user2.
 250          $this->assertEquals(
 251                  1,
 252                  $DB->count_records_sql("SELECT COUNT(gm.id)
 253                                            FROM {groups_members} gm
 254                                            JOIN {groups} g ON gm.groupid = g.id
 255                                           WHERE g.courseid = ?", [$course1->id])
 256          );
 257      }
 258  
 259      /**
 260       * Test for provider::get_users_in_context().
 261       */
 262      public function test_get_users_in_context() {
 263          global $DB;
 264  
 265          $this->resetAfterTest();
 266  
 267          $metaplugin = enrol_get_plugin('meta');
 268  
 269          $course1 = $this->getDataGenerator()->create_course();
 270          $course2 = $this->getDataGenerator()->create_course();
 271  
 272          $user1 = $this->getDataGenerator()->create_user();
 273          $user2 = $this->getDataGenerator()->create_user();
 274          $user3 = $this->getDataGenerator()->create_user();
 275  
 276          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 277  
 278          $this->enable_plugin();
 279          $metaplugin->add_instance($course1, array('customint1' => $course2->id, 'customint2' => $group1->id));
 280  
 281          $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'student');
 282          $this->getDataGenerator()->enrol_user($user2->id, $course2->id, 'student');
 283  
 284          // Check if user1 is enrolled into course1 in group 1.
 285          $this->assertTrue(groups_is_member($group1->id, $user1->id));
 286          $this->assertTrue($DB->record_exists('groups_members',
 287                  array(
 288                      'groupid' => $group1->id,
 289                      'userid' => $user1->id,
 290                      'component' => 'enrol_meta'
 291                  )
 292          ));
 293  
 294          $context = \context_course::instance($course1->id);
 295  
 296          $userlist = new \core_privacy\local\request\userlist($context, 'enrol_meta');
 297          \enrol_meta\privacy\provider::get_users_in_context($userlist);
 298  
 299          $this->assertEqualsCanonicalizing(
 300                  [$user1->id, $user2->id],
 301                  $userlist->get_userids());
 302      }
 303  }