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
 * Privacy tests for core_comment.
18
 *
19
 * @package    core_comment
20
 * @category   test
21
 * @copyright  2018 Adrian Greeve <adrian@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace core_comment\privacy;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
global $CFG;
28
 
29
require_once($CFG->dirroot . '/comment/locallib.php');
30
require_once($CFG->dirroot . '/comment/lib.php');
31
 
32
use core_privacy\local\request\approved_userlist;
33
use core_privacy\tests\provider_testcase;
34
use core_privacy\tests\request\approved_contextlist;
35
 
36
/**
37
 * Unit tests for comment/classes/privacy/policy
38
 *
39
 * @copyright  2018 Adrian Greeve <adrian@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 provider_testcase {
1 efrain 43
 
44
    protected function setUp(): void {
1441 ariadna 45
        parent::setUp();
1 efrain 46
        $this->resetAfterTest();
47
    }
48
 
49
    /**
50
     * Check the exporting of comments for a user id in a context.
51
     */
11 efrain 52
    public function test_export_comments(): void {
1 efrain 53
        $course = $this->getDataGenerator()->create_course();
54
        $context = \context_course::instance($course->id);
55
 
56
        $comment = $this->get_comment_object($context, $course);
57
 
58
        $user1 = $this->getDataGenerator()->create_user();
59
        $user2 = $this->getDataGenerator()->create_user();
60
 
61
        // Add comments.
62
        $comments = [];
63
        $firstcomment = 'This is the first comment';
64
        $this->setUser($user1);
65
        $comment->add($firstcomment);
66
        $comments[$user1->id] = $firstcomment;
67
 
68
        $secondcomment = 'From the second user';
69
        $this->setUser($user2);
70
        $comment->add($secondcomment);
71
        $comments[$user2->id] = $secondcomment;
72
 
73
        // Retrieve comments only for user1.
74
        $this->setUser($user1);
75
        $writer = \core_privacy\local\request\writer::with_context($context);
76
        provider::export_comments($context, 'block_comments', 'page_comments', 0, []);
77
 
78
        $data = $writer->get_data([get_string('commentsubcontext', 'core_comment')]);
79
        $exportedcomments = $data->comments;
80
 
81
        // There is only one comment made by this user.
82
        $this->assertCount(1, $exportedcomments);
83
        $comment = reset($exportedcomments);
84
        $this->assertEquals($comments[$user1->id], format_string($comment->content, FORMAT_PLAIN));
85
 
86
        // Retrieve comments from any user.
87
        provider::export_comments($context, 'block_comments', 'page_comments', 0, [], false);
88
 
89
        $data = $writer->get_data([get_string('commentsubcontext', 'core_comment')]);
90
        $exportedcomments = $data->comments;
91
 
92
        // The whole conversation is two comments.
93
        $this->assertCount(2, $exportedcomments);
94
        foreach ($exportedcomments as $comment) {
95
            $this->assertEquals($comments[$comment->userid], format_string($comment->content, FORMAT_PLAIN));
96
        }
97
    }
98
 
99
    /**
100
     * Tests the deletion of all comments in a context.
101
     */
