MDL-48374 behat: Check flag before searching for span on page
[moodle.git] / message / tests / events_test.php
blob0a8699e65b9cdd43c29e8ce78c9cff68c025909a
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.
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/>.
17 /**
18 * Events tests.
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
26 defined('MOODLE_INTERNAL') || die();
28 class core_message_events_testcase extends advanced_testcase {
30 /**
31 * Test set up.
33 * This is executed before running any test in this file.
35 public function setUp() {
36 $this->resetAfterTest();
39 /**
40 * Test the message contact added event.
42 public function test_message_contact_added() {
43 // Set this user as the admin.
44 $this->setAdminUser();
46 // Create a user to add to the admin's contact list.
47 $user = $this->getDataGenerator()->create_user();
49 // Trigger and capture the event when adding a contact.
50 $sink = $this->redirectEvents();
51 message_add_contact($user->id);
52 $events = $sink->get_events();
53 $event = reset($events);
55 // Check that the event data is valid.
56 $this->assertInstanceOf('\core\event\message_contact_added', $event);
57 $this->assertEquals(context_user::instance(2), $event->get_context());
58 $expected = array(SITEID, 'message', 'add contact', 'index.php?user1=' . $user->id .
59 '&amp;user2=2', $user->id);
60 $this->assertEventLegacyLogData($expected, $event);
61 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
62 $this->assertEquals($url, $event->get_url());
65 /**
66 * Test the message contact removed event.
68 public function test_message_contact_removed() {
69 // Set this user as the admin.
70 $this->setAdminUser();
72 // Create a user to add to the admin's contact list.
73 $user = $this->getDataGenerator()->create_user();
75 // Add the user to the admin's contact list.
76 message_add_contact($user->id);
78 // Trigger and capture the event when adding a contact.
79 $sink = $this->redirectEvents();
80 message_remove_contact($user->id);
81 $events = $sink->get_events();
82 $event = reset($events);
84 // Check that the event data is valid.
85 $this->assertInstanceOf('\core\event\message_contact_removed', $event);
86 $this->assertEquals(context_user::instance(2), $event->get_context());
87 $expected = array(SITEID, 'message', 'remove contact', 'index.php?user1=' . $user->id .
88 '&amp;user2=2', $user->id);
89 $this->assertEventLegacyLogData($expected, $event);
90 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
91 $this->assertEquals($url, $event->get_url());
94 /**
95 * Test the message contact blocked event.
97 public function test_message_contact_blocked() {
98 // Set this user as the admin.
99 $this->setAdminUser();
101 // Create a user to add to the admin's contact list.
102 $user = $this->getDataGenerator()->create_user();
103 $user2 = $this->getDataGenerator()->create_user();
105 // Add the user to the admin's contact list.
106 message_add_contact($user->id);
108 // Trigger and capture the event when blocking a contact.
109 $sink = $this->redirectEvents();
110 message_block_contact($user->id);
111 $events = $sink->get_events();
112 $event = reset($events);
114 // Check that the event data is valid.
115 $this->assertInstanceOf('\core\event\message_contact_blocked', $event);
116 $this->assertEquals(context_user::instance(2), $event->get_context());
117 $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user->id . '&amp;user2=2', $user->id);
118 $this->assertEventLegacyLogData($expected, $event);
119 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
120 $this->assertEquals($url, $event->get_url());
122 // Now blocking a user that is not a contact.
123 $sink->clear();
124 message_block_contact($user2->id);
125 $events = $sink->get_events();
126 $event = reset($events);
128 // Check that the event data is valid.
129 $this->assertInstanceOf('\core\event\message_contact_blocked', $event);
130 $this->assertEquals(context_user::instance(2), $event->get_context());
131 $expected = array(SITEID, 'message', 'block contact', 'index.php?user1=' . $user2->id . '&amp;user2=2', $user2->id);
132 $this->assertEventLegacyLogData($expected, $event);
133 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
134 $this->assertEquals($url, $event->get_url());
138 * Test the message contact unblocked event.
140 public function test_message_contact_unblocked() {
141 // Set this user as the admin.
142 $this->setAdminUser();
144 // Create a user to add to the admin's contact list.
145 $user = $this->getDataGenerator()->create_user();
147 // Add the user to the admin's contact list.
148 message_add_contact($user->id);
150 // Trigger and capture the event when unblocking a contact.
151 $sink = $this->redirectEvents();
152 message_unblock_contact($user->id);
153 $events = $sink->get_events();
154 $event = reset($events);
156 // Check that the event data is valid.
157 $this->assertInstanceOf('\core\event\message_contact_unblocked', $event);
158 $this->assertEquals(context_user::instance(2), $event->get_context());
159 $expected = array(SITEID, 'message', 'unblock contact', 'index.php?user1=' . $user->id . '&amp;user2=2', $user->id);
160 $this->assertEventLegacyLogData($expected, $event);
161 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
162 $this->assertEquals($url, $event->get_url());
166 * Test the message sent event.
168 * We can not use the message_send() function in the unit test to check that the event was fired as there is a
169 * conditional check to ensure a fake message is sent during unit tests when calling that particular function.
171 public function test_message_sent() {
172 $event = \core\event\message_sent::create(array(
173 'userid' => 1,
174 'context' => context_system::instance(),
175 'relateduserid' => 2,
176 'other' => array(
177 'messageid' => 3
181 // Trigger and capturing the event.
182 $sink = $this->redirectEvents();
183 $event->trigger();
184 $events = $sink->get_events();
185 $event = reset($events);
187 // Check that the event data is valid.
188 $this->assertInstanceOf('\core\event\message_sent', $event);
189 $this->assertEquals(context_system::instance(), $event->get_context());
190 $expected = array(SITEID, 'message', 'write', 'index.php?user=1&id=2&history=1#m3', 1);
191 $this->assertEventLegacyLogData($expected, $event);
192 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
193 $this->assertEquals($url, $event->get_url());
197 * Test the message viewed event.
199 public function test_message_viewed() {
200 global $DB;
202 // Create a message to mark as read.
203 $message = new stdClass();
204 $message->useridfrom = '1';
205 $message->useridto = '2';
206 $message->subject = 'Subject';
207 $message->message = 'Message';
208 $message->id = $DB->insert_record('message', $message);
210 // Trigger and capture the event.
211 $sink = $this->redirectEvents();
212 message_mark_message_read($message, time());
213 $events = $sink->get_events();
214 $event = reset($events);
216 // Check that the event data is valid.
217 $this->assertInstanceOf('\core\event\message_viewed', $event);
218 $this->assertEquals(context_user::instance(2), $event->get_context());
219 $url = new moodle_url('/message/index.php', array('user1' => $event->userid, 'user2' => $event->relateduserid));
220 $this->assertEquals($url, $event->get_url());