Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 11
Línea 23... Línea 23...
23
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
24
 */
Línea 25... Línea 25...
25
 
25
 
Línea 26... Línea -...
26
namespace core_message\event;
-
 
27
 
-
 
28
defined('MOODLE_INTERNAL') || die();
-
 
29
 
-
 
30
global $CFG;
26
namespace core_message\event;
Línea 31... Línea 27...
31
 
27
 
32
require_once($CFG->dirroot . '/message/tests/messagelib_test.php');
28
use core_message\tests\helper as testhelper;
33
 
29
 
34
/**
30
/**
35
 * Class containing the tests for message related events.
31
 * Class containing the tests for message related events.
36
 *
32
 *
37
 * @package core_message
33
 * @package core_message
38
 * @category test
34
 * @category test
39
 * @copyright 2014 Mark Nelson <markn@moodle.com>
35
 * @copyright 2014 Mark Nelson <markn@moodle.com>
40
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
41
 */
-
 
42
class events_test extends \core_message\messagelib_test {
-
 
43
 
-
 
44
    /**
-
 
45
     * Test set up.
-
 
46
     *
-
 
47
     * This is executed before running any test in this file.
-
 
48
     */
-
 
49
    public function setUp(): void {
-
 
50
        $this->resetAfterTest();
36
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51
    }
37
 */
52
 
38
final class events_test extends \advanced_testcase {
53
    /**
39
    /**
54
     * Test the message contact added event.
40
     * Test the message contact added event.
Línea -... Línea 41...
-
 
41
     */
-
 
42
    public function test_message_contact_added(): void {
55
     */
43
        global $USER;
56
    public function test_message_contact_added() {
44
 
Línea 57... Línea 45...
57
        global $USER;
45
        $this->resetAfterTest();
58
 
46
 
Línea 76... Línea 64...
76
    }
64
    }
Línea 77... Línea 65...
77
 
65
 
78
    /**
66
    /**
79
     * Test the message contact removed event.
67
     * Test the message contact removed event.
80
     */
68
     */
81
    public function test_message_contact_removed() {
69
    public function test_message_contact_removed(): void {
Línea -... Línea 70...
-
 
70
        global $USER;
-
 
71
 
82
        global $USER;
72
        $this->resetAfterTest();
83
 
73
 
Línea 84... Línea 74...
84
        // Set this user as the admin.
74
        // Set this user as the admin.
85
        $this->setAdminUser();
75
        $this->setAdminUser();
Línea 104... Línea 94...
104
    }
94
    }
Línea 105... Línea 95...
105
 
95
 
106
    /**
96
    /**
107
     * Test the message user blocked event.
97
     * Test the message user blocked event.
108
     */
98
     */
109
    public function test_message_user_blocked() {
99
    public function test_message_user_blocked(): void {
Línea -... Línea 100...
-
 
100
        global $USER;
-
 
101
 
110
        global $USER;
102
        $this->resetAfterTest();
111
 
103
 
Línea 112... Línea 104...
112
        // Set this user as the admin.
104
        // Set this user as the admin.
113
        $this->setAdminUser();
105
        $this->setAdminUser();
Línea 130... Línea 122...
130
    }
122
    }
Línea 131... Línea 123...
131
 
123
 
132
    /**
124
    /**
133
     * Test the message user unblocked event.
125
     * Test the message user unblocked event.
134
     */
126
     */
135
    public function test_message_user_unblocked() {
127
    public function test_message_user_unblocked(): void {
Línea -... Línea 128...
-
 
128
        global $USER;
-
 
129
 
136
        global $USER;
130
        $this->resetAfterTest();
137
 
131
 
Línea 138... Línea 132...
138
        // Set this user as the admin.
132
        // Set this user as the admin.
139
        $this->setAdminUser();
133
        $this->setAdminUser();
Línea 167... Línea 161...
167
     * Test the message sent event.
161
     * Test the message sent event.
168
     *
162
     *
169
     * We can not use the message_send() function in the unit test to check that the event was fired as there is a
163
     * We can not use the message_send() function in the unit test to check that the event was fired as there is a
170
     * conditional check to ensure a fake message is sent during unit tests when calling that particular function.
164
     * conditional check to ensure a fake message is sent during unit tests when calling that particular function.
171
     */
165
     */
172
    public function test_message_sent() {
166
    public function test_message_sent(): void {
173
        $event = \core\event\message_sent::create(array(
167
        $event = \core\event\message_sent::create(array(
174
            'objectid' => 3,
168
            'objectid' => 3,
175
            'userid' => 1,
169
            'userid' => 1,
176
            'context'  => \context_system::instance(),
170
            'context'  => \context_system::instance(),
177
            'relateduserid' => 2,
171
            'relateduserid' => 2,
Línea 193... Línea 187...
193
        $this->assertEquals($url, $event->get_url());
187
        $this->assertEquals($url, $event->get_url());
194
        $this->assertEquals(3, $event->objectid);
188
        $this->assertEquals(3, $event->objectid);
195
        $this->assertEquals(4, $event->other['courseid']);
189
        $this->assertEquals(4, $event->other['courseid']);
196
    }
190
    }
Línea 197... Línea 191...
197
 
191
 
198
    public function test_mesage_sent_without_other_courseid() {
-
 
199
 
192
    public function test_mesage_sent_without_other_courseid(): void {
200
        // Creating a message_sent event without other[courseid] leads to exception.
193
        // Creating a message_sent event without other[courseid] leads to exception.
201
        $this->expectException('coding_exception');
194
        $this->expectException('coding_exception');
Línea 202... Línea 195...
202
        $this->expectExceptionMessage('The \'courseid\' value must be set in other');
195
        $this->expectExceptionMessage('The \'courseid\' value must be set in other');
Línea 209... Línea 202...
209
                'messageid' => 3,
202
                'messageid' => 3,
210
            )
203
            )
211
        ));
204
        ));
212
    }
205
    }
Línea 213... Línea 206...
213
 
206
 
-
 
207
    public function test_mesage_sent_via_create_from_ids(): void {
-
 
208
        $this->resetAfterTest();
214
    public function test_mesage_sent_via_create_from_ids() {
209
 
215
        // Containing courseid.
210
        // Containing courseid.
Línea 216... Línea 211...
216
        $event = \core\event\message_sent::create_from_ids(1, 2, 3, 4);
211
        $event = \core\event\message_sent::create_from_ids(1, 2, 3, 4);
217
 
212
 
Línea 235... Línea 230...
235
     *
230
     *
236
     * We can't test events in any testing of the message_send() function as there is a conditional PHPUNIT check in message_send,
231
     * We can't test events in any testing of the message_send() function as there is a conditional PHPUNIT check in message_send,
237
     * resulting in fake messages being generated and captured under test. As a result, none of the events code, nor message
232
     * resulting in fake messages being generated and captured under test. As a result, none of the events code, nor message
238
     * processor code is called during testing.
233
     * processor code is called during testing.
239
     */
234
     */
240
    public function test_group_message_sent() {
235
    public function test_group_message_sent(): void {
-
 
236
        $this->resetAfterTest();
-
 
237
 
241
        $event = \core\event\group_message_sent::create([
238
        $event = \core\event\group_message_sent::create([
242
            'objectid' => 3,
239
            'objectid' => 3,
243
            'userid' => 1,
240
            'userid' => 1,
244
            'context'  => \context_system::instance(),
241
            'context'  => \context_system::instance(),
245
            'other' => [
242
            'other' => [
Línea 265... Línea 262...
265
    }
262
    }
Línea 266... Línea 263...
266
 
263
 
267
    /**
264
    /**
268
     * Test the group message sent event when created without a courseid.
265
     * Test the group message sent event when created without a courseid.
269
     */
266
     */
270
    public function test_group_message_sent_without_other_courseid() {
267
    public function test_group_message_sent_without_other_courseid(): void {
271
        // Creating a message_sent event without other[courseid] leads to exception.
268
        // Creating a message_sent event without other[courseid] leads to exception.
272
        $this->expectException('coding_exception');
269
        $this->expectException('coding_exception');
Línea 273... Línea 270...
273
        $this->expectExceptionMessage('The \'courseid\' value must be set in other');
270
        $this->expectExceptionMessage('The \'courseid\' value must be set in other');
Línea 284... Línea 281...
284
    }
281
    }
Línea 285... Línea 282...
285
 
282
 
286
    /**
283
    /**
287
     * Test the group message sent event when created without a conversationid.
284
     * Test the group message sent event when created without a conversationid.
288
     */
285
     */
289
    public function test_group_message_sent_without_other_conversationid() {
286
    public function test_group_message_sent_without_other_conversationid(): void {
290
        // Creating a message_sent event without other[courseid] leads to exception.
287
        // Creating a message_sent event without other[courseid] leads to exception.
291
        $this->expectException('coding_exception');
288
        $this->expectException('coding_exception');
Línea 292... Línea 289...
292
        $this->expectExceptionMessage('The \'conversationid\' value must be set in other');
289
        $this->expectExceptionMessage('The \'conversationid\' value must be set in other');
Línea 303... Línea 300...
303
    }
300
    }
Línea 304... Línea 301...
304
 
301
 
305
    /**
302
    /**
306
     * Test the group message sent event using the create_from_ids() method.
303
     * Test the group message sent event using the create_from_ids() method.
307
     */
304
     */
-
 
305
    public function test_group_message_sent_via_create_from_ids(): void {
-
 
306
        $this->resetAfterTest();
308
    public function test_group_message_sent_via_create_from_ids() {
307
 
309
        // Fields are: userfromid, conversationid, messageid, courseid.
308
        // Fields are: userfromid, conversationid, messageid, courseid.
Línea 310... Línea 309...
310
        $event = \core\event\group_message_sent::create_from_ids(1, 2, 3, 4);
309
        $event = \core\event\group_message_sent::create_from_ids(1, 2, 3, 4);
311
 
310
 
Línea 326... Línea 325...
326
    }
325
    }
Línea 327... Línea 326...
327
 
326
 
328
    /**
327
    /**
329
     * Test the message viewed event.
328
     * Test the message viewed event.
330
     */
329
     */
331
    public function test_message_viewed() {
330
    public function test_message_viewed(): void {
Línea -... Línea 331...
-
 
331
        global $DB;
-
 
332
 
332
        global $DB;
333
        $this->resetAfterTest();
333
 
334
 
334
        // Create users to send messages between.
335
        // Create users to send messages between.
Línea 335... Línea 336...
335
        $user1 = $this->getDataGenerator()->create_user();
336
        $user1 = $this->getDataGenerator()->create_user();
Línea 336... Línea 337...
336
        $user2 = $this->getDataGenerator()->create_user();
337
        $user2 = $this->getDataGenerator()->create_user();
337
 
338
 
338
        $messageid = $this->send_fake_message($user1, $user2);
339
        $messageid = testhelper::send_fake_message($user1, $user2);
339
 
340
 
Línea 358... Línea 359...
358
    }
359
    }
Línea 359... Línea 360...
359
 
360
 
360
    /**
361
    /**
361
     * Test the message deleted event.
362
     * Test the message deleted event.
362
     */
363
     */
363
    public function test_message_deleted() {
364
    public function test_message_deleted(): void {
Línea -... Línea 365...
-
 
365
        global $DB, $USER;
-
 
366
 
364
        global $DB, $USER;
367
        $this->resetAfterTest();
Línea 365... Línea 368...
365
 
368
 
366
        $this->setAdminUser();
369
        $this->setAdminUser();
367
 
370
 
Línea 368... Línea 371...
368
        // Create users to send messages between.
371
        // Create users to send messages between.
Línea 369... Línea 372...
369
        $user1 = $this->getDataGenerator()->create_user();
372
        $user1 = $this->getDataGenerator()->create_user();
370
        $user2 = $this->getDataGenerator()->create_user();
373
        $user2 = $this->getDataGenerator()->create_user();
371
 
374
 
372
        $messageid = $this->send_fake_message($user1, $user2);
375
        $messageid = testhelper::send_fake_message($user1, $user2);
Línea 389... Línea 392...
389
        $this->assertEquals($messageid, $event->other['messageid']);
392
        $this->assertEquals($messageid, $event->other['messageid']);
Línea 390... Línea 393...
390
 
393
 
Línea 391... Línea 394...
391
        $this->setUser($user1);
394
        $this->setUser($user1);
392
 
395
 
393
        // Create a read message.
396
        // Create a read message.
394
        $messageid = $this->send_fake_message($user1, $user2);
397
        $messageid = testhelper::send_fake_message($user1, $user2);
Línea 395... Línea 398...
395
        $m = $DB->get_record('messages', ['id' => $messageid]);
398
        $m = $DB->get_record('messages', ['id' => $messageid]);
396
        \core_message\api::mark_message_as_read($user2->id, $m);
399
        \core_message\api::mark_message_as_read($user2->id, $m);
Línea 414... Línea 417...
414
    }
417
    }
Línea 415... Línea 418...
415
 
418
 
416
    /**
419
    /**
417
     * Test the message deleted event is fired when deleting a conversation.
420
     * Test the message deleted event is fired when deleting a conversation.
418
     */
421
     */
419
    public function test_message_deleted_whole_conversation() {
422
    public function test_message_deleted_whole_conversation(): void {
Línea -... Línea 423...
-
 
423
        global $DB;
-
 
424
 
420
        global $DB;
425
        $this->resetAfterTest();
421
 
426
 
422
        // Create some users.
427
        // Create some users.
Línea 423... Línea 428...
423
        $user1 = self::getDataGenerator()->create_user();
428
        $user1 = self::getDataGenerator()->create_user();
424
        $user2 = self::getDataGenerator()->create_user();
429
        $user2 = self::getDataGenerator()->create_user();
Línea 425... Línea 430...
425
 
430
 
426
        // The person doing the deletion.
431
        // The person doing the deletion.
427
        $this->setUser($user1);
432
        $this->setUser($user1);
428
 
433
 
429
        // Send some messages back and forth.
434
        // Send some messages back and forth.
430
        $time = 1;
435
        $time = 1;
431
        $messages = [];
436
        $messages = [];
432
        $messages[] = $this->send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
437
        $messages[] = testhelper::send_fake_message($user1, $user2, 'Yo!', 0, $time + 1);
433
        $messages[] = $this->send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
438
        $messages[] = testhelper::send_fake_message($user2, $user1, 'Sup mang?', 0, $time + 2);
434
        $messages[] = $this->send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
439
        $messages[] = testhelper::send_fake_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 3);
435
        $messages[] = $this->send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
440
        $messages[] = testhelper::send_fake_message($user2, $user1, 'Word.', 0, $time + 4);
Línea 436... Línea 441...
436
        $messages[] = $this->send_fake_message($user1, $user2, 'You doing much?', 0, $time + 5);
441
        $messages[] = testhelper::send_fake_message($user1, $user2, 'You doing much?', 0, $time + 5);
437
        $messages[] = $this->send_fake_message($user2, $user1, 'Nah', 0, $time + 6);
442
        $messages[] = testhelper::send_fake_message($user2, $user1, 'Nah', 0, $time + 6);
438
        $messages[] = $this->send_fake_message($user1, $user2, 'You nubz0r!', 0, $time + 7);
443
        $messages[] = testhelper::send_fake_message($user1, $user2, 'You nubz0r!', 0, $time + 7);
439
        $messages[] = $this->send_fake_message($user2, $user1, 'Ouch.', 0, $time + 8);
444
        $messages[] = testhelper::send_fake_message($user2, $user1, 'Ouch.', 0, $time + 8);
Línea 485... Línea 490...
485
    }
490
    }
Línea 486... Línea 491...
486
 
491
 
487
    /**
492
    /**
488
     * Test the notification sent event.
493
     * Test the notification sent event.
489
     */
494
     */
-
 
495
    public function test_notification_sent(): void {
-
 
496
        $this->resetAfterTest();
490
    public function test_notification_sent() {
497
 
491
        // Create a course.
498
        // Create a course.
Línea 492... Línea 499...
492
        $course = $this->getDataGenerator()->create_course();
499
        $course = $this->getDataGenerator()->create_course();
493
 
500
 
494
        // Create users to send notification between.
501
        // Create users to send notification between.
Línea 495... Línea 502...
495
        $user1 = $this->getDataGenerator()->create_user();
502
        $user1 = $this->getDataGenerator()->create_user();
496
        $user2 = $this->getDataGenerator()->create_user();
503
        $user2 = $this->getDataGenerator()->create_user();
Línea 497... Línea 504...
497
 
504
 
498
        // Send a notification.
505
        // Send a notification.
Línea 499... Línea 506...
499
        $notificationid = $this->send_fake_message($user1, $user2, 'Hello world!', 1);
506
        $notificationid = testhelper::send_fake_message($user1, $user2, 'Hello world!', 1);
Línea 519... Línea 526...
519
    }
526
    }
Línea 520... Línea 527...
520
 
527
 
521
    /**
528
    /**
522
     * Test the notification sent event when null passed as course.
529
     * Test the notification sent event when null passed as course.
523
     */
530
     */
524
    public function test_notification_sent_with_null_course() {
531
    public function test_notification_sent_with_null_course(): void {
Línea -... Línea 532...
-
 
532
        $event = \core\event\notification_sent::create_from_ids(1, 1, 1, null);
-
 
533
 
525
        $event = \core\event\notification_sent::create_from_ids(1, 1, 1, null);
534
        $this->resetAfterTest();
526
 
535
 
527
        // Trigger and capture the event.
536
        // Trigger and capture the event.
528
        $sink = $this->redirectEvents();
537
        $sink = $this->redirectEvents();
529
        $event->trigger();
538
        $event->trigger();
Línea 536... Línea 545...
536
    }
545
    }
Línea 537... Línea 546...
537
 
546
 
538
    /**
547
    /**
539
     * Test the notification viewed event.
548
     * Test the notification viewed event.
540
     */
549
     */
541
    public function test_notification_viewed() {
550
    public function test_notification_viewed(): void {
Línea -... Línea 551...
-
 
551
        global $DB;
-
 
552
 
542
        global $DB;
553
        $this->resetAfterTest();
543
 
554
 
544
        // Create users to send notifications between.
555
        // Create users to send notifications between.
Línea 545... Línea 556...
545
        $user1 = $this->getDataGenerator()->create_user();
556
        $user1 = $this->getDataGenerator()->create_user();
546
        $user2 = $this->getDataGenerator()->create_user();
557
        $user2 = $this->getDataGenerator()->create_user();
Línea 547... Línea 558...
547
 
558
 
548
        // Send a notification.
559
        // Send a notification.
549
        $notificationid = $this->send_fake_message($user1, $user2, 'Hello world!', 1);
560
        $notificationid = testhelper::send_fake_message($user1, $user2, 'Hello world!', 1);
550
 
561