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 * Tests the events related to the user profile fields and categories.
22 * @copyright 2017 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->dirroot
. '/user/profile/definelib.php');
33 * Tests the events related to the user profile fields and categories.
37 * @copyright 2017 Mark Nelson <markn@moodle.com>
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class core_event_profile_field_testcase
extends advanced_testcase
{
45 public function setUp() {
46 $this->resetAfterTest();
50 * Test that triggering the user_info_category_created event works as expected.
52 public function test_user_info_category_created_event() {
55 // Create a new profile category.
56 $cat1 = new stdClass();
57 $cat1->name
= 'Example category';
59 $cat1->id
= $DB->insert_record('user_info_category', $cat1);
62 $sink = $this->redirectEvents();
63 \core\event\user_info_category_created
::create_from_category($cat1)->trigger();
64 $events = $sink->get_events();
67 // Confirm we got the right number of events.
68 $this->assertCount(1, $events);
70 // Validate that the event was correctly triggered.
71 $event = reset($events);
72 $this->assertInstanceOf('\core\event\user_info_category_created', $event);
73 $this->assertEquals($event->objectid
, $cat1->id
);
74 $this->assertEquals($event->other
['name'], $cat1->name
);
78 * Test that moving a user info category triggers an updated event.
80 public function test_user_info_category_updated_event() {
83 // Create new profile categories.
84 $cat1 = new stdClass();
85 $cat1->name
= 'Example category';
87 $cat1->id
= $DB->insert_record('user_info_category', $cat1);
89 $cat2 = new stdClass();
90 $cat2->name
= 'Example category 2';
92 $cat2->id
= $DB->insert_record('user_info_category', $cat2);
94 // Trigger the events.
95 $sink = $this->redirectEvents();
96 profile_move_category($cat1->id
, 'down');
97 $events = $sink->get_events();
100 // Should now have two events.
101 $this->assertCount(2, $events);
102 $event1 = array_shift($events);
103 $event2 = array_shift($events);
105 // Validate that the events were correctly triggered.
106 $this->assertInstanceOf('\core\event\user_info_category_updated', $event1);
107 $this->assertEquals($event1->objectid
, $cat1->id
);
108 $this->assertEquals($event1->other
['name'], $cat1->name
);
110 $this->assertInstanceOf('\core\event\user_info_category_updated', $event2);
111 $this->assertEquals($event2->objectid
, $cat2->id
);
112 $this->assertEquals($event2->other
['name'], $cat2->name
);
116 * Test that deleting a user info category triggers a delete event.
118 public function test_user_info_category_deleted_event() {
121 // Create new profile categories.
122 $cat1 = new stdClass();
123 $cat1->name
= 'Example category';
124 $cat1->sortorder
= 1;
125 $cat1->id
= $DB->insert_record('user_info_category', $cat1);
127 $cat2 = new stdClass();
128 $cat2->name
= 'Example category 2';
129 $cat2->sortorder
= 2;
130 $cat2->id
= $DB->insert_record('user_info_category', $cat2);
132 // Trigger the event.
133 $sink = $this->redirectEvents();
134 profile_delete_category($cat2->id
);
135 $events = $sink->get_events();
138 // Confirm we got the right number of events.
139 $this->assertCount(1, $events);
141 // Validate that the event was correctly triggered.
142 $event = reset($events);
143 $this->assertInstanceOf('\core\event\user_info_category_deleted', $event);
144 $this->assertEquals($event->objectid
, $cat2->id
);
145 $this->assertEquals($event->other
['name'], $cat2->name
);
149 * Test that creating a user info field triggers a create event.
151 public function test_user_info_field_created_event() {
154 // Create a new profile category.
155 $cat1 = new stdClass();
156 $cat1->name
= 'Example category';
157 $cat1->sortorder
= 1;
158 $cat1->id
= $DB->insert_record('user_info_category', $cat1);
160 // Create a new profile field.
161 $data = new stdClass();
162 $data->datatype
= 'text';
163 $data->shortname
= 'example';
164 $data->name
= 'Example field';
165 $data->description
= 'Hello this is an example.';
166 $data->required
= false;
167 $data->locked
= false;
168 $data->forceunique
= false;
169 $data->signup
= false;
170 $data->visible
= '0';
171 $data->categoryid
= $cat1->id
;
173 // Trigger the event.
174 $sink = $this->redirectEvents();
175 $field = new profile_define_base();
176 $field->define_save($data);
177 $events = $sink->get_events();
180 // Get the field that was created.
181 $field = $DB->get_record('user_info_field', array('shortname' => $data->shortname
));
183 // Confirm we got the right number of events.
184 $this->assertCount(1, $events);
186 // Validate that the event was correctly triggered.
187 $event = reset($events);
188 $this->assertInstanceOf('\core\event\user_info_field_created', $event);
189 $this->assertEquals($event->objectid
, $field->id
);
190 $this->assertEquals($event->other
['shortname'], $field->shortname
);
191 $this->assertEquals($event->other
['name'], $field->name
);
192 $this->assertEquals($event->other
['datatype'], $field->datatype
);
196 * Test that updating a user info field triggers an update event.
198 public function test_user_info_field_updated_event() {
201 // Create a new profile category.
202 $cat1 = new stdClass();
203 $cat1->name
= 'Example category';
204 $cat1->sortorder
= 1;
205 $cat1->id
= $DB->insert_record('user_info_category', $cat1);
207 // Create a new profile field.
208 $data = new stdClass();
209 $data->datatype
= 'text';
210 $data->shortname
= 'example';
211 $data->name
= 'Example field';
212 $data->description
= 'Hello this is an example.';
213 $data->required
= false;
214 $data->locked
= false;
215 $data->forceunique
= false;
216 $data->signup
= false;
217 $data->visible
= '0';
218 $data->categoryid
= $cat1->id
;
219 $data->id
= $DB->insert_record('user_info_field', $data);
221 // Trigger the event.
222 $sink = $this->redirectEvents();
223 $field = new profile_define_base();
224 $field->define_save($data);
225 $events = $sink->get_events();
228 // Confirm we got the right number of events.
229 $this->assertCount(1, $events);
231 // Validate that the event was correctly triggered.
232 $event = reset($events);
233 $this->assertInstanceOf('\core\event\user_info_field_updated', $event);
234 $this->assertEquals($event->objectid
, $data->id
);
235 $this->assertEquals($event->other
['shortname'], $data->shortname
);
236 $this->assertEquals($event->other
['name'], $data->name
);
237 $this->assertEquals($event->other
['datatype'], $data->datatype
);
241 * Test that moving a field triggers update events.
243 public function test_user_info_field_updated_event_move_field() {
246 // Create a new profile category.
247 $cat1 = new stdClass();
248 $cat1->name
= 'Example category';
249 $cat1->sortorder
= 1;
250 $cat1->id
= $DB->insert_record('user_info_category', $cat1);
252 // Create a new profile field.
253 $field1 = new stdClass();
254 $field1->datatype
= 'text';
255 $field1->shortname
= 'example';
256 $field1->name
= 'Example field';
257 $field1->description
= 'Hello this is an example.';
258 $field1->required
= false;
259 $field1->locked
= false;
260 $field1->forceunique
= false;
261 $field1->signup
= false;
262 $field1->visible
= '0';
263 $field1->categoryid
= $cat1->id
;
264 $field1->sortorder
= 1;
265 $field1->id
= $DB->insert_record('user_info_field', $field1);
267 // Create another that we will be moving.
268 $field2 = clone $field1;
269 $field2->datatype
= 'text';
270 $field2->shortname
= 'example2';
271 $field2->name
= 'Example field 2';
272 $field2->sortorder
= 2;
273 $field2->id
= $DB->insert_record('user_info_field', $field2);
275 // Trigger the events.
276 $sink = $this->redirectEvents();
277 profile_move_field($field2->id
, 'up');
278 $events = $sink->get_events();
281 // Should now have two events.
282 $this->assertCount(2, $events);
283 $event1 = array_shift($events);
284 $event2 = array_shift($events);
286 // Validate that the events were correctly triggered.
287 $this->assertInstanceOf('\core\event\user_info_field_updated', $event1);
288 $this->assertEquals($event1->objectid
, $field2->id
);
289 $this->assertEquals($event1->other
['shortname'], $field2->shortname
);
290 $this->assertEquals($event1->other
['name'], $field2->name
);
291 $this->assertEquals($event1->other
['datatype'], $field2->datatype
);
293 $this->assertInstanceOf('\core\event\user_info_field_updated', $event2);
294 $this->assertEquals($event2->objectid
, $field1->id
);
295 $this->assertEquals($event2->other
['shortname'], $field1->shortname
);
296 $this->assertEquals($event2->other
['name'], $field1->name
);
297 $this->assertEquals($event2->other
['datatype'], $field1->datatype
);
301 * Test that when we delete a category that contains a field, that the field being moved to
302 * another category triggers an update event.
304 public function test_user_info_field_updated_event_delete_category() {
307 // Create a new profile category.
308 $cat1 = new stdClass();
309 $cat1->name
= 'Example category';
310 $cat1->sortorder
= 1;
311 $cat1->id
= $DB->insert_record('user_info_category', $cat1);
313 $cat2 = new stdClass();
314 $cat2->name
= 'Example category';
315 $cat2->sortorder
= 1;
316 $cat2->id
= $DB->insert_record('user_info_category', $cat2);
318 // Create a new profile field.
319 $field = new stdClass();
320 $field->datatype
= 'text';
321 $field->shortname
= 'example';
322 $field->name
= 'Example field';
323 $field->description
= 'Hello this is an example.';
324 $field->required
= false;
325 $field->locked
= false;
326 $field->forceunique
= false;
327 $field->signup
= false;
328 $field->visible
= '0';
329 $field->categoryid
= $cat1->id
;
330 $field->id
= $DB->insert_record('user_info_field', $field);
332 // Trigger the event.
333 $sink = $this->redirectEvents();
334 profile_delete_category($cat1->id
);
335 $events = $sink->get_events();
338 // Check we got the right number of events.
339 $this->assertCount(2, $events);
341 // Validate that the event was correctly triggered.
342 $event = reset($events);
343 $this->assertInstanceOf('\core\event\user_info_field_updated', $event);
344 $this->assertEquals($event->objectid
, $field->id
);
345 $this->assertEquals($event->other
['shortname'], $field->shortname
);
346 $this->assertEquals($event->other
['name'], $field->name
);
347 $this->assertEquals($event->other
['datatype'], $field->datatype
);
351 * Test that deleting a user info field triggers a delete event.
353 public function test_user_info_field_deleted_event() {
356 // Create a new profile category.
357 $cat1 = new stdClass();
358 $cat1->name
= 'Example category';
359 $cat1->sortorder
= 1;
360 $cat1->id
= $DB->insert_record('user_info_category', $cat1);
362 // Create a new profile field.
363 $data = new stdClass();
364 $data->datatype
= 'text';
365 $data->shortname
= 'delete';
366 $data->name
= 'Example field for delete';
367 $data->description
= 'Hello this is an example.';
368 $data->required
= false;
369 $data->locked
= false;
370 $data->forceunique
= false;
371 $data->signup
= false;
372 $data->visible
= '0';
373 $data->categoryid
= $cat1->id
;
374 $data->id
= $DB->insert_record('user_info_field', $data, true);
376 // Trigger the event.
377 $sink = $this->redirectEvents();
378 profile_delete_field($data->id
);
379 $events = $sink->get_events();
382 // Confirm we got the right number of events.
383 $this->assertCount(1, $events);
385 // Validate that the event was correctly triggered.
386 $event = reset($events);
387 $this->assertInstanceOf('\core\event\user_info_field_deleted', $event);
388 $this->assertEquals($event->objectid
, $data->id
);
389 $this->assertEquals($event->other
['shortname'], $data->shortname
);
390 $this->assertEquals($event->other
['name'], $data->name
);
391 $this->assertEquals($event->other
['datatype'], $data->datatype
);