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.

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   * Unit tests for the tool_cohortroles implementation of the privacy API.
  19   *
  20   * @package    tool_cohortroles
  21   * @category   test
  22   * @copyright  2018 Zig Tan <zig@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  global $CFG;
  28  
  29  use \core_privacy\local\request\writer;
  30  use \core_privacy\local\request\approved_contextlist;
  31  use \tool_cohortroles\api;
  32  use \tool_cohortroles\privacy\provider;
  33  use core_privacy\local\request\approved_userlist;
  34  
  35  /**
  36   * Unit tests for the tool_cohortroles implementation of the privacy API.
  37   *
  38   * @copyright  2018 Zig Tan <zig@moodle.com>
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class tool_cohortroles_privacy_testcase extends \core_privacy\tests\provider_testcase {
  42  
  43      /**
  44       * Overriding setUp() function to always reset after tests.
  45       */
  46      public function setUp(): void {
  47          $this->resetAfterTest(true);
  48      }
  49  
  50      /**
  51       * Test for provider::get_contexts_for_userid().
  52       */
  53      public function test_get_contexts_for_userid() {
  54          global $DB;
  55  
  56          // Test setup.
  57          $user = $this->getDataGenerator()->create_user();
  58          $this->setUser($user);
  59          $this->setAdminUser();
  60  
  61          // Create course category.
  62          $coursecategory = $this->getDataGenerator()->create_category();
  63          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
  64          $systemctx = context_system::instance();
  65          // Create course.
  66          $course = $this->getDataGenerator()->create_course();
  67          $coursectx = \context_course::instance($course->id);
  68  
  69          $this->setup_test_scenario_data($user->id, $systemctx, 1);
  70          $this->setup_test_scenario_data($user->id, $coursecategoryctx, 1, 'Sausage roll 2',
  71              'sausageroll2');
  72          $this->setup_test_scenario_data($user->id, $coursectx, 1, 'Sausage roll 3',
  73              'sausageroll3');
  74  
  75          // Test the User's assigned cohortroles matches 3.
  76          $cohortroles = $DB->get_records('tool_cohortroles', ['userid' => $user->id]);
  77          $this->assertCount(3, $cohortroles);
  78  
  79          // Test the User's retrieved contextlist returns only the system and course category context.
  80          $contextlist = provider::get_contexts_for_userid($user->id);
  81          $contexts = $contextlist->get_contexts();
  82          $this->assertCount(2, $contexts);
  83  
  84          $contextlevels = array_column($contexts, 'contextlevel');
  85          $expected = [
  86              CONTEXT_SYSTEM,
  87              CONTEXT_COURSECAT
  88          ];
  89          // Test the User's contexts equal the system and course category context.
  90          $this->assertEqualsCanonicalizing($expected, $contextlevels);
  91      }
  92  
  93      /**
  94       * Test for provider::export_user_data().
  95       */
  96      public function test_export_user_data() {
  97          // Test setup.
  98          $user = $this->getDataGenerator()->create_user();
  99          $this->setUser($user);
 100          $this->setAdminUser();
 101  
 102          // Create course category.
 103          $coursecategory = $this->getDataGenerator()->create_category();
 104          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
 105          $systemctx = context_system::instance();
 106          // Create course.
 107          $course = $this->getDataGenerator()->create_course();
 108          $coursectx = \context_course::instance($course->id);
 109  
 110          $this->setup_test_scenario_data($user->id, $systemctx, 1);
 111          $this->setup_test_scenario_data($user->id, $coursecategoryctx, 1, 'Sausage roll 2',
 112              'sausageroll2');
 113          $this->setup_test_scenario_data($user->id, $coursectx, 1, 'Sausage roll 3',
 114              'sausageroll3');
 115  
 116          // Test the User's retrieved contextlist contains two contexts.
 117          $contextlist = provider::get_contexts_for_userid($user->id);
 118          $contexts = $contextlist->get_contexts();
 119          $this->assertCount(2, $contexts);
 120  
 121          // Add a system, course category and course context to the approved context list.
 122          $approvedcontextids = [
 123              $systemctx->id,
 124              $coursecategoryctx->id,
 125              $coursectx->id
 126          ];
 127  
 128          // Retrieve the User's tool_cohortroles data.
 129          $approvedcontextlist = new approved_contextlist($user, 'tool_cohortroles', $approvedcontextids);
 130          provider::export_user_data($approvedcontextlist);
 131  
 132          // Test the tool_cohortroles data is exported at the system context level.
 133          $writer = writer::with_context($systemctx);
 134          $this->assertTrue($writer->has_any_data());
 135          // Test the tool_cohortroles data is exported at the course category context level.
 136          $writer = writer::with_context($coursecategoryctx);
 137          $this->assertTrue($writer->has_any_data());
 138          // Test the tool_cohortroles data is not exported at the course context level.
 139          $writer = writer::with_context($coursectx);
 140          $this->assertFalse($writer->has_any_data());
 141      }
 142  
 143      /**
 144       * Test for provider::delete_data_for_all_users_in_context().
 145       */
 146      public function test_delete_data_for_all_users_in_context() {
 147          global $DB;
 148  
 149          // Test setup.
 150          $user = $this->getDataGenerator()->create_user();
 151          $this->setUser($user);
 152          $this->setAdminUser();
 153  
 154          // Create course category.
 155          $coursecategory = $this->getDataGenerator()->create_category();
 156          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
 157          $systemctx = context_system::instance();
 158  
 159          $this->setup_test_scenario_data($user->id, $systemctx, 1);
 160          $this->setup_test_scenario_data($user->id, $coursecategoryctx, 1, 'Sausage roll 2',
 161              'sausageroll2');
 162  
 163          // Test the User's assigned cohortroles matches 2.
 164          $cohortroles = $DB->get_records('tool_cohortroles', ['userid' => $user->id]);
 165          $this->assertCount(2, $cohortroles);
 166  
 167          // Test the User's retrieved contextlist contains two contexts.
 168          $contextlist = provider::get_contexts_for_userid($user->id);
 169          $contexts = $contextlist->get_contexts();
 170          $this->assertCount(2, $contexts);
 171  
 172          // Make sure the user data is only being deleted in within the system and course category context.
 173          $usercontext = context_user::instance($user->id);
 174          // Delete all the User's records in mdl_tool_cohortroles table by the user context.
 175          provider::delete_data_for_all_users_in_context($usercontext);
 176  
 177          // Test the cohort roles records in mdl_tool_cohortroles table is still present.
 178          $cohortroles = $DB->get_records('tool_cohortroles', ['userid' => $user->id]);
 179          $this->assertCount(2, $cohortroles);
 180  
 181          // Delete all the User's records in mdl_tool_cohortroles table by the specified system context.
 182          provider::delete_data_for_all_users_in_context($systemctx);
 183  
 184          // The user data in the system context should be deleted.
 185          // Test the User's retrieved contextlist contains one context (course category).
 186          $contextlist = provider::get_contexts_for_userid($user->id);
 187          $contexts = $contextlist->get_contexts();
 188          $this->assertCount(1, $contexts);
 189  
 190          // Delete all the User's records in mdl_tool_cohortroles table by the specified course category context.
 191          provider::delete_data_for_all_users_in_context($coursecategoryctx);
 192  
 193          // Test the cohort roles records in mdl_tool_cohortroles table is equals zero.
 194          $cohortroles = $DB->get_records('tool_cohortroles', ['userid' => $user->id]);
 195          $this->assertCount(0, $cohortroles);
 196  
 197          $contextlist = provider::get_contexts_for_userid($user->id);
 198          $contexts = $contextlist->get_contexts();
 199          $this->assertCount(0, $contexts);
 200      }
 201  
 202      /**
 203       * Test for provider::delete_data_for_user().
 204       */
 205      public function test_delete_data_for_user() {
 206          global $DB;
 207  
 208          // Test setup.
 209          $user = $this->getDataGenerator()->create_user();
 210          $this->setUser($user);
 211          $this->setAdminUser();
 212  
 213          // Create course category.
 214          $coursecategory = $this->getDataGenerator()->create_category();
 215          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
 216          $systemctx = context_system::instance();
 217  
 218          $this->setup_test_scenario_data($user->id, $systemctx, 1);
 219          $this->setup_test_scenario_data($user->id, $coursecategoryctx, 1, 'Sausage roll 2',
 220              'sausageroll2');
 221  
 222          // Test the User's assigned cohortroles matches 2.
 223          $cohortroles = $DB->get_records('tool_cohortroles', ['userid' => $user->id]);
 224          $this->assertCount(2, $cohortroles);
 225  
 226          // Test the User's retrieved contextlist contains two contexts.
 227          $contextlist = provider::get_contexts_for_userid($user->id);
 228          $contexts = $contextlist->get_contexts();
 229          $this->assertCount(2, $contexts);
 230  
 231          // Make sure the user data is only being deleted in within the system and the course category contexts.
 232          $usercontext = context_user::instance($user->id);
 233          // Delete all the User's records in mdl_tool_cohortroles table by the specified approved context list.
 234          $approvedcontextlist = new approved_contextlist($user, 'tool_cohortroles', [$usercontext->id]);
 235          provider::delete_data_for_user($approvedcontextlist);
 236  
 237          // Test the cohort roles records in mdl_tool_cohortroles table are still present.
 238          $cohortroles = $DB->get_records('tool_cohortroles', ['userid' => $user->id]);
 239          $this->assertCount(2, $cohortroles);
 240  
 241          // Delete all the User's records in mdl_tool_cohortroles table by the specified approved context list.
 242          $approvedcontextlist = new approved_contextlist($user, 'tool_cohortroles', $contextlist->get_contextids());
 243          provider::delete_data_for_user($approvedcontextlist);
 244  
 245          // Test the records in mdl_tool_cohortroles table is equals zero.
 246          $cohortroles = $DB->get_records('tool_cohortroles', ['userid' => $user->id]);
 247          $this->assertCount(0, $cohortroles);
 248      }
 249  
 250      /**
 251       * Test that only users within a course context are fetched.
 252       */
 253      public function test_get_users_in_context() {
 254          $component = 'tool_cohortroles';
 255  
 256          // Create a user.
 257          $user = $this->getDataGenerator()->create_user();
 258          $usercontext = context_user::instance($user->id);
 259  
 260          // Create course category.
 261          $coursecategory = $this->getDataGenerator()->create_category();
 262          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
 263          $systemctx = context_system::instance();
 264  
 265          $this->setAdminUser();
 266  
 267          $userlist = new \core_privacy\local\request\userlist($systemctx, $component);
 268          provider::get_users_in_context($userlist);
 269          $this->assertCount(0, $userlist);
 270  
 271          $this->setup_test_scenario_data($user->id, $systemctx, 1);
 272          $this->setup_test_scenario_data($user->id, $coursecategoryctx, 1, 'Sausage roll 2',
 273              'sausageroll2');
 274  
 275          // The list of users within the system context should contain user.
 276          provider::get_users_in_context($userlist);
 277          $this->assertCount(1, $userlist);
 278          $this->assertTrue(in_array($user->id, $userlist->get_userids()));
 279  
 280          // The list of users within the course category context should contain user.
 281          $userlist = new \core_privacy\local\request\userlist($coursecategoryctx, $component);
 282          provider::get_users_in_context($userlist);
 283          $this->assertCount(1, $userlist);
 284          $this->assertTrue(in_array($user->id, $userlist->get_userids()));
 285  
 286          // The list of users within the user context should be empty.
 287          $userlist2 = new \core_privacy\local\request\userlist($usercontext, $component);
 288          provider::get_users_in_context($userlist2);
 289          $this->assertCount(0, $userlist2);
 290      }
 291  
 292      /**
 293       * Test that data for users in approved userlist is deleted.
 294       */
 295      public function test_delete_data_for_users() {
 296          $component = 'tool_cohortroles';
 297  
 298          // Create user1.
 299          $user1 = $this->getDataGenerator()->create_user();
 300          // Create user2.
 301          $user2 = $this->getDataGenerator()->create_user();
 302          // Create user3.
 303          $user3 = $this->getDataGenerator()->create_user();
 304          $usercontext3 = context_user::instance($user3->id);
 305  
 306          // Create course category.
 307          $coursecategory = $this->getDataGenerator()->create_category();
 308          $coursecategoryctx = \context_coursecat::instance($coursecategory->id);
 309          $systemctx = context_system::instance();
 310  
 311          $this->setAdminUser();
 312  
 313          $this->setup_test_scenario_data($user1->id, $systemctx, 1);
 314          $this->setup_test_scenario_data($user2->id, $systemctx, 1, 'Sausage roll 2',
 315                  'sausageroll2');
 316          $this->setup_test_scenario_data($user3->id, $coursecategoryctx, 1, 'Sausage roll 3',
 317                  'sausageroll3');
 318  
 319          $userlist1 = new \core_privacy\local\request\userlist($systemctx, $component);
 320          provider::get_users_in_context($userlist1);
 321          $this->assertCount(2, $userlist1);
 322          $this->assertTrue(in_array($user1->id, $userlist1->get_userids()));
 323          $this->assertTrue(in_array($user2->id, $userlist1->get_userids()));
 324  
 325          // Convert $userlist1 into an approved_contextlist.
 326          $approvedlist1 = new approved_userlist($systemctx, $component, [$user1->id]);
 327          // Delete using delete_data_for_user.
 328          provider::delete_data_for_users($approvedlist1);
 329  
 330          // Re-fetch users in systemcontext.
 331          $userlist1 = new \core_privacy\local\request\userlist($systemctx, $component);
 332          provider::get_users_in_context($userlist1);
 333          // The user data of user1in systemcontext should be deleted.
 334          // The user data of user2 in systemcontext should be still present.
 335          $this->assertCount(1, $userlist1);
 336          $this->assertTrue(in_array($user2->id, $userlist1->get_userids()));
 337  
 338          // Convert $userlist1 into an approved_contextlist in the user context.
 339          $approvedlist2 = new approved_userlist($usercontext3, $component, $userlist1->get_userids());
 340          // Delete using delete_data_for_user.
 341          provider::delete_data_for_users($approvedlist2);
 342          // Re-fetch users in systemcontext.
 343          $userlist1 = new \core_privacy\local\request\userlist($systemctx, $component);
 344          provider::get_users_in_context($userlist1);
 345          // The user data in systemcontext should not be deleted.
 346          $this->assertCount(1, $userlist1);
 347      }
 348  
 349      /**
 350       * Helper function to setup tool_cohortroles records for testing a specific user.
 351       *
 352       * @param int $userid           The ID of the user used for testing.
 353       * @param int $nocohortroles    The number of tool_cohortroles to create for the user.
 354       * @param string $rolename      The name of the role to be created.
 355       * @param string $roleshortname The short name of the role to be created.
 356       * @throws \core_competency\invalid_persistent_exception
 357       * @throws coding_exception
 358       */
 359      protected function setup_test_scenario_data($userid, $context, $nocohortroles, $rolename = 'Sausage Roll',
 360                                                  $roleshortname = 'sausageroll') {
 361          $roleid = create_role($rolename, $roleshortname, 'mmmm');
 362  
 363          $result = new \stdClass();
 364          $result->contextid = $context->id;
 365  
 366          for ($c = 0; $c < $nocohortroles; $c++) {
 367              $cohort = $this->getDataGenerator()->create_cohort($result);
 368  
 369              $params = (object)array(
 370                  'userid' => $userid,
 371                  'roleid' => $roleid,
 372                  'cohortid' => $cohort->id
 373              );
 374  
 375              api::create_cohort_role_assignment($params);
 376          }
 377      }
 378  
 379  }