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 core\privacy;
18
 
19
use core_privacy\local\request\approved_contextlist;
20
use core_privacy\local\request\writer;
21
use core_privacy\tests\provider_testcase;
22
use core_privacy\local\request\approved_userlist;
23
use core\moodlenet\share_recorder;
24
 
25
/**
26
 * Privacy provider tests class.
27
 *
28
 * @package    core
29
 * @category   test
30
 * @copyright  2023 David Woloszyn <david.woloszyn@moodle.com>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1441 ariadna 32
 * @coversDefaultClass \core\privacy\provider
1 efrain 33
 */
1441 ariadna 34
final class provider_test extends provider_testcase {
1 efrain 35
 
36
    /**
37
     * Check that a user context is returned if there is any user data for this user.
38
     *
39
     * @covers ::get_contexts_for_userid
40
     */
11 efrain 41
    public function test_get_contexts_for_userid(): void {
1 efrain 42
        $this->resetAfterTest();
43
        $user = $this->getDataGenerator()->create_user();
44
 
45
        // Check that there are no contexts used for the user yet.
46
        $this->assertEmpty(provider::get_contexts_for_userid($user->id));
47
 
48
        // Insert a record.
49
        $this->insert_dummy_moodlenet_share_progress_record($user->id);
50
 
51
        // Check that we only get back one context.
52
        $contextlist = provider::get_contexts_for_userid($user->id);
53
        $this->assertCount(1, $contextlist);
54
 
55
        // Check that the context returned is the expected one.
56
        $usercontext = \context_user::instance($user->id);
57
        $this->assertEquals($usercontext->id, $contextlist->get_contextids()[0]);
58
    }
59
 
60
    /**
61
     * Test that only users within a user context are fetched.
62
     *
63
     * @covers ::get_users_in_context
64
     */
11 efrain 65
    public function test_get_users_in_context(): void {
1 efrain 66
        $this->resetAfterTest();
67
 
68
        // Create some users.
69
        $user1 = $this->getDataGenerator()->create_user();
70
        $user2 = $this->getDataGenerator()->create_user();
71
        $usercontext1 = \context_user::instance($user1->id);
72
        $usercontext2 = \context_user::instance($user2->id);
73
 
74
        // Get userlists and check they are empty for now.
75
        $userlist1 = new \core_privacy\local\request\userlist($usercontext1, 'core');
76
        provider::get_users_in_context($userlist1);
77
        $this->assertCount(0, $userlist1);
78
 
79
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, 'core');
80
        provider::get_users_in_context($userlist2);
81
        $this->assertCount(0, $userlist2);
82
 
83
        // Insert records for both users.
84
        $this->insert_dummy_moodlenet_share_progress_record($user1->id);
85
        $this->insert_dummy_moodlenet_share_progress_record($user2->id);
86
 
87
        // Check the userlists contain the correct users.
88
        $userlist1 = new \core_privacy\local\request\userlist($usercontext1, 'core');
89
        provider::get_users_in_context($userlist1);
90
        $this->assertCount(1, $userlist1);
91
        $this->assertEquals($user1->id, $userlist1->get_userids()[0]);
92
 
93
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, 'core');
94
        provider::get_users_in_context($userlist2);
95
        $this->assertCount(1, $userlist2);
96
        $this->assertEquals($user2->id, $userlist2->get_userids()[0]);
97
    }
98
 
99
    /**
100
     * Test that user data is exported correctly.
101
     *
102
     * @covers ::export_user_data
103
     */
11 efrain 104
    public function test_export_user_data(): void {
1 efrain 105
        global $DB;
106
        $this->resetAfterTest();
107
 
108
        // Create some users.
109
        $user1 = $this->getDataGenerator()->create_user();
110
        $user2 = $this->getDataGenerator()->create_user();
111
 
112
        // Insert a record for each user.
113
        $this->insert_dummy_moodlenet_share_progress_record($user1->id);
114
        $this->insert_dummy_moodlenet_share_progress_record($user2->id);
115
 
116
        $subcontexts = [
117
            get_string('privacy:metadata:moodlenet_share_progress', 'moodle')
118
        ];
119
 
120
        // Check if user1 has any exported data yet.
121
        $usercontext1 = \context_user::instance($user1->id);
122
        $writer = writer::with_context($usercontext1);
123
        $this->assertFalse($writer->has_any_data());
124
 
125
        // Export user1's data and check the count.
126
        $approvedlist = new approved_contextlist($user1, 'core', [$usercontext1->id]);
127
        provider::export_user_data($approvedlist);
128
        $data = (array)$writer->get_data($subcontexts);
129
        $this->assertCount(1, $data);
130
 
131
        // Get the inserted data.
132
        $userdata = $DB->get_record('moodlenet_share_progress', ['userid' => $user1->id]);
133
 
134
        // Check exported data against the inserted data.
135
        $this->assertEquals($userdata->id, reset($data)->id);
136
        $this->assertEquals($userdata->type, reset($data)->type);
137
        $this->assertEquals($userdata->courseid, reset($data)->courseid);
138
        $this->assertEquals($userdata->cmid, reset($data)->cmid);
139
        $this->assertEquals($userdata->userid, reset($data)->userid);
140
        $this->assertEquals($userdata->timecreated, reset($data)->timecreated);
141
        $this->assertEquals($userdata->resourceurl, reset($data)->resourceurl);
142
        $this->assertEquals($userdata->status, reset($data)->status);
143
    }
