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 core_cohort.
  19   *
  20   * @package    core_cohort
  21   * @category   test
  22   * @copyright  2018 Sara Arjona <sara@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_cohort\privacy\provider;
  29  use core_privacy\local\request\approved_contextlist;
  30  use core_privacy\local\request\writer;
  31  use core_privacy\tests\provider_testcase;
  32  use core_privacy\local\request\approved_userlist;
  33  
  34  /**
  35   * Unit tests for cohort\classes\privacy\provider.php
  36   *
  37   * @copyright  2018 Sara Arjona <sara@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class core_cohort_testcase extends provider_testcase {
  41  
  42      /**
  43       * Basic setup for these tests.
  44       */
  45      public function setUp() {
  46          $this->resetAfterTest(true);
  47      }
  48  
  49      /**
  50       * Test getting the context for the user ID related to this plugin.
  51       */
  52      public function test_get_contexts_for_userid() {
  53          // Create system cohort and category cohort.
  54          $coursecategory = $this->getDataGenerator()->create_category();
  55          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
  56          $systemctx = \context_system::instance();
  57          $categorycohort = $this->getDataGenerator()->create_cohort([
  58                  'contextid' => $coursecategoryctx->id,
  59                  'name' => 'Category cohort 1',
  60              ]);
  61          $systemcohort = $this->getDataGenerator()->create_cohort([
  62                  'contextid' => $systemctx->id,
  63                  'name' => 'System cohort 1'
  64              ]);
  65  
  66          // Create user and add to the system and category cohorts.
  67          $user = $this->getDataGenerator()->create_user();
  68          cohort_add_member($categorycohort->id, $user->id);
  69          cohort_add_member($systemcohort->id, $user->id);
  70  
  71          // User is member of 2 cohorts.
  72          $contextlist = provider::get_contexts_for_userid($user->id);
  73          $this->assertCount(2, (array) $contextlist->get_contextids());
  74          $this->assertContains($coursecategoryctx->id, $contextlist->get_contextids());
  75          $this->assertContains($systemctx->id, $contextlist->get_contextids());
  76      }
  77  
  78      /**
  79       * Test that data is exported correctly for this plugin.
  80       */
  81      public function test_export_user_data() {
  82          // Create system cohort and category cohort.
  83          $coursecategory = $this->getDataGenerator()->create_category();
  84          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
  85          $systemctx = \context_system::instance();
  86          $categorycohort = $this->getDataGenerator()->create_cohort([
  87                  'contextid' => $coursecategoryctx->id,
  88                  'name' => 'Category cohort 1',
  89              ]);
  90          $systemcohort1 = $this->getDataGenerator()->create_cohort([
  91                  'contextid' => $systemctx->id,
  92                  'name' => 'System cohort 1'
  93              ]);
  94          $systemcohort2 = $this->getDataGenerator()->create_cohort([
  95                  'contextid' => $systemctx->id,
  96                  'name' => 'System cohort 2'
  97              ]);
  98  
  99          // Create user and add to the system and category cohorts.
 100          $user = $this->getDataGenerator()->create_user();
 101          cohort_add_member($categorycohort->id, $user->id);
 102          cohort_add_member($systemcohort1->id, $user->id);
 103          cohort_add_member($systemcohort2->id, $user->id);
 104  
 105          // Validate system cohort exported data.
 106          $writer = writer::with_context($systemctx);
 107          $this->assertFalse($writer->has_any_data());
 108          $this->export_context_data_for_user($user->id, $systemctx, 'core_cohort');
 109          $data = $writer->get_related_data([], 'cohort');
 110          $this->assertCount(2, $data);
 111  
 112          // Validate category cohort exported data.
 113          $writer = writer::with_context($coursecategoryctx);
 114          $this->assertFalse($writer->has_any_data());
 115          $this->export_context_data_for_user($user->id, $coursecategoryctx, 'core_cohort');
 116          $data = $writer->get_related_data([], 'cohort');
 117          $this->assertCount(1, $data);
 118          $this->assertEquals($categorycohort->name, reset($data)->name);
 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          // Create system cohort and category cohort.
 128          $coursecategory = $this->getDataGenerator()->create_category();
 129          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
 130          $systemctx = \context_system::instance();
 131          $categorycohort = $this->getDataGenerator()->create_cohort([
 132                  'contextid' => $coursecategoryctx->id,
 133                  'name' => 'Category cohort 1',
 134                  'idnumber' => '',
 135                  'description' => ''
 136              ]);
 137          $systemcohort = $this->getDataGenerator()->create_cohort([
 138                  'contextid' => $systemctx->id,
 139                  'name' => 'System cohort 1'
 140              ]);
 141  
 142          // Create user and add to the system and category cohorts.
 143          $user = $this->getDataGenerator()->create_user();
 144          cohort_add_member($categorycohort->id, $user->id);
 145          cohort_add_member($systemcohort->id, $user->id);
 146  
 147          // Before deletion, we should have 2 entries in the cohort_members table.
 148          $count = $DB->count_records('cohort_members');
 149          $this->assertEquals(2, $count);
 150  
 151          // Delete data based on system context.
 152          provider::delete_data_for_all_users_in_context($systemctx);
 153  
 154          // After deletion, the cohort_members entries should have been deleted.
 155          $count = $DB->count_records('cohort_members');
 156          $this->assertEquals(1, $count);
 157  
 158          // Delete data based on category context.
 159          provider::delete_data_for_all_users_in_context($coursecategoryctx);
 160  
 161          // After deletion, the cohort_members entries should have been deleted.
 162          $count = $DB->count_records('cohort_members');
 163          $this->assertEquals(0, $count);
 164      }
 165  
 166      /**
 167       * Test for provider::delete_data_for_user().
 168       */
 169      public function test_delete_data_for_user() {
 170          global $DB;
 171  
 172          // Create system cohort and category cohort.
 173          $coursecategory = $this->getDataGenerator()->create_category();
 174          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
 175          $systemctx = \context_system::instance();
 176          $categorycohort = $this->getDataGenerator()->create_cohort([
 177                  'contextid' => $coursecategoryctx->id,
 178                  'name' => 'Category cohort 1',
 179                  'idnumber' => '',
 180                  'description' => ''
 181              ]);
 182          $systemcohort = $this->getDataGenerator()->create_cohort([
 183                  'contextid' => $systemctx->id,
 184                  'name' => 'System cohort 1'
 185              ]);
 186  
 187          // Create user and add to the system and category cohorts.
 188          $user1 = $this->getDataGenerator()->create_user();
 189          cohort_add_member($categorycohort->id, $user1->id);
 190          cohort_add_member($systemcohort->id, $user1->id);
 191  
 192          // Create another user and add to the system and category cohorts.
 193          $user2 = $this->getDataGenerator()->create_user();
 194          cohort_add_member($categorycohort->id, $user2->id);
 195          cohort_add_member($systemcohort->id, $user2->id);
 196  
 197          // Create another user and add to the system cohort.
 198          $user3 = $this->getDataGenerator()->create_user();
 199          cohort_add_member($systemcohort->id, $user3->id);
 200  
 201          // Before deletion, we should have 5 entries in the cohort_members table.
 202          $count = $DB->count_records('cohort_members');
 203          $this->assertEquals(5, $count);
 204  
 205          $contextlist = provider::get_contexts_for_userid($user1->id);
 206          $contexts = [];
 207          $contexts[] = \context_user::instance($user1->id)->id;
 208          $contexts = array_merge($contexts, $contextlist->get_contextids());
 209          $approvedcontextlist = new approved_contextlist($user1, 'cohort', $contexts);
 210          provider::delete_data_for_user($approvedcontextlist);
 211  
 212          // After deletion, the cohort_members entries for the first student should have been deleted.
 213          $count = $DB->count_records('cohort_members', ['userid' => $user1->id]);
 214          $this->assertEquals(0, $count);
 215          $count = $DB->count_records('cohort_members');
 216          $this->assertEquals(3, $count);
 217  
 218          // Confirm that the cohorts hasn't been removed.
 219          $cohortscount = $DB->get_records('cohort');
 220          $this->assertCount(2, (array) $cohortscount);
 221      }
 222  
 223      /**
 224       * Test that only users within a course context are fetched.
 225       */
 226      public function test_get_users_in_context() {
 227          $component = 'core_cohort';
 228  
 229          // Create system cohort and category cohort.
 230          $coursecategory = $this->getDataGenerator()->create_category();
 231          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
 232          $systemctx = \context_system::instance();
 233          $categorycohort = $this->getDataGenerator()->create_cohort([
 234              'contextid' => $coursecategoryctx->id,
 235              'name' => 'Category cohort 1',
 236          ]);
 237          // Create user.
 238          $user = $this->getDataGenerator()->create_user();
 239          $userctx = \context_user::instance($user->id);
 240  
 241          $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component);
 242          provider::get_users_in_context($userlist1);
 243          $this->assertCount(0, $userlist1);
 244  
 245          $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component);
 246          provider::get_users_in_context($userlist2);
 247          $this->assertCount(0, $userlist2);
 248  
 249          $systemcohort = $this->getDataGenerator()->create_cohort([
 250              'contextid' => $systemctx->id,
 251              'name' => 'System cohort 1'
 252          ]);
 253          // Create user and add to the system and category cohorts.
 254          cohort_add_member($categorycohort->id, $user->id);
 255          cohort_add_member($systemcohort->id, $user->id);
 256  
 257          // The list of users within the coursecat context should contain user.
 258          $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component);
 259          provider::get_users_in_context($userlist1);
 260          $this->assertCount(1, $userlist1);
 261          $expected = [$user->id];
 262          $actual = $userlist1->get_userids();
 263          $this->assertEquals($expected, $actual);
 264  
 265          // The list of users within the system context should contain user.
 266          $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component);
 267          provider::get_users_in_context($userlist2);
 268          $this->assertCount(1, $userlist2);
 269          $expected = [$user->id];
 270          $actual = $userlist2->get_userids();
 271          $this->assertEquals($expected, $actual);
 272  
 273          // The list of users within the user context should be empty.
 274          $userlist3 = new \core_privacy\local\request\userlist($userctx, $component);
 275          provider::get_users_in_context($userlist3);
 276          $this->assertCount(0, $userlist3);
 277      }
 278  
 279      /**
 280       * Test that data for users in approved userlist is deleted.
 281       */
 282      public function test_delete_data_for_users() {
 283          $component = 'core_cohort';
 284  
 285          // Create system cohort and category cohort.
 286          $coursecategory = $this->getDataGenerator()->create_category();
 287          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
 288          $systemctx = \context_system::instance();
 289          $categorycohort = $this->getDataGenerator()->create_cohort([
 290              'contextid' => $coursecategoryctx->id,
 291              'name' => 'Category cohort 1',
 292          ]);
 293          // Create user1.
 294          $user1 = $this->getDataGenerator()->create_user();
 295          $userctx1 = \context_user::instance($user1->id);
 296          // Create user2.
 297          $user2 = $this->getDataGenerator()->create_user();
 298  
 299          $systemcohort = $this->getDataGenerator()->create_cohort([
 300              'contextid' => $systemctx->id,
 301              'name' => 'System cohort 1'
 302          ]);
 303          // Create user and add to the system and category cohorts.
 304          cohort_add_member($categorycohort->id, $user1->id);
 305          cohort_add_member($systemcohort->id, $user1->id);
 306          cohort_add_member($categorycohort->id, $user2->id);
 307  
 308          $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component);
 309          provider::get_users_in_context($userlist1);
 310          $this->assertCount(2, $userlist1);
 311  
 312          $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component);
 313          provider::get_users_in_context($userlist2);
 314          $this->assertCount(1, $userlist2);
 315  
 316          // Convert $userlist1 into an approved_contextlist.
 317          $approvedlist1 = new approved_userlist($coursecategoryctx, $component, $userlist1->get_userids());
 318          // Delete using delete_data_for_user.
 319          provider::delete_data_for_users($approvedlist1);
 320  
 321          // Re-fetch users in coursecategoryctx.
 322          $userlist1 = new \core_privacy\local\request\userlist($coursecategoryctx, $component);
 323          provider::get_users_in_context($userlist1);
 324          // The user data in coursecategoryctx should be deleted.
 325          $this->assertCount(0, $userlist1);
 326          // Re-fetch users in coursecategoryctx.
 327          $userlist2 = new \core_privacy\local\request\userlist($systemctx, $component);
 328          provider::get_users_in_context($userlist2);
 329          // The user data in coursecontext2 should be still present.
 330          $this->assertCount(1, $userlist2);
 331  
 332          // Convert $userlist2 into an approved_contextlist in the user context.
 333          $approvedlist3 = new approved_userlist($userctx1, $component, $userlist2->get_userids());
 334          // Delete using delete_data_for_user.
 335          provider::delete_data_for_users($approvedlist3);
 336          // Re-fetch users in coursecontext1.
 337          $userlist3 = new \core_privacy\local\request\userlist($systemctx, $component);
 338          provider::get_users_in_context($userlist3);
 339          // The user data in systemcontext should not be deleted.
 340          $this->assertCount(1, $userlist3);
 341      }
 342  }