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