Proyectos de Subversion Moodle

Rev

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