Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.
   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_cohort.
  19   *
  20   * @package    enrol_cohort
  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_cohort\privacy\provider;
  31  
  32  /**
  33   * Unit tests for the enrol_cohort 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_cohort_privacy_testcase extends \core_privacy\tests\provider_testcase {
  39  
  40      /**
  41       * Test getting the context for the user ID related to this plugin.
  42       */
  43      public function test_get_contexts_for_userid() {
  44          global $DB;
  45  
  46          $this->resetAfterTest();
  47          $trace = new null_progress_trace();
  48  
  49          $cohortplugin = enrol_get_plugin('cohort');
  50          $user1 = $this->getDataGenerator()->create_user();
  51          $cat1 = $this->getDataGenerator()->create_category();
  52          $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
  53          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
  54          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  55          $cohort1 = $this->getDataGenerator()->create_cohort(
  56              array('contextid' => context_coursecat::instance($cat1->id)->id));
  57          $cohortplugin->add_instance($course1, array(
  58              'customint1' => $cohort1->id,
  59              'roleid' => $studentrole->id,
  60              'customint2' => $group1->id)
  61          );
  62  
  63          cohort_add_member($cohort1->id, $user1->id);
  64          enrol_cohort_sync($trace, $course1->id);
  65          // Check if user1 is enrolled into course1 in group 1.
  66          $this->assertEquals(1, $DB->count_records('role_assignments', array()));
  67          $this->assertTrue($DB->record_exists('groups_members', array(
  68              'groupid' => $group1->id,
  69              'userid' => $user1->id,
  70              'component' => 'enrol_cohort')
  71          ));
  72          // Check context course fro provider to user1.
  73          $context = \context_course::instance($course1->id);
  74          $contextlist = provider::get_contexts_for_userid($user1->id);
  75          $this->assertEquals($context->id, $contextlist->current()->id);
  76      }
  77  
  78      /**
  79       * Test that user data is exported correctly.
  80       */
  81      public function test_export_user_data() {
  82          global $DB;
  83  
  84          $this->resetAfterTest();
  85          $trace = new null_progress_trace();
  86  
  87          $cohortplugin = enrol_get_plugin('cohort');
  88          $user1 = $this->getDataGenerator()->create_user();
  89          $cat1 = $this->getDataGenerator()->create_category();
  90          $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
  91          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
  92          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
  93          $cohort1 = $this->getDataGenerator()->create_cohort(
  94              array('contextid' => context_coursecat::instance($cat1->id)->id));
  95          $cohortplugin->add_instance($course1, array(
  96              'customint1' => $cohort1->id,
  97              'roleid' => $studentrole->id,
  98              'customint2' => $group1->id)
  99          );
 100  
 101          cohort_add_member($cohort1->id, $user1->id);
 102          enrol_cohort_sync($trace, $course1->id);
 103          // Check if user1 is enrolled into course1 in group 1.
 104          $this->assertEquals(1, $DB->count_records('role_assignments', array()));
 105          $this->assertTrue($DB->record_exists('groups_members', array(
 106              'groupid' => $group1->id,
 107              'userid' => $user1->id,
 108              'component' => 'enrol_cohort')
 109          ));
 110  
 111          $this->setUser($user1);
 112          $contextlist = provider::get_contexts_for_userid($user1->id);
 113          $approvedcontextlist = new approved_contextlist($user1, 'enrol_cohort', $contextlist->get_contextids());
 114          provider::export_user_data($approvedcontextlist);
 115          foreach ($contextlist as $context) {
 116              $writer = writer::with_context($context);
 117              $data = $writer->get_data([
 118                  get_string('pluginname', 'enrol_cohort'),
 119                  get_string('groups', 'core_group')
 120              ]);
 121              $this->assertTrue($writer->has_any_data());
 122              if ($context->contextlevel == CONTEXT_COURSE) {
 123                  $exportedgroups = $data->groups;
 124                  // User1 only belongs to group1 via enrol_cohort.
 125                  $this->assertCount(1, $exportedgroups);
 126                  $exportedgroup = reset($exportedgroups);
 127                  $this->assertEquals($group1->name, $exportedgroup->name);
 128              }
 129          }
 130      }
 131  
 132      /**
 133       * Test for provider::delete_data_for_all_users_in_context().
 134       */
 135      public function test_delete_data_for_all_users_in_context() {
 136          global $DB;
 137  
 138          $this->resetAfterTest();
 139          $trace = new null_progress_trace();
 140  
 141          $cohortplugin = enrol_get_plugin('cohort');
 142          $user1 = $this->getDataGenerator()->create_user();
 143          $user2 = $this->getDataGenerator()->create_user();
 144          $user3 = $this->getDataGenerator()->create_user();
 145          $cat1 = $this->getDataGenerator()->create_category();
 146          $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
 147          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 148          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 149          $cohort1 = $this->getDataGenerator()->create_cohort(
 150              array('contextid' => context_coursecat::instance($cat1->id)->id));
 151          $cohortplugin->add_instance($course1, array(
 152              'customint1' => $cohort1->id,
 153              'roleid' => $studentrole->id,
 154              'customint2' => $group1->id)
 155          );
 156  
 157          cohort_add_member($cohort1->id, $user1->id);
 158          cohort_add_member($cohort1->id, $user2->id);
 159          cohort_add_member($cohort1->id, $user3->id);
 160          enrol_cohort_sync($trace, $course1->id);
 161          $this->assertEquals(
 162                  3,
 163                  $DB->count_records_sql("SELECT COUNT(gm.id)
 164                                        FROM {groups_members} gm
 165                                        JOIN {groups} g ON gm.groupid = g.id
 166                                       WHERE g.courseid = ?", [$course1->id])
 167          );
 168  
 169          $coursecontext1 = context_course::instance($course1->id);
 170          provider::delete_data_for_all_users_in_context($coursecontext1);
 171          $this->assertEquals(
 172              0,
 173              $DB->count_records_sql("SELECT COUNT(gm.id)
 174                                        FROM {groups_members} gm
 175                                        JOIN {groups} g ON gm.groupid = g.id
 176                                       WHERE g.courseid = ?", [$course1->id])
 177          );
 178      }
 179  
 180      /**
 181       * Test for provider::delete_data_for_user().
 182       */
 183      public function test_delete_data_for_user() {
 184          global $DB;
 185  
 186          $this->resetAfterTest();
 187          $trace = new null_progress_trace();
 188  
 189          $cohortplugin = enrol_get_plugin('cohort');
 190          $user1 = $this->getDataGenerator()->create_user();
 191          $user2 = $this->getDataGenerator()->create_user();
 192          $user3 = $this->getDataGenerator()->create_user();
 193          $cat1 = $this->getDataGenerator()->create_category();
 194          $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
 195          $course2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
 196          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 197          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
 198          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 199          $cohort1 = $this->getDataGenerator()->create_cohort(
 200              array('contextid' => context_coursecat::instance($cat1->id)->id));
 201          $cohortplugin->add_instance($course1, array(
 202              'customint1' => $cohort1->id,
 203              'roleid' => $studentrole->id,
 204              'customint2' => $group1->id)
 205          );
 206          $cohortplugin->add_instance($course2, array(
 207              'customint1' => $cohort1->id,
 208              'roleid' => $studentrole->id,
 209              'customint2' => $group2->id)
 210          );
 211  
 212          $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
 213          $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
 214          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 215          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
 216  
 217          cohort_add_member($cohort1->id, $user1->id);
 218          enrol_cohort_sync($trace, $course1->id);
 219  
 220          $this->assertEquals(
 221                  3,
 222                  $DB->count_records_sql("SELECT COUNT(gm.id)
 223                                            FROM {groups_members} gm
 224                                            JOIN {groups} g ON gm.groupid = g.id
 225                                           WHERE g.courseid = ?", [$course1->id])
 226          );
 227  
 228          $this->assertEquals(
 229                  1,
 230                  $DB->count_records_sql("SELECT COUNT(gm.id)
 231                                            FROM {groups_members} gm
 232                                            JOIN {groups} g ON gm.groupid = g.id
 233                                           WHERE g.courseid = ?", [$course2->id])
 234          );
 235  
 236          $this->setUser($user1);
 237          $coursecontext1 = context_course::instance($course1->id);
 238          $coursecontext2 = context_course::instance($course2->id);
 239          $approvedcontextlist = new \core_privacy\tests\request\approved_contextlist($user1, 'enrol_cohort',
 240                  [$coursecontext1->id, $coursecontext2->id]);
 241          provider::delete_data_for_user($approvedcontextlist);
 242          // Check we have 2 users in groups because we are deleted user1.
 243          $this->assertEquals(
 244                  2,
 245                  $DB->count_records_sql("SELECT COUNT(gm.id)
 246                                            FROM {groups_members} gm
 247                                            JOIN {groups} g ON gm.groupid = g.id
 248                                           WHERE g.courseid = ?", [$course1->id])
 249          );
 250          // Check we have not users in groups.
 251          $this->assertEquals(
 252                  0,
 253                  $DB->count_records_sql("SELECT COUNT(gm.id)
 254                                            FROM {groups_members} gm
 255                                            JOIN {groups} g ON gm.groupid = g.id
 256                                           WHERE g.courseid = ?", [$course2->id])
 257          );
 258      }
 259  
 260      /**
 261       * Test for provider::delete_data_for_users().
 262       */
 263      public function test_delete_data_for_users() {
 264          global $DB;
 265  
 266          $this->resetAfterTest();
 267  
 268          $trace = new null_progress_trace();
 269  
 270          $cohortplugin = enrol_get_plugin('cohort');
 271  
 272          $user1 = $this->getDataGenerator()->create_user();
 273          $user2 = $this->getDataGenerator()->create_user();
 274          $user3 = $this->getDataGenerator()->create_user();
 275  
 276          $cat1 = $this->getDataGenerator()->create_category();
 277  
 278          $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
 279          $course2 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
 280  
 281          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 282          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course2->id));
 283  
 284          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 285  
 286          $cohort1 = $this->getDataGenerator()->create_cohort(
 287                  array('contextid' => context_coursecat::instance($cat1->id)->id));
 288          $cohortplugin->add_instance($course1, array(
 289              'customint1' => $cohort1->id,
 290              'roleid' => $studentrole->id,
 291              'customint2' => $group1->id)
 292          );
 293          $cohortplugin->add_instance($course2, array(
 294              'customint1' => $cohort1->id,
 295              'roleid' => $studentrole->id,
 296              'customint2' => $group2->id)
 297          );
 298  
 299          $this->getDataGenerator()->enrol_user($user2->id, $course1->id);
 300          $this->getDataGenerator()->enrol_user($user3->id, $course1->id);
 301          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user2->id));
 302          $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user3->id));
 303  
 304          cohort_add_member($cohort1->id, $user1->id);
 305          enrol_cohort_sync($trace, $course1->id);
 306  
 307          $this->assertEquals(
 308                  3,
 309                  $DB->count_records_sql("SELECT COUNT(gm.id)
 310                                            FROM {groups_members} gm
 311                                            JOIN {groups} g ON gm.groupid = g.id
 312                                           WHERE g.courseid = ?", [$course1->id])
 313          );
 314  
 315          $this->assertEquals(
 316                  1,
 317                  $DB->count_records_sql("SELECT COUNT(gm.id)
 318                                            FROM {groups_members} gm
 319                                            JOIN {groups} g ON gm.groupid = g.id
 320                                           WHERE g.courseid = ?", [$course2->id])
 321          );
 322  
 323          $coursecontext1 = context_course::instance($course1->id);
 324  
 325          $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_cohort',
 326                  [$user1->id, $user2->id]);
 327          provider::delete_data_for_users($approveduserlist);
 328  
 329          // Check we have 2 users in groups because we have deleted user1.
 330          // User2's membership is manual and is not as the result of a cohort enrolment.
 331          $this->assertEquals(
 332                  2,
 333                  $DB->count_records_sql("SELECT COUNT(gm.id)
 334                                            FROM {groups_members} gm
 335                                            JOIN {groups} g ON gm.groupid = g.id
 336                                           WHERE g.courseid = ?", [$course1->id])
 337          );
 338  
 339          // Check that course2 is not touched.
 340          $this->assertEquals(
 341                  1,
 342                  $DB->count_records_sql("SELECT COUNT(gm.id)
 343                                            FROM {groups_members} gm
 344                                            JOIN {groups} g ON gm.groupid = g.id
 345                                           WHERE g.courseid = ?", [$course2->id])
 346          );
 347      }
 348  
 349      /**
 350       * Test for provider::get_users_in_context().
 351       */
 352      public function test_get_users_in_context() {
 353          global $DB;
 354  
 355          $this->resetAfterTest();
 356  
 357          $trace = new null_progress_trace();
 358  
 359          $cohortplugin = enrol_get_plugin('cohort');
 360  
 361          $cat1 = $this->getDataGenerator()->create_category();
 362          $course1 = $this->getDataGenerator()->create_course(array('category' => $cat1->id));
 363          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course1->id));
 364          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 365          $cohort1 = $this->getDataGenerator()->create_cohort(
 366                  array('contextid' => context_coursecat::instance($cat1->id)->id));
 367          $cohortplugin->add_instance($course1, array(
 368              'customint1' => $cohort1->id,
 369              'roleid' => $studentrole->id,
 370              'customint2' => $group1->id)
 371          );
 372  
 373          $user1 = $this->getDataGenerator()->create_user();
 374  
 375          cohort_add_member($cohort1->id, $user1->id);
 376          enrol_cohort_sync($trace, $course1->id);
 377  
 378          // Check if user1 is enrolled into course1 in group 1.
 379          $this->assertEquals(1, $DB->count_records('role_assignments', array()));
 380          $this->assertTrue($DB->record_exists('groups_members', array(
 381              'groupid' => $group1->id,
 382              'userid' => $user1->id,
 383              'component' => 'enrol_cohort')
 384          ));
 385  
 386          $context = \context_course::instance($course1->id);
 387  
 388          $userlist = new \core_privacy\local\request\userlist($context, 'enrol_cohort');
 389          \enrol_cohort\privacy\provider::get_users_in_context($userlist);
 390  
 391          $this->assertEquals([$user1->id], $userlist->get_userids());
 392      }
 393  }