11 efrain 102
    public function test_delete_comments_for_all_users(): void {
1 efrain 103
        global $DB;
104
 
105
        $course1 = $this->getDataGenerator()->create_course();
106
        $course2 = $this->getDataGenerator()->create_course();
107
 
108
        $coursecontext1 = \context_course::instance($course1->id);
109
        $coursecontext2 = \context_course::instance($course2->id);
110
 
111
        $user1 = $this->getDataGenerator()->create_user();
112
        $user2 = $this->getDataGenerator()->create_user();
113
 
114
        $comment1 = $this->get_comment_object($coursecontext1, $course1);
115
        $comment2 = $this->get_comment_object($coursecontext2, $course2);
116
 
117
        $this->setUser($user1);
118
        $comment1->add('First comment for user 1 on comment 1');
119
        $comment2->add('First comment for user 1 on comment 2');
120
        $this->setUser($user2);
121
        $comment1->add('First comment for user 2 on comment 1');
122
        $comment2->add('First comment for user 2 on comment 2');
123
 
124
        // Because of the way things are set up with validation, creating an entry with the same context in a different component
125
        // or comment area is a huge pain. We're just going to jam entries into the table instead.
126
        $record = (object) [
127
            'contextid' => $coursecontext1->id,
128
            'component' => 'block_comments',
129
            'commentarea' => 'other_comments',
130
            'itemid' => 2,
131
            'content' => 'Comment user 1 different comment area',
132
            'format' => 0,
133
            'userid' => $user1->id,
134
            'timecreated' => time()
135
        ];
136
        $DB->insert_record('comments', $record);
137
        $record = (object) [
138
            'contextid' => $coursecontext1->id,
139
            'component' => 'tool_dataprivacy',
140
            'commentarea' => 'page_comments',
141
            'itemid' => 2,
142
            'content' => 'Comment user 1 different component',
143
            'format' => 0,
144
            'userid' => $user1->id,
145
            'timecreated' => time()
146
        ];
147
        $DB->insert_record('comments', $record);
148
 
149
        // Delete only for the first context. All records in the comments table for this context should be removed.
150
        provider::delete_comments_for_all_users($coursecontext1, 'block_comments', 'page_comments', 0);
151
        // No records left here.
152
        $this->assertCount(0, $comment1->get_comments());
153
        // All of the records are left intact here.
154
        $this->assertCount(2, $comment2->get_comments());
155
        // Check the other comment area.
156
        $result = $DB->get_records('comments', ['commentarea' => 'other_comments']);
157
        $this->assertCount(1, $result);
158
        $data = array_shift($result);
159
        $this->assertEquals('other_comments', $data->commentarea);
160
        // Check the different component, same commentarea.
161
        $result = $DB->get_records('comments', ['component' => 'tool_dataprivacy']);
162
        $this->assertCount(1, $result);
163
        $data = array_shift($result);
164
        $this->assertEquals('tool_dataprivacy', $data->component);
165
    }
166
 
167
    /**
168
     * Tests the deletion of all comments in a context.
169
     */
11 efrain 170
    public function test_delete_comments_for_all_users_select(): void {
1 efrain 171
        global $DB;
172
 
173
        $course1 = $this->getDataGenerator()->create_course();
174
        $course2 = $this->getDataGenerator()->create_course();
175
 
176
        $coursecontext1 = \context_course::instance($course1->id);
177
        $coursecontext2 = \context_course::instance($course2->id);
178
 
179
        $user1 = $this->getDataGenerator()->create_user();
180
        $user2 = $this->getDataGenerator()->create_user();
181
 
182
        $comment1 = $this->get_comment_object($coursecontext1, $course1);
183
        $comment2 = $this->get_comment_object($coursecontext2, $course2);
184
 
185
        $this->setUser($user1);
186
        $comment1->add('First comment for user 1 on comment 1');
187
        $comment2->add('First comment for user 1 on comment 2');
188
        $this->setUser($user2);
189
        $comment1->add('First comment for user 2 on comment 1');
190
        $comment2->add('First comment for user 2 on comment 2');
191
 
192
        // Because of the way things are set up with validation, creating an entry with the same context in a different component
193
        // or comment area is a huge pain. We're just going to jam entries into the table instead.
194
        $record = (object) [
195
            'contextid' => $coursecontext1->id,
196
            'component' => 'block_comments',
197
            'commentarea' => 'other_comments',
198
            'itemid' => 2,
199
            'content' => 'Comment user 1 different comment area',
200
            'format' => 0,
201
            'userid' => $user1->id,
202
            'timecreated' => time()
203
        ];
204
        $DB->insert_record('comments', $record);
205
        $record = (object) [
206
            'contextid' => $coursecontext1->id,
207
            'component' => 'tool_dataprivacy',
208
            'commentarea' => 'page_comments',
209
            'itemid' => 2,
210
            'content' => 'Comment user 1 different component',
211
            'format' => 0,
212
            'userid' => $user1->id,
213
            'timecreated' => time()
214
        ];
215
        $DB->insert_record('comments', $record);
216
 
217
        // Delete only for the first context. All records in the comments table for this context should be removed.
218
        list($sql, $params) = $DB->get_in_or_equal([0, 1, 2, 3], SQL_PARAMS_NAMED);
219
        provider::delete_comments_for_all_users_select($coursecontext1,
220
            'block_comments', 'page_comments', $sql, $params);
221
        // No records left here.
222
        $this->assertCount(0, $comment1->get_comments());
223
        // All of the records are left intact here.
224
        $this->assertCount(2, $comment2->get_comments());
225
        // Check the other comment area.
226
        $result = $DB->get_records('comments', ['commentarea' => 'other_comments']);
227
        $this->assertCount(1, $result);
228
        $data = array_shift($result);
229
        $this->assertEquals('other_comments', $data->commentarea);
230
        // Check the different component, same commentarea.
231
        $result = $DB->get_records('comments', ['component' => 'tool_dataprivacy']);
232
        $this->assertCount(1, $result);
233
        $data = array_shift($result);
234
        $this->assertEquals('tool_dataprivacy', $data->component);
235
    }
