Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
/**
18
 * Events tests.
19
 *
20
 * @package core_message
21
 * @category test
22
 * @copyright 2014 Mark Nelson <markn@moodle.com>
23
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
 
26
namespace core_message\event;
27
 
11 efrain 28
use core_message\tests\helper as testhelper;
1 efrain 29
 
30
/**
31
 * Class containing the tests for message related events.
32
 *
33
 * @package core_message
34
 * @category test
35
 * @copyright 2014 Mark Nelson <markn@moodle.com>
36
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37
 */
11 efrain 38
final class events_test extends \advanced_testcase {
1 efrain 39
    /**
40
     * Test the message contact added event.
41
     */
11 efrain 42
    public function test_message_contact_added(): void {
1 efrain 43
        global $USER;
44
 
11 efrain 45
        $this->resetAfterTest();
46
 
1 efrain 47
        // Set this user as the admin.
48
        $this->setAdminUser();
49
 
50
        // Create a user to add to the admin's contact list.
51
        $user = $this->getDataGenerator()->create_user();
52
 
53
        // Trigger and capture the event when adding a contact.
54
        $sink = $this->redirectEvents();
55
        \core_message\api::add_contact($USER->id, $user->id);
56
        $events = $sink->get_events();
57
        $event = reset($events);
58
 
59
        // Check that the event data is valid.
60
        $this->assertInstanceOf('\core\event\message_contact_added', $event);
61
        $this->assertEquals(\context_user::instance(2), $event->get_context());
62
        $url = new \moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
63
        $this->assertEquals($url, $event->get_url());
64
    }
65
 
66
    /**
67
     * Test the message contact removed event.
68
     */
11 efrain 69
    public function test_message_contact_removed(): void {
1 efrain 70
        global $USER;
71
 
11 efrain 72
        $this->resetAfterTest();
73
 
1 efrain 74
        // Set this user as the admin.
75
        $this->setAdminUser();
76
 
77
        // Create a user to add to the admin's contact list.
78
        $user = $this->getDataGenerator()->create_user();
79
 
80
        // Add the user to the admin's contact list.
81
        \core_message\api::add_contact($USER->id, $user->id);
82
 
83
        // Trigger and capture the event when adding a contact.
84
        $sink = $this->redirectEvents();
85
        \core_message\api::remove_contact($USER->id, $user->id);
86
        $events = $sink->get_events();
87
        $event = reset($events);
88
 
89
        // Check that the event data is valid.
90
        $this->assertInstanceOf('\core\event\message_contact_removed', $event);
91
        $this->assertEquals(\context_user::instance(2), $event->get_context());
92
        $url = new \moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
93
        $this->assertEquals($url, $event->get_url());
94
    }
95
 
96
    /**
97
     * Test the message user blocked event.
98
     */
11 efrain 99
    public function test_message_user_blocked(): void {
1 efrain 100
        global $USER;
101
 
11 efrain 102
        $this->resetAfterTest();
103
 
1 efrain 104
        // Set this user as the admin.
105
        $this->setAdminUser();
106
 
107
        // Create a user to add to the admin's contact list.
108
        $user = $this->getDataGenerator()->create_user();
109
 
110
        // Add the user to the admin's contact list.
111
        \core_message\api::add_contact($USER->id, $user->id);
112
 
113
        // Trigger and capture the event when blocking a contact.
114
        $sink = $this->redirectEvents();
115
        \core_message\api::block_user($USER->id, $user->id);
116
        $events = $sink->get_events();
117
        $event = reset($events);
118
 
119
        // Check that the event data is valid.
120
        $this->assertInstanceOf('\core\event\message_user_blocked', $event);
121
        $this->assertEquals(\context_user::instance(2), $event->get_context());
122
    }
123
 
124
    /**
125
     * Test the message user unblocked event.
126
     */
11 efrain 127
    public function test_message_user_unblocked(): void {
1 efrain 128
        global $USER;
129
 
11 efrain 130
        $this->resetAfterTest();
131
 
1 efrain 132
        // Set this user as the admin.
133
        $this->setAdminUser();
134
 
135
        // Create a user to add to the admin's contact list.
136
        $user = $this->getDataGenerator()->create_user();
137
 
138
        // Add the user to the admin's contact list.
139
        \core_message\api::add_contact($USER->id, $user->id);
140
 
141
        // Block the user.
142
        \core_message\api::block_user($USER->id, $user->id);
143
        // Make sure that we have 1 blocked user.
144
        $this->assertEquals(1, \core_message\api::count_blocked_users());
145
 
146
        // Trigger and capture the event when unblocking a contact.
147
        $sink = $this->redirectEvents();
148
        \core_message\api::unblock_user($USER->id, $user->id);
149
        $events = $sink->get_events();
150
        $event = reset($events);
151
 
152
        // Check that the event data is valid.
153
        $this->assertInstanceOf('\core\event\message_user_unblocked', $event);
154
        $this->assertEquals(\context_user::instance(2), $event->get_context());
155
 
156
        // Make sure that we have no blocked users.
157
        $this->assertEmpty(\core_message\api::count_blocked_users());
158
    }
159
 
160
    /**
161
     * Test the message sent event.
162
     *
163
     * We can not use the message_send() function in the unit test to check that the event was fired as there is a
164
     * conditional check to ensure a fake message is sent during unit tests when calling that particular function.
165
     */
11 efrain 166
    public function test_message_sent(): void {
1 efrain 167
        $event = \core\event\message_sent::create(array(
168
            'objectid' => 3,
169
            'userid' => 1,
170
            'context'  => \context_system::instance(),
171
            'relateduserid' => 2,
172
            'other' => array(
173
                'courseid' => 4
174
            )
175
        ));
176
 
177
        // Trigger and capturing the event.
178
        $sink = $this->redirectEvents();
179
        $event->trigger();
180
        $events = $sink->get_events();
181
        $event = reset($events);
182
 
183
        // Check that the event data is valid.
184
        $this->assertInstanceOf('\core\event\message_sent', $event);
185
        $this->assertEquals(\context_system::instance(), $event->get_context());
186
        $url = new \moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
187
        $this->assertEquals($url, $event->get_url());
188
        $this->assertEquals(3, $event->objectid);
189
        $this->assertEquals(4, $event->other['courseid']);
190
    }
191
 
11 efrain 192
    public function test_mesage_sent_without_other_courseid(): void {
1 efrain 193
        // Creating a message_sent event without other[courseid] leads to exception.
194
        $this->expectException('coding_exception');
195
        $this->expectExceptionMessage('The \'courseid\' value must be set in other');
196
 
197
        $event = \core\event\message_sent::create(array(
198
            'userid' => 1,
199
            'context'  => \context_system::instance(),
200
            'relateduserid' => 2,
201
            'other' => array(
202
                'messageid' => 3,
203
            )
204
        ));
205
    }
206
 
11 efrain 207
    public function test_mesage_sent_via_create_from_ids(): void {
208
        $this->resetAfterTest();
209
 
1 efrain 210
        // Containing courseid.
211
        $event = \core\event\message_sent::create_from_ids(1, 2, 3, 4);
212
 
213
        // Trigger and capturing the event.
214
        $sink = $this->redirectEvents();
215
        $event->trigger();
216
        $events = $sink->get_events();
217
        $event = reset($events);
218
 
219
        // Check that the event data is valid.
220
        $this->assertInstanceOf('\core\event\message_sent', $event);
221
        $this->assertEquals(\context_system::instance(), $event->get_context());
222
        $url = new \moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
223
        $this->assertEquals($url, $event->get_url());
224
        $this->assertEquals(3, $event->objectid);
225
        $this->assertEquals(4, $event->other['courseid']);
226
    }
227
 
228
    /**
229
     * Test the group message sent event.
230
     *
231
     * We can't test events in any testing of the message_send() function as there is a conditional PHPUNIT check in message_send,
232
     * resulting in fake messages being generated and captured under test. As a result, none of the events code, nor message
233
     * processor code is called during testing.
234
     */
11 efrain 235
    public function test_group_message_sent(): void {
236
        $this->resetAfterTest();
237
 
1 efrain 238
        $event = \core\event\group_message_sent::create([
239
            'objectid' => 3,
240
            'userid' => 1,
241
            'context'  => \context_system::instance(),
242
            'other' => [
243
                'courseid' => 4,
244
                'conversationid' => 54
245
            ]
246
        ]);
247
 
248
        // Trigger and capture the event.
249
        $sink = $this->redirectEvents();
250
        $event->trigger();
251
        $events = $sink->get_events();
252
        $event = reset($events);
253
 
254
        // Check that the event data is valid.
255
        $this->assertInstanceOf('\core\event\group_message_sent', $event);
256
        $this->assertEquals(\context_system::instance(), $event->get_context());
257
        $url = new \moodle_url('/message/index.php');
258
        $this->assertEquals($url, $event->get_url());
259
        $this->assertEquals(3, $event->objectid);
260
        $this->assertEquals(4, $event->other['courseid']);
261
        $this->assertEquals(54, $event->other['conversationid']);
262
    }
263
 
264
    /**
265
     * Test the group message sent event when created without a courseid.
266
     */
11 efrain 267
    public function test_group_message_sent_without_other_courseid(): void {
1 efrain 268
        // Creating a message_sent event without other[courseid] leads to exception.
269
        $this->expectException('coding_exception');
270
        $this->expectExceptionMessage('The \'courseid\' value must be set in other');
271
 
272
        $event = \core\event\group_message_sent::create([
273
            'userid' => 1,
274
            'objectid' => 3,
275
            'context'  => \context_system::instance(),
276
            'relateduserid' => 2,
277
            'other' => [
278
                'conversationid' => 34
279
            ]
280
        ]);
281
    }
282
 
283
    /**
284
     * Test the group message sent event when created without a conversationid.
285
     */
11 efrain 286
    public function test_group_message_sent_without_other_conversationid(): void {
1 efrain 287
        // Creating a message_sent event without other[courseid] leads to exception.
288
        $this->expectException('coding_exception');
289
        $this->expectExceptionMessage('The \'conversationid\' value must be set in other');
290
 
291
        $event = \core\event\group_message_sent::create([
292
            'userid' => 1,
293
            'objectid' => 3,
294
            'context'  => \context_system::instance(),
295
            'relateduserid' => 2,
296
            'other' => [
297
                'courseid' => 44,
298
            ]
299
        ]);
300
    }
301
 
302
    /**
303
     * Test the group message sent event using the create_from_ids() method.
304
     */
11 efrain 305
    public function test_group_message_sent_via_create_from_ids(): void {
306
        $this->resetAfterTest();
307
 
1 efrain 308
        // Fields are: userfromid, conversationid, messageid, courseid.
309
        $event = \core\event\group_message_sent::create_from_ids(1, 2, 3, 4);
310
 
311
        // Trigger and capture the event.
312
        $sink = $this->redirectEvents();
313
        $event->trigger();
314
        $events = $sink->get_events();
315
        $event = reset($events);
316
 
317
        // Check that the event data is valid.
318
        $this->assertInstanceOf('\core\event\group_message_sent', $event);
319
        $this->assertEquals(\context_system::instance(), $event->get_context());
320
        $this->assertEquals(new \moodle_url('/message/index.php'), $event->get_url());
321
        $this->assertEquals(1, $event->userid);
322
        $this->assertEquals(2, $event->other['conversationid']);
323
        $this->assertEquals(3, $event->objectid);
324
        $this->assertEquals(4, $event->other['courseid']);
325
    }
326
 
327
    /**
328
     * Test the message viewed event.
329
     */
11 efrain 330
    public function test_message_viewed(): void {
1 efrain 331
        global $DB;
332
 
11 efrain 333
        $this->resetAfterTest();
334
 
1 efrain 335
        // Create users to send messages between.
336
        $user1 = $this->getDataGenerator()->create_user();
337
        $user2 = $this->getDataGenerator()->create_user();
338
 
11 efrain 339
        $messageid = testhelper::send_fake_message($user1, $user2);
1 efrain 340
 
341
        // Trigger and capture the event.
342
        $sink = $this->redirectEvents();
343
        $message = $DB->get_record('messages', ['id' => $messageid]);
344
        \core_message\api::mark_message_as_read($user1->id, $message);
345
        $events = $sink->get_events();
346
        $event = reset($events);
347
 
348
        // Get the usage action.
349
        $mua = $DB->get_record('message_user_actions', ['userid' => $user1->id, 'messageid' => $messageid,
350
            'action' => \core_message\api::MESSAGE_ACTION_READ]);
351
 
352
        // Check that the event data is valid.
353
        $this->assertInstanceOf('\core\event\message_viewed', $event);
354
        $this->assertEquals(\context_user::instance($user1->id), $event->get_context());
355
        $this->assertEquals($mua->id, $event->objectid);
356
        $this->assertEquals($messageid, $event->other['messageid']);
357
        $url = new \moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
358
        $this->assertEquals($url, $event->get_url());
359
    }
360
 
361
    /**
362
     * Test the message deleted event.
363
     */
11 efrain 364
    public function test_message_deleted(): void {
1 efrain 365
        global $DB, $USER;
366
 
11 efrain 367
        $this->resetAfterTest();
368
 
1 efrain 369
        $this->setAdminUser();
370
 
371
        // Create users to send messages between.
372
        $user1 = $this->getDataGenerator()->create_user();
373
        $user2 = $this->getDataGenerator()->create_user();
374
 
11 efrain 375
        $messageid = testhelper::send_fake_message($user1, $user2);
1 efrain 376
 
377
        // Trigger and capture the event.
378
        $sink = $this->redirectEvents();
379
        \core_message\api::delete_message($user1->id, $messageid);
380
        $events = $sink->get_events();
381
        $event = reset($events);
382
 
383
        // Get the usage action.
384
        $mua = $DB->get_record('message_user_actions', ['userid' => $user1->id, 'messageid' => $messageid,
385
            'action' => \core_message\api::MESSAGE_ACTION_DELETED]);
386
 
387
        // Check that the event data is valid.
388
        $this->assertInstanceOf('\core\event\message_deleted', $event);
389
        $this->assertEquals($USER->id, $event->userid); // The user who deleted it.
390
        $this->assertEquals($user1->id, $event->relateduserid);
391
        $this->assertEquals($mua->id, $event->objectid);
392
        $this->assertEquals($messageid, $event->other['messageid']);
393
 
394
        $this->setUser($user1);
395
 
396
        // Create a read message.
11 efrain 397
        $messageid = testhelper::send_fake_message($user1, $user2);
1 efrain 398
        $m = $DB->get_record('messages', ['id' => $messageid]);
399
        \core_message\api::mark_message_as_read($user2->id, $m);
400
 
401
        // Trigger and capture the event.
402
        $sink = $this->redirectEvents();
403
        \core_message\api::delete_message($user2->id, $messageid);
404
        $events = $sink->get_events();
405
        $event = reset($events);
406
 
407
        // Get the usage action.
408
        $mua = $DB->get_record('message_user_actions', ['userid' => $user2->id, 'messageid' => $messageid,
409
            'action' => \core_message\api::MESSAGE_ACTION_DELETED]);
410
 
411
        // Check that the event data is valid.
412
        $this->assertInstanceOf('\core\event\message_deleted', $event);
413
        $this->assertEquals($user1->id, $event->userid);
414
        $this->assertEquals($user2->id, $event->relateduserid);
415
        $this->assertEquals($mua->id, $event->objectid);
416
        $this->assertEquals($messageid, $event->other['messageid']);
417
    }
418
 
419
    /**
420
     * Test the message deleted event is fired when deleting a conversation.
421
     */
11 efrain 422
    public function test_message_deleted_whole_conversation(): void {
1 efrain 423
        global $DB;
424
 
11 efrain 425
        $this->resetAfterTest();
426
 
1 efrain 427
        // Create some users.
428
        $user1 = self::getDataGenerator()->create_user();
429
        $user2 = self::getDataGenerator()->create_user();
430
 
431
        // The person doing the deletion.
432
        $this->setUser($user1);
433
 
434
        // Send some messages back and forth.
435
        $time = 1;
436
        $messages = [];
11 efrain 437
        $messages[] = testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
438
        $messages[] = testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
439
        $messages[] = testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
440
        $messages[] = testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
441
        $messages[] = testhelper::send_fake_message($user1, $user2, 'You doing much?', 0, $time + 5);
442
        $messages[] = testhelper::send_fake_message($user2, $user1, 'Nah', 0, $time + 6);
443
        $messages[] = testhelper::send_fake_message($user1, $user2, 'You nubz0r!', 0, $time + 7);
444
        $messages[] = testhelper::send_fake_message($user2, $user1, 'Ouch.', 0, $time + 8);
1 efrain 445
 
446
        // Mark the last 4 messages as read.
447
        $m5 = $DB->get_record('messages', ['id' => $messages[4]]);
448
        $m6 = $DB->get_record('messages', ['id' => $messages[5]]);
449
        $m7 = $DB->get_record('messages', ['id' => $messages[6]]);
450
        $m8 = $DB->get_record('messages', ['id' => $messages[7]]);
451
        \core_message\api::mark_message_as_read($user2->id, $m5);
452
        \core_message\api::mark_message_as_read($user1->id, $m6);
453
        \core_message\api::mark_message_as_read($user2->id, $m7);
454
        \core_message\api::mark_message_as_read($user1->id, $m8);
455
 
456
        // Trigger and capture the event.
457
        $sink = $this->redirectEvents();
458
        $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]);
