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 mod_assign\privacy;
18
 
19
use core_privacy\local\request\writer;
20
use core_privacy\local\request\approved_contextlist;
21
use mod_assign\privacy\provider;
1441 ariadna 22
use mod_assign\tests\provider_testcase;
1 efrain 23
 
24
/**
25
 * Unit tests for mod/assign/classes/privacy/
26
 *
1441 ariadna 27
 * @package    mod_assign
1 efrain 28
 * @copyright  2018 Adrian Greeve <adrian@moodle.com>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 30
 * @covers \mod_assign\privacy\provider
1 efrain 31
 */
1441 ariadna 32
final class provider_test extends provider_testcase {
1 efrain 33
    /**
34
     * Test that getting the contexts for a user works.
35
     */
11 efrain 36
    public function test_get_contexts_for_userid(): void {
1 efrain 37
        global $DB;
38
        $this->resetAfterTest();
39
 
40
        $course1 = $this->getDataGenerator()->create_course();
41
        $course2 = $this->getDataGenerator()->create_course();
42
        $course3 = $this->getDataGenerator()->create_course();
43
 
44
        $user1 = $this->getDataGenerator()->create_user();
45
        $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student');
46
        $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 'student');
47
        // Need a second user to create content in other assignments.
48
        $user2 = $this->getDataGenerator()->create_user();
49
        $this->getDataGenerator()->enrol_user($user2->id, $course2->id, 'student');
50
 
51
        // Create multiple assignments.
52
        // Assignment with a text submission.
53
        $assign1 = $this->create_instance(['course' => $course1]);
54
        // Assignment two in a different course that the user is not enrolled in.
55
        $assign2 = $this->create_instance(['course' => $course2]);
56
        // Assignment three has an entry in the override table.
57
        $assign3 = $this->create_instance(['course' => $course3, 'cutoffdate' => time()]);
58
        // Assignment four - blind marking.
59
        $assign4 = $this->create_instance(['course' => $course1, 'blindmarking' => 1]);
60
        // Assignment five - user flags.
61
        $assign5 = $this->create_instance(['course' => $course3]);
62
 
63
        // Override has to be manually inserted into the DB.
64
        $overridedata = new \stdClass();
65
        $overridedata->assignid = $assign3->get_instance()->id;
66
        $overridedata->userid = $user1->id;
67
        $overridedata->duedate = time();
68
        $DB->insert_record('assign_overrides', $overridedata);
69
        // Assign unique id for blind marking in assignment four for user 1.
70
        \assign::get_uniqueid_for_user_static($assign4->get_instance()->id, $user1->id);
71
        // Create an entry in the user flags table.
72
        $assign5->get_user_flags($user1->id, true);
73
 
74
        // The user will be in these contexts.
75
        $usercontextids = [
76
            $assign1->get_context()->id,
77
            $assign3->get_context()->id,
78
            $assign4->get_context()->id,
79
            $assign5->get_context()->id,
80
        ];
81
 
82
        $submission = new \stdClass();
83
        $submission->assignment = $assign1->get_instance()->id;
84
        $submission->userid = $user1->id;
85
        $submission->timecreated = time();
86
        $submission->onlinetext_editor = ['text' => 'Submission text',
87
                                         'format' => FORMAT_MOODLE];
88
 
89
        $this->setUser($user1);
90
        $notices = [];
91
        $assign1->save_submission($submission, $notices);
92
 
93
        // Create a submission for the second assignment.
94
        $submission->assignment = $assign2->get_instance()->id;
95
        $submission->userid = $user2->id;
96
        $this->setUser($user2);
97
        $assign2->save_submission($submission, $notices);
98
 
99
        $contextlist = provider::get_contexts_for_userid($user1->id);
100
        $this->assertEquals(count($usercontextids), count($contextlist->get_contextids()));
101
        // There should be no difference between the contexts.
102
        $this->assertEmpty(array_diff($usercontextids, $contextlist->get_contextids()));
103
    }
104
 
105
    /**
106
     * Test returning a list of user IDs related to a context (assign).
107
     */
