Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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