459
        \core_message\api::delete_conversation_by_id($user1->id, $conversationid);
460
        $events = $sink->get_events();
461
 
462
        // Get the user actions for the messages deleted by that user.
463
        $muas = $DB->get_records('message_user_actions', ['userid' => $user1->id,
464
            'action' => \core_message\api::MESSAGE_ACTION_DELETED], 'timecreated ASC');
465
        $this->assertCount(8, $muas);
466
 
467
        // Create a list we can use for testing.
468
        $muatest = [];
469
        foreach ($muas as $mua) {
470
            $muatest[$mua->messageid] = $mua;
471
        }
472
 
473
        // Check that there were the correct number of events triggered.
474
        $this->assertEquals(8, count($events));
475
 
476
        // Check that the event data is valid.
477
        $i = 1;
478
        foreach ($events as $event) {
479
            $messageid = $messages[$i - 1];
480
 
481
            $this->assertInstanceOf('\core\event\message_deleted', $event);
482
 
483
            $this->assertEquals($muatest[$messageid]->id, $event->objectid);
484
            $this->assertEquals($user1->id, $event->userid);
485
            $this->assertEquals($user1->id, $event->relateduserid);
486
            $this->assertEquals($messageid, $event->other['messageid']);
487
 
488
            $i++;
489
        }
