mirrored from git://git.moodle.org/moodle.git
/
privacy_test.php
178 lines (162 loc) · 8.41 KB
/
privacy_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy tests for core_course.
*
* @package core_course
* @category test
* @copyright 2018 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/completion/tests/fixtures/completion_creation.php');
/**
* Unit tests for course/classes/privacy/policy
*
* @copyright 2018 Adrian Greeve <adrian@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_course_privacy_testcase extends \core_privacy\tests\provider_testcase {
use completion_creation;
/**
* Test getting the appropriate context for the userid. This should only ever
* return the user context for the user id supplied.
*/
public function test_get_contexts_for_userid() {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->create_course_completion();
$this->complete_course($user);
$contextlist = \core_course\privacy\provider::get_contexts_for_userid($user->id);
$this->assertEquals($this->coursecontext->id, $contextlist->current()->id);
}
/**
* Test that user data is exported.
*/
public function test_export_user_data() {
$this->resetAfterTest();
$user = $this->getDataGenerator()->create_user();
$this->create_course_completion();
$this->complete_course($user);
$approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'core_course',
[$this->coursecontext->id]);
$writer = \core_privacy\local\request\writer::with_context($this->coursecontext);
\core_course\privacy\provider::export_user_data($approvedlist);
$completiondata = $writer->get_data([get_string('privacy:completionpath', 'course')]);
$this->assertEquals('In progress', $completiondata->status);
$this->assertCount(2, $completiondata->criteria);
}
/**
* Verify that if a module context is included in the contextlist_collection and its parent course is not, the
* export_context_data() call picks this up, and that the contextual course information is included.
*/
public function test_export_context_data_module_context_only() {
$this->resetAfterTest();
// Create a course and a single module.
$course1 = $this->getDataGenerator()->create_course(['fullname' => 'Course 1', 'shortname' => 'C1']);
$context1 = context_course::instance($course1->id);
$modassign = $this->getDataGenerator()->create_module('assign', ['course' => $course1->id, 'name' => 'assign test 1']);
$assigncontext = context_module::instance($modassign->cmid);
// Now, let's assume during user info export, only the coursemodule context is returned in the contextlist_collection.
$user = $this->getDataGenerator()->create_user();
$collection = new \core_privacy\local\request\contextlist_collection($user->id);
$approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'mod_assign', [$assigncontext->id]);
$collection->add_contextlist($approvedlist);
// Now, verify that core_course will detect this, and add relevant contextual information.
\core_course\privacy\provider::export_context_data($collection);
$writer = \core_privacy\local\request\writer::with_context($context1);
$this->assertTrue($writer->has_any_data());
$writerdata = $writer->get_data();
$this->assertObjectHasAttribute('fullname', $writerdata);
$this->assertObjectHasAttribute('shortname', $writerdata);
$this->assertObjectHasAttribute('idnumber', $writerdata);
$this->assertObjectHasAttribute('summary', $writerdata);
}
/**
* Verify that if a module context and its parent course context are both included in the contextlist_collection, that course
* contextual information is present in the export.
*/
public function test_export_context_data_course_and_module_contexts() {
$this->resetAfterTest();
// Create a course and a single module.
$course1 = $this->getDataGenerator()->create_course(['fullname' => 'Course 1', 'shortname' => 'C1']);
$context1 = context_course::instance($course1->id);
$modassign = $this->getDataGenerator()->create_module('assign', ['course' => $course1->id, 'name' => 'assign test 1']);
$assigncontext = context_module::instance($modassign->cmid);
// Now, assume during user info export, that both module and course contexts are returned in the contextlist_collection.
$user = $this->getDataGenerator()->create_user();
$collection = new \core_privacy\local\request\contextlist_collection($user->id);
$approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'mod_assign', [$assigncontext->id]);
$approvedlist2 = new \core_privacy\local\request\approved_contextlist($user, 'core_course', [$context1->id]);
$collection->add_contextlist($approvedlist);
$collection->add_contextlist($approvedlist2);
// Now, verify that core_course still adds relevant contextual information, even for courses which are explicitly listed in
// the contextlist_collection.
\core_course\privacy\provider::export_context_data($collection);
$writer = \core_privacy\local\request\writer::with_context($context1);
$this->assertTrue($writer->has_any_data());
$writerdata = $writer->get_data();
$this->assertObjectHasAttribute('fullname', $writerdata);
$this->assertObjectHasAttribute('shortname', $writerdata);
$this->assertObjectHasAttribute('idnumber', $writerdata);
$this->assertObjectHasAttribute('summary', $writerdata);
}
/**
* Test deleting all user data for one context.
*/
public function test_delete_data_for_all_users_in_context() {
global $DB;
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$this->create_course_completion();
$this->complete_course($user1);
$this->complete_course($user2);
$records = $DB->get_records('course_modules_completion');
$this->assertCount(2, $records);
$records = $DB->get_records('course_completion_crit_compl');
$this->assertCount(2, $records);
\core_course\privacy\provider::delete_data_for_all_users_in_context($this->coursecontext);
$records = $DB->get_records('course_modules_completion');
$this->assertCount(0, $records);
$records = $DB->get_records('course_completion_crit_compl');
$this->assertCount(0, $records);
}
/**
* Test deleting data for only one user.
*/
public function test_delete_data_for_user() {
global $DB;
$this->resetAfterTest();
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$this->create_course_completion();
$this->complete_course($user1);
$this->complete_course($user2);
$records = $DB->get_records('course_modules_completion');
$this->assertCount(2, $records);
$records = $DB->get_records('course_completion_crit_compl');
$this->assertCount(2, $records);
$approvedlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_course',
[$this->coursecontext->id]);
\core_course\privacy\provider::delete_data_for_user($approvedlist);
$records = $DB->get_records('course_modules_completion');
$this->assertCount(1, $records);
$records = $DB->get_records('course_completion_crit_compl');
$this->assertCount(1, $records);
}
}