11 efrain 108
    public function test_get_users_in_context(): void {
1 efrain 109
        global $DB;
110
 
111
        $this->resetAfterTest();
112
 
113
        $course = $this->getDataGenerator()->create_course();
114
 
115
        // Only made a comment on a submission.
116
        $user1 = $this->getDataGenerator()->create_user();
117
        // User 2 only has information about an activity override.
118
        $user2 = $this->getDataGenerator()->create_user();
119
        // User 3 made a submission.
120
        $user3 = $this->getDataGenerator()->create_user();
121
        // User 4 makes a submission and it is marked by the teacher.
122
        $user4 = $this->getDataGenerator()->create_user();
123
        // Grading and providing feedback as a teacher.
124
        $user5 = $this->getDataGenerator()->create_user();
125
        // This user has no entries and should not show up.
126
        $user6 = $this->getDataGenerator()->create_user();
127
 
128
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
129
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
130
        $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
131
        $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
132
        $this->getDataGenerator()->enrol_user($user5->id, $course->id, 'editingteacher');
133
        $this->getDataGenerator()->enrol_user($user6->id, $course->id, 'student');
134
 
135
        $assign1 = $this->create_instance(['course' => $course,
136
                'assignsubmission_onlinetext_enabled' => true,
137
                'assignfeedback_comments_enabled' => true]);
138
        $assign2 = $this->create_instance(['course' => $course]);
139
 
140
        $context = $assign1->get_context();
141
 
142
        // Jam an entry in the comments table for user 1.
143
        $comment = (object) [
144
            'contextid' => $context->id,
145
            'component' => 'assignsubmission_comments',
146
            'commentarea' => 'submission_comments',
147
            'itemid' => 5,
148
            'content' => 'A comment by user 1',
149
            'format' => 0,
150
            'userid' => $user1->id,
151
            'timecreated' => time()
152
        ];
153
        $DB->insert_record('comments', $comment);
154
 
155
        $this->setUser($user5); // Set the user to the teacher.
156
 
157
        $overridedata = new \stdClass();
158
        $overridedata->assignid = $assign1->get_instance()->id;
159
        $overridedata->userid = $user2->id;
160
        $overridedata->duedate = time();
161
        $overridedata->allowsubmissionsfromdate = time();
162
        $overridedata->cutoffdate = time();
163
        $DB->insert_record('assign_overrides', $overridedata);
164
 
165
        $submissiontext = 'My first submission';
166
        $submission = $this->create_submission($assign1, $user3, $submissiontext);
167
 
168
        $submissiontext = 'My first submission';
169
        $submission = $this->create_submission($assign1, $user4, $submissiontext);
170
 
171
        $this->setUser($user5);
172
 
173
        $grade = '72.00';
174
        $teachercommenttext = 'This is better. Thanks.';
175
        $data = new \stdClass();
176
        $data->attemptnumber = 1;
177
        $data->grade = $grade;
178
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext, 'format' => FORMAT_MOODLE];
179
 
180
        // Give the submission a grade.
181
        $assign1->save_grade($user4->id, $data);
182
 
183
        $userlist = new \core_privacy\local\request\userlist($context, 'assign');
184
        provider::get_users_in_context($userlist);
185
        $userids = $userlist->get_userids();
186
        $this->assertTrue(in_array($user1->id, $userids));
187
        $this->assertTrue(in_array($user2->id, $userids));
188
        $this->assertTrue(in_array($user3->id, $userids));
189
        $this->assertTrue(in_array($user4->id, $userids));
190
        $this->assertTrue(in_array($user5->id, $userids));
191
        $this->assertFalse(in_array($user6->id, $userids));
192
    }
193
 
194
    /**
195
     * Test that a student with multiple submissions and grades is returned with the correct data.
196
     */