490
    }
491
 
492
    /**
493
     * Test the notification sent event.
494
     */
11 efrain 495
    public function test_notification_sent(): void {
496
        $this->resetAfterTest();
497
 
1 efrain 498
        // Create a course.
499
        $course = $this->getDataGenerator()->create_course();
500
 
501
        // Create users to send notification between.
502
        $user1 = $this->getDataGenerator()->create_user();
503
        $user2 = $this->getDataGenerator()->create_user();
504
 
505
        // Send a notification.
11 efrain 506
        $notificationid = testhelper::send_fake_message($user1, $user2, 'Hello world!', 1);
1 efrain 507
 
508
        // Containing courseid.
509
        $event = \core\event\notification_sent::create_from_ids($user1->id, $user2->id, $notificationid, $course->id);
510
 
511
        // Trigger and capturing the event.
512
        $sink = $this->redirectEvents();
513
        $event->trigger();
514
        $events = $sink->get_events();
515
        $event = reset($events);
516
 
517
        // Check that the event data is valid.
518
        $this->assertInstanceOf('\core\event\notification_sent', $event);
519
        $this->assertEquals($notificationid, $event->objectid);
520
        $this->assertEquals($user1->id, $event->userid);
521
        $this->assertEquals($user2->id, $event->relateduserid);
522
        $this->assertEquals(\context_system::instance(), $event->get_context());
523
        $this->assertEquals($course->id, $event->other['courseid']);
524
        $url = new \moodle_url('/message/output/popup/notifications.php', array('notificationid' => $event->objectid));
525
        $this->assertEquals($url, $event->get_url());
526
    }
