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
 * Unit tests for the core_repository implementation of the privacy API.
18
 *
19
 * @package    core_repository
20
 * @category   test
21
 * @copyright  2018 Zig Tan <zig@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace core_repository\privacy;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
use core_privacy\local\metadata\collection;
29
use core_privacy\local\request\writer;
30
use core_privacy\local\request\approved_contextlist;
31
use core_repository\privacy\provider;
32
use core_privacy\local\request\approved_userlist;
33
 
34
/**
35
 * Unit tests for the core_repository implementation of the privacy API.
36
 *
37
 * @copyright  2018 Zig Tan <zig@moodle.com>
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
1441 ariadna 40
final class provider_test extends \core_privacy\tests\provider_testcase {
1 efrain 41
 
42
    /**
43
     * Overriding setUp() function to always reset after tests.
44
     */
45
    public function setUp(): void {
1441 ariadna 46
        parent::setUp();
1 efrain 47
        $this->resetAfterTest(true);
48
    }
49
 
50
    /**
51
     * Test for provider::get_contexts_for_userid().
52
     */
11 efrain 53
    public function test_get_contexts_for_userid(): void {
1 efrain 54
        // Test setup.
55
        $user = $this->getDataGenerator()->create_user();
56
        $this->setUser($user);
57
 
58
        // Test the User's retrieved contextlist is empty because no repository_instances have added for the User yet.
59
        $contextlist = provider::get_contexts_for_userid($user->id);
60
        $contexts = $contextlist->get_contexts();
61
        $this->assertCount(0, $contexts);
62
 
63
        // Create 3 repository_instances records for the User.
64
        $this->setup_test_scenario_data($user->id, 3);
65
 
66
        // Test the User's retrieved contextlist contains only one context.
67
        $contextlist = provider::get_contexts_for_userid($user->id);
68
        $contexts = $contextlist->get_contexts();
69
        $this->assertCount(1, $contexts);
70
 
71
        // Test the User's contexts equal the User's own context.
72
        $context = reset($contexts);
73
        $this->assertEquals(CONTEXT_USER, $context->contextlevel);
74
        $this->assertEquals($user->id, $context->instanceid);
75
    }
76
 
77
    /**
78
     * Test for provider::export_user_data().
79
     */
11 efrain 80
    public function test_export_user_data(): void {
1 efrain 81
        // Test setup.
82
        $user = $this->getDataGenerator()->create_user();
83
        $this->setUser($user);
84
 
85
        // Create 3 repository_instances records for the User.
86
        $this->setup_test_scenario_data($user->id, 3);
87
 
88
        // Test the User's retrieved contextlist contains only one context.
89
        $contextlist = provider::get_contexts_for_userid($user->id);
90
        $contexts = $contextlist->get_contexts();
91
        $this->assertCount(1, $contexts);
92
 
93
        // Test the User's contexts equal the User's own context.
94
        $context = reset($contexts);
95
        $this->assertEquals(CONTEXT_USER, $context->contextlevel);
96
        $this->assertEquals($user->id, $context->instanceid);
97
 
98
        // Retrieve repository_instances data only for this user.
99
        $approvedcontextlist = new approved_contextlist($user, 'core_repository', $contextlist->get_contextids());
100
        provider::export_user_data($approvedcontextlist);
101
 
102
        // Test the repository_instances data is exported at the User context level.
103
        $user = $approvedcontextlist->get_user();
104
        $contextuser = \context_user::instance($user->id);
105
        $writer = writer::with_context($contextuser);
106
        $this->assertTrue($writer->has_any_data());
107
    }
108
 
109
    /**
110
     * Test for provider::delete_data_for_all_users_in_context().
111
     */
11 efrain 112
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 113
        global $DB;
114
 
115
        // Test setup.
116
        $user = $this->getDataGenerator()->create_user();
117
        $this->setUser($user);
118
 
119
        // Create 3 repository_instances records for the User.
120
        $this->setup_test_scenario_data($user->id, 3);
121
 
122
        // Test the User's retrieved contextlist contains only one context.
123
        $contextlist = provider::get_contexts_for_userid($user->id);
124
        $contexts = $contextlist->get_contexts();
125
        $this->assertCount(1, $contexts);
126
 
127
        // Test the User's contexts equal the User's own context.
128
        $context = reset($contexts);
129
        $this->assertEquals(CONTEXT_USER, $context->contextlevel);
130
 
131
        // Delete all the User's records in mdl_repository_instances table by the specified User context.
132
        provider::delete_data_for_all_users_in_context($context);
133
 
134
        // Test the cohort roles records in mdl_repository_instances table is equals zero.
135
        $repositoryinstances = $DB->get_records('repository_instances', ['userid' => $user->id]);
136
        $this->assertCount(0, $repositoryinstances);
137
    }
138
 
139
    /**
140
     * Test for provider::delete_data_for_user().
141
     */