236
 
237
    /**
238
     * Tests deletion of comments for a specified user and contexts.
239
     */
11 efrain 240
    public function test_delete_comments_for_user(): void {
1 efrain 241
        global $DB;
242
 
243
        $course1 = $this->getDataGenerator()->create_course();
244
        $course2 = $this->getDataGenerator()->create_course();
245
        $course3 = $this->getDataGenerator()->create_course();
246
 
247
        $coursecontext1 = \context_course::instance($course1->id);
248
        $coursecontext2 = \context_course::instance($course2->id);
249
        $coursecontext3 = \context_course::instance($course3->id);
250
 
251
        $user1 = $this->getDataGenerator()->create_user();
252
        $user2 = $this->getDataGenerator()->create_user();
253
 
254
        $comment1 = $this->get_comment_object($coursecontext1, $course1);
255
        $comment2 = $this->get_comment_object($coursecontext2, $course2);
256
        $comment3 = $this->get_comment_object($coursecontext3, $course3);
257
 
258
        $this->setUser($user1);
259
        $comment1->add('First comment for user 1');
260
        $comment2->add('User 1 comment in second comment');
261
 
262
        $this->setUser($user2);
263
        $comment2->add('User two replied in comment two');
264
        $comment3->add('Comment three for user 2.');
265
 
266
        // Because of the way things are set up with validation, creating an entry with the same context in a different component
267
        // or comment area is a huge pain. We're just going to jam entries into the table instead.
268
        $record = (object) [
269
            'contextid' => $coursecontext1->id,
270
            'component' => 'block_comments',
271
            'commentarea' => 'other_comments',
272
            'itemid' => 2,
273
            'content' => 'Comment user 1 different comment area',
274
            'format' => 0,
275
            'userid' => $user1->id,
276
            'timecreated' => time()
277
        ];
278
        $DB->insert_record('comments', $record);
279
        $record = (object) [
280
            'contextid' => $coursecontext1->id,
281
            'component' => 'tool_dataprivacy',
282
            'commentarea' => 'page_comments',
283
            'itemid' => 2,
284
            'content' => 'Comment user 1 different component',
285
            'format' => 0,
286
            'userid' => $user1->id,
287
            'timecreated' => time()
288
        ];
289
        $DB->insert_record('comments', $record);
290
 
291
        // Delete the comments for user 1.
292
        $approvedcontextlist = new approved_contextlist($user1, 'block_comments',
293
                [$coursecontext1->id, $coursecontext2->id]);
294
        provider::delete_comments_for_user($approvedcontextlist, 'block_comments', 'page_comments', 0);
295
 
296
        // No comments left in comments 1 as only user 1 commented there.
297
        $this->assertCount(0, $comment1->get_comments());
298
        // Only user 2 comments left in comments 2.
299
        $comment2comments = $comment2->get_comments();
300
        $this->assertCount(1, $comment2comments);
301
        $data = array_shift($comment2comments);
302
        $this->assertEquals($user2->id, $data->userid);
303
        // Nothing changed here as user 1 did not leave a comment.
304
        $comment3comments = $comment3->get_comments();
305
        $this->assertCount(1, $comment3comments);
306
        $data = array_shift($comment3comments);
307
        $this->assertEquals($user2->id, $data->userid);
308
        // Check the other comment area.
309
        $result = $DB->get_records('comments', ['commentarea' => 'other_comments']);
310
        $this->assertCount(1, $result);
311
        $data = array_shift($result);
312
        $this->assertEquals('other_comments', $data->commentarea);
313
        // Check the different component, same commentarea.
314
        $result = $DB->get_records('comments', ['component' => 'tool_dataprivacy']);
315
        $this->assertCount(1, $result);
316
        $data = array_shift($result);
317
        $this->assertEquals('tool_dataprivacy', $data->component);
318
    }
319
 
320
    /**
321
     * Tests deletion of comments for a specified userlist and context.
322
     */