11 efrain 197
    public function test_export_user_data_student(): void {
1 efrain 198
        global $DB;
199
        $this->resetAfterTest();
200
        $course = $this->getDataGenerator()->create_course();
201
        $coursecontext = \context_course::instance($course->id);
202
 
203
        $user = $this->getDataGenerator()->create_user();
204
        $teacher = $this->getDataGenerator()->create_user();
205
        $this->getDataGenerator()->enrol_user($user->id, $course->id, 'student');
206
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher');
207
        $assign = $this->create_instance([
208
                'course' => $course,
209
                'name' => 'Assign 1',
210
                'attemptreopenmethod' => ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
211
                'maxattempts' => 3,
212
                'assignsubmission_onlinetext_enabled' => true,
213
                'assignfeedback_comments_enabled' => true
214
            ]);
215
 
216
        $context = $assign->get_context();
217
        // Create some submissions (multiple attempts) for a student.
218
        $submissiontext = 'My first submission';
219
        $submission = $this->create_submission($assign, $user, $submissiontext);
220
 
221
        $this->setUser($teacher);
222
 
223
        $overridedata = new \stdClass();
224
        $overridedata->assignid = $assign->get_instance()->id;
225
        $overridedata->userid = $user->id;
226
        $overridedata->duedate = time();
227
        $overridedata->allowsubmissionsfromdate = time();
228
        $overridedata->cutoffdate = time();
229
        $DB->insert_record('assign_overrides', $overridedata);
230
 
231
        $grade1 = '67.00';
232
        $teachercommenttext = 'Please try again.';
233
        $data = new \stdClass();
234
        $data->attemptnumber = 0;
235
        $data->grade = $grade1;
236
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext, 'format' => FORMAT_MOODLE];
237
 
238
        // Give the submission a grade.
239
        $assign->save_grade($user->id, $data);
240
 
241
        $submissiontext2 = 'My second submission';
242
        $submission = $this->create_submission($assign, $user, $submissiontext2, 1);
243
 
244
        $this->setUser($teacher);
245
 
246
        $grade2 = '72.00';
247
        $teachercommenttext2 = 'This is better. Thanks.';
248
        $data = new \stdClass();
249
        $data->attemptnumber = 1;
250
        $data->grade = $grade2;
251
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext2, 'format' => FORMAT_MOODLE];
252
 
253
        // Give the submission a grade.
254
        $assign->save_grade($user->id, $data);
255
 
256
        /** @var \core_privacy\tests\request\content_writer $writer */
257
        $writer = writer::with_context($context);
258
        $this->assertFalse($writer->has_any_data());
259
 
260
        // The student should have some text submitted.
261
        // Add the course context as well to make sure there is no error.
262
        $approvedlist = new approved_contextlist($user, 'mod_assign', [$context->id, $coursecontext->id]);
263
        provider::export_user_data($approvedlist);
264
 
265
        // Check that we have general details about the assignment.
266
        $this->assertEquals('Assign 1', $writer->get_data()->name);
267
        // Check Submissions.
268
        $this->assertEquals($submissiontext, $writer->get_data(['attempt 1', 'Submission Text'])->text);
269
        $this->assertEquals($submissiontext2, $writer->get_data(['attempt 2', 'Submission Text'])->text);
270
        $this->assertEquals(1, $writer->get_data(['attempt 1', 'submission'])->attemptnumber);
271
        $this->assertEquals(2, $writer->get_data(['attempt 2', 'submission'])->attemptnumber);
272
        // Check grades.
273
        $this->assertEquals((float)$grade1, $writer->get_data(['attempt 1', 'grade'])->grade);
274
        $this->assertEquals((float)$grade2, $writer->get_data(['attempt 2', 'grade'])->grade);
275
        // Check feedback.
276
        $this->assertStringContainsString($teachercommenttext, $writer->get_data(['attempt 1', 'Feedback comments'])->commenttext);
277
        $this->assertStringContainsString($teachercommenttext2, $writer->get_data(['attempt 2', 'Feedback comments'])->commenttext);
278
 
279
        // Check override data was exported correctly.
280
        $overrideexport = $writer->get_data(['Overrides']);
281
        $this->assertEquals(\core_privacy\local\request\transform::datetime($overridedata->duedate),
282
                $overrideexport->duedate);
283
        $this->assertEquals(\core_privacy\local\request\transform::datetime($overridedata->cutoffdate),
284
                $overrideexport->cutoffdate);
285
        $this->assertEquals(\core_privacy\local\request\transform::datetime($overridedata->allowsubmissionsfromdate),
286
                $overrideexport->allowsubmissionsfromdate);