11 efrain 142
    public function test_delete_data_for_user(): void {
1 efrain 143
        global $DB;
144
 
145
        // Test setup.
146
        $user = $this->getDataGenerator()->create_user();
147
        $this->setUser($user);
148
 
149
        // Create 3 repository_instances records for the User.
150
        $this->setup_test_scenario_data($user->id, 3);
151
 
152
        // Test the User's retrieved contextlist contains only one context.
153
        $contextlist = provider::get_contexts_for_userid($user->id);
154
        $contexts = $contextlist->get_contexts();
155
        $this->assertCount(1, $contexts);
156
 
157
        // Test the User's contexts equal the User's own context.
158
        $context = reset($contexts);
159
        $this->assertEquals(CONTEXT_USER, $context->contextlevel);
160
 
161
        // Delete all the User's records in mdl_repository_instances table by the specified User approved context list.
162
        $approvedcontextlist = new approved_contextlist($user, 'repository_instances', $contextlist->get_contextids());
163
        provider::delete_data_for_user($approvedcontextlist);
164
 
165
        // Test the cohort roles records in mdl_repository_instances table is equals zero.
166
        $repositoryinstances = $DB->get_records('repository_instances', ['userid' => $user->id]);
167
        $this->assertCount(0, $repositoryinstances);
168
    }
169
 
170
    /**
171
     * Test that only users with a user context are fetched.
172
     */
11 efrain 173
    public function test_get_users_in_context(): void {
1 efrain 174
        $this->resetAfterTest();
175
 
176
        $component = 'core_repository';
177
        // Create a user.
178
        $user = $this->getDataGenerator()->create_user();
179
        $usercontext = \context_user::instance($user->id);
180
        $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
181
        // The list of users should not return anything yet (related data still haven't been created).
182
        provider::get_users_in_context($userlist);
183
        $this->assertCount(0, $userlist);
184
 
185
        // Create 3 repository_instances records for user.
186
        $this->setup_test_scenario_data($user->id, 3);
187
 
188
        // The list of users for user context should return the user.
189
        provider::get_users_in_context($userlist);
190
        $this->assertCount(1, $userlist);
191
        $expected = [$user->id];
192
        $actual = $userlist->get_userids();
193
        $this->assertEquals($expected, $actual);
194
 
195
        // The list of users for system context should not return any users.
196
        $systemcontext = \context_system::instance();
197
        $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
198
        provider::get_users_in_context($userlist);
199
        $this->assertCount(0, $userlist);
200
    }
201
 
202
    /**
203
     * Test that data for users in approved userlist is deleted.
204
     */
11 efrain 205
    public function test_delete_data_for_users(): void {
1 efrain 206
        $this->resetAfterTest();
207
 
208
        $component = 'core_repository';
209
        // Create user1.
210
        $user1 = $this->getDataGenerator()->create_user();
211
        $this->setUser($user1);
212
        $usercontext1 = \context_user::instance($user1->id);
213
        // Create list of users with a related user data in usercontext1.
214
        $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
215
 
216
        // Create a user2.
217
        $user2 = $this->getDataGenerator()->create_user();
218
        $this->setUser($user2);
219
        $usercontext2 = \context_user::instance($user2->id);
220
        // Create list of users with a related user data in usercontext2.
221
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
222
 
223
        // Create repository_instances record for user1.
224
        $this->setup_test_scenario_data($user1->id, 1);
225
        // Create repository_instances record for user2.
226
        $this->setup_test_scenario_data($user2->id, 1);
227
 
228
        // Ensure the user list for usercontext1 contains user1.
229
        provider::get_users_in_context($userlist1);
230
        $this->assertCount(1, $userlist1);
231
        // Ensure the user list for usercontext2 contains user2.
232
        provider::get_users_in_context($userlist2);
233
        $this->assertCount(1, $userlist2);
234
 
235
        // Convert $userlist1 into an approved_contextlist.
236
        $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
237
 
238
        // Delete using delete_data_for_user.
239
        provider::delete_data_for_users($approvedlist);
240
 
241
        // Re-fetch users in the usercontext1 - The user list should now be empty.
242
        $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
243
        provider::get_users_in_context($userlist1);
244
        $this->assertCount(0, $userlist1);
245
        // Re-fetch users in the usercontext2 - The user list should not be empty.
246
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
247
        provider::get_users_in_context($userlist2);
248
        $this->assertCount(1, $userlist2);
249
 
250
        // User data should be only removed in the user context.
251
        $systemcontext = \context_system::instance();
252
        // Add userlist2 to the approved user list in the system context.
253
        $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
254
        // Delete user1 data using delete_data_for_user.
255
        provider::delete_data_for_users($approvedlist);
256
        // Re-fetch users in usercontext2 - The user list should not be empty (user2).
257
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
258
        provider::get_users_in_context($userlist2);
259
        $this->assertCount(1, $userlist2);
260
    }
261
 
262
    /**
263
     * Helper function to setup repository_instances records for testing a specific user.
264
     *
265
     * @param int $userid       The Id of the User used for testing.
266
     * @param int $noscenarios  The number of repository_instance records to create for the User.
267
     */
268
    private function setup_test_scenario_data($userid, $noscenarios) {
269
        global $DB;
270
 
271
        for ($i = 0; $i < $noscenarios; $i++) {
272
            $repositoryinstance = (object)[
273
                'typeid' => ($i + 1),
274
                'name' => 'My Test Repo',
275
                'userid' => $userid,
276
                'contextid' => 1,
277
                'username' => 'some username',
278
                'password' => 'some password',
279
                'timecreated' => date('u'),
280
                'timemodified' => date('u'),
281
                'readonly' => 0
282
            ];
283
            $DB->insert_record('repository_instances', $repositoryinstance);
284
        }
285
    }
286
 
287
}