Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | 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 assignsubmission_comments.
19
 *
20
 * @package    assignsubmission_comments
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 assignsubmission_comments\privacy;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
global $CFG;
29
require_once($CFG->dirroot . '/mod/assign/tests/privacy/provider_test.php');
30
 
31
use mod_assign\privacy\useridlist;
32
 
33
/**
34
 * Unit tests for mod/assign/submission/comments/classes/privacy/
35
 *
36
 * @copyright  2018 Adrian Greeve <adrian@moodle.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class provider_test extends \mod_assign\privacy\provider_test {
40
 
41
    /**
42
     * Convenience function for creating feedback data.
43
     *
44
     * @param  object   $assign         assign object
45
     * @param  stdClass $student        user object
46
     * @param  string   $submissiontext Submission text
47
     * @return array   Submission plugin object and the submission object and the comment object.
48
     */
49
    protected function create_comment_submission($assign, $student, $submissiontext) {
50
 
51
        $submission = $assign->get_user_submission($student->id, true);
52
 
53
        $plugin = $assign->get_submission_plugin_by_type('comments');
54
 
55
        $context = $assign->get_context();
56
        $options = new \stdClass();
57
        $options->area = 'submission_comments';
58
        $options->course = $assign->get_course();
59
        $options->context = $context;
60
        $options->itemid = $submission->id;
61
        $options->component = 'assignsubmission_comments';
62
        $options->showcount = true;
63
        $options->displaycancel = true;
64
 
65
        $comment = new \comment($options);
66
        $comment->set_post_permission(true);
67
 
68
        $this->setUser($student);
69
 
70
        $comment->add($submissiontext);
71
 
72
        return [$plugin, $submission, $comment];
73
    }
74
 
75
    /**
76
     * Quick test to make sure that get_metadata returns something.
77
     */
78
    public function test_get_metadata() {
79
        $collection = new \core_privacy\local\metadata\collection('assignsubmission_comments');
80
        $collection = \assignsubmission_comments\privacy\provider::get_metadata($collection);
81
        $this->assertNotEmpty($collection);
82
    }
83
 
84
    /**
85
     * Test returning the context for a user who has made a comment in an assignment.
86
     */
87
    public function test_get_context_for_userid_within_submission() {
88
        $this->resetAfterTest();
89
        // Create course, assignment, submission, and then a feedback comment.
90
        $course = $this->getDataGenerator()->create_course();
91
        // Student.
92
        $user1 = $this->getDataGenerator()->create_user();
93
        // Teacher.
94
        $user2 = $this->getDataGenerator()->create_user();
95
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
96
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
97
        $assign = $this->create_instance(['course' => $course]);
98
 
99
        $context = $assign->get_context();
100
 
101
        $studentcomment = 'Comment from user 1';
102
        list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
103
        $teachercomment = 'From the teacher';
104
        $this->setUser($user2);
105
        $comment->add($teachercomment);
106
 
107
        $contextlist = new \core_privacy\local\request\contextlist();
108
        \assignsubmission_comments\privacy\provider::get_context_for_userid_within_submission($user2->id, $contextlist);
109
        $this->assertEquals($context->id, $contextlist->get_contextids()[0]);
110
    }
111
 
112
    /**
113
     * Test returning student ids given a user ID.
114
     */
115
    public function test_get_student_user_ids() {
116
        $this->resetAfterTest();
117
        // Create course, assignment, submission, and then a feedback comment.
118
        $course = $this->getDataGenerator()->create_course();
119
        // Student.
120
        $user1 = $this->getDataGenerator()->create_user();
121
        // Teacher.
122
        $user2 = $this->getDataGenerator()->create_user();
123
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
124
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
125
        $assign = $this->create_instance(['course' => $course]);
126
 
127
        $context = $assign->get_context();
128
 
129
        $studentcomment = 'Comment from user 1';
130
        list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
131
        $teachercomment = 'From the teacher';
132
        $this->setUser($user2);
133
        $comment->add($teachercomment);
134
 
135
        $useridlist = new useridlist($user2->id, $assign->get_instance()->id);
136
        \assignsubmission_comments\privacy\provider::get_student_user_ids($useridlist);
137
        $this->assertEquals($user1->id, $useridlist->get_userids()[0]->id);
138
    }
139
 
140
    /**
141
     * Test returning users related to a given context.
142
     */