287
    }
288
 
289
    /**
290
     * Tests the data returned for a teacher.
291
     */
11 efrain 292
    public function test_export_user_data_teacher(): void {
1 efrain 293
        $this->resetAfterTest();
294
        $course = $this->getDataGenerator()->create_course();
295
        $coursecontext = \context_course::instance($course->id);
296
 
297
        $user1 = $this->getDataGenerator()->create_user();
298
        $user2 = $this->getDataGenerator()->create_user();
299
        $teacher = $this->getDataGenerator()->create_user();
300
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
301
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
302
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher');
303
        $assign = $this->create_instance([
304
                'course' => $course,
305
                'name' => 'Assign 1',
306
                'attemptreopenmethod' => ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
307
                'maxattempts' => 3,
308
                'assignsubmission_onlinetext_enabled' => true,
309
                'assignfeedback_comments_enabled' => true
310
            ]);
311
 
312
        $context = $assign->get_context();
313
 
314
        // Create and grade some submissions from the students.
315
        $submissiontext = 'My first submission';
316
        $submission = $this->create_submission($assign, $user1, $submissiontext);
317
 
318
        $this->setUser($teacher);
319
 
320
        $grade1 = '54.00';
321
        $teachercommenttext = 'Comment on user 1 attempt 1.';
322
        $data = new \stdClass();
323
        $data->attemptnumber = 0;
324
        $data->grade = $grade1;
325
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext, 'format' => FORMAT_MOODLE];
326
 
327
        // Give the submission a grade.
328
        $assign->save_grade($user1->id, $data);
329
 
330
        // Create and grade some submissions from the students.
331
        $submissiontext2 = 'My first submission for user 2';
332
        $submission = $this->create_submission($assign, $user2, $submissiontext2);
333
 
334
        $this->setUser($teacher);
335
 
336
        $grade2 = '56.00';
337
        $teachercommenttext2 = 'Comment on user 2 first attempt.';
338
        $data = new \stdClass();
339
        $data->attemptnumber = 0;
340
        $data->grade = $grade2;
341
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext2, 'format' => FORMAT_MOODLE];
342
 
343
        // Give the submission a grade.
344
        $assign->save_grade($user2->id, $data);
345
 
346
        // Create and grade some submissions from the students.
347
        $submissiontext3 = 'My second submission for user 2';
348
        $submission = $this->create_submission($assign, $user2, $submissiontext3, 1);
349
 
350
        $this->setUser($teacher);
351
 
352
        $grade3 = '83.00';
353
        $teachercommenttext3 = 'Comment on user 2 another attempt.';
354
        $data = new \stdClass();
355
        $data->attemptnumber = 1;
356
        $data->grade = $grade3;
357
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext3, 'format' => FORMAT_MOODLE];
358
 
359
        // Give the submission a grade.
360
        $assign->save_grade($user2->id, $data);
361
 
362
        // Set up some flags.
363
        $duedate = time();
364
        $flagdata = $assign->get_user_flags($teacher->id, true);
365
        $flagdata->mailed = 1;
366
        $flagdata->extensionduedate = $duedate;
367
        $assign->update_user_flags($flagdata);
368
 
369
        /** @var \core_privacy\tests\request\content_writer $writer */
370
        $writer = writer::with_context($context);
371
        $this->assertFalse($writer->has_any_data());
372
 
373
        // The student should have some text submitted.
374
        $approvedlist = new approved_contextlist($teacher, 'mod_assign', [$context->id, $coursecontext->id]);
375
        provider::export_user_data($approvedlist);
376
 
377
        // Check flag metadata.
378
        $metadata = $writer->get_all_metadata();
379
        $this->assertEquals(\core_privacy\local\request\transform::yesno(1), $metadata['mailed']->value);
380
        $this->assertEquals(\core_privacy\local\request\transform::datetime($duedate), $metadata['extensionduedate']->value);
381
 
382
        // Check for student grades given.
383
        $student1grade = $writer->get_data(['studentsubmissions', $user1->id, 'attempt 1', 'grade']);
384
        $this->assertEquals((float)$grade1, $student1grade->grade);