144
 
145
    /**
146
     * Test deleting all user data for a specific context.
147
     *
148
     * @covers ::delete_data_for_all_users_in_context
149
     */
11 efrain 150
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 151
        global $DB;
152
        $this->resetAfterTest();
153
 
154
        // Create some users.
155
        $user1 = $this->getDataGenerator()->create_user();
156
        $user2 = $this->getDataGenerator()->create_user();
157
 
158
        // Insert a record for each user.
159
        $this->insert_dummy_moodlenet_share_progress_record($user1->id);
160
        $this->insert_dummy_moodlenet_share_progress_record($user2->id);
161
 
162
        // Get all users' data.
163
        $usersdata = $DB->get_records('moodlenet_share_progress', []);
164
        $this->assertCount(2, $usersdata);
165
 
166
        // Delete everything for a user1 in context.
167
        $usercontext1 = \context_user::instance($user1->id);
168
        provider::delete_data_for_all_users_in_context($usercontext1);
169
 
170
        // Check what is remaining belongs to user2.
171
        $usersdata = $DB->get_records('moodlenet_share_progress', []);
172
        $this->assertCount(1, $usersdata);
173
        $this->assertEquals($user2->id, reset($usersdata)->userid);
174
    }
175
 
176
    /**
177
     * Test deleting a user's data for a specific context.
178
     *
179
     * @covers ::delete_data_for_user
180
     */
11 efrain 181
    public function test_delete_data_for_user(): void {
1 efrain 182
        global $DB;
183
        $this->resetAfterTest();
184
 
185
        // Create some users.
186
        $user1 = $this->getDataGenerator()->create_user();
187
        $user2 = $this->getDataGenerator()->create_user();
188
 
189
        // Insert a record for each user.
190
        $this->insert_dummy_moodlenet_share_progress_record($user1->id);
191
        $this->insert_dummy_moodlenet_share_progress_record($user2->id);
192
 
193
        // Get all users' data.
194
        $usersdata = $DB->get_records('moodlenet_share_progress', []);
195
        $this->assertCount(2, $usersdata);
196
 
197
        // Delete everything for user1.
198
        $usercontext1 = \context_user::instance($user1->id);
199
        $approvedlist = new approved_contextlist($user1, 'core', [$usercontext1->id]);
200
        provider::delete_data_for_user($approvedlist);
201
 
202
        // Check what is remaining belongs to user2.
203
        $usersdata = $DB->get_records('moodlenet_share_progress', []);
204
        $this->assertCount(1, $usersdata);
205
        $this->assertEquals($user2->id, reset($usersdata)->userid);
206
    }
207
 
208
    /**
209
     * Test that data for users in an approved userlist is deleted.
210
     *
211
     * @covers ::delete_data_for_users
212
     */
11 efrain 213
    public function test_delete_data_for_users(): void {
1 efrain 214
        global $DB;
215
        $this->resetAfterTest();
216
 
217
        // Create some users.
218
        $user1 = $this->getDataGenerator()->create_user();
219
        $user2 = $this->getDataGenerator()->create_user();
220
        $usercontext1 = \context_user::instance($user1->id);
221
        $usercontext2 = \context_user::instance($user2->id);
222
 
223
        // Insert a record for each user.
224
        $this->insert_dummy_moodlenet_share_progress_record($user1->id);
225
        $this->insert_dummy_moodlenet_share_progress_record($user2->id);
226
 
227
        // Check the count on all user's data.
228
        $usersdata = $DB->get_records('moodlenet_share_progress', []);
229
        $this->assertCount(2, $usersdata);
230
 
231
        // Attempt to delete data for user1 using user2's context (should have no effect).
232
        $approvedlist = new approved_userlist($usercontext2, 'core', [$user1->id]);
233
        provider::delete_data_for_users($approvedlist);
234
        $usersdata = $DB->get_records('moodlenet_share_progress', []);
235
        $this->assertCount(2, $usersdata);
236
 
237
        // Delete data for user1 using its correct context.
238
        $approvedlist = new approved_userlist($usercontext1, 'core', [$user1->id]);
239
        provider::delete_data_for_users($approvedlist);
240
 
241
        // Check what is remaining belongs to user2.
242
        $usersdata = $DB->get_records('moodlenet_share_progress', []);
243
        $this->assertCount(1, $usersdata);
244
        $this->assertEquals($user2->id, reset($usersdata)->userid);
245
    }
246
 
247
    /**
248
     * Helper function to insert a MoodleNet share progress record for use in the tests.
249
     *
250
     * @param int $userid The ID of the user to link the record to.
251
     */
252
    protected function insert_dummy_moodlenet_share_progress_record(int $userid): void {
253
        $sharetype = share_recorder::TYPE_ACTIVITY;
254
        $courseid = 123;
255
        $cmid = 456;
256
        share_recorder::insert_share_progress($sharetype, $userid, $courseid, $cmid);
257
    }
258
}