143
    public function test_get_userids_from_context() {
144
        // Get a bunch of users making comments.
145
        // Some in one context some in another.
146
        $this->resetAfterTest();
147
        $course = $this->getDataGenerator()->create_course();
148
        // Only in first context.
149
        $user1 = $this->getDataGenerator()->create_user();
150
        $user2 = $this->getDataGenerator()->create_user();
151
        // First and second context.
152
        $user3 = $this->getDataGenerator()->create_user();
153
        // Second context only.
154
        $user4 = $this->getDataGenerator()->create_user();
155
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
156
        $assign1 = $this->create_instance(['course' => $course]);
157
        $assign2 = $this->create_instance(['course' => $course]);
158
 
159
        $assigncontext1 = $assign1->get_context();
160
        $assigncontext2 = $assign2->get_context();
161
 
162
        $user1comment = 'Comment from user 1';
163
        list($plugin, $submission, $comment) = $this->create_comment_submission($assign1, $user1, $user1comment);
164
        $user2comment = 'From user 2';
165
        $this->setUser($user2);
166
        $comment->add($user2comment);
167
        $user3comment = 'User 3 comment';
168
        $this->setUser($user3);
169
        $comment->add($user3comment);
170
        $user4comment = 'Comment from user 4';
171
        list($plugin, $submission, $comment) = $this->create_comment_submission($assign2, $user4, $user4comment);
172
        $user3secondcomment = 'Comment on user 4 post.';
173
        $this->setUser($user3);
174
        $comment->add($user3comment);
175
 
176
        $userlist = new \core_privacy\local\request\userlist($assigncontext1, 'assignsubmission_comments');
177
        \assignsubmission_comments\privacy\provider::get_userids_from_context($userlist);
178
        $userids = $userlist->get_userids();
179
        $this->assertCount(3, $userids);
180
        // User 1,2 and 3 are the expected ones in the array. User 4 isn't.
181
        $this->assertContainsEquals($user1->id, $userids);
182
        $this->assertContainsEquals($user2->id, $userids);
183
        $this->assertContainsEquals($user3->id, $userids);
184
        $this->assertNotContainsEquals($user4->id, $userids);
185
    }
186
 
187
    /**
188
     * Test that comments are exported for a user.
189
     */
190
    public function test_export_submission_user_data() {
191
        $this->resetAfterTest();
192
        // Create course, assignment, submission, and then a feedback comment.
193
        $course = $this->getDataGenerator()->create_course();
194
        // Student.
195
        $user1 = $this->getDataGenerator()->create_user();
196
        // Teacher.
197
        $user2 = $this->getDataGenerator()->create_user();
198
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
199
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'editingteacher');
200
        $assign = $this->create_instance(['course' => $course]);
201
 
202
        $context = $assign->get_context();
203
 
204
        $studentcomment = 'Comment from user 1';
205
        list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
206
        $teachercomment = 'From the teacher';
207
        $this->setUser($user2);
208
        $comment->add($teachercomment);
209
 
210
        $writer = \core_privacy\local\request\writer::with_context($context);
211
        $this->assertFalse($writer->has_any_data());
212
 
213
        // The student should be able to see the teachers feedback.
214
        $exportdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, $submission);
215
        \assignsubmission_comments\privacy\provider::export_submission_user_data($exportdata);
216
        $exportedcomments = $writer->get_data(['Comments']);
217
 
218
        // Can't rely on these comments coming out in order.
219
        if ($exportedcomments->comments[0]->userid == $user1->id) {
220
            $exportedstudentcomment = $exportedcomments->comments[0]->content;
221
            $exportedteachercomment = $exportedcomments->comments[1]->content;
222
        } else {
223
            $exportedstudentcomment = $exportedcomments->comments[1]->content;
224
            $exportedteachercomment = $exportedcomments->comments[0]->content;
225
        }
226
        $this->assertCount(2, $exportedcomments->comments);
227
        $this->assertStringContainsString($studentcomment, $exportedstudentcomment);
228
        $this->assertStringContainsString($teachercomment, $exportedteachercomment);
229
    }
230
 
231
    /**
232
     * Test that all comments are deleted for this context.
233
     */
234
    public function test_delete_submission_for_context() {
235
        global $DB;
236
        $this->resetAfterTest();
237
 
238
        // Create course, assignment, submission, and then a feedback comment.
239
        $course = $this->getDataGenerator()->create_course();
240
        // Student.
241
        $user1 = $this->getDataGenerator()->create_user();
242
        $user2 = $this->getDataGenerator()->create_user();
243
        // Teacher.
244
        $user3 = $this->getDataGenerator()->create_user();
245
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
246
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
247
        $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
248
        $assign = $this->create_instance(['course' => $course]);
249
 
250
        $context = $assign->get_context();
251
 
252
        $studentcomment = 'Comment from user 1';
253
        list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
254
        $studentcomment = 'Comment from user 2';
255
        list($plugin2, $submission2, $comment2) = $this->create_comment_submission($assign, $user2, $studentcomment);
256
        $teachercomment1 = 'From the teacher';
257
        $teachercomment2 = 'From the teacher for second student.';
258
        $this->setUser($user3);
259
        $comment->add($teachercomment1);
260
        $comment2->add($teachercomment2);
261
 
262
        // Only need the context in this plugin for this operation.
263
        $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign);