385
        $student2grade1 = $writer->get_data(['studentsubmissions', $user2->id, 'attempt 1', 'grade']);
386
        $this->assertEquals((float)$grade2, $student2grade1->grade);
387
        $student2grade2 = $writer->get_data(['studentsubmissions', $user2->id, 'attempt 2', 'grade']);
388
        $this->assertEquals((float)$grade3, $student2grade2->grade);
389
        // Check for feedback given to students.
390
        $this->assertStringContainsString($teachercommenttext, $writer->get_data(['studentsubmissions', $user1->id, 'attempt 1',
391
                'Feedback comments'])->commenttext);
392
        $this->assertStringContainsString($teachercommenttext2, $writer->get_data(['studentsubmissions', $user2->id, 'attempt 1',
393
                'Feedback comments'])->commenttext);
394
        $this->assertStringContainsString($teachercommenttext3, $writer->get_data(['studentsubmissions', $user2->id, 'attempt 2',
395
                'Feedback comments'])->commenttext);
396
    }
397
 
398
    /**
399
     * A test for deleting all user data for a given context.
400
     */
11 efrain 401
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 402
        global $DB;
403
        $this->resetAfterTest();
404
        $course = $this->getDataGenerator()->create_course();
405
 
406
        $user1 = $this->getDataGenerator()->create_user();
407
        $user2 = $this->getDataGenerator()->create_user();
408
        $teacher = $this->getDataGenerator()->create_user();
409
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
410
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
411
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher');
412
        $assign = $this->create_instance([
413
                'course' => $course,
414
                'name' => 'Assign 1',
415
                'attemptreopenmethod' => ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
416
                'maxattempts' => 3,
417
                'assignsubmission_onlinetext_enabled' => true,
418
                'assignfeedback_comments_enabled' => true
419
            ]);
420
 
421
        $context = $assign->get_context();
422
 
423
        // Create and grade some submissions from the students.
424
        $submissiontext = 'My first submission';
425
        $submission = $this->create_submission($assign, $user1, $submissiontext);
426
 
427
        $this->setUser($teacher);
428
 
429
        // Overrides for both students.
430
        $overridedata = new \stdClass();
431
        $overridedata->assignid = $assign->get_instance()->id;
432
        $overridedata->userid = $user1->id;
433
        $overridedata->duedate = time();
434
        $DB->insert_record('assign_overrides', $overridedata);
435
        $overridedata->userid = $user2->id;
436
        $DB->insert_record('assign_overrides', $overridedata);
437
        assign_update_events($assign);
438
 
439
        $grade1 = '54.00';
440
        $teachercommenttext = 'Comment on user 1 attempt 1.';
441
        $data = new \stdClass();
442
        $data->attemptnumber = 0;
443
        $data->grade = $grade1;
444
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext, 'format' => FORMAT_MOODLE];
445
 
446
        // Give the submission a grade.
447
        $assign->save_grade($user1->id, $data);
448
 
449
        // Create and grade some submissions from the students.
450
        $submissiontext2 = 'My first submission for user 2';
451
        $submission = $this->create_submission($assign, $user2, $submissiontext2);
452
 
453
        $this->setUser($teacher);
454
 
455
        $grade2 = '56.00';
456
        $teachercommenttext2 = 'Comment on user 2 first attempt.';
457
        $data = new \stdClass();
458
        $data->attemptnumber = 0;
459
        $data->grade = $grade2;
460
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext2, 'format' => FORMAT_MOODLE];
461
 
462
        // Give the submission a grade.
463
        $assign->save_grade($user2->id, $data);
464
 
465
        // Create and grade some submissions from the students.
466
        $submissiontext3 = 'My second submission for user 2';
467
        $submission = $this->create_submission($assign, $user2, $submissiontext3, 1);
468
 
469
        $this->setUser($teacher);
470
 
471
        $grade3 = '83.00';
472
        $teachercommenttext3 = 'Comment on user 2 another attempt.';
473
        $data = new \stdClass();
474
        $data->attemptnumber = 1;
475
        $data->grade = $grade3;
476
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext3, 'format' => FORMAT_MOODLE];
477
 