11 efrain 323
    public function test_delete_comments_for_users(): void {
1 efrain 324
        global $DB;
325
 
326
        $course1 = $this->getDataGenerator()->create_course();
327
        $course2 = $this->getDataGenerator()->create_course();
328
        $course3 = $this->getDataGenerator()->create_course();
329
 
330
        $coursecontext1 = \context_course::instance($course1->id);
331
        $coursecontext2 = \context_course::instance($course2->id);
332
        $coursecontext3 = \context_course::instance($course3->id);
333
 
334
        $user1 = $this->getDataGenerator()->create_user();
335
        $user2 = $this->getDataGenerator()->create_user();
336
        $user3 = $this->getDataGenerator()->create_user();
337
 
338
        $comment1 = $this->get_comment_object($coursecontext1, $course1);
339
        $comment2 = $this->get_comment_object($coursecontext2, $course2);
340
        $comment3 = $this->get_comment_object($coursecontext3, $course3);
341
 
342
        $this->setUser($user1);
343
        $comment1->add('First comment for user 1');
344
        $comment2->add('User 1 comment in second comment');
345
 
346
        $this->setUser($user2);
347
        $comment2->add('User two replied in comment two');
348
 
349
        $this->setUser($user3);
350
        $comment2->add('User 3 also writing on comment 2, but will not be deleted');
351
        $comment3->add('Only user 3 commenting in comment 3.');
352
 
353
        // Because of the way things are set up with validation, creating an entry with the same context in a different component
354
        // or comment area is a huge pain. We're just going to jam entries into the table instead.
355
        $record = (object) [
356
            'contextid' => $coursecontext1->id,
357
            'component' => 'block_comments',
358
            'commentarea' => 'other_comments',
359
            'itemid' => 2,
360
            'content' => 'Comment user 1 different comment area',
361
            'format' => 0,
362
            'userid' => $user1->id,
363
            'timecreated' => time()
364
        ];
365
        $DB->insert_record('comments', $record);
366
        $record = (object) [
367
            'contextid' => $coursecontext1->id,
368
            'component' => 'tool_dataprivacy',
369
            'commentarea' => 'page_comments',
370
            'itemid' => 2,
371
            'content' => 'Comment user 1 different component',
372
            'format' => 0,
373
            'userid' => $user1->id,
374
            'timecreated' => time()
375
        ];
376
        $DB->insert_record('comments', $record);
377
 
378
        // Delete the comments for users 1 and 2 in all 3 contexts.
379
        $approvedusers = [$user1->id, $user2->id];
380
 
381
        $approveduserlist = new approved_userlist($coursecontext1, 'block_comments', $approvedusers);
382
        provider::delete_comments_for_users($approveduserlist, 'block_comments', 'page_comments');
383
 
384
        $approveduserlist = new approved_userlist($coursecontext2, 'block_comments', $approvedusers);
385
        provider::delete_comments_for_users($approveduserlist, 'block_comments', 'page_comments');
386
 
387
        $approveduserlist = new approved_userlist($coursecontext3, 'block_comments', $approvedusers);
388
        provider::delete_comments_for_users($approveduserlist, 'block_comments', 'page_comments');
389
 
390
        // No comments left in comments 1 as only user 1 commented there.
391
        $this->assertCount(0, $comment1->get_comments());
392
 
393
        // Only user 3's comment left in comments 2 as user 1 and 2 were approved for deletion.
394
        $comment2comments = $comment2->get_comments();
395
        $this->assertCount(1, $comment2comments);
396
        $comment2comment = array_shift($comment2comments);
397
        $this->assertEquals($user3->id, $comment2comment->userid);
398
 
399
        // Nothing changed here as user 1 and 2 did not leave a comment.
400
        $comment3comments = $comment3->get_comments();
401
        $this->assertCount(1, $comment3comments);
402
        $data = array_shift($comment3comments);
403
        $this->assertEquals($user3->id, $data->userid);
404
 
405
        // Check the other comment area.
406
        $result = $DB->get_records('comments', ['commentarea' => 'other_comments']);
407
        $this->assertCount(1, $result);
408
        $data = array_shift($result);
409
        $this->assertEquals('other_comments', $data->commentarea);
410
 
411
        // Check the different component, same commentarea.
412
        $result = $DB->get_records('comments', ['component' => 'tool_dataprivacy']);
413
        $this->assertCount(1, $result);
414
        $data = array_shift($result);
415
        $this->assertEquals('tool_dataprivacy', $data->component);
416
    }
417
 
418
    /**
419
     * Creates a comment object
420
     *
421
     * @param  context $context A context object.
422
     * @param  stdClass $course A course object.
423
     * @return comment The comment object.
424
     */
425
    protected function get_comment_object($context, $course) {
426
        // Comment on course page.
427
        $args = new \stdClass;
428
        $args->context = $context;
429
        $args->course = $course;
430
        $args->area = 'page_comments';
431
        $args->itemid = 0;
432
        $args->component = 'block_comments';
433
        $comment = new \comment($args);
434
        $comment->set_post_permission(true);
435
        return $comment;
436
    }
437
}