Proyectos de Subversion Moodle

Rev

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