478
        // Give the submission a grade.
479
        $assign->save_grade($user2->id, $data);
480
 
481
        // Delete all user data for this assignment.
482
        provider::delete_data_for_all_users_in_context($context);
483
 
484
        // Check all relevant tables.
485
        $records = $DB->get_records('assign_submission');
486
        $this->assertEmpty($records);
487
        $records = $DB->get_records('assign_grades');
488
        $this->assertEmpty($records);
489
        $records = $DB->get_records('assignsubmission_onlinetext');
490
        $this->assertEmpty($records);
491
        $records = $DB->get_records('assignfeedback_comments');
492
        $this->assertEmpty($records);
493
 
494
        // Check that overrides and the calendar events are deleted.
495
        $records = $DB->get_records('event');
496
        $this->assertEmpty($records);
497
        $records = $DB->get_records('assign_overrides');
498
        $this->assertEmpty($records);
499
    }
500
 
501
    /**
502
     * A test for deleting all user data for one user.
503
     */
11 efrain 504
    public function test_delete_data_for_user(): void {
1 efrain 505
        global $DB;
506
        $this->resetAfterTest();
507
        $course = $this->getDataGenerator()->create_course();
508
 
509
        $coursecontext = \context_course::instance($course->id);
510
 
511
        $user1 = $this->getDataGenerator()->create_user();
512
        $user2 = $this->getDataGenerator()->create_user();
513
        $teacher = $this->getDataGenerator()->create_user();
514
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
515
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
516
        $this->getDataGenerator()->enrol_user($teacher->id, $course->id, 'editingteacher');
517
        $assign = $this->create_instance([
518
                'course' => $course,
519
                'name' => 'Assign 1',
520
                'attemptreopenmethod' => ASSIGN_ATTEMPT_REOPEN_METHOD_MANUAL,
521
                'maxattempts' => 3,
522
                'assignsubmission_onlinetext_enabled' => true,
523
                'assignfeedback_comments_enabled' => true
524
            ]);
525
 
526
        $context = $assign->get_context();
527
 
528
        // Create and grade some submissions from the students.
529
        $submissiontext = 'My first submission';
530
        $submission1 = $this->create_submission($assign, $user1, $submissiontext);
531
 
532
        $this->setUser($teacher);
533
 
534
        // Overrides for both students.
535
        $overridedata = new \stdClass();
536
        $overridedata->assignid = $assign->get_instance()->id;
537
        $overridedata->userid = $user1->id;
538
        $overridedata->duedate = time();
539
        $DB->insert_record('assign_overrides', $overridedata);
540
        $overridedata->userid = $user2->id;
541
        $DB->insert_record('assign_overrides', $overridedata);
542
        assign_update_events($assign);
543
 
544
        $grade1 = '54.00';
545
        $teachercommenttext = 'Comment on user 1 attempt 1.';
546
        $data = new \stdClass();
547
        $data->attemptnumber = 0;
548
        $data->grade = $grade1;
549
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext, 'format' => FORMAT_MOODLE];
550
 
551
        // Give the submission a grade.
552
        $assign->save_grade($user1->id, $data);
553
 
554
        // Create and grade some submissions from the students.
555
        $submissiontext2 = 'My first submission for user 2';
556
        $submission2 = $this->create_submission($assign, $user2, $submissiontext2);
557
 
558
        $this->setUser($teacher);
559
 
560
        $grade2 = '56.00';
561
        $teachercommenttext2 = 'Comment on user 2 first attempt.';
562
        $data = new \stdClass();
563
        $data->attemptnumber = 0;
564
        $data->grade = $grade2;
565
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext2, 'format' => FORMAT_MOODLE];
566
 
567
        // Give the submission a grade.
568
        $assign->save_grade($user2->id, $data);
569
 
570
        // Create and grade some submissions from the students.
571
        $submissiontext3 = 'My second submission for user 2';
572
        $submission3 = $this->create_submission($assign, $user2, $submissiontext3, 1);
573
 
574
        $this->setUser($teacher);
575
 
576
        $grade3 = '83.00';
577
        $teachercommenttext3 = 'Comment on user 2 another attempt.';
