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
namespace qbank_comment\privacy;
18
 
19
use comment;
20
use context;
21
use context_course;
22
use core_privacy\local\metadata\collection;
23
use qbank_comment\privacy\provider;
24
use core_privacy\local\request\approved_userlist;
25
use stdClass;
26
 
27
/**
28
 * Privacy api tests.
29
 *
30
 * @package    qbank_comment
31
 * @copyright  2021 Catalyst IT Australia Pty Ltd
32
 * @author     Safat Shahin <safatshahin@catalyst-au.net>
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
1441 ariadna 35
final class provider_test extends \core_privacy\tests\provider_testcase {
1 efrain 36
 
37
    /** @var stdClass A teacher who is only enrolled in course1. */
38
    protected $teacher1;
39
 
40
    /** @var stdClass A teacher who is only enrolled in course2. */
41
    protected $teacher2;
42
 
43
    /** @var stdClass A teacher who is enrolled in both course1 and course2. */
44
    protected $teacher3;
45
 
46
    /** @var stdClass A test course. */
47
    protected $course1;
48
 
49
    /** @var stdClass A test course. */
50
    protected $course2;
51
 
52
    /**
53
     * Set up function for tests in this class.
54
     */
55
    protected function setUp(): void {
56
        global $DB;
1441 ariadna 57
        parent::setUp();
1 efrain 58
 
59
        $this->resetAfterTest();
60
        $this->setAdminUser();
61
 
62
        // Create courses.
63
        $generator = $this->getDataGenerator();
64
        $this->course1 = $generator->create_course();
65
        $this->course2 = $generator->create_course();
66
 
67
        // Create and enrol teachers.
68
        $this->teacher1 = $generator->create_user();
69
        $this->teacher2 = $generator->create_user();
70
        $this->teacher3 = $generator->create_user();
71
 
72
        $studentrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
73
        $generator->enrol_user($this->teacher1->id,  $this->course1->id, $studentrole->id);
74
        $generator->enrol_user($this->teacher2->id,  $this->course2->id, $studentrole->id);
75
        $generator->enrol_user($this->teacher3->id,  $this->course1->id, $studentrole->id);
76
        $generator->enrol_user($this->teacher3->id,  $this->course2->id, $studentrole->id);
77
    }
78
 
79
    /**
80
     * Posts a comment on a given context.
81
     *
82
     * @param string $text The comment's text.
83
     * @param context $context The context on which we want to put the comment.
84
     */
85
    protected function add_comment($text, context $context) {
86
        $args = new stdClass;
87
        $args->context = $context;
88
        $args->area = 'question';
89
        $args->itemid = 0;
90
        $args->component = 'qbank_comment';
91
        $args->linktext = get_string('commentheader', 'qbank_comment');
92
        $args->notoggle = true;
93
        $args->autostart = true;
94
        $args->displaycancel = false;
95
        $comment = new comment($args);
96
 
97
        $comment->add($text);
98
    }
99
 
100
    /**
101
     * Test for provider::get_metadata().
102
     */
11 efrain 103
    public function test_get_metadata(): void {
1 efrain 104
        $collection = new collection('qbank_comment');
105
        $newcollection = provider::get_metadata($collection);
106
        $itemcollection = $newcollection->get_collection();
107
        $this->assertCount(1, $itemcollection);
108
 
109
        $link = reset($itemcollection);
110
 
111
        $this->assertEquals('core_comment', $link->get_name());
112
        $this->assertEmpty($link->get_privacy_fields());
113
        $this->assertEquals('privacy:metadata:core_comment', $link->get_summary());
114
    }
115
 
116
    /**
117
     * Test for provider::get_contexts_for_userid() when user had not posted any comments..
118
     */
11 efrain 119
    public function test_get_contexts_for_userid_no_comment(): void {
1 efrain 120
        $this->setUser($this->teacher1);
121
        $coursecontext1 = context_course::instance($this->course1->id);
122
        $this->add_comment('New comment', $coursecontext1);
123
 
124
        $this->setUser($this->teacher2);
125
        $contextlist = provider::get_contexts_for_userid($this->teacher2->id);
126
        $this->assertCount(0, $contextlist);
127
    }
128
 
129
    /**
130
     * Test for provider::get_contexts_for_userid().
131
     */
11 efrain 132
    public function test_get_contexts_for_userid(): void {
1 efrain 133
        $coursecontext1 = context_course::instance($this->course1->id);
134
        $coursecontext2 = context_course::instance($this->course2->id);
135
 
136
        $this->setUser($this->teacher3);
137
        $this->add_comment('New comment', $coursecontext1);
138
        $this->add_comment('New comment', $coursecontext1);
139
        $this->add_comment('New comment', $coursecontext2);
140
 
141
        $contextlist = provider::get_contexts_for_userid($this->teacher3->id);
142
        $this->assertCount(2, $contextlist);
143
 
144
        $contextids = $contextlist->get_contextids();
1441 ariadna 145
        $this->assertEqualsCanonicalizing([$coursecontext1->id, $coursecontext2->id], array_values($contextids));
1 efrain 146
    }
