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
/**
18
 * Base class for unit tests for profilefield_text.
19
 *
20
 * @package    profilefield_text
21
 * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace profilefield_text\privacy;
25
 
26
use core_privacy\tests\provider_testcase;
27
use profilefield_text\privacy\provider;
28
use core_privacy\local\request\approved_userlist;
29
 
30
/**
31
 * Unit tests for user\profile\field\text\classes\privacy\provider.php
32
 *
33
 * @copyright  2018 Mihail Geshoski <mihail@moodle.com>
34
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
1441 ariadna 36
final class provider_test extends provider_testcase {
1 efrain 37
 
38
    /**
39
     * Basic setup for these tests.
40
     */
41
    public function setUp(): void {
1441 ariadna 42
        parent::setUp();
1 efrain 43
        $this->resetAfterTest(true);
44
    }
45
 
46
    /**
47
     * Test getting the context for the user ID related to this plugin.
48
     */
11 efrain 49
    public function test_get_contexts_for_userid(): void {
1 efrain 50
        global $DB;
51
        // Create profile category.
52
        $categoryid = $this->add_profile_category();
53
        // Create profile field.
54
        $profilefieldid = $this->add_profile_field($categoryid, 'text');
55
        // Create a user.
56
        $user = $this->getDataGenerator()->create_user();
57
        $this->add_user_info_data($user->id, $profilefieldid, 'test data');
58
        // Get the field that was created.
59
        $userfielddata = $DB->get_records('user_info_data', array('userid' => $user->id));
60
        // Confirm we got the right number of user field data.
61
        $this->assertCount(1, $userfielddata);
62
        $context = \context_user::instance($user->id);
63
        $contextlist = provider::get_contexts_for_userid($user->id);
64
        $this->assertEquals($context, $contextlist->current());
65
    }
66
 
67
    /**
68
     * Test that data is exported correctly for this plugin.
69
     */
11 efrain 70
    public function test_export_user_data(): void {
1 efrain 71
        // Create profile category.
72
        $categoryid = $this->add_profile_category();
73
        // Create text profile field.
74
        $textprofilefieldid = $this->add_profile_field($categoryid, 'text');
75
        // Create checkbox profile field.
76
        $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
77
        // Create a user.
78
        $user = $this->getDataGenerator()->create_user();
79
        $context = \context_user::instance($user->id);
80
        // Add text user info data.
81
        $this->add_user_info_data($user->id, $textprofilefieldid, 'test text');
82
        // Add checkbox user info data.
83
        $this->add_user_info_data($user->id, $checkboxprofilefieldid, 'test data');
84
        $writer = \core_privacy\local\request\writer::with_context($context);
85
        $this->assertFalse($writer->has_any_data());
86
        $this->export_context_data_for_user($user->id, $context, 'profilefield_text');
87
        $data = $writer->get_data([get_string('pluginname', 'profilefield_text')]);
88
        $this->assertCount(3, (array) $data);
89
        $this->assertEquals('Test field', $data->name);
90
        $this->assertEquals('This is a test.', $data->description);
91
        $this->assertEquals('test text', $data->data);
92
    }
93
 
94
    /**
95
     * Test that user data is deleted using the context.
96
     */
11 efrain 97
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 98
        global $DB;
99
        // Create profile category.
100
        $categoryid = $this->add_profile_category();
101
        // Create text profile field.
102
        $textprofilefieldid = $this->add_profile_field($categoryid, 'text');
103
        // Create checkbox profile field.
104
        $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
105
        // Create a user.
106
        $user = $this->getDataGenerator()->create_user();
107
        $context = \context_user::instance($user->id);
108
        // Add text user info data.
109
        $this->add_user_info_data($user->id, $textprofilefieldid, 'test text');
110
        // Add checkbox user info data.
111
        $this->add_user_info_data($user->id, $checkboxprofilefieldid, 'test data');
112
        // Check that we have two entries.
113
        $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
114
        $this->assertCount(2, $userinfodata);
115
        provider::delete_data_for_all_users_in_context($context);
116
        // Check that the correct profile field has been deleted.
117
        $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
118
        $this->assertCount(1, $userinfodata);
119
        $this->assertNotEquals('test text', reset($userinfodata)->data);
120
    }
121
 
122
    /**
123
     * Test that user data is deleted for this user.
124
     */
11 efrain 125
    public function test_delete_data_for_user(): void {
1 efrain 126
        global $DB;
127
        // Create profile category.
128
        $categoryid = $this->add_profile_category();
129
        // Create text profile field.
130
        $textprofilefieldid = $this->add_profile_field($categoryid, 'text');
131
        // Create checkbox profile field.
132
        $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
133
        // Create a user.
134
        $user = $this->getDataGenerator()->create_user();
135
        $context = \context_user::instance($user->id);
136
        // Add text user info data.
137
        $this->add_user_info_data($user->id, $textprofilefieldid, 'test text');
138
        // Add checkbox user info data.
139
        $this->add_user_info_data($user->id, $checkboxprofilefieldid, 'test data');
140
        // Check that we have two entries.
141
        $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
142
        $this->assertCount(2, $userinfodata);
143
        $approvedlist = new \core_privacy\local\request\approved_contextlist($user, 'profilefield_text',
144
            [$context->id]);
145
        provider::delete_data_for_user($approvedlist);
146
        // Check that the correct profile field has been deleted.
147
        $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id]);
148
        $this->assertCount(1, $userinfodata);
149
        $this->assertNotEquals('test text', reset($userinfodata)->data);
150
    }
151
 
152
    /**
153
     * Test that only users with a user context are fetched.
154
     */