578
        $data = new \stdClass();
579
        $data->attemptnumber = 1;
580
        $data->grade = $grade3;
581
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext3, 'format' => FORMAT_MOODLE];
582
 
583
        // Give the submission a grade.
584
        $assign->save_grade($user2->id, $data);
585
 
586
        // Delete user 2's data.
587
        $approvedlist = new approved_contextlist($user2, 'mod_assign', [$context->id, $coursecontext->id]);
588
        provider::delete_data_for_user($approvedlist);
589
 
590
        // Check all relevant tables.
591
        $records = $DB->get_records('assign_submission');
592
        foreach ($records as $record) {
593
            $this->assertEquals($user1->id, $record->userid);
594
            $this->assertNotEquals($user2->id, $record->userid);
595
        }
596
        $records = $DB->get_records('assign_grades');
597
        foreach ($records as $record) {
598
            $this->assertEquals($user1->id, $record->userid);
599
            $this->assertNotEquals($user2->id, $record->userid);
600
        }
601
        $records = $DB->get_records('assignsubmission_onlinetext');
602
        $this->assertCount(1, $records);
603
        $record = array_shift($records);
604
        // The only submission is for user 1.
605
        $this->assertEquals($submission1->id, $record->submission);
606
        $records = $DB->get_records('assignfeedback_comments');
607
        $this->assertCount(1, $records);
608
        $record = array_shift($records);
609
        // The only record is the feedback comment for user 1.
610
        $this->assertEquals($teachercommenttext, $record->commenttext);
611
 
612
        // Check calendar events as well as assign overrides.
613
        $records = $DB->get_records('event');
614
        $this->assertCount(1, $records);
615
        $record = array_shift($records);
616
        // The remaining event should be for user 1.
617
        $this->assertEquals($user1->id, $record->userid);
618
        // Now for assign_overrides
619
        $records = $DB->get_records('assign_overrides');
620
        $this->assertCount(1, $records);
621
        $record = array_shift($records);
622
        // The remaining event should be for user 1.
623
        $this->assertEquals($user1->id, $record->userid);
624
    }
625
 
626
    /**
627
     * A test for deleting all user data for a bunch of users.
628
     */
11 efrain 629
    public function test_delete_data_for_users(): void {
1 efrain 630
        global $DB;
631
 
632
        $this->resetAfterTest();
633
 
634
        $course = $this->getDataGenerator()->create_course();
635
 
636
        // Only made a comment on a submission.
637
        $user1 = $this->getDataGenerator()->create_user();
638
        // User 2 only has information about an activity override.
639
        $user2 = $this->getDataGenerator()->create_user();
640
        // User 3 made a submission.
641
        $user3 = $this->getDataGenerator()->create_user();
642
        // User 4 makes a submission and it is marked by the teacher.
643
        $user4 = $this->getDataGenerator()->create_user();
644
        // Grading and providing feedback as a teacher.
645
        $user5 = $this->getDataGenerator()->create_user();
646
        // This user has entries in assignment 2 and should not have their data deleted.
647
        $user6 = $this->getDataGenerator()->create_user();
648
 
649
        $this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
650
        $this->getDataGenerator()->enrol_user($user2->id, $course->id, 'student');
651
        $this->getDataGenerator()->enrol_user($user3->id, $course->id, 'student');
652
        $this->getDataGenerator()->enrol_user($user4->id, $course->id, 'student');
653
        $this->getDataGenerator()->enrol_user($user5->id, $course->id, 'editingteacher');
654
        $this->getDataGenerator()->enrol_user($user6->id, $course->id, 'student');
655
 
656
        $assign1 = $this->create_instance(['course' => $course,
657
                'assignsubmission_onlinetext_enabled' => true,
658
                'assignfeedback_comments_enabled' => true]);
659
        $assign2 = $this->create_instance(['course' => $course,
660
                'assignsubmission_onlinetext_enabled' => true,
661
                'assignfeedback_comments_enabled' => true]);
662
 
663
        $context = $assign1->get_context();
664
 
665
        // Jam an entry in the comments table for user 1.
666
        $comment = (object) [
667
            'contextid' => $context->id,
668
            'component' => 'assignsubmission_comments',
669
            'commentarea' => 'submission_comments',
670
            'itemid' => 5,
671
            'content' => 'A comment by user 1',
672
            'format' => 0,
673
            'userid' => $user1->id,
674
            'timecreated' => time()
675
        ];
676
        $DB->insert_record('comments', $comment);
677
 
678
        $this->setUser($user5); // Set the user to the teacher.
679
 
680
        $overridedata = new \stdClass();
681
        $overridedata->assignid = $assign1->get_instance()->id;
682
        $overridedata->userid = $user2->id;
683
        $overridedata->duedate = time();
684
        $overridedata->allowsubmissionsfromdate = time();
685
        $overridedata->cutoffdate = time();
686
        $DB->insert_record('assign_overrides', $overridedata);
687
 
688
        $submissiontext = 'My first submission';
689
        $submission = $this->create_submission($assign1, $user3, $submissiontext);
690
 
691
        $submissiontext = 'My first submission';
692
        $submission = $this->create_submission($assign1, $user4, $submissiontext);
693
 
694
        $submissiontext = 'My first submission';
695
        $submission = $this->create_submission($assign2, $user6, $submissiontext);
696
 
697
        $this->setUser($user5);
698
 
699
        $grade = '72.00';
700
        $teachercommenttext = 'This is better. Thanks.';
701
        $data = new \stdClass();
702
        $data->attemptnumber = 1;
703
        $data->grade = $grade;
704
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext, 'format' => FORMAT_MOODLE];
705
 