147
 
148
    /**
149
     * Test for provider::export_user_data() when the user has not posted any comments.
150
     */
11 efrain 151
    public function test_export_for_context_no_comment(): void {
1 efrain 152
        $coursecontext1 = context_course::instance($this->course1->id);
153
        $coursecontext2 = context_course::instance($this->course2->id);
154
 
155
        $this->setUser($this->teacher1);
156
        $this->add_comment('New comment', $coursecontext1);
157
 
158
        $this->setUser($this->teacher2);
159
 
160
        $this->setUser($this->teacher2);
161
        $this->export_context_data_for_user($this->teacher1->id, $coursecontext2, 'qbank_comment');
162
        $writer = \core_privacy\local\request\writer::with_context($coursecontext2);
163
        $this->assertFalse($writer->has_any_data());
164
    }
165
 
166
    /**
167
     * Test for provider::export_user_data().
168
     */
11 efrain 169
    public function test_export_for_context(): void {
1 efrain 170
        $coursecontext1 = context_course::instance($this->course1->id);
171
        $coursecontext2 = context_course::instance($this->course2->id);
172
 
173
        $this->setUser($this->teacher3);
174
        $this->add_comment('New comment', $coursecontext1);
175
        $this->add_comment('New comment', $coursecontext1);
176
        $this->add_comment('New comment', $coursecontext2);
177
 
178
        // Export all of the data for the context.
179
        $this->export_context_data_for_user($this->teacher3->id, $coursecontext1, 'qbank_comment');
180
        $writer = \core_privacy\local\request\writer::with_context($coursecontext1);
181
        $this->assertTrue($writer->has_any_data());
182
    }
183
 
184
    /**
185
     * Test for provider::delete_data_for_all_users_in_context().
186
     */
11 efrain 187
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 188
        global $DB;
189
 
190
        $coursecontext1 = context_course::instance($this->course1->id);
191
        $coursecontext2 = context_course::instance($this->course2->id);
192
 
193
        $this->setUser($this->teacher1);
194
        $this->add_comment('New comment', $coursecontext1);
195
 
196
        $this->setUser($this->teacher2);
197
        $this->add_comment('New comment', $coursecontext2);
198
 
199
        $this->setUser($this->teacher3);
200
        $this->add_comment('New comment', $coursecontext1);
201
        $this->add_comment('New comment', $coursecontext1);
202
        $this->add_comment('New comment', $coursecontext2);
203
 
204
        // Before deletion, we should have 3 comments in $coursecontext1 and 2 comments in $coursecontext2.
205
        $this->assertEquals(
206
                3,
207
                $DB->count_records('comments', ['component' => 'qbank_comment', 'contextid' => $coursecontext1->id])
208
        );
209
        $this->assertEquals(
210
                2,
211
                $DB->count_records('comments', ['component' => 'qbank_comment', 'contextid' => $coursecontext2->id])
212
        );
213
 
214
        // Delete data based on context.
215
        provider::delete_data_for_all_users_in_context($coursecontext1);
216
 
217
        // After deletion, the comments for $coursecontext1 should have been deleted.
218
        $this->assertEquals(
219
                0,
220
                $DB->count_records('comments', ['component' => 'qbank_comment', 'contextid' => $coursecontext1->id])
221
        );
222
        $this->assertEquals(
223
                2,
224
                $DB->count_records('comments', ['component' => 'qbank_comment', 'contextid' => $coursecontext2->id])
225
        );
226
    }
227
 
228
    /**
229
     * Test for provider::delete_data_for_user().
230
     */