11 efrain 155
    public function test_get_users_in_context(): void {
1 efrain 156
        $this->resetAfterTest();
157
 
158
        $component = 'profilefield_text';
159
        // Create profile category.
160
        $categoryid = $this->add_profile_category();
161
        // Create text profile field.
162
        $profilefieldid = $this->add_profile_field($categoryid, 'text');
163
 
164
        // Create a user.
165
        $user = $this->getDataGenerator()->create_user();
166
        $usercontext = \context_user::instance($user->id);
167
        // The list of users should not return anything yet (related data still haven't been created).
168
        $userlist = new \core_privacy\local\request\userlist($usercontext, $component);
169
        provider::get_users_in_context($userlist);
170
        $this->assertCount(0, $userlist);
171
 
172
        $this->add_user_info_data($user->id, $profilefieldid, 'test data');
173
 
174
        // The list of users for user context should return the user.
175
        provider::get_users_in_context($userlist);
176
        $this->assertCount(1, $userlist);
177
        $expected = [$user->id];
178
        $actual = $userlist->get_userids();
179
        $this->assertEquals($expected, $actual);
180
 
181
        // The list of users for system context should not return any users.
182
        $systemcontext = \context_system::instance();
183
        $userlist = new \core_privacy\local\request\userlist($systemcontext, $component);
184
        provider::get_users_in_context($userlist);
185
        $this->assertCount(0, $userlist);
186
    }
187
 
188
    /**
189
     * Test that data for users in approved userlist is deleted.
190
     */
11 efrain 191
    public function test_delete_data_for_users(): void {
1 efrain 192
        $this->resetAfterTest();
193
 
194
        $component = 'profilefield_text';
195
        // Create profile category.
196
        $categoryid = $this->add_profile_category();
197
        // Create text profile field.
198
        $profilefieldid = $this->add_profile_field($categoryid, 'text');
199
 
200
        // Create user1.
201
        $user1 = $this->getDataGenerator()->create_user();
202
        $usercontext1 = \context_user::instance($user1->id);
203
        // Create user2.
204
        $user2 = $this->getDataGenerator()->create_user();
205
        $usercontext2 = \context_user::instance($user2->id);
206
 
207
        $this->add_user_info_data($user1->id, $profilefieldid, 'test data');
208
        $this->add_user_info_data($user2->id, $profilefieldid, 'test data');
209
 
210
        // The list of users for usercontext1 should return user1.
211
        $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
212
        provider::get_users_in_context($userlist1);
213
        $this->assertCount(1, $userlist1);
214
        $expected = [$user1->id];
215
        $actual = $userlist1->get_userids();
216
        $this->assertEquals($expected, $actual);
217
 
218
        // The list of users for usercontext2 should return user2.
219
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
220
        provider::get_users_in_context($userlist2);
221
        $this->assertCount(1, $userlist2);
222
        $expected = [$user2->id];
223
        $actual = $userlist2->get_userids();
224
        $this->assertEquals($expected, $actual);
225
 
226
        // Add userlist1 to the approved user list.
227
        $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
228
 
229
        // Delete user data using delete_data_for_user for usercontext1.
230
        provider::delete_data_for_users($approvedlist);
231
 
232
        // Re-fetch users in usercontext1 - The user list should now be empty.
233
        $userlist1 = new \core_privacy\local\request\userlist($usercontext1, $component);
234
        provider::get_users_in_context($userlist1);
235
        $this->assertCount(0, $userlist1);
236
 
237
        // Re-fetch users in usercontext2 - The user list should not be empty (user2).
238
        $userlist2 = new \core_privacy\local\request\userlist($usercontext2, $component);
239
        provider::get_users_in_context($userlist2);
240
        $this->assertCount(1, $userlist2);
241
 
242
        // User data should be only removed in the user context.
243
        $systemcontext = \context_system::instance();
244
        // Add userlist2 to the approved user list in the system context.
245
        $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
246
        // Delete user1 data using delete_data_for_user.
247
        provider::delete_data_for_users($approvedlist);
248
        // Re-fetch users in usercontext2 - The user list should not be empty (user2).
249
        $userlist1 = new \core_privacy\local\request\userlist($usercontext2, $component);
250
        provider::get_users_in_context($userlist1);
251
        $this->assertCount(1, $userlist1);
252
    }
253
 
254
    /**
255
     * Add dummy user info data.
256
     *
257
     * @param int $userid The ID of the user
258
     * @param int $fieldid The ID of the field
259
     * @param string $data The data
260
     */
261
    private function add_user_info_data($userid, $fieldid, $data) {
262
        global $DB;
263
 
264
        $userinfodata = array(
265
            'userid' => $userid,
266
            'fieldid' => $fieldid,
267
            'data' => $data,
268
            'dataformat' => 0
269
        );
270
 
271
        $DB->insert_record('user_info_data', $userinfodata);
272
    }
273
 
274
    /**
275
     * Add dummy profile category.
276
     *
277
     * @return int The ID of the profile category
278
     */
279
    private function add_profile_category() {
280
        $cat = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Test category']);
281
        return $cat->id;
282
    }
283
 
284
    /**
285
     * Add dummy profile field.
286
     *
287
     * @param int $categoryid The ID of the profile category
288
     * @param string $datatype The datatype of the profile field
289
     * @return int The ID of the profile field
290
     */
291
    private function add_profile_field($categoryid, $datatype) {
292
        $data = $this->getDataGenerator()->create_custom_profile_field([
293
            'datatype' => $datatype,
294
            'shortname' => 'tstField',
295
            'name' => 'Test field',
296
            'description' => 'This is a test.',
297
            'categoryid' => $categoryid,
298
        ]);
299
        return $data->id;
300
    }
301
}