Differences Between: [Versions 39 and 310]
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 * Tests the events related to the user profile fields and categories. 19 * 20 * @package core 21 * @category test 22 * @copyright 2017 Mark Nelson <markn@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 30 require_once($CFG->dirroot . '/user/profile/definelib.php'); 31 32 /** 33 * Tests the events related to the user profile fields and categories. 34 * 35 * @package core 36 * @category test 37 * @copyright 2017 Mark Nelson <markn@moodle.com> 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class core_event_profile_field_testcase extends advanced_testcase { 41 42 /** 43 * Test set up. 44 */ 45 public function setUp(): void { 46 $this->resetAfterTest(); 47 } 48 49 /** 50 * Test that triggering the user_info_category_created event works as expected. 51 */ 52 public function test_user_info_category_created_event() { 53 global $DB; 54 55 // Create a new profile category. 56 $cat1 = new stdClass(); 57 $cat1->name = 'Example category'; 58 $cat1->sortorder = $DB->count_records('user_info_category') + 1; 59 $cat1->id = $DB->insert_record('user_info_category', $cat1); 60 61 // Trigger the event. 62 $sink = $this->redirectEvents(); 63 \core\event\user_info_category_created::create_from_category($cat1)->trigger(); 64 $events = $sink->get_events(); 65 $sink->close(); 66 67 // Confirm we got the right number of events. 68 $this->assertCount(1, $events); 69 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); 75 } 76 77 /** 78 * Test that moving a user info category triggers an updated event. 79 */ 80 public function test_user_info_category_updated_event() { 81 global $DB; 82 83 // Create new profile categories. 84 $cat1 = new stdClass(); 85 $cat1->name = 'Example category'; 86 $cat1->sortorder = $DB->count_records('user_info_category') + 1; 87 $cat1->id = $DB->insert_record('user_info_category', $cat1); 88 89 $cat2 = new stdClass(); 90 $cat2->name = 'Example category 2'; 91 $cat2->sortorder = $DB->count_records('user_info_category') + 1; 92 $cat2->id = $DB->insert_record('user_info_category', $cat2); 93 94 // Trigger the events. 95 $sink = $this->redirectEvents(); 96 profile_move_category($cat1->id, 'down'); 97 $events = $sink->get_events(); 98 $sink->close(); 99 100 // Should now have two events. 101 $this->assertCount(2, $events); 102 $event1 = array_shift($events); 103 $event2 = array_shift($events); 104 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); 109 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); 113 } 114 115 /** 116 * Test that deleting a user info category triggers a delete event. 117 */ 118 public function test_user_info_category_deleted_event() { 119 global $DB; 120 121 // Create new profile categories. 122 $cat1 = new stdClass(); 123 $cat1->name = 'Example category'; 124 $cat1->sortorder = $DB->count_records('user_info_category') + 1; 125 $cat1->id = $DB->insert_record('user_info_category', $cat1); 126 127 $cat2 = new stdClass(); 128 $cat2->name = 'Example category 2'; 129 $cat2->sortorder = $DB->count_records('user_info_category') + 1; 130 $cat2->id = $DB->insert_record('user_info_category', $cat2); 131 132 // Trigger the event. 133 $sink = $this->redirectEvents(); 134 profile_delete_category($cat2->id); 135 $events = $sink->get_events(); 136 $sink->close(); 137 138 // Confirm we got the right number of events. 139 $this->assertCount(1, $events); 140 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); 146 } 147 148 /** 149 * Test that creating a user info field triggers a create event. 150 */ 151 public function test_user_info_field_created_event() { 152 global $DB; 153 154 // Create a new profile category. 155 $cat1 = new stdClass(); 156 $cat1->name = 'Example category'; 157 $cat1->sortorder = $DB->count_records('user_info_category') + 1; 158 $cat1->id = $DB->insert_record('user_info_category', $cat1); 159 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; 172 173 // Trigger the event. 174 $sink = $this->redirectEvents(); 175 $field = new profile_define_base(); 176 $field->define_save($data); 177 $events = $sink->get_events(); 178 $sink->close(); 179 180 // Get the field that was created. 181 $field = $DB->get_record('user_info_field', array('shortname' => $data->shortname)); 182 183 // Confirm we got the right number of events. 184 $this->assertCount(1, $events); 185 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); 193 } 194 195 /** 196 * Test that updating a user info field triggers an update event. 197 */ 198 public function test_user_info_field_updated_event() { 199 global $DB; 200 201 // Create a new profile category. 202 $cat1 = new stdClass(); 203 $cat1->name = 'Example category'; 204 $cat1->sortorder = $DB->count_records('user_info_category') + 1; 205 $cat1->id = $DB->insert_record('user_info_category', $cat1); 206 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); 220 221 // Trigger the event. 222 $sink = $this->redirectEvents(); 223 $field = new profile_define_base(); 224 $field->define_save($data); 225 $events = $sink->get_events(); 226 $sink->close(); 227 228 // Confirm we got the right number of events. 229 $this->assertCount(1, $events); 230 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); 238 } 239 240 /** 241 * Test that moving a field triggers update events. 242 */ 243 public function test_user_info_field_updated_event_move_field() { 244 global $DB; 245 246 // Create a new profile category. 247 $cat1 = new stdClass(); 248 $cat1->name = 'Example category'; 249 $cat1->sortorder = $DB->count_records('user_info_category') + 1; 250 $cat1->id = $DB->insert_record('user_info_category', $cat1); 251 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 = $DB->count_records('user_info_field') + 1; 265 $field1->id = $DB->insert_record('user_info_field', $field1); 266 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 = $DB->count_records('user_info_field') + 1; 273 $field2->id = $DB->insert_record('user_info_field', $field2); 274 275 // Trigger the events. 276 $sink = $this->redirectEvents(); 277 profile_move_field($field2->id, 'up'); 278 $events = $sink->get_events(); 279 $sink->close(); 280 281 // Should now have two events. 282 $this->assertCount(2, $events); 283 $event1 = array_shift($events); 284 $event2 = array_shift($events); 285 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); 292 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); 298 } 299 300 /** 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. 303 */ 304 public function test_user_info_field_updated_event_delete_category() { 305 global $DB; 306 307 // Create a new profile category. 308 $cat1 = new stdClass(); 309 $cat1->name = 'Example category'; 310 $cat1->sortorder = $DB->count_records('user_info_category') + 1; 311 $cat1->id = $DB->insert_record('user_info_category', $cat1); 312 313 $cat2 = new stdClass(); 314 $cat2->name = 'Example category'; 315 $cat2->sortorder = $DB->count_records('user_info_category') + 1; 316 $cat2->id = $DB->insert_record('user_info_category', $cat2); 317 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); 331 332 // Trigger the event. 333 $sink = $this->redirectEvents(); 334 profile_delete_category($cat1->id); 335 $events = $sink->get_events(); 336 $sink->close(); 337 338 // Check we got the right number of events. 339 $this->assertCount(2, $events); 340 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); 348 } 349 350 /** 351 * Test that deleting a user info field triggers a delete event. 352 */ 353 public function test_user_info_field_deleted_event() { 354 global $DB; 355 356 // Create a new profile category. 357 $cat1 = new stdClass(); 358 $cat1->name = 'Example category'; 359 $cat1->sortorder = $DB->count_records('user_info_category') + 1; 360 $cat1->id = $DB->insert_record('user_info_category', $cat1); 361 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); 375 376 // Trigger the event. 377 $sink = $this->redirectEvents(); 378 profile_delete_field($data->id); 379 $events = $sink->get_events(); 380 $sink->close(); 381 382 // Confirm we got the right number of events. 383 $this->assertCount(1, $events); 384 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); 392 } 393 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body