11 efrain 231
    public function test_delete_data_for_user(): void {
1 efrain 232
        global $DB;
233
 
234
        $coursecontext1 = context_course::instance($this->course1->id);
235
        $coursecontext2 = context_course::instance($this->course2->id);
236
 
237
        $this->setUser($this->teacher1);
238
        $this->add_comment('New comment', $coursecontext1);
239
 
240
        $this->setUser($this->teacher2);
241
        $this->add_comment('New comment', $coursecontext2);
242
 
243
        $this->setUser($this->teacher3);
244
        $this->add_comment('New comment', $coursecontext1);
245
        $this->add_comment('New comment', $coursecontext1);
246
        $this->add_comment('New comment', $coursecontext2);
247
 
248
        // Before deletion, we should have 3 comments in $coursecontext1 and 2 comments in $coursecontext2,
249
        // and 3 comments by student12 in $coursecontext1 and $coursecontext2 combined.
250
        $this->assertEquals(
251
                3,
252
                $DB->count_records('comments', ['component' => 'qbank_comment', 'contextid' => $coursecontext1->id])
253
        );
254
        $this->assertEquals(
255
                2,
256
                $DB->count_records('comments', ['component' => 'qbank_comment', 'contextid' => $coursecontext2->id])
257
        );
258
        $this->assertEquals(
259
                3,
260
                $DB->count_records('comments', ['component' => 'qbank_comment', 'userid' => $this->teacher3->id])
261
        );
262
 
263
        $contextlist = new \core_privacy\local\request\approved_contextlist($this->teacher3, 'qbank_comment',
264
                [$coursecontext1->id, $coursecontext2->id]);
265
        provider::delete_data_for_user($contextlist);
266
 
267
        // After deletion, the comments for the student12 should have been deleted.
268
        $this->assertEquals(
269
                1,
270
                $DB->count_records('comments', ['component' => 'qbank_comment', 'contextid' => $coursecontext1->id])
271
        );
272
        $this->assertEquals(
273
                1,
274
                $DB->count_records('comments', ['component' => 'qbank_comment', 'contextid' => $coursecontext2->id])
275
        );
276
        $this->assertEquals(
277
                0,
278
                $DB->count_records('comments', ['component' => 'qbank_comment', 'userid' => $this->teacher3->id])
279
        );
280
    }
281
 
282
    /**
283
     * Test that only users within a course context are fetched.
284
     */
11 efrain 285
    public function test_get_users_in_context(): void {
1 efrain 286
        $component = 'qbank_comment';
287
 
288
        $coursecontext1 = context_course::instance($this->course1->id);
289
        $coursecontext2 = context_course::instance($this->course2->id);
290
 
291
        $userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
292
        provider::get_users_in_context($userlist1);
293
        $this->assertCount(0, $userlist1);
294
 
295
        $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
296
        provider::get_users_in_context($userlist2);
297
        $this->assertCount(0, $userlist2);
298
 
299
        $this->setUser($this->teacher3);
300
        $this->add_comment('New comment', $coursecontext1);
301
        $this->add_comment('New comment', $coursecontext2);
302
        $this->setUser($this->teacher1);
303
        $this->add_comment('New comment', $coursecontext1);
304
 
305
        // The list of users should contain teacher3 and user1.
306
        provider::get_users_in_context($userlist1);
307
        $this->assertCount(2, $userlist1);
308
        $this->assertTrue(in_array($this->teacher1->id, $userlist1->get_userids()));
309
        $this->assertTrue(in_array($this->teacher3->id, $userlist1->get_userids()));
310
 
311
        // The list of users should contain teacher3.
312
        provider::get_users_in_context($userlist2);
313
        $this->assertCount(1, $userlist2);
314
        $expected = [$this->teacher3->id];
315
        $actual = $userlist2->get_userids();
316
        $this->assertEquals($expected, $actual);
317
    }
318
 
319
    /**
320
     * Test that data for users in approved userlist is deleted.
321
     */
11 efrain 322
    public function test_delete_data_for_users(): void {
1 efrain 323
        $component = 'qbank_comment';
324
 
325
        $coursecontext1 = context_course::instance($this->course1->id);
326
        $coursecontext2 = context_course::instance($this->course2->id);
327
 
328
        $this->setUser($this->teacher3);
329
        $this->add_comment('New comment', $coursecontext1);
330
        $this->add_comment('New comment', $coursecontext2);
331
        $this->setUser($this->teacher1);
332
        $this->add_comment('New comment', $coursecontext1);
333
 
334
        $userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
335
        provider::get_users_in_context($userlist1);
336
        $this->assertCount(2, $userlist1);
337
 
338
        $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
339
        provider::get_users_in_context($userlist2);
340
        $this->assertCount(1, $userlist2);
341
 
342
        // Convert $userlist1 into an approved_contextlist.
343
        $approvedlist1 = new approved_userlist($coursecontext1, $component, $userlist1->get_userids());
344
        // Delete using delete_data_for_user.
345
        provider::delete_data_for_users($approvedlist1);
346
 
347
        // Re-fetch users in coursecontext1.
348
        $userlist1 = new \core_privacy\local\request\userlist($coursecontext1, $component);
349
        provider::get_users_in_context($userlist1);
350
        // The user data in coursecontext1 should be deleted.
351
        $this->assertCount(0, $userlist1);
352
 
353
        // Re-fetch users in coursecontext2.
354
        $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, $component);
355
        provider::get_users_in_context($userlist2);
356
        // The user data in coursecontext2 should be still present.
357
        $this->assertCount(1, $userlist2);
358
    }
359
}