| 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\event;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use profile_define_base;
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           22 | 
              | 
        
        
            | 
            | 
           23 | 
           global $CFG;
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           require_once($CFG->dirroot . '/user/profile/definelib.php');
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           /**
  | 
        
        
            | 
            | 
           28 | 
            * Tests the events related to the user profile fields and categories.
  | 
        
        
            | 
            | 
           29 | 
            *
  | 
        
        
            | 
            | 
           30 | 
            * @package   core
  | 
        
        
            | 
            | 
           31 | 
            * @category  test
  | 
        
        
            | 
            | 
           32 | 
            * @copyright 2017 Mark Nelson <markn@moodle.com>
  | 
        
        
            | 
            | 
           33 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           34 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           35 | 
           final class profile_field_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
               /**
  | 
        
        
            | 
            | 
           38 | 
                * Test set up.
  | 
        
        
            | 
            | 
           39 | 
                */
  | 
        
        
            | 
            | 
           40 | 
               public function setUp(): void {
  | 
        
        
           | 1441 | 
           ariadna | 
           41 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           42 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           43 | 
               }
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
               /**
  | 
        
        
            | 
            | 
           46 | 
                * Test that triggering the user_info_category_created event works as expected.
  | 
        
        
            | 
            | 
           47 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           48 | 
               public function test_user_info_category_created_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           49 | 
                   // Create a new profile category.
  | 
        
        
            | 
            | 
           50 | 
                   $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
                   // Trigger the event.
  | 
        
        
            | 
            | 
           53 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           54 | 
                   \core\event\user_info_category_created::create_from_category($cat1)->trigger();
  | 
        
        
            | 
            | 
           55 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           56 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           57 | 
              | 
        
        
            | 
            | 
           58 | 
                   // Confirm we got the right number of events.
  | 
        
        
            | 
            | 
           59 | 
                   $this->assertCount(1, $events);
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
                   // Validate that the event was correctly triggered.
  | 
        
        
            | 
            | 
           62 | 
                   $event = reset($events);
  | 
        
        
            | 
            | 
           63 | 
                   $this->assertInstanceOf('\core\event\user_info_category_created', $event);
  | 
        
        
            | 
            | 
           64 | 
                   $this->assertEquals($event->objectid, $cat1->id);
  | 
        
        
            | 
            | 
           65 | 
                   $this->assertEquals($event->other['name'], $cat1->name);
  | 
        
        
            | 
            | 
           66 | 
               }
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
               /**
  | 
        
        
            | 
            | 
           69 | 
                * Test that moving a user info category triggers an updated event.
  | 
        
        
            | 
            | 
           70 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           71 | 
               public function test_user_info_category_updated_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           72 | 
                   global $DB;
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
                   // Create new profile categories.
  | 
        
        
            | 
            | 
           75 | 
                   $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
  | 
        
        
            | 
            | 
           76 | 
                   $cat2 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category 2']);
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
                   // Trigger the events.
  | 
        
        
            | 
            | 
           79 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           80 | 
                   profile_move_category($cat1->id, 'down');
  | 
        
        
            | 
            | 
           81 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           82 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   // Should now have two events.
  | 
        
        
            | 
            | 
           85 | 
                   $this->assertCount(2, $events);
  | 
        
        
            | 
            | 
           86 | 
                   $event1 = array_shift($events);
  | 
        
        
            | 
            | 
           87 | 
                   $event2 = array_shift($events);
  | 
        
        
            | 
            | 
           88 | 
              | 
        
        
            | 
            | 
           89 | 
                   // Validate that the events were correctly triggered.
  | 
        
        
            | 
            | 
           90 | 
                   $this->assertInstanceOf('\core\event\user_info_category_updated', $event1);
  | 
        
        
            | 
            | 
           91 | 
                   $this->assertEquals($event1->objectid, $cat1->id);
  | 
        
        
            | 
            | 
           92 | 
                   $this->assertEquals($event1->other['name'], $cat1->name);
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
                   $this->assertInstanceOf('\core\event\user_info_category_updated', $event2);
  | 
        
        
            | 
            | 
           95 | 
                   $this->assertEquals($event2->objectid, $cat2->id);
  | 
        
        
            | 
            | 
           96 | 
                   $this->assertEquals($event2->other['name'], $cat2->name);
  | 
        
        
            | 
            | 
           97 | 
               }
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
               /**
  | 
        
        
            | 
            | 
           100 | 
                * Test that deleting a user info category triggers a delete event.
  | 
        
        
            | 
            | 
           101 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           102 | 
               public function test_user_info_category_deleted_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           103 | 
                   // Create new profile categories.
  | 
        
        
            | 
            | 
           104 | 
                   $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
  | 
        
        
            | 
            | 
           105 | 
                   $cat2 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category 2']);
  | 
        
        
            | 
            | 
           106 | 
              | 
        
        
            | 
            | 
           107 | 
                   // Trigger the event.
  | 
        
        
            | 
            | 
           108 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           109 | 
                   profile_delete_category($cat2->id);
  | 
        
        
            | 
            | 
           110 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           111 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
                   // Confirm we got the right number of events.
  | 
        
        
            | 
            | 
           114 | 
                   $this->assertCount(1, $events);
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
                   // Validate that the event was correctly triggered.
  | 
        
        
            | 
            | 
           117 | 
                   $event = reset($events);
  | 
        
        
            | 
            | 
           118 | 
                   $this->assertInstanceOf('\core\event\user_info_category_deleted', $event);
  | 
        
        
            | 
            | 
           119 | 
                   $this->assertEquals($event->objectid, $cat2->id);
  | 
        
        
            | 
            | 
           120 | 
                   $this->assertEquals($event->other['name'], $cat2->name);
  | 
        
        
            | 
            | 
           121 | 
               }
  | 
        
        
            | 
            | 
           122 | 
              | 
        
        
            | 
            | 
           123 | 
               /**
  | 
        
        
            | 
            | 
           124 | 
                * Test that creating a user info field triggers a create event.
  | 
        
        
            | 
            | 
           125 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           126 | 
               public function test_user_info_field_created_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           127 | 
                   global $DB;
  | 
        
        
            | 
            | 
           128 | 
              | 
        
        
            | 
            | 
           129 | 
                   // Create a new profile category.
  | 
        
        
            | 
            | 
           130 | 
                   $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
                   // Create a new profile field.
  | 
        
        
            | 
            | 
           133 | 
                   $data = new \stdClass();
  | 
        
        
            | 
            | 
           134 | 
                   $data->datatype = 'text';
  | 
        
        
            | 
            | 
           135 | 
                   $data->shortname = 'example';
  | 
        
        
            | 
            | 
           136 | 
                   $data->name = 'Example field';
  | 
        
        
            | 
            | 
           137 | 
                   $data->description = 'Hello this is an example.';
  | 
        
        
            | 
            | 
           138 | 
                   $data->required = false;
  | 
        
        
            | 
            | 
           139 | 
                   $data->locked = false;
  | 
        
        
            | 
            | 
           140 | 
                   $data->forceunique = false;
  | 
        
        
            | 
            | 
           141 | 
                   $data->signup = false;
  | 
        
        
            | 
            | 
           142 | 
                   $data->visible = '0';
  | 
        
        
            | 
            | 
           143 | 
                   $data->categoryid = $cat1->id;
  | 
        
        
            | 
            | 
           144 | 
              | 
        
        
            | 
            | 
           145 | 
                   // Trigger the event.
  | 
        
        
            | 
            | 
           146 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           147 | 
                   $field = new profile_define_base();
  | 
        
        
            | 
            | 
           148 | 
                   $field->define_save($data);
  | 
        
        
            | 
            | 
           149 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           150 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           151 | 
              | 
        
        
            | 
            | 
           152 | 
                   // Get the field that was created.
  | 
        
        
            | 
            | 
           153 | 
                   $field = $DB->get_record('user_info_field', array('shortname' => $data->shortname));
  | 
        
        
            | 
            | 
           154 | 
              | 
        
        
            | 
            | 
           155 | 
                   // Confirm we got the right number of events.
  | 
        
        
            | 
            | 
           156 | 
                   $this->assertCount(1, $events);
  | 
        
        
            | 
            | 
           157 | 
              | 
        
        
            | 
            | 
           158 | 
                   // Validate that the event was correctly triggered.
  | 
        
        
            | 
            | 
           159 | 
                   $event = reset($events);
  | 
        
        
            | 
            | 
           160 | 
                   $this->assertInstanceOf('\core\event\user_info_field_created', $event);
  | 
        
        
            | 
            | 
           161 | 
                   $this->assertEquals($event->objectid, $field->id);
  | 
        
        
            | 
            | 
           162 | 
                   $this->assertEquals($event->other['shortname'], $field->shortname);
  | 
        
        
            | 
            | 
           163 | 
                   $this->assertEquals($event->other['name'], $field->name);
  | 
        
        
            | 
            | 
           164 | 
                   $this->assertEquals($event->other['datatype'], $field->datatype);
  | 
        
        
            | 
            | 
           165 | 
               }
  | 
        
        
            | 
            | 
           166 | 
              | 
        
        
            | 
            | 
           167 | 
               /**
  | 
        
        
            | 
            | 
           168 | 
                * Test that updating a user info field triggers an update event.
  | 
        
        
            | 
            | 
           169 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           170 | 
               public function test_user_info_field_updated_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           171 | 
                   // Create a new profile category.
  | 
        
        
            | 
            | 
           172 | 
                   $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
  | 
        
        
            | 
            | 
           173 | 
              | 
        
        
            | 
            | 
           174 | 
                   // Create a new profile field.
  | 
        
        
            | 
            | 
           175 | 
                   $data = $this->getDataGenerator()->create_custom_profile_field([
  | 
        
        
            | 
            | 
           176 | 
                       'datatype' => 'text',
  | 
        
        
            | 
            | 
           177 | 
                       'shortname' => 'example',
  | 
        
        
            | 
            | 
           178 | 
                       'name' => 'Example field',
  | 
        
        
            | 
            | 
           179 | 
                       'description' => 'Hello this is an example.',
  | 
        
        
            | 
            | 
           180 | 
                       'categoryid' => $cat1->id,
  | 
        
        
            | 
            | 
           181 | 
                   ]);
  | 
        
        
            | 
            | 
           182 | 
              | 
        
        
            | 
            | 
           183 | 
                   // Trigger the event.
  | 
        
        
            | 
            | 
           184 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           185 | 
                   $field = new profile_define_base();
  | 
        
        
            | 
            | 
           186 | 
                   $field->define_save($data);
  | 
        
        
            | 
            | 
           187 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           188 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           189 | 
              | 
        
        
            | 
            | 
           190 | 
                   // Confirm we got the right number of events.
  | 
        
        
            | 
            | 
           191 | 
                   $this->assertCount(1, $events);
  | 
        
        
            | 
            | 
           192 | 
              | 
        
        
            | 
            | 
           193 | 
                   // Validate that the event was correctly triggered.
  | 
        
        
            | 
            | 
           194 | 
                   $event = reset($events);
  | 
        
        
            | 
            | 
           195 | 
                   $this->assertInstanceOf('\core\event\user_info_field_updated', $event);
  | 
        
        
            | 
            | 
           196 | 
                   $this->assertEquals($event->objectid, $data->id);
  | 
        
        
            | 
            | 
           197 | 
                   $this->assertEquals($event->other['shortname'], $data->shortname);
  | 
        
        
            | 
            | 
           198 | 
                   $this->assertEquals($event->other['name'], $data->name);
  | 
        
        
            | 
            | 
           199 | 
                   $this->assertEquals($event->other['datatype'], $data->datatype);
  | 
        
        
            | 
            | 
           200 | 
               }
  | 
        
        
            | 
            | 
           201 | 
              | 
        
        
            | 
            | 
           202 | 
               /**
  | 
        
        
            | 
            | 
           203 | 
                * Test that moving a field triggers update events.
  | 
        
        
            | 
            | 
           204 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           205 | 
               public function test_user_info_field_updated_event_move_field(): void {
  | 
        
        
           | 1 | 
           efrain | 
           206 | 
                   // Create a new profile category.
  | 
        
        
            | 
            | 
           207 | 
                   $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
  | 
        
        
            | 
            | 
           208 | 
              | 
        
        
            | 
            | 
           209 | 
                   // Create a new profile field.
  | 
        
        
            | 
            | 
           210 | 
                   $field1 = $this->getDataGenerator()->create_custom_profile_field([
  | 
        
        
            | 
            | 
           211 | 
                       'datatype' => 'text',
  | 
        
        
            | 
            | 
           212 | 
                       'shortname' => 'example',
  | 
        
        
            | 
            | 
           213 | 
                       'name' => 'Example field',
  | 
        
        
            | 
            | 
           214 | 
                       'description' => 'Hello this is an example.',
  | 
        
        
            | 
            | 
           215 | 
                       'categoryid' => $cat1->id,
  | 
        
        
            | 
            | 
           216 | 
                   ]);
  | 
        
        
            | 
            | 
           217 | 
              | 
        
        
            | 
            | 
           218 | 
                   // Create another that we will be moving.
  | 
        
        
            | 
            | 
           219 | 
                   $field2 = $this->getDataGenerator()->create_custom_profile_field([
  | 
        
        
            | 
            | 
           220 | 
                       'datatype' => 'text',
  | 
        
        
            | 
            | 
           221 | 
                       'shortname' => 'example2',
  | 
        
        
            | 
            | 
           222 | 
                       'name' => 'Example field 2',
  | 
        
        
            | 
            | 
           223 | 
                       'categoryid' => $cat1->id,
  | 
        
        
            | 
            | 
           224 | 
                   ]);
  | 
        
        
            | 
            | 
           225 | 
              | 
        
        
            | 
            | 
           226 | 
                   // Trigger the events.
  | 
        
        
            | 
            | 
           227 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           228 | 
                   profile_move_field($field2->id, 'up');
  | 
        
        
            | 
            | 
           229 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           230 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           231 | 
              | 
        
        
            | 
            | 
           232 | 
                   // Should now have two events.
  | 
        
        
            | 
            | 
           233 | 
                   $this->assertCount(2, $events);
  | 
        
        
            | 
            | 
           234 | 
                   $event1 = array_shift($events);
  | 
        
        
            | 
            | 
           235 | 
                   $event2 = array_shift($events);
  | 
        
        
            | 
            | 
           236 | 
              | 
        
        
            | 
            | 
           237 | 
                   // Validate that the events were correctly triggered.
  | 
        
        
            | 
            | 
           238 | 
                   $this->assertInstanceOf('\core\event\user_info_field_updated', $event1);
  | 
        
        
            | 
            | 
           239 | 
                   $this->assertEquals($event1->objectid, $field2->id);
  | 
        
        
            | 
            | 
           240 | 
                   $this->assertEquals($event1->other['shortname'], $field2->shortname);
  | 
        
        
            | 
            | 
           241 | 
                   $this->assertEquals($event1->other['name'], $field2->name);
  | 
        
        
            | 
            | 
           242 | 
                   $this->assertEquals($event1->other['datatype'], $field2->datatype);
  | 
        
        
            | 
            | 
           243 | 
              | 
        
        
            | 
            | 
           244 | 
                   $this->assertInstanceOf('\core\event\user_info_field_updated', $event2);
  | 
        
        
            | 
            | 
           245 | 
                   $this->assertEquals($event2->objectid, $field1->id);
  | 
        
        
            | 
            | 
           246 | 
                   $this->assertEquals($event2->other['shortname'], $field1->shortname);
  | 
        
        
            | 
            | 
           247 | 
                   $this->assertEquals($event2->other['name'], $field1->name);
  | 
        
        
            | 
            | 
           248 | 
                   $this->assertEquals($event2->other['datatype'], $field1->datatype);
  | 
        
        
            | 
            | 
           249 | 
               }
  | 
        
        
            | 
            | 
           250 | 
              | 
        
        
            | 
            | 
           251 | 
               /**
  | 
        
        
            | 
            | 
           252 | 
                * Test that when we delete a category that contains a field, that the field being moved to
  | 
        
        
            | 
            | 
           253 | 
                * another category triggers an update event.
  | 
        
        
            | 
            | 
           254 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           255 | 
               public function test_user_info_field_updated_event_delete_category(): void {
  | 
        
        
           | 1 | 
           efrain | 
           256 | 
                   // Create profile categories.
  | 
        
        
            | 
            | 
           257 | 
                   $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
  | 
        
        
            | 
            | 
           258 | 
                   $cat2 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
  | 
        
        
            | 
            | 
           259 | 
              | 
        
        
            | 
            | 
           260 | 
                   // Create a new profile field.
  | 
        
        
            | 
            | 
           261 | 
                   $field = $this->getDataGenerator()->create_custom_profile_field([
  | 
        
        
            | 
            | 
           262 | 
                       'datatype' => 'text',
  | 
        
        
            | 
            | 
           263 | 
                       'shortname' => 'example',
  | 
        
        
            | 
            | 
           264 | 
                       'name' => 'Example field',
  | 
        
        
            | 
            | 
           265 | 
                       'description' => 'Hello this is an example.',
  | 
        
        
            | 
            | 
           266 | 
                       'categoryid' => $cat1->id,
  | 
        
        
            | 
            | 
           267 | 
                   ]);
  | 
        
        
            | 
            | 
           268 | 
              | 
        
        
            | 
            | 
           269 | 
                   // Trigger the event.
  | 
        
        
            | 
            | 
           270 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           271 | 
                   profile_delete_category($cat1->id);
  | 
        
        
            | 
            | 
           272 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           273 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           274 | 
              | 
        
        
            | 
            | 
           275 | 
                   // Check we got the right number of events.
  | 
        
        
            | 
            | 
           276 | 
                   $this->assertCount(2, $events);
  | 
        
        
            | 
            | 
           277 | 
              | 
        
        
            | 
            | 
           278 | 
                   // Validate that the event was correctly triggered.
  | 
        
        
            | 
            | 
           279 | 
                   $event = reset($events);
  | 
        
        
            | 
            | 
           280 | 
                   $this->assertInstanceOf('\core\event\user_info_field_updated', $event);
  | 
        
        
            | 
            | 
           281 | 
                   $this->assertEquals($event->objectid, $field->id);
  | 
        
        
            | 
            | 
           282 | 
                   $this->assertEquals($event->other['shortname'], $field->shortname);
  | 
        
        
            | 
            | 
           283 | 
                   $this->assertEquals($event->other['name'], $field->name);
  | 
        
        
            | 
            | 
           284 | 
                   $this->assertEquals($event->other['datatype'], $field->datatype);
  | 
        
        
            | 
            | 
           285 | 
               }
  | 
        
        
            | 
            | 
           286 | 
              | 
        
        
            | 
            | 
           287 | 
               /**
  | 
        
        
            | 
            | 
           288 | 
                * Test that deleting a user info field triggers a delete event.
  | 
        
        
            | 
            | 
           289 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           290 | 
               public function test_user_info_field_deleted_event(): void {
  | 
        
        
           | 1 | 
           efrain | 
           291 | 
                   // Create a new profile category.
  | 
        
        
            | 
            | 
           292 | 
                   $cat1 = $this->getDataGenerator()->create_custom_profile_field_category(['name' => 'Example category']);
  | 
        
        
            | 
            | 
           293 | 
              | 
        
        
            | 
            | 
           294 | 
                   // Create a new profile field.
  | 
        
        
            | 
            | 
           295 | 
                   $data = $this->getDataGenerator()->create_custom_profile_field([
  | 
        
        
            | 
            | 
           296 | 
                       'datatype' => 'text',
  | 
        
        
            | 
            | 
           297 | 
                       'shortname' => 'delete',
  | 
        
        
            | 
            | 
           298 | 
                       'name' => 'Example field for delete',
  | 
        
        
            | 
            | 
           299 | 
                       'description' => 'Hello this is an example.',
  | 
        
        
            | 
            | 
           300 | 
                       'categoryid' => $cat1->id,
  | 
        
        
            | 
            | 
           301 | 
                   ]);
  | 
        
        
            | 
            | 
           302 | 
              | 
        
        
            | 
            | 
           303 | 
                   // Trigger the event.
  | 
        
        
            | 
            | 
           304 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           305 | 
                   profile_delete_field($data->id);
  | 
        
        
            | 
            | 
           306 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           307 | 
                   $sink->close();
  | 
        
        
            | 
            | 
           308 | 
              | 
        
        
            | 
            | 
           309 | 
                   // Confirm we got the right number of events.
  | 
        
        
            | 
            | 
           310 | 
                   $this->assertCount(1, $events);
  | 
        
        
            | 
            | 
           311 | 
              | 
        
        
            | 
            | 
           312 | 
                   // Validate that the event was correctly triggered.
  | 
        
        
            | 
            | 
           313 | 
                   $event = reset($events);
  | 
        
        
            | 
            | 
           314 | 
                   $this->assertInstanceOf('\core\event\user_info_field_deleted', $event);
  | 
        
        
            | 
            | 
           315 | 
                   $this->assertEquals($event->objectid, $data->id);
  | 
        
        
            | 
            | 
           316 | 
                   $this->assertEquals($event->other['shortname'], $data->shortname);
  | 
        
        
            | 
            | 
           317 | 
                   $this->assertEquals($event->other['name'], $data->name);
  | 
        
        
            | 
            | 
           318 | 
                   $this->assertEquals($event->other['datatype'], $data->datatype);
  | 
        
        
            | 
            | 
           319 | 
               }
  | 
        
        
            | 
            | 
           320 | 
           }
  |