Proyectos de Subversion Moodle

Rev

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