264
        \assignsubmission_comments\privacy\provider::delete_submission_for_context($requestdata);
265
 
266
        $results = $DB->get_records('comments', ['contextid' => $context->id]);
267
        $this->assertEmpty($results);
268
    }
269
 
270
    /**
271
     * Test that the comments for a user are deleted.
272
     */
273
    public function test_delete_submission_for_userid() {
274
        global $DB;
275
        $this->resetAfterTest();
276
        // Create course, assignment, submission, and then a feedback comment.
277
        $course = $this->getDataGenerator()->create_course();
278
        // Student.
279
        $user1 = $this->getDataGenerator()->create_user();
280
        $user2 = $this->getDataGenerator()->create_user();
281
        // Teacher.
282
        $user3 = $this->getDataGenerator()->create_user();
283
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
284
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
285
        $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'editingteacher');
286
        $assign = $this->create_instance(['course' => $course]);
287
 
288
        $context = $assign->get_context();
289
 
290
        $studentcomment = 'Comment from user 1';
291
        list($plugin, $submission, $comment) = $this->create_comment_submission($assign, $user1, $studentcomment);
292
        $studentcomment = 'Comment from user 2';
293
        list($plugin2, $submission2, $comment2) = $this->create_comment_submission($assign, $user2, $studentcomment);
294
        $teachercomment1 = 'From the teacher';
295
        $teachercomment2 = 'From the teacher for second student.';
296
        $this->setUser($user3);
297
        $comment->add($teachercomment1);
298
        $comment2->add($teachercomment2);
299
 
300
        // Provide full details to delete the comments.
301
        $requestdata = new \mod_assign\privacy\assign_plugin_request_data($context, $assign, null, [], $user1);
302
        \assignsubmission_comments\privacy\provider::delete_submission_for_userid($requestdata);
303
 
304
        $results = $DB->get_records('comments', ['contextid' => $context->id]);
305
        // We are only deleting the comments for user1 (one comment) so we should have three left.
306
        $this->assertCount(3, $results);
307
        foreach ($results as $result) {
308
            // Check that none of the comments are from user1.
309
            $this->assertNotEquals($user1->id, $result->userid);
310
        }
311
    }
312
 
313
    /**
314
     * Test deletion of all submissions for a context works.
315
     */
316
    public function test_delete_submissions() {
317
        global $DB;
318
        // Get a bunch of users making comments.
319
        // Some in one context some in another.
320
        $this->resetAfterTest();
321
        $course = $this->getDataGenerator()->create_course();
322
        // Only in first context.
323
        $user1 = $this->getDataGenerator()->create_user();
324
        $user2 = $this->getDataGenerator()->create_user();
325
        // First and second context.
326
        $user3 = $this->getDataGenerator()->create_user();
327
        // Second context only.
328
        $user4 = $this->getDataGenerator()->create_user();
329
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
330
        $assign1 = $this->create_instance(['course' => $course]);
331
        $assign2 = $this->create_instance(['course' => $course]);
332
 
333
        $assigncontext1 = $assign1->get_context();
334
        $assigncontext2 = $assign2->get_context();
335
 
336
        $user1comment = 'Comment from user 1';
337
        list($plugin, $submission, $comment) = $this->create_comment_submission($assign1, $user1, $user1comment);
338
        $user2comment = 'From user 2';
339
        $this->setUser($user2);
340
        $comment->add($user2comment);
341
        $user3comment = 'User 3 comment';
342
        $this->setUser($user3);
343
        $comment->add($user3comment);
344
        $user4comment = 'Comment from user 4';
345
        list($plugin, $submission, $comment) = $this->create_comment_submission($assign2, $user4, $user4comment);
346
        $user3secondcomment = 'Comment on user 4 post.';
347
        $this->setUser($user3);
348
        $comment->add($user3comment);
349
 
350
        // There should be three entries. One for the first three users.
351
        $results = $DB->get_records('comments', ['contextid' => $assigncontext1->id]);
352
        $this->assertCount(3, $results);
353
 
354
        $deletedata = new \mod_assign\privacy\assign_plugin_request_data($assigncontext1, $assign1);
355
        $deletedata->set_userids([$user1->id, $user3->id]);
356
        \assignsubmission_comments\privacy\provider::delete_submissions($deletedata);
357
 
358
        // We should be left with just a comment from user 2.
359
        $results = $DB->get_records('comments', ['contextid' => $assigncontext1->id]);
360
        $this->assertCount(1, $results);
361
        $this->assertEquals($user2comment, current($results)->content);
362
    }
363
}