527
 
528
    /**
529
     * Test the notification sent event when null passed as course.
530
     */
11 efrain 531
    public function test_notification_sent_with_null_course(): void {
1 efrain 532
        $event = \core\event\notification_sent::create_from_ids(1, 1, 1, null);
533
 
11 efrain 534
        $this->resetAfterTest();
535
 
1 efrain 536
        // Trigger and capture the event.
537
        $sink = $this->redirectEvents();
538
        $event->trigger();
539
        $events = $sink->get_events();
540
        $event = reset($events);
541
 
542
        // Check that the event data is valid.
543
        $this->assertInstanceOf('\core\event\notification_sent', $event);
544
        $this->assertEquals(SITEID, $event->other['courseid']);
545
    }
546
 
547
    /**
548
     * Test the notification viewed event.
549
     */
11 efrain 550
    public function test_notification_viewed(): void {
1 efrain 551
        global $DB;
552
 
11 efrain 553
        $this->resetAfterTest();
554
 
1 efrain 555
        // Create users to send notifications between.
556
        $user1 = $this->getDataGenerator()->create_user();
557
        $user2 = $this->getDataGenerator()->create_user();
558
 
559
        // Send a notification.
11 efrain 560
        $notificationid = testhelper::send_fake_message($user1, $user2, 'Hello world!', 1);
1 efrain 561
 
562
        // Trigger and capture the event.
563
        $sink = $this->redirectEvents();
564
        $notification = $DB->get_record('notifications', ['id' => $notificationid]);
565
        \core_message\api::mark_notification_as_read($notification);
566
        $events = $sink->get_events();
567
        $event = reset($events);
568
 
569
        // Check that the event data is valid.
570
        $this->assertInstanceOf('\core\event\notification_viewed', $event);
571
        $this->assertEquals($notificationid, $event->objectid);
572
        $this->assertEquals($user2->id, $event->userid);
573
        $this->assertEquals($user1->id, $event->relateduserid);
574
        $this->assertEquals(\context_user::instance($user2->id), $event->get_context());
575
        $url = new \moodle_url('/message/output/popup/notifications.php', array('notificationid' => $event->objectid));
576
        $this->assertEquals($url, $event->get_url());
577
    }
578
}