706
        // Give the submission a grade.
707
        $assign1->save_grade($user4->id, $data);
708
 
709
        $this->setUser($user5);
710
 
711
        $grade = '81.00';
712
        $teachercommenttext = 'This is nice.';
713
        $data = new \stdClass();
714
        $data->attemptnumber = 1;
715
        $data->grade = $grade;
716
        $data->assignfeedbackcomments_editor = ['text' => $teachercommenttext, 'format' => FORMAT_MOODLE];
717
 
718
        // Give the submission a grade.
719
        $assign2->save_grade($user6->id, $data);
720
 
721
        // Check data is in place.
722
        $data = $DB->get_records('assign_submission');
723
        // We should have one entry for user 3 and two entries each for user 4 and 6.
724
        $this->assertCount(5, $data);
725
        $usercounts = [
726
            $user3->id => 0,
727
            $user4->id => 0,
728
            $user6->id => 0
729
        ];
730
        foreach ($data as $datum) {
731
            $usercounts[$datum->userid]++;
732
        }
733
        $this->assertEquals(1, $usercounts[$user3->id]);
734
        $this->assertEquals(2, $usercounts[$user4->id]);
735
        $this->assertEquals(2, $usercounts[$user6->id]);
736
 
737
        $data = $DB->get_records('assign_grades');
738
        // Two entries in assign_grades, one for each grade given.
739
        $this->assertCount(2, $data);
740
 
741
        $data = $DB->get_records('assign_overrides');
742
        $this->assertCount(1, $data);
743
 
744
        $data = $DB->get_records('comments');
745
        $this->assertCount(1, $data);
746
 
747
        $userlist = new \core_privacy\local\request\approved_userlist($context, 'assign', [$user1->id, $user2->id]);
748
        provider::delete_data_for_users($userlist);
749
 
750
        $data = $DB->get_records('assign_overrides');
751
        $this->assertEmpty($data);
752
 
753
        $data = $DB->get_records('comments');
754
        $this->assertEmpty($data);
755
 
756
        $data = $DB->get_records('assign_submission');
757
        // No change here.
758
        $this->assertCount(5, $data);
759
 
760
        $userlist = new \core_privacy\local\request\approved_userlist($context, 'assign', [$user3->id, $user5->id]);
761
        provider::delete_data_for_users($userlist);
762
 
763
        $data = $DB->get_records('assign_submission');
764
        // Only the record for user 3 has been deleted.
765
        $this->assertCount(4, $data);
766
 
767
        $data = $DB->get_records('assign_grades');
768
        // Grades should be unchanged.
769
        $this->assertCount(2, $data);
770
    }
771
}