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 tool_courserating\privacy;
|
|
|
18 |
|
|
|
19 |
use core_privacy\local\request\writer;
|
|
|
20 |
use core_privacy\local\request\approved_contextlist;
|
|
|
21 |
use tool_courserating\api;
|
|
|
22 |
use core_privacy\local\request\approved_userlist;
|
|
|
23 |
use tool_courserating\local\models\flag;
|
|
|
24 |
use tool_courserating\local\models\rating;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
* Tests for privacy provider class
|
|
|
28 |
*
|
|
|
29 |
* @package tool_courserating
|
|
|
30 |
* @covers \tool_courserating\privacy\provider
|
|
|
31 |
* @copyright 2022 Marina Glancy <marina.glancy@gmail.com>
|
|
|
32 |
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
33 |
*/
|
|
|
34 |
final class provider_test extends \core_privacy\tests\provider_testcase {
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Overriding setUp() function to always reset after tests.
|
|
|
38 |
*/
|
|
|
39 |
public function setUp(): void {
|
|
|
40 |
$this->resetAfterTest(true);
|
|
|
41 |
set_config(\tool_courserating\constants::SETTING_RATINGMODE,
|
|
|
42 |
\tool_courserating\constants::RATEBY_ANYTIME, 'tool_courserating');
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Test for provider::get_contexts_for_userid().
|
|
|
47 |
*/
|
|
|
48 |
public function test_get_contexts_for_userid(): void {
|
|
|
49 |
global $DB;
|
|
|
50 |
|
|
|
51 |
$user = $this->getDataGenerator()->create_user();
|
|
|
52 |
$course = $this->getDataGenerator()->create_course();
|
|
|
53 |
$this->setUser($user);
|
|
|
54 |
\tool_courserating\api::set_rating($course->id, (object)['rating' => 5]);
|
|
|
55 |
|
|
|
56 |
$this->setAdminUser();
|
|
|
57 |
|
|
|
58 |
$contextlist = provider::get_contexts_for_userid($user->id);
|
|
|
59 |
$contexts = $contextlist->get_contexts();
|
|
|
60 |
$this->assertCount(1, $contexts);
|
|
|
61 |
|
|
|
62 |
$courseids = array_column($contexts, 'instanceid');
|
|
|
63 |
$this->assertEqualsCanonicalizing([$course->id], $courseids);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/**
|
|
|
67 |
* Test for provider::export_user_data().
|
|
|
68 |
*/
|
|
|
69 |
public function test_export_user_data(): void {
|
|
|
70 |
|
|
|
71 |
[$user, $course] = $this->setup_test_scenario_data();
|
|
|
72 |
$coursectx = \context_course::instance($course->id);
|
|
|
73 |
$this->setAdminUser();
|
|
|
74 |
|
|
|
75 |
// Test the User's retrieved contextlist contains two contexts.
|
|
|
76 |
$contextlist = provider::get_contexts_for_userid($user->id);
|
|
|
77 |
$contexts = $contextlist->get_contexts();
|
|
|
78 |
$this->assertCount(1, $contexts);
|
|
|
79 |
|
|
|
80 |
// Add a system, course category and course context to the approved context list.
|
|
|
81 |
$systemctx = \context_system::instance();
|
|
|
82 |
$approvedcontextids = [
|
|
|
83 |
$systemctx->id,
|
|
|
84 |
$coursectx->id,
|
|
|
85 |
];
|
|
|
86 |
|
|
|
87 |
// Retrieve the User's tool_cohortroles data.
|
|
|
88 |
$approvedcontextlist = new approved_contextlist($user, 'tool_courserating', $approvedcontextids);
|
|
|
89 |
provider::export_user_data($approvedcontextlist);
|
|
|
90 |
|
|
|
91 |
// Test the tool_cohortroles data is exported at the system context level.
|
|
|
92 |
$writer = writer::with_context($systemctx);
|
|
|
93 |
$this->assertFalse($writer->has_any_data());
|
|
|
94 |
// Test the tool_cohortroles data is not exported at the course context level.
|
|
|
95 |
$writer = writer::with_context($coursectx);
|
|
|
96 |
$this->assertTrue($writer->has_any_data());
|
|
|
97 |
$this->assertNotEmpty($writer->get_data(['Course ratings', $course->shortname]));
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* Set up scenario data
|
|
|
102 |
*
|
|
|
103 |
* @return array
|
|
|
104 |
*/
|
|
|
105 |
protected function setup_test_scenario_data() {
|
|
|
106 |
$user1 = $this->getDataGenerator()->create_user();
|
|
|
107 |
$user2 = $this->getDataGenerator()->create_user();
|
|
|
108 |
$course = $this->getDataGenerator()->create_course(['shortname' => 'c1']);
|
|
|
109 |
$this->setUser($user2);
|
|
|
110 |
$r = \tool_courserating\api::set_rating($course->id, (object)['rating' => 5]);
|
|
|
111 |
$this->setUser($user1);
|
|
|
112 |
\tool_courserating\api::set_rating($course->id, (object)['rating' => 4]);
|
|
|
113 |
api::flag_review($r->get('id'));
|
|
|
114 |
return [$user1, $course];
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
/**
|
|
|
118 |
* Test for provider::delete_data_for_all_users_in_context().
|
|
|
119 |
*/
|
|
|
120 |
public function test_delete_data_for_all_users_in_context(): void {
|
|
|
121 |
global $DB;
|
|
|
122 |
|
|
|
123 |
[$user, $course] = $this->setup_test_scenario_data();
|
|
|
124 |
$coursectx = \context_course::instance($course->id);
|
|
|
125 |
$this->setAdminUser();
|
|
|
126 |
|
|
|
127 |
provider::delete_data_for_all_users_in_context($coursectx);
|
|
|
128 |
$this->assertEmpty($DB->get_records(rating::TABLE));
|
|
|
129 |
$this->assertEmpty($DB->get_records(flag::TABLE));
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Test for provider::delete_data_for_user().
|
|
|
134 |
*/
|
|
|
135 |
public function test_delete_data_for_user(): void {
|
|
|
136 |
global $DB;
|
|
|
137 |
|
|
|
138 |
[$user, $course] = $this->setup_test_scenario_data();
|
|
|
139 |
$coursectx = \context_course::instance($course->id);
|
|
|
140 |
$this->setAdminUser();
|
|
|
141 |
|
|
|
142 |
// Test the User's retrieved contextlist contains two contexts.
|
|
|
143 |
$contextlist = provider::get_contexts_for_userid($user->id);
|
|
|
144 |
$contexts = $contextlist->get_contexts();
|
|
|
145 |
$this->assertCount(1, $contexts);
|
|
|
146 |
|
|
|
147 |
$approvedcontextlist = new approved_contextlist($user, 'tool_courserating', [$coursectx->id]);
|
|
|
148 |
provider::delete_data_for_user($approvedcontextlist);
|
|
|
149 |
}
|
|
|
150 |
|
|
|
151 |
/**
|
|
|
152 |
* Test that only users within a course context are fetched.
|
|
|
153 |
*/
|
|
|
154 |
public function test_get_users_in_context(): void {
|
|
|
155 |
$component = 'tool_courserating';
|
|
|
156 |
|
|
|
157 |
[$user, $course] = $this->setup_test_scenario_data();
|
|
|
158 |
$coursectx = \context_course::instance($course->id);
|
|
|
159 |
$this->setAdminUser();
|
|
|
160 |
|
|
|
161 |
$userlist = new \core_privacy\local\request\userlist($coursectx, $component);
|
|
|
162 |
provider::get_users_in_context($userlist);
|
|
|
163 |
$this->assertCount(2, $userlist);
|
|
|
164 |
$this->assertTrue(in_array($user->id, $userlist->get_userids()));
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* Test that data for users in approved userlist is deleted.
|
|
|
169 |
*/
|
|
|
170 |
public function test_delete_data_for_users(): void {
|
|
|
171 |
$component = 'tool_courserating';
|
|
|
172 |
|
|
|
173 |
[$user, $course] = $this->setup_test_scenario_data();
|
|
|
174 |
$coursectx = \context_course::instance($course->id);
|
|
|
175 |
$this->setAdminUser();
|
|
|
176 |
|
|
|
177 |
$userlist1 = new \core_privacy\local\request\userlist($coursectx, $component);
|
|
|
178 |
provider::get_users_in_context($userlist1);
|
|
|
179 |
$this->assertCount(2, $userlist1);
|
|
|
180 |
$this->assertTrue(in_array($user->id, $userlist1->get_userids()));
|
|
|
181 |
$userids = $userlist1->get_userids();
|
|
|
182 |
|
|
|
183 |
$approvedlist1 = new approved_userlist($coursectx, $component, $userids);
|
|
|
184 |
provider::delete_data_for_users($approvedlist1);
|
|
|
185 |
|
|
|
186 |
$userlist1 = new \core_privacy\local\request\userlist($coursectx, $component);
|
|
|
187 |
provider::get_users_in_context($userlist1);
|
|
|
188 |
$this->assertCount(0, $userlist1);
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
}
|