2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Base class for unit tests for profilefield_datetime.
20 * @package profilefield_datetime
21 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 use \core_privacy\tests\provider_testcase
;
28 use profilefield_datetime\privacy\provider
;
29 use core_privacy\local\request\approved_userlist
;
32 * Unit tests for user\profile\field\datetime\classes\privacy\provider.php
34 * @copyright 2018 Mihail Geshoski <mihail@moodle.com>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class profilefield_datetime_testcase
extends provider_testcase
{
40 * Basic setup for these tests.
42 public function setUp() {
43 $this->resetAfterTest(true);
47 * Test getting the context for the user ID related to this plugin.
49 public function test_get_contexts_for_userid() {
51 // Create profile category.
52 $categoryid = $this->add_profile_category();
53 // Create profile field.
54 $profilefieldid = $this->add_profile_field($categoryid, 'datetime');
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());
68 * Test that data is exported correctly for this plugin.
70 public function test_export_user_data() {
71 // Create profile category.
72 $categoryid = $this->add_profile_category();
73 // Create datetime profile field.
74 $datetimeprofilefieldid = $this->add_profile_field($categoryid, 'datetime');
75 // Create checkbox profile field.
76 $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
78 $user = $this->getDataGenerator()->create_user();
79 $context = context_user
::instance($user->id
);
80 // Add datetime user info data.
81 $this->add_user_info_data($user->id
, $datetimeprofilefieldid, '1524067200');
82 // Add checkbox user info data.
83 $this->add_user_info_data($user->id
, $checkboxprofilefieldid, 'test data');
85 $writer = \core_privacy\local\request\writer
::with_context($context);
86 $this->assertFalse($writer->has_any_data());
87 $this->export_context_data_for_user($user->id
, $context, 'profilefield_datetime');
88 $data = $writer->get_data([get_string('pluginname', 'profilefield_datetime')]);
89 $this->assertCount(3, (array) $data);
90 $this->assertEquals('Test field', $data->name
);
91 $this->assertEquals('This is a test.', $data->description
);
92 $this->assertEquals('19 April 2018', $data->data
);
96 * Test that user data is deleted using the context.
98 public function test_delete_data_for_all_users_in_context() {
100 // Create profile category.
101 $categoryid = $this->add_profile_category();
102 // Create datetime profile field.
103 $datetimeprofilefieldid = $this->add_profile_field($categoryid, 'datetime');
104 // Create checkbox profile field.
105 $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
107 $user = $this->getDataGenerator()->create_user();
108 $context = context_user
::instance($user->id
);
109 // Add datetime user info data.
110 $this->add_user_info_data($user->id
, $datetimeprofilefieldid, '1524067200');
111 // Add checkbox user info data.
112 $this->add_user_info_data($user->id
, $checkboxprofilefieldid, 'test data');
113 // Check that we have two entries.
114 $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id
]);
115 $this->assertCount(2, $userinfodata);
116 provider
::delete_data_for_all_users_in_context($context);
117 // Check that the correct profile field has been deleted.
118 $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id
]);
119 $this->assertCount(1, $userinfodata);
120 $this->assertNotEquals('1524067200', reset($userinfodata)->data
);
124 * Test that user data is deleted for this user.
126 public function test_delete_data_for_user() {
128 // Create profile category.
129 $categoryid = $this->add_profile_category();
130 // Create datetime profile field.
131 $datetimeprofilefieldid = $this->add_profile_field($categoryid, 'datetime');
132 // Create checkbox profile field.
133 $checkboxprofilefieldid = $this->add_profile_field($categoryid, 'checkbox');
135 $user = $this->getDataGenerator()->create_user();
136 $context = context_user
::instance($user->id
);
137 // Add datetime user info data.
138 $this->add_user_info_data($user->id
, $datetimeprofilefieldid, '1524067200');
139 // Add checkbox user info data.
140 $this->add_user_info_data($user->id
, $checkboxprofilefieldid, 'test data');
141 // Check that we have two entries.
142 $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id
]);
143 $this->assertCount(2, $userinfodata);
144 $approvedlist = new \core_privacy\local\request\approved_contextlist
($user, 'profilefield_datetime',
146 provider
::delete_data_for_user($approvedlist);
147 // Check that the correct profile field has been deleted.
148 $userinfodata = $DB->get_records('user_info_data', ['userid' => $user->id
]);
149 $this->assertCount(1, $userinfodata);
150 $this->assertNotEquals('1524067200', reset($userinfodata)->data
);
154 * Test that only users with a user context are fetched.
156 public function test_get_users_in_context() {
157 $this->resetAfterTest();
159 $component = 'profilefield_datetime';
161 // Create profile category.
162 $categoryid = $this->add_profile_category();
163 // Create profile field.
164 $profilefieldid = $this->add_profile_field($categoryid, 'datetime');
167 $user = $this->getDataGenerator()->create_user();
168 $usercontext = context_user
::instance($user->id
);
170 // The list of users should not return anything yet (related data still haven't been created).
171 $userlist = new \core_privacy\local\request\
userlist($usercontext, $component);
172 provider
::get_users_in_context($userlist);
173 $this->assertCount(0, $userlist);
175 $this->add_user_info_data($user->id
, $profilefieldid, 'test data');
177 // The list of users for user context should return the user.
178 provider
::get_users_in_context($userlist);
179 $this->assertCount(1, $userlist);
180 $expected = [$user->id
];
181 $actual = $userlist->get_userids();
182 $this->assertEquals($expected, $actual);
184 // The list of users for system context should not return any users.
185 $systemcontext = context_system
::instance();
186 $userlist = new \core_privacy\local\request\
userlist($systemcontext, $component);
187 provider
::get_users_in_context($userlist);
188 $this->assertCount(0, $userlist);
192 * Test that data for users in approved userlist is deleted.
194 public function test_delete_data_for_users() {
195 $this->resetAfterTest();
197 $component = 'profilefield_datetime';
198 // Create profile category.
199 $categoryid = $this->add_profile_category();
200 // Create profile field.
201 $profilefieldid = $this->add_profile_field($categoryid, 'datetime');
204 $user1 = $this->getDataGenerator()->create_user();
205 $usercontext1 = context_user
::instance($user1->id
);
208 $user2 = $this->getDataGenerator()->create_user();
209 $usercontext2 = context_user
::instance($user2->id
);
211 $this->add_user_info_data($user1->id
, $profilefieldid, 'test data');
212 $this->add_user_info_data($user2->id
, $profilefieldid, 'test data');
214 // The list of users for usercontext1 should return user1.
215 $userlist1 = new \core_privacy\local\request\
userlist($usercontext1, $component);
216 provider
::get_users_in_context($userlist1);
217 $this->assertCount(1, $userlist1);
218 $expected = [$user1->id
];
219 $actual = $userlist1->get_userids();
220 $this->assertEquals($expected, $actual);
222 // The list of users for usercontext2 should return user2.
223 $userlist2 = new \core_privacy\local\request\
userlist($usercontext2, $component);
224 provider
::get_users_in_context($userlist2);
225 $this->assertCount(1, $userlist2);
226 $expected = [$user2->id
];
227 $actual = $userlist2->get_userids();
228 $this->assertEquals($expected, $actual);
230 // Add userlist1 to the approved user list.
231 $approvedlist = new approved_userlist($usercontext1, $component, $userlist1->get_userids());
233 // Delete user data using delete_data_for_user for usercontext1.
234 provider
::delete_data_for_users($approvedlist);
236 // Re-fetch users in usercontext1 - The user list should now be empty.
237 $userlist1 = new \core_privacy\local\request\
userlist($usercontext1, $component);
238 provider
::get_users_in_context($userlist1);
239 $this->assertCount(0, $userlist1);
241 // Re-fetch users in usercontext2 - The user list should not be empty (user2).
242 $userlist2 = new \core_privacy\local\request\
userlist($usercontext2, $component);
243 provider
::get_users_in_context($userlist2);
244 $this->assertCount(1, $userlist2);
246 // User data should be only removed in the user context.
247 $systemcontext = context_system
::instance();
248 // Add userlist2 to the approved user list in the system context.
249 $approvedlist = new approved_userlist($systemcontext, $component, $userlist2->get_userids());
250 // Delete user1 data using delete_data_for_user.
251 provider
::delete_data_for_users($approvedlist);
252 // Re-fetch users in usercontext2 - The user list should not be empty (user2).
253 $userlist1 = new \core_privacy\local\request\
userlist($usercontext2, $component);
254 provider
::get_users_in_context($userlist1);
255 $this->assertCount(1, $userlist1);
259 * Add dummy user info data.
261 * @param int $userid The ID of the user
262 * @param int $fieldid The ID of the field
263 * @param string $data The data
265 private function add_user_info_data($userid, $fieldid, $data) {
267 $userinfodata = array(
269 'fieldid' => $fieldid,
274 $DB->insert_record('user_info_data', $userinfodata);
278 * Add dummy profile category.
280 * @return int The ID of the profile category
282 private function add_profile_category() {
284 // Create a new profile category.
285 $cat = new stdClass();
286 $cat->name
= 'Test category';
289 return $DB->insert_record('user_info_category', $cat);
293 * Add dummy profile field.
295 * @param int $categoryid The ID of the profile category
296 * @param string $datatype The datatype of the profile field
297 * @return int The ID of the profile field
299 private function add_profile_field($categoryid, $datatype) {
301 // Create a new profile field.
302 $data = new stdClass();
303 $data->datatype
= $datatype;
304 $data->shortname
= 'tstField';
305 $data->name
= 'Test field';
306 $data->description
= 'This is a test.';
307 $data->required
= false;
308 $data->locked
= false;
309 $data->forceunique
= false;
310 $data->signup
= false;
311 $data->visible
= '0';
312 $data->categoryid
= $categoryid;
314 return $DB->insert_record('user_info_field', $data);