Commit | Line | Data |
---|---|---|
e6432668 JM |
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 | * External message functions unit tests | |
19 | * | |
20 | * @package core_message | |
21 | * @category external | |
22 | * @copyright 2012 Jerome Mouneyrac | |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
24 | */ | |
25 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
28 | global $CFG; | |
29 | ||
30 | require_once($CFG->dirroot . '/webservice/tests/helpers.php'); | |
31 | require_once($CFG->dirroot . '/message/externallib.php'); | |
32 | ||
8252b7c2 | 33 | class core_message_externallib_testcase extends externallib_advanced_testcase { |
e6432668 | 34 | |
6ff4464b JL |
35 | /** |
36 | * Tests set up | |
37 | */ | |
38 | protected function setUp() { | |
39 | global $CFG; | |
40 | ||
41 | require_once($CFG->dirroot . '/message/lib.php'); | |
42 | } | |
43 | ||
d6731600 FM |
44 | /** |
45 | * Send a fake message. | |
46 | * | |
47 | * {@link message_send()} does not support transaction, this function will simulate a message | |
48 | * sent from a user to another. We should stop using it once {@link message_send()} will support | |
49 | * transactions. This is not clean at all, this is just used to add rows to the table. | |
50 | * | |
51 | * @param stdClass $userfrom user object of the one sending the message. | |
52 | * @param stdClass $userto user object of the one receiving the message. | |
53 | * @param string $message message to send. | |
7d69958e | 54 | * @param int $notification is the message a notification. |
6aa01968 | 55 | * @param int $time the time the message was sent |
d6731600 | 56 | */ |
6aa01968 | 57 | protected function send_message($userfrom, $userto, $message = 'Hello world!', $notification = 0, $time = 0) { |
d6731600 | 58 | global $DB; |
6aa01968 MN |
59 | |
60 | if (empty($time)) { | |
61 | $time = time(); | |
62 | } | |
63 | ||
883ce421 MN |
64 | if ($notification) { |
65 | $record = new stdClass(); | |
66 | $record->useridfrom = $userfrom->id; | |
67 | $record->useridto = $userto->id; | |
68 | $record->subject = 'No subject'; | |
69 | $record->fullmessage = $message; | |
70 | $record->smallmessage = $message; | |
71 | $record->timecreated = $time; | |
72 | ||
73 | return $DB->insert_record('notifications', $record); | |
74 | } | |
75 | ||
b2cd17e6 MN |
76 | if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) { |
77 | $conversationid = \core_message\api::create_conversation_between_users([$userfrom->id, | |
78 | $userto->id]); | |
883ce421 MN |
79 | } |
80 | ||
81 | // Ok, send the message. | |
d6731600 FM |
82 | $record = new stdClass(); |
83 | $record->useridfrom = $userfrom->id; | |
883ce421 | 84 | $record->conversationid = $conversationid; |
d6731600 FM |
85 | $record->subject = 'No subject'; |
86 | $record->fullmessage = $message; | |
883ce421 | 87 | $record->smallmessage = $message; |
6aa01968 | 88 | $record->timecreated = $time; |
4d146f1a | 89 | |
883ce421 | 90 | return $DB->insert_record('messages', $record); |
d6731600 FM |
91 | } |
92 | ||
e6432668 | 93 | /** |
f219eac7 | 94 | * Test send_instant_messages. |
e6432668 JM |
95 | */ |
96 | public function test_send_instant_messages() { | |
f219eac7 | 97 | global $DB, $USER; |
e6432668 | 98 | |
f219eac7 | 99 | $this->resetAfterTest(); |
e6432668 | 100 | |
7356e732 EL |
101 | // Transactions used in tests, tell phpunit use alternative reset method. |
102 | $this->preventResetByRollback(); | |
e6432668 | 103 | |
f219eac7 MN |
104 | $user1 = self::getDataGenerator()->create_user(); |
105 | $user2 = self::getDataGenerator()->create_user(); | |
e6432668 | 106 | |
f219eac7 MN |
107 | $this->setUser($user1); |
108 | ||
109 | // Create test message data. | |
110 | $message1 = array(); | |
111 | $message1['touserid'] = $user2->id; | |
112 | $message1['text'] = 'the message.'; | |
113 | $message1['clientmsgid'] = 4; | |
114 | $messages = array($message1); | |
115 | ||
116 | $sentmessages = core_message_external::send_instant_messages($messages); | |
117 | $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); | |
118 | ||
119 | $sentmessage = reset($sentmessages); | |
120 | ||
121 | $sql = "SELECT m.*, mcm.userid as useridto | |
122 | FROM {messages} m | |
123 | INNER JOIN {message_conversations} mc | |
124 | ON m.conversationid = mc.id | |
125 | INNER JOIN {message_conversation_members} mcm | |
126 | ON mcm.conversationid = mc.id | |
127 | WHERE mcm.userid != ? | |
128 | AND m.id = ?"; | |
129 | $themessage = $DB->get_record_sql($sql, [$USER->id, $sentmessage['msgid']]); | |
130 | ||
131 | // Confirm that the message was inserted correctly. | |
132 | $this->assertEquals($themessage->useridfrom, $user1->id); | |
133 | $this->assertEquals($themessage->useridto, $message1['touserid']); | |
134 | $this->assertEquals($themessage->smallmessage, $message1['text']); | |
135 | $this->assertEquals($sentmessage['clientmsgid'], $message1['clientmsgid']); | |
136 | } | |
137 | ||
138 | /** | |
139 | * Test send_instant_messages to a user who has blocked you. | |
140 | */ | |
141 | public function test_send_instant_messages_blocked_user() { | |
142 | global $DB; | |
143 | ||
144 | $this->resetAfterTest(); | |
145 | ||
146 | // Transactions used in tests, tell phpunit use alternative reset method. | |
147 | $this->preventResetByRollback(); | |
e6432668 JM |
148 | |
149 | $user1 = self::getDataGenerator()->create_user(); | |
f219eac7 MN |
150 | $user2 = self::getDataGenerator()->create_user(); |
151 | ||
152 | $this->setUser($user1); | |
153 | ||
154 | \core_message\api::block_user($user2->id, $user1->id); | |
e6432668 JM |
155 | |
156 | // Create test message data. | |
157 | $message1 = array(); | |
f219eac7 | 158 | $message1['touserid'] = $user2->id; |
e6432668 JM |
159 | $message1['text'] = 'the message.'; |
160 | $message1['clientmsgid'] = 4; | |
161 | $messages = array($message1); | |
162 | ||
163 | $sentmessages = core_message_external::send_instant_messages($messages); | |
f219eac7 | 164 | $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); |
e6432668 | 165 | |
f219eac7 MN |
166 | $sentmessage = reset($sentmessages); |
167 | ||
168 | $this->assertEquals(get_string('userisblockingyou', 'message'), $sentmessage['errormessage']); | |
169 | ||
170 | $this->assertEquals(0, $DB->count_records('messages')); | |
171 | } | |
172 | ||
173 | /** | |
174 | * Test send_instant_messages when sending a message to a non-contact who has blocked non-contacts. | |
175 | */ | |
176 | public function test_send_instant_messages_block_non_contacts() { | |
177 | global $DB; | |
178 | ||
179 | $this->resetAfterTest(true); | |
180 | ||
181 | // Transactions used in tests, tell phpunit use alternative reset method. | |
182 | $this->preventResetByRollback(); | |
183 | ||
184 | $user1 = self::getDataGenerator()->create_user(); | |
185 | $user2 = self::getDataGenerator()->create_user(); | |
186 | ||
187 | $this->setUser($user1); | |
188 | ||
189 | // Set the user preference so user 2 does not accept messages from non-contacts. | |
190 | set_user_preference('message_blocknoncontacts', 1, $user2); | |
191 | ||
192 | // Create test message data. | |
193 | $message1 = array(); | |
194 | $message1['touserid'] = $user2->id; | |
195 | $message1['text'] = 'the message.'; | |
196 | $message1['clientmsgid'] = 4; | |
197 | $messages = array($message1); | |
198 | ||
199 | $sentmessages = core_message_external::send_instant_messages($messages); | |
200 | $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); | |
201 | ||
202 | $sentmessage = reset($sentmessages); | |
203 | ||
204 | $this->assertEquals(get_string('userisblockingyounoncontact', 'message', fullname($user2)), $sentmessage['errormessage']); | |
205 | ||
206 | $this->assertEquals(0, $DB->count_records('messages')); | |
207 | } | |
208 | ||
209 | /** | |
210 | * Test send_instant_messages when sending a message to a contact who has blocked non-contacts. | |
211 | */ | |
212 | public function test_send_instant_messages_block_non_contacts_but_am_contact() { | |
213 | global $DB, $USER; | |
214 | ||
215 | $this->resetAfterTest(true); | |
216 | ||
217 | // Transactions used in tests, tell phpunit use alternative reset method. | |
218 | $this->preventResetByRollback(); | |
219 | ||
220 | $user1 = self::getDataGenerator()->create_user(); | |
221 | $user2 = self::getDataGenerator()->create_user(); | |
222 | ||
223 | $this->setUser($user1); | |
224 | ||
225 | // Set the user preference so user 2 does not accept messages from non-contacts. | |
226 | set_user_preference('message_blocknoncontacts', 1, $user2); | |
227 | ||
228 | \core_message\api::add_contact($user1->id, $user2->id); | |
229 | ||
230 | // Create test message data. | |
231 | $message1 = array(); | |
232 | $message1['touserid'] = $user2->id; | |
233 | $message1['text'] = 'the message.'; | |
234 | $message1['clientmsgid'] = 4; | |
235 | $messages = array($message1); | |
236 | ||
237 | $sentmessages = core_message_external::send_instant_messages($messages); | |
fb695f6e JM |
238 | $sentmessages = external_api::clean_returnvalue(core_message_external::send_instant_messages_returns(), $sentmessages); |
239 | ||
f219eac7 MN |
240 | $sentmessage = reset($sentmessages); |
241 | ||
883ce421 MN |
242 | $sql = "SELECT m.*, mcm.userid as useridto |
243 | FROM {messages} m | |
244 | INNER JOIN {message_conversations} mc | |
245 | ON m.conversationid = mc.id | |
246 | INNER JOIN {message_conversation_members} mcm | |
247 | ON mcm.conversationid = mc.id | |
248 | WHERE mcm.userid != ? | |
249 | AND m.id = ?"; | |
f219eac7 | 250 | $themessage = $DB->get_record_sql($sql, [$USER->id, $sentmessage['msgid']]); |
e6432668 JM |
251 | |
252 | // Confirm that the message was inserted correctly. | |
f219eac7 | 253 | $this->assertEquals($themessage->useridfrom, $user1->id); |
e6432668 JM |
254 | $this->assertEquals($themessage->useridto, $message1['touserid']); |
255 | $this->assertEquals($themessage->smallmessage, $message1['text']); | |
f219eac7 MN |
256 | $this->assertEquals($sentmessage['clientmsgid'], $message1['clientmsgid']); |
257 | } | |
258 | ||
259 | /** | |
260 | * Test send_instant_messages with no capabilities | |
261 | */ | |
262 | public function test_send_instant_messages_no_capability() { | |
263 | global $DB; | |
264 | ||
265 | $this->resetAfterTest(true); | |
266 | ||
267 | // Transactions used in tests, tell phpunit use alternative reset method. | |
268 | $this->preventResetByRollback(); | |
269 | ||
270 | $user1 = self::getDataGenerator()->create_user(); | |
271 | $user2 = self::getDataGenerator()->create_user(); | |
272 | ||
273 | $this->setUser($user1); | |
274 | ||
275 | // Unset the required capabilities by the external function. | |
276 | $contextid = context_system::instance()->id; | |
277 | $userrole = $DB->get_record('role', array('shortname' => 'user')); | |
278 | $this->unassignUserCapability('moodle/site:sendmessage', $contextid, $userrole->id); | |
279 | ||
280 | // Create test message data. | |
281 | $message1 = array(); | |
282 | $message1['touserid'] = $user2->id; | |
283 | $message1['text'] = 'the message.'; | |
284 | $message1['clientmsgid'] = 4; | |
285 | $messages = array($message1); | |
286 | ||
287 | $this->expectException('required_capability_exception'); | |
288 | core_message_external::send_instant_messages($messages); | |
289 | } | |
290 | ||
291 | /** | |
292 | * Test send_instant_messages when messaging is disabled. | |
293 | */ | |
294 | public function test_send_instant_messages_messaging_disabled() { | |
295 | global $CFG; | |
296 | ||
297 | $this->resetAfterTest(true); | |
298 | ||
299 | // Transactions used in tests, tell phpunit use alternative reset method. | |
300 | $this->preventResetByRollback(); | |
301 | ||
302 | $user1 = self::getDataGenerator()->create_user(); | |
303 | $user2 = self::getDataGenerator()->create_user(); | |
304 | ||
305 | $this->setUser($user1); | |
306 | ||
307 | // Disable messaging. | |
308 | $CFG->messaging = 0; | |
309 | ||
310 | // Create test message data. | |
311 | $message1 = array(); | |
312 | $message1['touserid'] = $user2->id; | |
313 | $message1['text'] = 'the message.'; | |
314 | $message1['clientmsgid'] = 4; | |
315 | $messages = array($message1); | |
316 | ||
317 | $this->expectException('moodle_exception'); | |
318 | core_message_external::send_instant_messages($messages); | |
e6432668 | 319 | } |
d6731600 FM |
320 | |
321 | /** | |
322 | * Test create_contacts. | |
323 | */ | |
324 | public function test_create_contacts() { | |
325 | $this->resetAfterTest(true); | |
326 | ||
327 | $user1 = self::getDataGenerator()->create_user(); | |
328 | $user2 = self::getDataGenerator()->create_user(); | |
329 | $user3 = self::getDataGenerator()->create_user(); | |
330 | $user4 = self::getDataGenerator()->create_user(); | |
331 | $user5 = self::getDataGenerator()->create_user(); | |
332 | $this->setUser($user1); | |
333 | ||
334 | // Adding a contact. | |
335 | $return = core_message_external::create_contacts(array($user2->id)); | |
f219eac7 | 336 | $this->assertDebuggingCalled(); |
d6731600 FM |
337 | $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return); |
338 | $this->assertEquals(array(), $return); | |
339 | ||
340 | // Adding a contact who is already a contact. | |
341 | $return = core_message_external::create_contacts(array($user2->id)); | |
f219eac7 | 342 | $this->assertDebuggingCalled(); |
d6731600 FM |
343 | $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return); |
344 | $this->assertEquals(array(), $return); | |
345 | ||
346 | // Adding multiple contacts. | |
347 | $return = core_message_external::create_contacts(array($user3->id, $user4->id)); | |
f219eac7 | 348 | $this->assertDebuggingCalledCount(2); |
d6731600 FM |
349 | $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return); |
350 | $this->assertEquals(array(), $return); | |
351 | ||
352 | // Adding a non-existing user. | |
353 | $return = core_message_external::create_contacts(array(99999)); | |
f219eac7 | 354 | $this->assertDebuggingCalled(); |
d6731600 FM |
355 | $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return); |
356 | $this->assertCount(1, $return); | |
357 | $return = array_pop($return); | |
358 | $this->assertEquals($return['warningcode'], 'contactnotcreated'); | |
359 | $this->assertEquals($return['itemid'], 99999); | |
360 | ||
361 | // Adding contacts with valid and invalid parameters. | |
362 | $return = core_message_external::create_contacts(array($user5->id, 99999)); | |
f219eac7 | 363 | $this->assertDebuggingCalledCount(2); |
d6731600 FM |
364 | $return = external_api::clean_returnvalue(core_message_external::create_contacts_returns(), $return); |
365 | $this->assertCount(1, $return); | |
366 | $return = array_pop($return); | |
367 | $this->assertEquals($return['warningcode'], 'contactnotcreated'); | |
368 | $this->assertEquals($return['itemid'], 99999); | |
343ba16c SL |
369 | |
370 | // Try to add a contact to another user, should throw an exception. | |
371 | // All assertions must be added before this point. | |
372 | $this->expectException('required_capability_exception'); | |
373 | core_message_external::create_contacts(array($user2->id), $user3->id); | |
d6731600 FM |
374 | } |
375 | ||
376 | /** | |
377 | * Test delete_contacts. | |
378 | */ | |
379 | public function test_delete_contacts() { | |
380 | $this->resetAfterTest(true); | |
381 | ||
382 | $user1 = self::getDataGenerator()->create_user(); | |
383 | $user2 = self::getDataGenerator()->create_user(); | |
384 | $user3 = self::getDataGenerator()->create_user(); | |
385 | $user4 = self::getDataGenerator()->create_user(); | |
386 | $user5 = self::getDataGenerator()->create_user(); | |
387 | $user6 = self::getDataGenerator()->create_user(); | |
388 | $this->setUser($user1); | |
f219eac7 MN |
389 | |
390 | \core_message\api::add_contact($user1->id, $user3->id); | |
391 | \core_message\api::add_contact($user1->id, $user4->id); | |
392 | \core_message\api::add_contact($user1->id, $user5->id); | |
393 | \core_message\api::add_contact($user1->id, $user6->id); | |
d6731600 FM |
394 | |
395 | // Removing a non-contact. | |
396 | $return = core_message_external::delete_contacts(array($user2->id)); | |
397 | $this->assertNull($return); | |
398 | ||
399 | // Removing one contact. | |
400 | $return = core_message_external::delete_contacts(array($user3->id)); | |
401 | $this->assertNull($return); | |
402 | ||
403 | // Removing multiple contacts. | |
404 | $return = core_message_external::delete_contacts(array($user4->id, $user5->id)); | |
405 | $this->assertNull($return); | |
406 | ||
407 | // Removing contact from unexisting user. | |
408 | $return = core_message_external::delete_contacts(array(99999)); | |
409 | $this->assertNull($return); | |
410 | ||
411 | // Removing mixed valid and invalid data. | |
412 | $return = core_message_external::delete_contacts(array($user6->id, 99999)); | |
413 | $this->assertNull($return); | |
343ba16c SL |
414 | |
415 | // Try to delete a contact of another user contact list, should throw an exception. | |
416 | // All assertions must be added before this point. | |
417 | $this->expectException('required_capability_exception'); | |
418 | core_message_external::delete_contacts(array($user2->id), $user3->id); | |
d6731600 FM |
419 | } |
420 | ||
421 | /** | |
422 | * Test block_contacts. | |
423 | */ | |
424 | public function test_block_contacts() { | |
425 | $this->resetAfterTest(true); | |
426 | ||
427 | $user1 = self::getDataGenerator()->create_user(); | |
428 | $user2 = self::getDataGenerator()->create_user(); | |
429 | $user3 = self::getDataGenerator()->create_user(); | |
430 | $user4 = self::getDataGenerator()->create_user(); | |
431 | $user5 = self::getDataGenerator()->create_user(); | |
432 | $this->setUser($user1); | |
f219eac7 MN |
433 | |
434 | \core_message\api::add_contact($user1->id, $user3->id); | |
435 | \core_message\api::add_contact($user1->id, $user4->id); | |
436 | \core_message\api::add_contact($user1->id, $user5->id); | |
d6731600 FM |
437 | |
438 | // Blocking a contact. | |
439 | $return = core_message_external::block_contacts(array($user2->id)); | |
f219eac7 | 440 | $this->assertDebuggingCalled(); |
d6731600 FM |
441 | $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return); |
442 | $this->assertEquals(array(), $return); | |
443 | ||
444 | // Blocking a contact who is already a contact. | |
445 | $return = core_message_external::block_contacts(array($user2->id)); | |
f219eac7 | 446 | $this->assertDebuggingCalled(); |
d6731600 FM |
447 | $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return); |
448 | $this->assertEquals(array(), $return); | |
449 | ||
450 | // Blocking multiple contacts. | |
451 | $return = core_message_external::block_contacts(array($user3->id, $user4->id)); | |
f219eac7 | 452 | $this->assertDebuggingCalledCount(2); |
d6731600 FM |
453 | $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return); |
454 | $this->assertEquals(array(), $return); | |
455 | ||
456 | // Blocking a non-existing user. | |
457 | $return = core_message_external::block_contacts(array(99999)); | |
f219eac7 | 458 | $this->assertDebuggingCalled(); |
d6731600 FM |
459 | $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return); |
460 | $this->assertCount(1, $return); | |
461 | $return = array_pop($return); | |
462 | $this->assertEquals($return['warningcode'], 'contactnotblocked'); | |
463 | $this->assertEquals($return['itemid'], 99999); | |
464 | ||
465 | // Blocking contacts with valid and invalid parameters. | |
466 | $return = core_message_external::block_contacts(array($user5->id, 99999)); | |
f219eac7 | 467 | $this->assertDebuggingCalledCount(2); |
d6731600 FM |
468 | $return = external_api::clean_returnvalue(core_message_external::block_contacts_returns(), $return); |
469 | $this->assertCount(1, $return); | |
470 | $return = array_pop($return); | |
471 | $this->assertEquals($return['warningcode'], 'contactnotblocked'); | |
472 | $this->assertEquals($return['itemid'], 99999); | |
343ba16c SL |
473 | |
474 | // Try to block a contact of another user contact list, should throw an exception. | |
475 | // All assertions must be added before this point. | |
476 | $this->expectException('required_capability_exception'); | |
477 | core_message_external::block_contacts(array($user2->id), $user3->id); | |
d6731600 FM |
478 | } |
479 | ||
480 | /** | |
481 | * Test unblock_contacts. | |
482 | */ | |
483 | public function test_unblock_contacts() { | |
484 | $this->resetAfterTest(true); | |
485 | ||
486 | $user1 = self::getDataGenerator()->create_user(); | |
487 | $user2 = self::getDataGenerator()->create_user(); | |
488 | $user3 = self::getDataGenerator()->create_user(); | |
489 | $user4 = self::getDataGenerator()->create_user(); | |
490 | $user5 = self::getDataGenerator()->create_user(); | |
491 | $user6 = self::getDataGenerator()->create_user(); | |
492 | $this->setUser($user1); | |
f219eac7 MN |
493 | |
494 | \core_message\api::add_contact($user1->id, $user3->id); | |
495 | \core_message\api::add_contact($user1->id, $user4->id); | |
496 | \core_message\api::add_contact($user1->id, $user5->id); | |
497 | \core_message\api::add_contact($user1->id, $user6->id); | |
d6731600 FM |
498 | |
499 | // Removing a non-contact. | |
500 | $return = core_message_external::unblock_contacts(array($user2->id)); | |
f219eac7 | 501 | $this->assertDebuggingCalled(); |
d6731600 FM |
502 | $this->assertNull($return); |
503 | ||
504 | // Removing one contact. | |
505 | $return = core_message_external::unblock_contacts(array($user3->id)); | |
f219eac7 | 506 | $this->assertDebuggingCalled(); |
d6731600 FM |
507 | $this->assertNull($return); |
508 | ||
509 | // Removing multiple contacts. | |
510 | $return = core_message_external::unblock_contacts(array($user4->id, $user5->id)); | |
f219eac7 | 511 | $this->assertDebuggingCalledCount(2); |
d6731600 FM |
512 | $this->assertNull($return); |
513 | ||
514 | // Removing contact from unexisting user. | |
515 | $return = core_message_external::unblock_contacts(array(99999)); | |
f219eac7 | 516 | $this->assertDebuggingCalled(); |
d6731600 FM |
517 | $this->assertNull($return); |
518 | ||
519 | // Removing mixed valid and invalid data. | |
520 | $return = core_message_external::unblock_contacts(array($user6->id, 99999)); | |
f219eac7 | 521 | $this->assertDebuggingCalledCount(2); |
d6731600 FM |
522 | $this->assertNull($return); |
523 | ||
343ba16c SL |
524 | // Try to unblock a contact of another user contact list, should throw an exception. |
525 | // All assertions must be added before this point. | |
526 | $this->expectException('required_capability_exception'); | |
527 | core_message_external::unblock_contacts(array($user2->id), $user3->id); | |
f219eac7 | 528 | $this->assertDebuggingCalled(); |
d6731600 FM |
529 | } |
530 | ||
52284186 MN |
531 | /** |
532 | * Test getting contact requests. | |
533 | */ | |
534 | public function test_get_contact_requests() { | |
535 | $this->resetAfterTest(); | |
536 | ||
537 | $user1 = self::getDataGenerator()->create_user(); | |
538 | $user2 = self::getDataGenerator()->create_user(); | |
539 | $user3 = self::getDataGenerator()->create_user(); | |
540 | ||
541 | $this->setUser($user1); | |
542 | ||
543 | // Block one user, their request should not show up. | |
544 | \core_message\api::block_user($user1->id, $user3->id); | |
545 | ||
546 | \core_message\api::create_contact_request($user2->id, $user1->id); | |
547 | \core_message\api::create_contact_request($user3->id, $user1->id); | |
548 | ||
549 | $requests = core_message_external::get_contact_requests($user1->id); | |
550 | $requests = external_api::clean_returnvalue(core_message_external::get_contact_requests_returns(), $requests); | |
551 | ||
552 | $this->assertCount(1, $requests); | |
553 | ||
554 | $request = reset($requests); | |
555 | ||
556 | $this->assertEquals($user2->id, $request['id']); | |
557 | $this->assertEquals($user2->picture, $request['picture']); | |
558 | $this->assertEquals($user2->firstname, $request['firstname']); | |
559 | $this->assertEquals($user2->lastname, $request['lastname']); | |
560 | $this->assertEquals($user2->firstnamephonetic, $request['firstnamephonetic']); | |
561 | $this->assertEquals($user2->lastnamephonetic, $request['lastnamephonetic']); | |
562 | $this->assertEquals($user2->middlename, $request['middlename']); | |
563 | $this->assertEquals($user2->alternatename, $request['alternatename']); | |
564 | $this->assertEquals($user2->email, $request['email']); | |
565 | } | |
566 | ||
567 | /** | |
568 | * Test getting contact requests with messaging disabled. | |
569 | */ | |
570 | public function test_get_contact_requests_messaging_disabled() { | |
571 | global $CFG; | |
572 | ||
573 | $this->resetAfterTest(); | |
574 | ||
575 | // Create some skeleton data just so we can call the WS. | |
576 | $user1 = self::getDataGenerator()->create_user(); | |
577 | ||
578 | $this->setUser($user1); | |
579 | ||
580 | // Disable messaging. | |
581 | $CFG->messaging = 0; | |
582 | ||
583 | // Ensure an exception is thrown. | |
584 | $this->expectException('moodle_exception'); | |
585 | core_message_external::get_contact_requests($user1->id); | |
586 | } | |
587 | ||
588 | /** | |
589 | * Test getting contact requests with no permission. | |
590 | */ | |
591 | public function test_get_contact_requests_no_permission() { | |
592 | $this->resetAfterTest(); | |
593 | ||
594 | // Create some skeleton data just so we can call the WS. | |
595 | $user1 = self::getDataGenerator()->create_user(); | |
596 | $user2 = self::getDataGenerator()->create_user(); | |
597 | $user3 = self::getDataGenerator()->create_user(); | |
598 | ||
599 | $this->setUser($user3); | |
600 | ||
601 | // Ensure an exception is thrown. | |
602 | $this->expectException('required_capability_exception'); | |
603 | core_message_external::create_contact_request($user1->id, $user2->id); | |
604 | } | |
605 | ||
606 | /** | |
607 | * Test creating a contact request. | |
608 | */ | |
609 | public function test_create_contact_request() { | |
0d203bbf | 610 | global $CFG, $DB; |
52284186 MN |
611 | |
612 | $this->resetAfterTest(); | |
613 | ||
614 | $user1 = self::getDataGenerator()->create_user(); | |
615 | $user2 = self::getDataGenerator()->create_user(); | |
616 | ||
617 | $this->setUser($user1); | |
618 | ||
0d203bbf MN |
619 | // Allow users to message anyone site-wide. |
620 | $CFG->messagingallusers = 1; | |
621 | ||
52284186 MN |
622 | $return = core_message_external::create_contact_request($user1->id, $user2->id); |
623 | $return = external_api::clean_returnvalue(core_message_external::create_contact_request_returns(), $return); | |
624 | $this->assertEquals(array(), $return); | |
625 | ||
626 | $request = $DB->get_records('message_contact_requests'); | |
627 | ||
628 | $this->assertCount(1, $request); | |
629 | ||
630 | $request = reset($request); | |
631 | ||
632 | $this->assertEquals($user1->id, $request->userid); | |
633 | $this->assertEquals($user2->id, $request->requesteduserid); | |
634 | } | |
635 | ||
0d203bbf MN |
636 | /** |
637 | * Test creating a contact request when not allowed. | |
638 | */ | |
639 | public function test_create_contact_request_not_allowed() { | |
640 | global $CFG; | |
641 | ||
642 | $this->resetAfterTest(); | |
643 | ||
644 | $user1 = self::getDataGenerator()->create_user(); | |
645 | $user2 = self::getDataGenerator()->create_user(); | |
646 | ||
647 | $this->setUser($user1); | |
648 | ||
649 | $CFG->messagingallusers = 0; | |
650 | ||
651 | $return = core_message_external::create_contact_request($user1->id, $user2->id); | |
652 | $return = external_api::clean_returnvalue(core_message_external::create_contact_request_returns(), $return); | |
653 | ||
654 | $warning = reset($return); | |
655 | ||
656 | $this->assertEquals('user', $warning['item']); | |
657 | $this->assertEquals($user2->id, $warning['itemid']); | |
658 | $this->assertEquals('cannotcreatecontactrequest', $warning['warningcode']); | |
659 | $this->assertEquals('You are unable to create a contact request for this user', $warning['message']); | |
660 | } | |
661 | ||
52284186 MN |
662 | /** |
663 | * Test creating a contact request with messaging disabled. | |
664 | */ | |
665 | public function test_create_contact_request_messaging_disabled() { | |
666 | global $CFG; | |
667 | ||
668 | $this->resetAfterTest(); | |
669 | ||
670 | // Create some skeleton data just so we can call the WS. | |
671 | $user1 = self::getDataGenerator()->create_user(); | |
672 | $user2 = self::getDataGenerator()->create_user(); | |
673 | ||
674 | $this->setUser($user1); | |
675 | ||
676 | // Disable messaging. | |
677 | $CFG->messaging = 0; | |
678 | ||
679 | // Ensure an exception is thrown. | |
680 | $this->expectException('moodle_exception'); | |
681 | core_message_external::create_contact_request($user1->id, $user2->id); | |
682 | } | |
683 | ||
684 | /** | |
685 | * Test creating a contact request with no permission. | |
686 | */ | |
687 | public function test_create_contact_request_no_permission() { | |
688 | $this->resetAfterTest(); | |
689 | ||
690 | // Create some skeleton data just so we can call the WS. | |
691 | $user1 = self::getDataGenerator()->create_user(); | |
692 | $user2 = self::getDataGenerator()->create_user(); | |
693 | $user3 = self::getDataGenerator()->create_user(); | |
694 | ||
695 | $this->setUser($user3); | |
696 | ||
697 | // Ensure an exception is thrown. | |
698 | $this->expectException('required_capability_exception'); | |
699 | core_message_external::create_contact_request($user1->id, $user2->id); | |
700 | } | |
701 | ||
702 | /** | |
703 | * Test confirming a contact request. | |
704 | */ | |
705 | public function test_confirm_contact_request() { | |
706 | global $DB; | |
707 | ||
708 | $this->resetAfterTest(); | |
709 | ||
710 | $user1 = self::getDataGenerator()->create_user(); | |
711 | $user2 = self::getDataGenerator()->create_user(); | |
712 | ||
713 | $this->setUser($user1); | |
714 | ||
715 | \core_message\api::create_contact_request($user1->id, $user2->id); | |
716 | ||
717 | $this->setUser($user2); | |
718 | ||
719 | $return = core_message_external::confirm_contact_request($user1->id, $user2->id); | |
720 | $return = external_api::clean_returnvalue(core_message_external::confirm_contact_request_returns(), $return); | |
721 | $this->assertEquals(array(), $return); | |
722 | ||
723 | $this->assertEquals(0, $DB->count_records('message_contact_requests')); | |
724 | ||
725 | $contact = $DB->get_records('message_contacts'); | |
726 | ||
727 | $this->assertCount(1, $contact); | |
728 | ||
729 | $contact = reset($contact); | |
730 | ||
731 | $this->assertEquals($user1->id, $contact->userid); | |
732 | $this->assertEquals($user2->id, $contact->contactid); | |
733 | } | |
734 | ||
735 | /** | |
736 | * Test confirming a contact request with messaging disabled. | |
737 | */ | |
738 | public function test_confirm_contact_request_messaging_disabled() { | |
739 | global $CFG; | |
740 | ||
741 | $this->resetAfterTest(); | |
742 | ||
743 | // Create some skeleton data just so we can call the WS. | |
744 | $user1 = self::getDataGenerator()->create_user(); | |
745 | $user2 = self::getDataGenerator()->create_user(); | |
746 | ||
747 | $this->setUser($user1); | |
748 | ||
749 | // Disable messaging. | |
750 | $CFG->messaging = 0; | |
751 | ||
752 | // Ensure an exception is thrown. | |
753 | $this->expectException('moodle_exception'); | |
754 | core_message_external::confirm_contact_request($user1->id, $user2->id); | |
755 | } | |
756 | ||
757 | /** | |
758 | * Test confirming a contact request with no permission. | |
759 | */ | |
760 | public function test_confirm_contact_request_no_permission() { | |
761 | $this->resetAfterTest(); | |
762 | ||
763 | // Create some skeleton data just so we can call the WS. | |
764 | $user1 = self::getDataGenerator()->create_user(); | |
765 | $user2 = self::getDataGenerator()->create_user(); | |
766 | $user3 = self::getDataGenerator()->create_user(); | |
767 | ||
768 | $this->setUser($user3); | |
769 | ||
770 | // Ensure an exception is thrown. | |
771 | $this->expectException('required_capability_exception'); | |
772 | core_message_external::confirm_contact_request($user1->id, $user2->id); | |
773 | } | |
774 | ||
775 | /** | |
776 | * Test declining a contact request. | |
777 | */ | |
778 | public function test_decline_contact_request() { | |
779 | global $DB; | |
780 | ||
781 | $this->resetAfterTest(); | |
782 | ||
783 | $user1 = self::getDataGenerator()->create_user(); | |
784 | $user2 = self::getDataGenerator()->create_user(); | |
785 | ||
786 | $this->setUser($user1); | |
787 | ||
788 | \core_message\api::create_contact_request($user1->id, $user2->id); | |
789 | ||
790 | $this->setUser($user2); | |
791 | ||
792 | $return = core_message_external::decline_contact_request($user1->id, $user2->id); | |
793 | $return = external_api::clean_returnvalue(core_message_external::decline_contact_request_returns(), $return); | |
794 | $this->assertEquals(array(), $return); | |
795 | ||
796 | $this->assertEquals(0, $DB->count_records('message_contact_requests')); | |
797 | $this->assertEquals(0, $DB->count_records('message_contacts')); | |
798 | } | |
799 | ||
800 | /** | |
801 | * Test declining a contact request with messaging disabled. | |
802 | */ | |
803 | public function test_decline_contact_request_messaging_disabled() { | |
804 | global $CFG; | |
805 | ||
806 | $this->resetAfterTest(); | |
807 | ||
808 | // Create some skeleton data just so we can call the WS. | |
809 | $user1 = self::getDataGenerator()->create_user(); | |
810 | $user2 = self::getDataGenerator()->create_user(); | |
811 | ||
812 | $this->setUser($user1); | |
813 | ||
814 | // Disable messaging. | |
815 | $CFG->messaging = 0; | |
816 | ||
817 | // Ensure an exception is thrown. | |
818 | $this->expectException('moodle_exception'); | |
819 | core_message_external::decline_contact_request($user1->id, $user2->id); | |
820 | } | |
821 | ||
822 | /** | |
823 | * Test declining a contact request with no permission. | |
824 | */ | |
825 | public function test_decline_contact_request_no_permission() { | |
826 | $this->resetAfterTest(); | |
827 | ||
828 | // Create some skeleton data just so we can call the WS. | |
829 | $user1 = self::getDataGenerator()->create_user(); | |
830 | $user2 = self::getDataGenerator()->create_user(); | |
831 | $user3 = self::getDataGenerator()->create_user(); | |
832 | ||
833 | $this->setUser($user3); | |
834 | ||
835 | // Ensure an exception is thrown. | |
836 | $this->expectException('required_capability_exception'); | |
837 | core_message_external::decline_contact_request($user1->id, $user2->id); | |
838 | } | |
839 | ||
840 | /** | |
841 | * Test blocking a user. | |
842 | */ | |
843 | public function test_block_user() { | |
844 | global $DB; | |
845 | ||
846 | $this->resetAfterTest(true); | |
847 | ||
848 | $user1 = self::getDataGenerator()->create_user(); | |
849 | $user2 = self::getDataGenerator()->create_user(); | |
850 | ||
851 | $this->setUser($user1); | |
852 | ||
853 | // Blocking a user. | |
854 | $return = core_message_external::block_user($user1->id, $user2->id); | |
855 | $return = external_api::clean_returnvalue(core_message_external::block_user_returns(), $return); | |
856 | $this->assertEquals(array(), $return); | |
857 | ||
858 | // Get list of blocked users. | |
859 | $record = $DB->get_record('message_users_blocked', []); | |
860 | ||
861 | $this->assertEquals($user1->id, $record->userid); | |
862 | $this->assertEquals($user2->id, $record->blockeduserid); | |
863 | ||
864 | // Blocking a user who is already blocked. | |
865 | $return = core_message_external::block_user($user1->id, $user2->id); | |
866 | $return = external_api::clean_returnvalue(core_message_external::block_user_returns(), $return); | |
867 | $this->assertEquals(array(), $return); | |
868 | ||
869 | $this->assertEquals(1, $DB->count_records('message_users_blocked')); | |
870 | } | |
871 | ||
872 | /** | |
873 | * Test blocking a user with messaging disabled. | |
874 | */ | |
875 | public function test_block_user_messaging_disabled() { | |
876 | global $CFG; | |
877 | ||
878 | $this->resetAfterTest(); | |
879 | ||
880 | // Create some skeleton data just so we can call the WS. | |
881 | $user1 = self::getDataGenerator()->create_user(); | |
882 | $user2 = self::getDataGenerator()->create_user(); | |
883 | ||
884 | $this->setUser($user1); | |
885 | ||
886 | // Disable messaging. | |
887 | $CFG->messaging = 0; | |
888 | ||
889 | // Ensure an exception is thrown. | |
890 | $this->expectException('moodle_exception'); | |
891 | core_message_external::block_user($user1->id, $user2->id); | |
892 | } | |
893 | ||
894 | /** | |
895 | * Test blocking a user with no permission. | |
896 | */ | |
897 | public function test_block_user_no_permission() { | |
898 | $this->resetAfterTest(); | |
899 | ||
900 | // Create some skeleton data just so we can call the WS. | |
901 | $user1 = self::getDataGenerator()->create_user(); | |
902 | $user2 = self::getDataGenerator()->create_user(); | |
903 | $user3 = self::getDataGenerator()->create_user(); | |
904 | ||
905 | $this->setUser($user3); | |
906 | ||
907 | // Ensure an exception is thrown. | |
908 | $this->expectException('required_capability_exception'); | |
909 | core_message_external::block_user($user1->id, $user2->id); | |
910 | } | |
911 | ||
912 | /** | |
913 | * Test unblocking a user. | |
914 | */ | |
915 | public function test_unblock_user() { | |
916 | global $DB; | |
917 | ||
918 | $this->resetAfterTest(true); | |
919 | ||
920 | $user1 = self::getDataGenerator()->create_user(); | |
921 | $user2 = self::getDataGenerator()->create_user(); | |
922 | ||
923 | $this->setUser($user1); | |
924 | ||
925 | // Block the user. | |
926 | \core_message\api::block_user($user1->id, $user2->id); | |
927 | ||
928 | // Unblocking a user. | |
929 | $return = core_message_external::unblock_user($user1->id, $user2->id); | |
930 | $return = external_api::clean_returnvalue(core_message_external::unblock_user_returns(), $return); | |
931 | $this->assertEquals(array(), $return); | |
932 | ||
933 | $this->assertEquals(0, $DB->count_records('message_users_blocked')); | |
934 | ||
935 | // Unblocking a user who is already unblocked. | |
936 | $return = core_message_external::unblock_user($user1->id, $user2->id); | |
937 | $return = external_api::clean_returnvalue(core_message_external::unblock_user_returns(), $return); | |
938 | $this->assertEquals(array(), $return); | |
939 | ||
940 | $this->assertEquals(0, $DB->count_records('message_users_blocked')); | |
941 | } | |
942 | ||
943 | /** | |
944 | * Test unblocking a user with messaging disabled. | |
945 | */ | |
946 | public function test_unblock_user_messaging_disabled() { | |
947 | global $CFG; | |
948 | ||
949 | $this->resetAfterTest(); | |
950 | ||
951 | // Create some skeleton data just so we can call the WS. | |
952 | $user1 = self::getDataGenerator()->create_user(); | |
953 | $user2 = self::getDataGenerator()->create_user(); | |
954 | ||
955 | $this->setUser($user1); | |
956 | ||
957 | // Disable messaging. | |
958 | $CFG->messaging = 0; | |
959 | ||
960 | // Ensure an exception is thrown. | |
961 | $this->expectException('moodle_exception'); | |
962 | core_message_external::unblock_user($user1->id, $user2->id); | |
963 | } | |
964 | ||
965 | /** | |
966 | * Test unblocking a user with no permission. | |
967 | */ | |
968 | public function test_unblock_user_no_permission() { | |
969 | $this->resetAfterTest(); | |
970 | ||
971 | // Create some skeleton data just so we can call the WS. | |
972 | $user1 = self::getDataGenerator()->create_user(); | |
973 | $user2 = self::getDataGenerator()->create_user(); | |
974 | $user3 = self::getDataGenerator()->create_user(); | |
975 | ||
976 | $this->setUser($user3); | |
977 | ||
978 | // Ensure an exception is thrown. | |
979 | $this->expectException('required_capability_exception'); | |
980 | core_message_external::unblock_user($user1->id, $user2->id); | |
981 | } | |
982 | ||
d6731600 FM |
983 | /** |
984 | * Test get_contacts. | |
985 | */ | |
986 | public function test_get_contacts() { | |
987 | $this->resetAfterTest(true); | |
988 | ||
989 | $user1 = self::getDataGenerator()->create_user(); | |
990 | $user_stranger = self::getDataGenerator()->create_user(); | |
991 | $user_offline1 = self::getDataGenerator()->create_user(); | |
992 | $user_offline2 = self::getDataGenerator()->create_user(); | |
993 | $user_offline3 = self::getDataGenerator()->create_user(); | |
994 | $user_online = new stdClass(); | |
995 | $user_online->lastaccess = time(); | |
996 | $user_online = self::getDataGenerator()->create_user($user_online); | |
997 | $user_blocked = self::getDataGenerator()->create_user(); | |
0b074e88 | 998 | $noreplyuser = core_user::get_user(core_user::NOREPLY_USER); |
d6731600 FM |
999 | |
1000 | // Login as user1. | |
1001 | $this->setUser($user1); | |
f219eac7 MN |
1002 | \core_message\api::add_contact($user1->id, $user_offline1->id); |
1003 | \core_message\api::add_contact($user1->id, $user_offline2->id); | |
1004 | \core_message\api::add_contact($user1->id, $user_offline3->id); | |
1005 | \core_message\api::add_contact($user1->id, $user_online->id); | |
d6731600 FM |
1006 | |
1007 | // User_stranger sends a couple of messages to user1. | |
1008 | $this->send_message($user_stranger, $user1, 'Hello there!'); | |
1009 | $this->send_message($user_stranger, $user1, 'How you goin?'); | |
1010 | $this->send_message($user_stranger, $user1, 'Cya!'); | |
0b074e88 | 1011 | $this->send_message($noreplyuser, $user1, 'I am not a real user'); |
d6731600 FM |
1012 | |
1013 | // User_blocked sends a message to user1. | |
1014 | $this->send_message($user_blocked, $user1, 'Here, have some spam.'); | |
1015 | ||
1016 | // Retrieve the contacts of the user. | |
1017 | $this->setUser($user1); | |
1018 | $contacts = core_message_external::get_contacts(); | |
1019 | $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts); | |
1020 | $this->assertCount(3, $contacts['offline']); | |
1021 | $this->assertCount(1, $contacts['online']); | |
0b074e88 | 1022 | $this->assertCount(3, $contacts['strangers']); |
d6731600 | 1023 | core_message_external::block_contacts(array($user_blocked->id)); |
f219eac7 | 1024 | $this->assertDebuggingCalled(); |
d6731600 FM |
1025 | $contacts = core_message_external::get_contacts(); |
1026 | $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts); | |
1027 | $this->assertCount(3, $contacts['offline']); | |
1028 | $this->assertCount(1, $contacts['online']); | |
0b074e88 | 1029 | $this->assertCount(2, $contacts['strangers']); |
d6731600 FM |
1030 | |
1031 | // Checking some of the fields returned. | |
1032 | $stranger = array_pop($contacts['strangers']); | |
01393790 | 1033 | |
0b074e88 JL |
1034 | $this->assertEquals(core_user::NOREPLY_USER, $stranger['id']); |
1035 | $this->assertEquals(1, $stranger['unread']); | |
01393790 JL |
1036 | |
1037 | // Check that deleted users are not returned. | |
1038 | delete_user($user_offline1); | |
1039 | delete_user($user_stranger); | |
1040 | delete_user($user_online); | |
1041 | $contacts = core_message_external::get_contacts(); | |
1042 | $contacts = external_api::clean_returnvalue(core_message_external::get_contacts_returns(), $contacts); | |
1043 | $this->assertCount(2, $contacts['offline']); | |
1044 | $this->assertCount(0, $contacts['online']); | |
1045 | $this->assertCount(1, $contacts['strangers']); | |
d6731600 FM |
1046 | } |
1047 | ||
1048 | /** | |
1049 | * Test search_contacts. | |
52f3e060 | 1050 | * @expectedException moodle_exception |
d6731600 FM |
1051 | */ |
1052 | public function test_search_contacts() { | |
1053 | global $DB; | |
1054 | $this->resetAfterTest(true); | |
1055 | ||
1056 | $course1 = $this->getDataGenerator()->create_course(); | |
1057 | $course2 = $this->getDataGenerator()->create_course(); | |
1058 | ||
1059 | $user1 = new stdClass(); | |
1060 | $user1->firstname = 'X'; | |
1061 | $user1->lastname = 'X'; | |
1062 | $user1 = $this->getDataGenerator()->create_user($user1); | |
1063 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id); | |
1064 | $this->getDataGenerator()->enrol_user($user1->id, $course2->id); | |
1065 | ||
1066 | $user2 = new stdClass(); | |
1067 | $user2->firstname = 'Eric'; | |
1068 | $user2->lastname = 'Cartman'; | |
1069 | $user2 = self::getDataGenerator()->create_user($user2); | |
1070 | $user3 = new stdClass(); | |
1071 | $user3->firstname = 'Stan'; | |
1072 | $user3->lastname = 'Marsh'; | |
1073 | $user3 = self::getDataGenerator()->create_user($user3); | |
1074 | self::getDataGenerator()->enrol_user($user3->id, $course1->id); | |
1075 | $user4 = new stdClass(); | |
1076 | $user4->firstname = 'Kyle'; | |
1077 | $user4->lastname = 'Broflovski'; | |
1078 | $user4 = self::getDataGenerator()->create_user($user4); | |
1079 | $user5 = new stdClass(); | |
1080 | $user5->firstname = 'Kenny'; | |
1081 | $user5->lastname = 'McCormick'; | |
1082 | $user5 = self::getDataGenerator()->create_user($user5); | |
1083 | self::getDataGenerator()->enrol_user($user5->id, $course2->id); | |
1084 | ||
d6731600 | 1085 | $this->setUser($user1); |
2e2d1977 | 1086 | |
d6731600 FM |
1087 | $results = core_message_external::search_contacts('r'); |
1088 | $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); | |
2e2d1977 AD |
1089 | $this->assertCount(5, $results); // Users 2 through 5 + admin |
1090 | ||
d6731600 FM |
1091 | $results = core_message_external::search_contacts('r', true); |
1092 | $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); | |
1093 | $this->assertCount(2, $results); | |
2e2d1977 | 1094 | |
d6731600 FM |
1095 | $results = core_message_external::search_contacts('Kyle', false); |
1096 | $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); | |
1097 | $this->assertCount(1, $results); | |
1098 | $result = reset($results); | |
1099 | $this->assertEquals($user4->id, $result['id']); | |
2e2d1977 | 1100 | |
d6731600 FM |
1101 | $results = core_message_external::search_contacts('y', false); |
1102 | $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); | |
1103 | $this->assertCount(2, $results); | |
2e2d1977 | 1104 | |
d6731600 FM |
1105 | $results = core_message_external::search_contacts('y', true); |
1106 | $results = external_api::clean_returnvalue(core_message_external::search_contacts_returns(), $results); | |
1107 | $this->assertCount(1, $results); | |
1108 | $result = reset($results); | |
1109 | $this->assertEquals($user5->id, $result['id']); | |
1110 | ||
1111 | // Empty query, will throw an exception. | |
d6731600 FM |
1112 | $results = core_message_external::search_contacts(''); |
1113 | } | |
6ff4464b JL |
1114 | |
1115 | /** | |
1116 | * Test get_messages. | |
1117 | */ | |
1118 | public function test_get_messages() { | |
ea21d637 | 1119 | global $CFG, $DB; |
6ff4464b JL |
1120 | $this->resetAfterTest(true); |
1121 | ||
1122 | $this->preventResetByRollback(); | |
1123 | // This mark the messages as read!. | |
1124 | $sink = $this->redirectMessages(); | |
1125 | ||
1126 | $user1 = self::getDataGenerator()->create_user(); | |
1127 | $user2 = self::getDataGenerator()->create_user(); | |
1128 | $user3 = self::getDataGenerator()->create_user(); | |
1129 | ||
1130 | $course = self::getDataGenerator()->create_course(); | |
1131 | ||
1132 | // Send a message from one user to another. | |
1133 | message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE); | |
1134 | message_post_message($user1, $user3, 'some random text 2', FORMAT_MOODLE); | |
1135 | message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE); | |
1136 | message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE); | |
1137 | message_post_message($user3, $user1, 'some random text 5', FORMAT_MOODLE); | |
1138 | ||
1139 | $this->setUser($user1); | |
1140 | // Get read conversations from user1 to user2. | |
1141 | $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0); | |
1142 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1143 | $this->assertCount(1, $messages['messages']); | |
1144 | ||
ea21d637 JL |
1145 | // Delete the message. |
1146 | $message = array_shift($messages['messages']); | |
883ce421 | 1147 | \core_message\api::delete_message($user1->id, $message['id']); |
ea21d637 JL |
1148 | |
1149 | $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', true, true, 0, 0); | |
1150 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1151 | $this->assertCount(0, $messages['messages']); | |
1152 | ||
6ff4464b JL |
1153 | // Get unread conversations from user1 to user2. |
1154 | $messages = core_message_external::get_messages($user2->id, $user1->id, 'conversations', false, true, 0, 0); | |
1155 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1156 | $this->assertCount(0, $messages['messages']); | |
1157 | ||
1158 | // Get read messages send from user1. | |
1159 | $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0); | |
1160 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
ea21d637 | 1161 | $this->assertCount(1, $messages['messages']); |
6ff4464b JL |
1162 | |
1163 | $this->setUser($user2); | |
1164 | // Get read conversations from any user to user2. | |
1165 | $messages = core_message_external::get_messages($user2->id, 0, 'conversations', true, true, 0, 0); | |
1166 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1167 | $this->assertCount(2, $messages['messages']); | |
1168 | ||
ea21d637 JL |
1169 | // Conversations from user3 to user2. |
1170 | $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0); | |
1171 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1172 | $this->assertCount(1, $messages['messages']); | |
1173 | ||
1174 | // Delete the message. | |
1175 | $message = array_shift($messages['messages']); | |
883ce421 | 1176 | \core_message\api::delete_message($user2->id, $message['id']); |
ea21d637 JL |
1177 | |
1178 | $messages = core_message_external::get_messages($user2->id, $user3->id, 'conversations', true, true, 0, 0); | |
1179 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1180 | $this->assertCount(0, $messages['messages']); | |
1181 | ||
6ff4464b JL |
1182 | $this->setUser($user3); |
1183 | // Get read notifications received by user3. | |
1184 | $messages = core_message_external::get_messages($user3->id, 0, 'notifications', true, true, 0, 0); | |
1185 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1186 | $this->assertCount(0, $messages['messages']); | |
1187 | ||
1188 | // Now, create some notifications... | |
1189 | // We are creating fake notifications but based on real ones. | |
1190 | ||
39d2c688 | 1191 | // This one comes from a disabled plugin's provider and therefore is not sent. |
cc350fd9 AD |
1192 | $eventdata = new \core\message\message(); |
1193 | $eventdata->courseid = $course->id; | |
39d2c688 | 1194 | $eventdata->notification = 1; |
6ff4464b JL |
1195 | $eventdata->modulename = 'moodle'; |
1196 | $eventdata->component = 'enrol_paypal'; | |
1197 | $eventdata->name = 'paypal_enrolment'; | |
1198 | $eventdata->userfrom = get_admin(); | |
1199 | $eventdata->userto = $user1; | |
1200 | $eventdata->subject = "Moodle: PayPal payment"; | |
1201 | $eventdata->fullmessage = "Your PayPal payment is pending."; | |
1202 | $eventdata->fullmessageformat = FORMAT_PLAIN; | |
1203 | $eventdata->fullmessagehtml = ''; | |
1204 | $eventdata->smallmessage = ''; | |
1205 | message_send($eventdata); | |
39d2c688 DM |
1206 | $this->assertDebuggingCalled('Attempt to send msg from a provider enrol_paypal/paypal_enrolment '. |
1207 | 'that is inactive or not allowed for the user id='.$user1->id); | |
1208 | ||
1209 | // This one omits notification = 1. | |
1210 | $message = new \core\message\message(); | |
1211 | $message->courseid = $course->id; | |
1212 | $message->component = 'enrol_manual'; | |
1213 | $message->name = 'expiry_notification'; | |
1214 | $message->userfrom = $user2; | |
1215 | $message->userto = $user1; | |
1216 | $message->subject = 'Test: This is not a notification but otherwise is valid'; | |
1217 | $message->fullmessage = 'Test: Full message'; | |
1218 | $message->fullmessageformat = FORMAT_MARKDOWN; | |
1219 | $message->fullmessagehtml = markdown_to_html($message->fullmessage); | |
1220 | $message->smallmessage = $message->subject; | |
1221 | $message->contexturlname = $course->fullname; | |
1222 | $message->contexturl = (string)new moodle_url('/course/view.php', array('id' => $course->id)); | |
1223 | message_send($message); | |
6ff4464b | 1224 | |
cc350fd9 | 1225 | $message = new \core\message\message(); |
880fc15b | 1226 | $message->courseid = $course->id; |
6ff4464b JL |
1227 | $message->notification = 1; |
1228 | $message->component = 'enrol_manual'; | |
1229 | $message->name = 'expiry_notification'; | |
1230 | $message->userfrom = $user2; | |
1231 | $message->userto = $user1; | |
1232 | $message->subject = 'Enrolment expired'; | |
1233 | $message->fullmessage = 'Enrolment expired blah blah blah'; | |
1234 | $message->fullmessageformat = FORMAT_MARKDOWN; | |
1235 | $message->fullmessagehtml = markdown_to_html($message->fullmessage); | |
1236 | $message->smallmessage = $message->subject; | |
1237 | $message->contexturlname = $course->fullname; | |
1238 | $message->contexturl = (string)new moodle_url('/course/view.php', array('id' => $course->id)); | |
1239 | message_send($message); | |
1240 | ||
1241 | $userfrom = core_user::get_noreply_user(); | |
1242 | $userfrom->maildisplay = true; | |
0e8b5160 EM |
1243 | $eventdata = new \core\message\message(); |
1244 | $eventdata->courseid = $course->id; | |
6ff4464b JL |
1245 | $eventdata->component = 'moodle'; |
1246 | $eventdata->name = 'badgecreatornotice'; | |
1247 | $eventdata->userfrom = $userfrom; | |
1248 | $eventdata->userto = $user1; | |
1249 | $eventdata->notification = 1; | |
1250 | $eventdata->subject = 'New badge'; | |
1251 | $eventdata->fullmessage = format_text_email($eventdata->subject, FORMAT_HTML); | |
1252 | $eventdata->fullmessageformat = FORMAT_PLAIN; | |
1253 | $eventdata->fullmessagehtml = $eventdata->subject; | |
1254 | $eventdata->smallmessage = $eventdata->subject; | |
1255 | message_send($eventdata); | |
1256 | ||
cc350fd9 AD |
1257 | $eventdata = new \core\message\message(); |
1258 | $eventdata->courseid = $course->id; | |
6ff4464b JL |
1259 | $eventdata->name = 'submission'; |
1260 | $eventdata->component = 'mod_feedback'; | |
1261 | $eventdata->userfrom = $user1; | |
1262 | $eventdata->userto = $user2; | |
1263 | $eventdata->subject = 'Feedback submitted'; | |
1264 | $eventdata->fullmessage = 'Feedback submitted from an user'; | |
1265 | $eventdata->fullmessageformat = FORMAT_PLAIN; | |
1266 | $eventdata->fullmessagehtml = '<strong>Feedback submitted</strong>'; | |
1267 | $eventdata->smallmessage = ''; | |
1268 | message_send($eventdata); | |
1269 | ||
1270 | $this->setUser($user1); | |
1271 | // Get read notifications from any user to user1. | |
1272 | $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 0); | |
1273 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1274 | $this->assertCount(3, $messages['messages']); | |
1275 | ||
1276 | // Get one read notifications from any user to user1. | |
1277 | $messages = core_message_external::get_messages($user1->id, 0, 'notifications', true, true, 0, 1); | |
1278 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1279 | $this->assertCount(1, $messages['messages']); | |
1280 | ||
1281 | // Get unread notifications from any user to user1. | |
1282 | $messages = core_message_external::get_messages($user1->id, 0, 'notifications', false, true, 0, 0); | |
1283 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1284 | $this->assertCount(0, $messages['messages']); | |
1285 | ||
1286 | // Get read both type of messages from any user to user1. | |
1287 | $messages = core_message_external::get_messages($user1->id, 0, 'both', true, true, 0, 0); | |
1288 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1289 | $this->assertCount(4, $messages['messages']); | |
1290 | ||
1291 | // Get read notifications from no-reply-user to user1. | |
1292 | $messages = core_message_external::get_messages($user1->id, $userfrom->id, 'notifications', true, true, 0, 0); | |
1293 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1294 | $this->assertCount(1, $messages['messages']); | |
1295 | ||
1296 | // Get notifications send by user1 to any user. | |
1297 | $messages = core_message_external::get_messages(0, $user1->id, 'notifications', true, true, 0, 0); | |
1298 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1299 | $this->assertCount(1, $messages['messages']); | |
1300 | ||
1301 | // Test warnings. | |
1302 | $CFG->messaging = 0; | |
1303 | ||
1304 | $messages = core_message_external::get_messages(0, $user1->id, 'both', true, true, 0, 0); | |
1305 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1306 | $this->assertCount(1, $messages['warnings']); | |
1307 | ||
1308 | // Test exceptions. | |
1309 | ||
1310 | // Messaging disabled. | |
1311 | try { | |
1312 | $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0); | |
1313 | $this->fail('Exception expected due messaging disabled.'); | |
1314 | } catch (moodle_exception $e) { | |
1315 | $this->assertEquals('disabled', $e->errorcode); | |
1316 | } | |
1317 | ||
1318 | $CFG->messaging = 1; | |
1319 | ||
1320 | // Invalid users. | |
1321 | try { | |
1322 | $messages = core_message_external::get_messages(0, 0, 'conversations', true, true, 0, 0); | |
1323 | $this->fail('Exception expected due invalid users.'); | |
1324 | } catch (moodle_exception $e) { | |
1325 | $this->assertEquals('accessdenied', $e->errorcode); | |
1326 | } | |
1327 | ||
1328 | // Invalid user ids. | |
1329 | try { | |
1330 | $messages = core_message_external::get_messages(2500, 0, 'conversations', true, true, 0, 0); | |
1331 | $this->fail('Exception expected due invalid users.'); | |
1332 | } catch (moodle_exception $e) { | |
1333 | $this->assertEquals('invaliduser', $e->errorcode); | |
1334 | } | |
1335 | ||
1336 | // Invalid users (permissions). | |
1337 | $this->setUser($user2); | |
1338 | try { | |
1339 | $messages = core_message_external::get_messages(0, $user1->id, 'conversations', true, true, 0, 0); | |
1340 | $this->fail('Exception expected due invalid user.'); | |
1341 | } catch (moodle_exception $e) { | |
1342 | $this->assertEquals('accessdenied', $e->errorcode); | |
1343 | } | |
1344 | ||
1345 | } | |
ff1f3739 | 1346 | |
c57fadcc MN |
1347 | /** |
1348 | * Test get_messages where we want all messages from a user, sent to any user. | |
1349 | */ | |
1350 | public function test_get_messages_useridto_all() { | |
1351 | $this->resetAfterTest(true); | |
1352 | ||
1353 | $user1 = self::getDataGenerator()->create_user(); | |
1354 | $user2 = self::getDataGenerator()->create_user(); | |
1355 | $user3 = self::getDataGenerator()->create_user(); | |
1356 | ||
1357 | $this->setUser($user1); | |
1358 | ||
1359 | // Send a message from user 1 to two other users. | |
1360 | $this->send_message($user1, $user2, 'some random text 1', 0, 1); | |
1361 | $this->send_message($user1, $user3, 'some random text 2', 0, 2); | |
1362 | ||
1363 | // Get messages sent from user 1. | |
1364 | $messages = core_message_external::get_messages(0, $user1->id, 'conversations', false, false, 0, 0); | |
1365 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1366 | ||
1367 | // Confirm the data is correct. | |
1368 | $messages = $messages['messages']; | |
1369 | $this->assertCount(2, $messages); | |
1370 | ||
1371 | $message1 = array_shift($messages); | |
1372 | $message2 = array_shift($messages); | |
1373 | ||
1374 | $this->assertEquals($user1->id, $message1['useridfrom']); | |
1375 | $this->assertEquals($user2->id, $message1['useridto']); | |
1376 | ||
1377 | $this->assertEquals($user1->id, $message2['useridfrom']); | |
1378 | $this->assertEquals($user3->id, $message2['useridto']); | |
1379 | } | |
1380 | ||
1381 | /** | |
1382 | * Test get_messages where we want all messages to a user, sent by any user. | |
1383 | */ | |
1384 | public function test_get_messages_useridfrom_all() { | |
1385 | $this->resetAfterTest(); | |
1386 | ||
1387 | $user1 = self::getDataGenerator()->create_user(); | |
1388 | $user2 = self::getDataGenerator()->create_user(); | |
1389 | $user3 = self::getDataGenerator()->create_user(); | |
1390 | ||
1391 | $this->setUser($user1); | |
1392 | ||
1393 | // Send a message to user 1 from two other users. | |
1394 | $this->send_message($user2, $user1, 'some random text 1', 0, 1); | |
1395 | $this->send_message($user3, $user1, 'some random text 2', 0, 2); | |
1396 | ||
1397 | // Get messages sent to user 1. | |
1398 | $messages = core_message_external::get_messages($user1->id, 0, 'conversations', false, false, 0, 0); | |
1399 | $messages = external_api::clean_returnvalue(core_message_external::get_messages_returns(), $messages); | |
1400 | ||
1401 | // Confirm the data is correct. | |
1402 | $messages = $messages['messages']; | |
1403 | $this->assertCount(2, $messages); | |
1404 | ||
1405 | $message1 = array_shift($messages); | |
1406 | $message2 = array_shift($messages); | |
1407 | ||
1408 | $this->assertEquals($user2->id, $message1['useridfrom']); | |
1409 | $this->assertEquals($user1->id, $message1['useridto']); | |
1410 | ||
1411 | $this->assertEquals($user3->id, $message2['useridfrom']); | |
1412 | $this->assertEquals($user1->id, $message2['useridto']); | |
1413 | } | |
1414 | ||
ff1f3739 JL |
1415 | /** |
1416 | * Test get_blocked_users. | |
1417 | */ | |
1418 | public function test_get_blocked_users() { | |
1419 | $this->resetAfterTest(true); | |
1420 | ||
1421 | $user1 = self::getDataGenerator()->create_user(); | |
1422 | $userstranger = self::getDataGenerator()->create_user(); | |
1423 | $useroffline1 = self::getDataGenerator()->create_user(); | |
1424 | $useroffline2 = self::getDataGenerator()->create_user(); | |
1425 | $userblocked = self::getDataGenerator()->create_user(); | |
1426 | ||
1427 | // Login as user1. | |
1428 | $this->setUser($user1); | |
f219eac7 MN |
1429 | |
1430 | \core_message\api::add_contact($user1->id, $useroffline1->id); | |
1431 | \core_message\api::add_contact($user1->id, $useroffline2->id); | |
ff1f3739 JL |
1432 | |
1433 | // The userstranger sends a couple of messages to user1. | |
1434 | $this->send_message($userstranger, $user1, 'Hello there!'); | |
1435 | $this->send_message($userstranger, $user1, 'How you goin?'); | |
1436 | ||
1437 | // The userblocked sends a message to user1. | |
1438 | // Note that this user is not blocked at this point. | |
1439 | $this->send_message($userblocked, $user1, 'Here, have some spam.'); | |
1440 | ||
1441 | // Retrieve the list of blocked users. | |
1442 | $this->setUser($user1); | |
1443 | $blockedusers = core_message_external::get_blocked_users($user1->id); | |
1444 | $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); | |
1445 | $this->assertCount(0, $blockedusers['users']); | |
1446 | ||
1447 | // Block the $userblocked and retrieve again the list. | |
1448 | core_message_external::block_contacts(array($userblocked->id)); | |
f219eac7 | 1449 | $this->assertDebuggingCalled(); |
ff1f3739 JL |
1450 | $blockedusers = core_message_external::get_blocked_users($user1->id); |
1451 | $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); | |
1452 | $this->assertCount(1, $blockedusers['users']); | |
1453 | ||
01393790 JL |
1454 | // Remove the $userblocked and check that the list now is empty. |
1455 | delete_user($userblocked); | |
1456 | $blockedusers = core_message_external::get_blocked_users($user1->id); | |
1457 | $blockedusers = external_api::clean_returnvalue(core_message_external::get_blocked_users_returns(), $blockedusers); | |
1458 | $this->assertCount(0, $blockedusers['users']); | |
ff1f3739 JL |
1459 | } |
1460 | ||
b6795827 JL |
1461 | /** |
1462 | * Test mark_message_read. | |
1463 | */ | |
1464 | public function test_mark_message_read() { | |
1465 | $this->resetAfterTest(true); | |
1466 | ||
1467 | $user1 = self::getDataGenerator()->create_user(); | |
1468 | $user2 = self::getDataGenerator()->create_user(); | |
1469 | $user3 = self::getDataGenerator()->create_user(); | |
1470 | ||
1471 | // Login as user1. | |
1472 | $this->setUser($user1); | |
f219eac7 MN |
1473 | \core_message\api::add_contact($user1->id, $user2->id); |
1474 | \core_message\api::add_contact($user1->id, $user3->id); | |
b6795827 JL |
1475 | |
1476 | // The user2 sends a couple of messages to user1. | |
1477 | $this->send_message($user2, $user1, 'Hello there!'); | |
1478 | $this->send_message($user2, $user1, 'How you goin?'); | |
1479 | $this->send_message($user3, $user1, 'How you goin?'); | |
1480 | $this->send_message($user3, $user2, 'How you goin?'); | |
1481 | ||
1482 | // Retrieve all messages sent by user2 (they are currently unread). | |
1483 | $lastmessages = message_get_messages($user1->id, $user2->id, 0, false); | |
1484 | ||
1485 | $messageids = array(); | |
1486 | foreach ($lastmessages as $m) { | |
1487 | $messageid = core_message_external::mark_message_read($m->id, time()); | |
1488 | $messageids[] = external_api::clean_returnvalue(core_message_external::mark_message_read_returns(), $messageid); | |
1489 | } | |
1490 | ||
1491 | // Retrieve all messages sent (they are currently read). | |
1492 | $lastmessages = message_get_messages($user1->id, $user2->id, 0, true); | |
1493 | $this->assertCount(2, $lastmessages); | |
1494 | $this->assertArrayHasKey($messageids[0]['messageid'], $lastmessages); | |
1495 | $this->assertArrayHasKey($messageids[1]['messageid'], $lastmessages); | |
1496 | ||
1497 | // Retrieve all messages sent by any user (that are currently unread). | |
1498 | $lastmessages = message_get_messages($user1->id, 0, 0, false); | |
1499 | $this->assertCount(1, $lastmessages); | |
1500 | ||
1501 | // Invalid message ids. | |
1502 | try { | |
883ce421 | 1503 | $messageid = core_message_external::mark_message_read(1337, time()); |
b6795827 JL |
1504 | $this->fail('Exception expected due invalid messageid.'); |
1505 | } catch (dml_missing_record_exception $e) { | |
883ce421 | 1506 | $this->assertEquals('invalidrecordunknown', $e->errorcode); |
b6795827 JL |
1507 | } |
1508 | ||
1509 | // A message to a different user. | |
1510 | $lastmessages = message_get_messages($user2->id, $user3->id, 0, false); | |
1511 | $messageid = array_pop($lastmessages)->id; | |
1512 | try { | |
1513 | $messageid = core_message_external::mark_message_read($messageid, time()); | |
1514 | $this->fail('Exception expected due invalid messageid.'); | |
1515 | } catch (invalid_parameter_exception $e) { | |
1516 | $this->assertEquals('invalidparameter', $e->errorcode); | |
1517 | } | |
2b595d96 MN |
1518 | } |
1519 | ||
1520 | /** | |
1521 | * Test mark_notification_read. | |
1522 | */ | |
1523 | public function test_mark_notification_read() { | |
1524 | $this->resetAfterTest(true); | |
1525 | ||
1526 | $user1 = self::getDataGenerator()->create_user(); | |
1527 | $user2 = self::getDataGenerator()->create_user(); | |
1528 | $user3 = self::getDataGenerator()->create_user(); | |
1529 | ||
1530 | // Login as user1. | |
1531 | $this->setUser($user1); | |
f219eac7 MN |
1532 | \core_message\api::add_contact($user1->id, $user2->id); |
1533 | \core_message\api::add_contact($user1->id, $user3->id); | |
b6795827 | 1534 | |
2b595d96 MN |
1535 | // The user2 sends a couple of notifications to user1. |
1536 | $this->send_message($user2, $user1, 'Hello there!', 1); | |
1537 | $this->send_message($user2, $user1, 'How you goin?', 1); | |
1538 | $this->send_message($user3, $user1, 'How you goin?', 1); | |
1539 | $this->send_message($user3, $user2, 'How you goin?', 1); | |
1540 | ||
1541 | // Retrieve all notifications sent by user2 (they are currently unread). | |
1542 | $lastnotifications = message_get_messages($user1->id, $user2->id, 1, false); | |
1543 | ||
1544 | $notificationids = array(); | |
1545 | foreach ($lastnotifications as $n) { | |
1546 | $notificationid = core_message_external::mark_notification_read($n->id, time()); | |
1547 | $notificationids[] = external_api::clean_returnvalue(core_message_external::mark_notification_read_returns(), | |
1548 | $notificationid); | |
1549 | } | |
1550 | ||
1551 | // Retrieve all notifications sent (they are currently read). | |
1552 | $lastnotifications = message_get_messages($user1->id, $user2->id, 1, true); | |
1553 | $this->assertCount(2, $lastnotifications); | |
1554 | $this->assertArrayHasKey($notificationids[1]['notificationid'], $lastnotifications); | |
1555 | $this->assertArrayHasKey($notificationids[0]['notificationid'], $lastnotifications); | |
1556 | ||
1557 | // Retrieve all notifications sent by any user (that are currently unread). | |
1558 | $lastnotifications = message_get_messages($user1->id, 0, 1, false); | |
1559 | $this->assertCount(1, $lastnotifications); | |
1560 | ||
1561 | // Invalid notification ids. | |
1562 | try { | |
1563 | $notificationid = core_message_external::mark_notification_read(1337, time()); | |
1564 | $this->fail('Exception expected due invalid notificationid.'); | |
1565 | } catch (dml_missing_record_exception $e) { | |
1566 | $this->assertEquals('invalidrecord', $e->errorcode); | |
1567 | } | |
1568 | ||
1569 | // A notification to a different user. | |
1570 | $lastnotifications = message_get_messages($user2->id, $user3->id, 1, false); | |
1571 | $notificationid = array_pop($lastnotifications)->id; | |
1572 | try { | |
1573 | $notificationid = core_message_external::mark_notification_read($notificationid, time()); | |
1574 | $this->fail('Exception expected due invalid notificationid.'); | |
1575 | } catch (invalid_parameter_exception $e) { | |
1576 | $this->assertEquals('invalidparameter', $e->errorcode); | |
1577 | } | |
b6795827 JL |
1578 | } |
1579 | ||
419b1128 JL |
1580 | /** |
1581 | * Test delete_message. | |
1582 | */ | |
1583 | public function test_delete_message() { | |
1584 | global $DB; | |
1585 | $this->resetAfterTest(true); | |
1586 | ||
1587 | $user1 = self::getDataGenerator()->create_user(); | |
1588 | $user2 = self::getDataGenerator()->create_user(); | |
1589 | $user3 = self::getDataGenerator()->create_user(); | |
1590 | $user4 = self::getDataGenerator()->create_user(); | |
1591 | ||
1592 | // Login as user1. | |
1593 | $this->setUser($user1); | |
f219eac7 MN |
1594 | \core_message\api::add_contact($user1->id, $user2->id); |
1595 | \core_message\api::add_contact($user1->id, $user3->id); | |
419b1128 JL |
1596 | |
1597 | // User user1 does not interchange messages with user3. | |
1598 | $m1to2 = message_post_message($user1, $user2, 'some random text 1', FORMAT_MOODLE); | |
1599 | $m2to3 = message_post_message($user2, $user3, 'some random text 3', FORMAT_MOODLE); | |
1600 | $m3to2 = message_post_message($user3, $user2, 'some random text 4', FORMAT_MOODLE); | |
1601 | $m3to4 = message_post_message($user3, $user4, 'some random text 4', FORMAT_MOODLE); | |
1602 | ||
1603 | // Retrieve all messages sent by user2 (they are currently unread). | |
1604 | $lastmessages = message_get_messages($user1->id, $user2->id, 0, false); | |
1605 | ||
1606 | // Delete a message not read, as a user from. | |
1607 | $result = core_message_external::delete_message($m1to2, $user1->id, false); | |
1608 | $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); | |
1609 | $this->assertTrue($result['status']); | |
1610 | $this->assertCount(0, $result['warnings']); | |
883ce421 MN |
1611 | $mua = $DB->get_record('message_user_actions', array('messageid' => $m1to2, 'userid' => $user1->id)); |
1612 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua->action); | |
419b1128 JL |
1613 | |
1614 | // Try to delete the same message again. | |
1615 | $result = core_message_external::delete_message($m1to2, $user1->id, false); | |
1616 | $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); | |
1617 | $this->assertFalse($result['status']); | |
1618 | ||
1619 | // Try to delete a message that does not belong to me. | |
1620 | try { | |
1621 | $messageid = core_message_external::delete_message($m2to3, $user3->id, false); | |
1622 | $this->fail('Exception expected due invalid messageid.'); | |
1623 | } catch (moodle_exception $e) { | |
1624 | $this->assertEquals('You do not have permission to delete this message', $e->errorcode); | |
1625 | } | |
1626 | ||
1627 | $this->setUser($user3); | |
1628 | // Delete a message not read, as a user to. | |
1629 | $result = core_message_external::delete_message($m2to3, $user3->id, false); | |
1630 | $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); | |
1631 | $this->assertTrue($result['status']); | |
1632 | $this->assertCount(0, $result['warnings']); | |
883ce421 MN |
1633 | $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m2to3, 'userid' => $user3->id, |
1634 | 'action' => \core_message\api::MESSAGE_ACTION_DELETED))); | |
419b1128 JL |
1635 | |
1636 | // Delete a message read. | |
548936a6 MN |
1637 | $message = $DB->get_record('messages', ['id' => $m3to2]); |
1638 | \core_message\api::mark_message_as_read($user3->id, $message, time()); | |
883ce421 | 1639 | $result = core_message_external::delete_message($m3to2, $user3->id); |
419b1128 JL |
1640 | $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); |
1641 | $this->assertTrue($result['status']); | |
1642 | $this->assertCount(0, $result['warnings']); | |
883ce421 MN |
1643 | $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m3to2, 'userid' => $user3->id, |
1644 | 'action' => \core_message\api::MESSAGE_ACTION_DELETED))); | |
419b1128 JL |
1645 | |
1646 | // Invalid message ids. | |
1647 | try { | |
1648 | $result = core_message_external::delete_message(-1, $user1->id); | |
1649 | $this->fail('Exception expected due invalid messageid.'); | |
1650 | } catch (dml_missing_record_exception $e) { | |
08cb8a34 | 1651 | $this->assertEquals('invalidrecord', $e->errorcode); |
419b1128 JL |
1652 | } |
1653 | ||
1654 | // Invalid user. | |
1655 | try { | |
1656 | $result = core_message_external::delete_message($m1to2, -1, false); | |
1657 | $this->fail('Exception expected due invalid user.'); | |
1658 | } catch (moodle_exception $e) { | |
1659 | $this->assertEquals('invaliduser', $e->errorcode); | |
1660 | } | |
1661 | ||
1662 | // Not active user. | |
1663 | delete_user($user2); | |
1664 | try { | |
1665 | $result = core_message_external::delete_message($m1to2, $user2->id, false); | |
1666 | $this->fail('Exception expected due invalid user.'); | |
1667 | } catch (moodle_exception $e) { | |
1668 | $this->assertEquals('userdeleted', $e->errorcode); | |
1669 | } | |
1670 | ||
1671 | // Now, as an admin, try to delete any message. | |
1672 | $this->setAdminUser(); | |
1673 | $result = core_message_external::delete_message($m3to4, $user4->id, false); | |
1674 | $result = external_api::clean_returnvalue(core_message_external::delete_message_returns(), $result); | |
1675 | $this->assertTrue($result['status']); | |
1676 | $this->assertCount(0, $result['warnings']); | |
883ce421 MN |
1677 | $this->assertTrue($DB->record_exists('message_user_actions', array('messageid' => $m3to4, 'userid' => $user4->id, |
1678 | 'action' => \core_message\api::MESSAGE_ACTION_DELETED))); | |
419b1128 JL |
1679 | |
1680 | } | |
1681 | ||
3274d5ca RW |
1682 | public function test_mark_all_notifications_as_read_invalid_user_exception() { |
1683 | $this->resetAfterTest(true); | |
1684 | ||
6aa01968 MN |
1685 | $this->expectException('moodle_exception'); |
1686 | core_message_external::mark_all_notifications_as_read(-2132131, 0); | |
3274d5ca RW |
1687 | } |
1688 | ||
1689 | public function test_mark_all_notifications_as_read_access_denied_exception() { | |
1690 | $this->resetAfterTest(true); | |
1691 | ||
1692 | $sender = $this->getDataGenerator()->create_user(); | |
1693 | $user = $this->getDataGenerator()->create_user(); | |
1694 | ||
1695 | $this->setUser($user); | |
6aa01968 MN |
1696 | $this->expectException('moodle_exception'); |
1697 | core_message_external::mark_all_notifications_as_read($sender->id, 0); | |
3274d5ca RW |
1698 | } |
1699 | ||
1700 | public function test_mark_all_notifications_as_read_missing_from_user_exception() { | |
1701 | $this->resetAfterTest(true); | |
1702 | ||
1703 | $sender = $this->getDataGenerator()->create_user(); | |
1704 | ||
1705 | $this->setUser($sender); | |
6aa01968 MN |
1706 | $this->expectException('moodle_exception'); |
1707 | core_message_external::mark_all_notifications_as_read($sender->id, 99999); | |
3274d5ca RW |
1708 | } |
1709 | ||
1710 | public function test_mark_all_notifications_as_read() { | |
7d69958e RW |
1711 | global $DB; |
1712 | ||
3274d5ca RW |
1713 | $this->resetAfterTest(true); |
1714 | ||
1715 | $sender1 = $this->getDataGenerator()->create_user(); | |
1716 | $sender2 = $this->getDataGenerator()->create_user(); | |
1717 | $sender3 = $this->getDataGenerator()->create_user(); | |
1718 | $recipient = $this->getDataGenerator()->create_user(); | |
1719 | ||
1720 | $this->setUser($recipient); | |
1721 | ||
6aa01968 MN |
1722 | $this->send_message($sender1, $recipient, 'Notification', 1); |
1723 | $this->send_message($sender1, $recipient, 'Notification', 1); | |
1724 | $this->send_message($sender2, $recipient, 'Notification', 1); | |
1725 | $this->send_message($sender2, $recipient, 'Notification', 1); | |
1726 | $this->send_message($sender3, $recipient, 'Notification', 1); | |
1727 | $this->send_message($sender3, $recipient, 'Notification', 1); | |
3274d5ca RW |
1728 | |
1729 | core_message_external::mark_all_notifications_as_read($recipient->id, $sender1->id); | |
883ce421 MN |
1730 | $readnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', [$recipient->id]); |
1731 | $unreadnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NULL', [$recipient->id]); | |
3274d5ca | 1732 | |
7d69958e RW |
1733 | $this->assertCount(2, $readnotifications); |
1734 | $this->assertCount(4, $unreadnotifications); | |
3274d5ca RW |
1735 | |
1736 | core_message_external::mark_all_notifications_as_read($recipient->id, 0); | |
883ce421 MN |
1737 | $readnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NOT NULL', [$recipient->id]); |
1738 | $unreadnotifications = $DB->get_records_select('notifications', 'useridto = ? AND timeread IS NULL', [$recipient->id]); | |
3274d5ca | 1739 | |
7d69958e RW |
1740 | $this->assertCount(6, $readnotifications); |
1741 | $this->assertCount(0, $unreadnotifications); | |
3274d5ca | 1742 | } |
e86f0cb4 JL |
1743 | |
1744 | /** | |
1745 | * Test get_user_notification_preferences | |
1746 | */ | |
1747 | public function test_get_user_notification_preferences() { | |
1748 | $this->resetAfterTest(true); | |
1749 | ||
1750 | $user = self::getDataGenerator()->create_user(); | |
1751 | $this->setUser($user); | |
1752 | ||
1753 | // Set a couple of preferences to test. | |
1754 | set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user); | |
1755 | set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user); | |
1756 | ||
1757 | $prefs = core_message_external::get_user_notification_preferences(); | |
1758 | $prefs = external_api::clean_returnvalue(core_message_external::get_user_notification_preferences_returns(), $prefs); | |
1759 | // Check processors. | |
46c5c883 | 1760 | $this->assertGreaterThanOrEqual(2, count($prefs['preferences']['processors'])); |
e86f0cb4 JL |
1761 | $this->assertEquals($user->id, $prefs['preferences']['userid']); |
1762 | ||
1763 | // Check components. | |
46c5c883 | 1764 | $this->assertGreaterThanOrEqual(8, count($prefs['preferences']['components'])); |
e86f0cb4 JL |
1765 | |
1766 | // Check some preferences that we previously set. | |
1767 | $found = 0; | |
1768 | foreach ($prefs['preferences']['components'] as $component) { | |
1769 | foreach ($component['notifications'] as $prefdata) { | |
1770 | if ($prefdata['preferencekey'] != 'message_provider_mod_assign_assign_notification') { | |
1771 | continue; | |
1772 | } | |
1773 | foreach ($prefdata['processors'] as $processor) { | |
1774 | if ($processor['name'] == 'popup') { | |
1775 | $this->assertTrue($processor['loggedin']['checked']); | |
1776 | $found++; | |
1777 | } else if ($processor['name'] == 'email') { | |
1778 | $this->assertTrue($processor['loggedoff']['checked']); | |
1779 | $found++; | |
1780 | } | |
1781 | } | |
1782 | } | |
1783 | } | |
1784 | $this->assertEquals(2, $found); | |
1785 | } | |
1786 | ||
1787 | /** | |
1788 | * Test get_user_notification_preferences permissions | |
1789 | */ | |
1790 | public function test_get_user_notification_preferences_permissions() { | |
1791 | $this->resetAfterTest(true); | |
1792 | ||
1793 | $user = self::getDataGenerator()->create_user(); | |
1794 | $otheruser = self::getDataGenerator()->create_user(); | |
1795 | $this->setUser($user); | |
1796 | ||
1797 | $this->expectException('moodle_exception'); | |
1798 | $prefs = core_message_external::get_user_notification_preferences($otheruser->id); | |
1799 | } | |
6aa01968 MN |
1800 | |
1801 | /** | |
1802 | * Tests searching users in a course. | |
1803 | */ | |
1804 | public function test_messagearea_search_users_in_course() { | |
1805 | $this->resetAfterTest(true); | |
1806 | ||
1807 | // Create some users. | |
1808 | $user1 = new stdClass(); | |
1809 | $user1->firstname = 'User'; | |
1810 | $user1->lastname = 'One'; | |
1811 | $user1 = self::getDataGenerator()->create_user($user1); | |
1812 | ||
1813 | // The person doing the search. | |
1814 | $this->setUser($user1); | |
1815 | ||
1816 | // Set the second user's status to online by setting their last access to now. | |
1817 | $user2 = new stdClass(); | |
1818 | $user2->firstname = 'User'; | |
1819 | $user2->lastname = 'Two'; | |
1820 | $user2->lastaccess = time(); | |
1821 | $user2 = self::getDataGenerator()->create_user($user2); | |
1822 | ||
1823 | // Block the second user. | |
f219eac7 | 1824 | \core_message\api::block_user($user1->id, $user2->id); |
6aa01968 MN |
1825 | |
1826 | $user3 = new stdClass(); | |
1827 | $user3->firstname = 'User'; | |
1828 | $user3->lastname = 'Three'; | |
1829 | $user3 = self::getDataGenerator()->create_user($user3); | |
1830 | ||
1831 | // Create a course. | |
1832 | $course1 = new stdClass(); | |
1833 | $course1->fullname = 'Course'; | |
1834 | $course1->shortname = 'One'; | |
1835 | $course1 = $this->getDataGenerator()->create_course(); | |
1836 | ||
1837 | // Enrol the user we are doing the search for and one user in the course. | |
1838 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id); | |
1839 | $this->getDataGenerator()->enrol_user($user2->id, $course1->id); | |
1840 | ||
1841 | // Perform a search. | |
1842 | $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User'); | |
1843 | ||
1844 | // We need to execute the return values cleaning process to simulate the web service. | |
1845 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(), | |
1846 | $result); | |
1847 | ||
1848 | // Check that we only retrieved a user that was enrolled, and that the user performing the search was not returned. | |
1849 | $users = $result['contacts']; | |
1850 | $this->assertCount(1, $users); | |
1851 | ||
1852 | $user = $users[0]; | |
1853 | $this->assertEquals($user2->id, $user['userid']); | |
1854 | $this->assertEquals(fullname($user2), $user['fullname']); | |
1855 | $this->assertFalse($user['ismessaging']); | |
1856 | $this->assertFalse($user['sentfromcurrentuser']); | |
1857 | $this->assertNull($user['lastmessage']); | |
1858 | $this->assertNull($user['messageid']); | |
cb805753 | 1859 | $this->assertNull($user['isonline']); |
6aa01968 MN |
1860 | $this->assertFalse($user['isread']); |
1861 | $this->assertTrue($user['isblocked']); | |
1862 | $this->assertNull($user['unreadcount']); | |
1863 | } | |
1864 | ||
1865 | /** | |
1866 | * Tests searching users in course as another user. | |
1867 | */ | |
1868 | public function test_messagearea_search_users_in_course_as_other_user() { | |
1869 | $this->resetAfterTest(true); | |
1870 | ||
1871 | // The person doing the search for another user. | |
1872 | $this->setAdminUser(); | |
1873 | ||
1874 | // Create some users. | |
1875 | $user1 = new stdClass(); | |
1876 | $user1->firstname = 'User'; | |
1877 | $user1->lastname = 'One'; | |
1878 | $user1 = self::getDataGenerator()->create_user($user1); | |
1879 | ||
1880 | $user2 = new stdClass(); | |
1881 | $user2->firstname = 'User'; | |
1882 | $user2->lastname = 'Two'; | |
1883 | $user2 = self::getDataGenerator()->create_user($user2); | |
1884 | ||
1885 | $user3 = new stdClass(); | |
1886 | $user3->firstname = 'User'; | |
1887 | $user3->lastname = 'Three'; | |
1888 | $user3 = self::getDataGenerator()->create_user($user3); | |
1889 | ||
1890 | // Create a course. | |
1891 | $course1 = new stdClass(); | |
1892 | $course1->fullname = 'Course'; | |
1893 | $course1->shortname = 'One'; | |
1894 | $course1 = $this->getDataGenerator()->create_course(); | |
1895 | ||
1896 | // Enrol the user we are doing the search for and one user in the course. | |
1897 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id); | |
1898 | $this->getDataGenerator()->enrol_user($user2->id, $course1->id); | |
1899 | ||
1900 | // Perform a search. | |
1901 | $result = core_message_external::data_for_messagearea_search_users_in_course($user1->id, $course1->id, 'User'); | |
1902 | ||
1903 | // We need to execute the return values cleaning process to simulate the web service server. | |
1904 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_in_course_returns(), | |
1905 | $result); | |
1906 | ||
1907 | // Check that we got the user enrolled, and that the user we are performing the search on behalf of was not returned. | |
1908 | $users = $result['contacts']; | |
1909 | $this->assertCount(1, $users); | |
1910 | ||
1911 | $user = $users[0]; | |
1912 | $this->assertEquals($user2->id, $user['userid']); | |
1913 | $this->assertEquals(fullname($user2), $user['fullname']); | |
1914 | $this->assertFalse($user['ismessaging']); | |
1915 | $this->assertFalse($user['sentfromcurrentuser']); | |
1916 | $this->assertNull($user['lastmessage']); | |
1917 | $this->assertNull($user['messageid']); | |
1918 | $this->assertFalse($user['isonline']); | |
1919 | $this->assertFalse($user['isread']); | |
1920 | $this->assertFalse($user['isblocked']); | |
1921 | $this->assertNull($user['unreadcount']); | |
1922 | } | |
1923 | ||
1924 | /** | |
1925 | * Tests searching users in course as another user without the proper capabilities. | |
1926 | */ | |
1927 | public function test_messagearea_search_users_in_course_as_other_user_without_cap() { | |
1928 | $this->resetAfterTest(true); | |
1929 | ||
1930 | // Create some users. | |
1931 | $user1 = self::getDataGenerator()->create_user(); | |
1932 | $user2 = self::getDataGenerator()->create_user(); | |
1933 | ||
1934 | // The person doing the search for another user. | |
1935 | $this->setUser($user1); | |
1936 | ||
1937 | // Create a course. | |
1938 | $course = $this->getDataGenerator()->create_course(); | |
1939 | ||
1940 | // Ensure an exception is thrown. | |
1941 | $this->expectException('moodle_exception'); | |
1942 | core_message_external::data_for_messagearea_search_users_in_course($user2->id, $course->id, 'User'); | |
1943 | } | |
1944 | ||
1945 | /** | |
1946 | * Tests searching users in course with messaging disabled. | |
1947 | */ | |
1948 | public function test_messagearea_search_users_in_course_messaging_disabled() { | |
1949 | global $CFG; | |
1950 | ||
1951 | $this->resetAfterTest(true); | |
1952 | ||
1953 | // Create some skeleton data just so we can call the WS.. | |
1954 | $user = self::getDataGenerator()->create_user(); | |
1955 | $course = $this->getDataGenerator()->create_course(); | |
1956 | ||
1957 | // The person doing the search for another user. | |
1958 | $this->setUser($user); | |
1959 | ||
1960 | // Disable messaging. | |
1961 | $CFG->messaging = 0; | |
1962 | ||
1963 | // Ensure an exception is thrown. | |
1964 | $this->expectException('moodle_exception'); | |
1965 | core_message_external::data_for_messagearea_search_users_in_course($user->id, $course->id, 'User'); | |
1966 | } | |
1967 | ||
1968 | /** | |
1969 | * Tests searching users. | |
1970 | */ | |
1971 | public function test_messagearea_search_users() { | |
1972 | $this->resetAfterTest(true); | |
1973 | ||
1974 | // Create some users. | |
1975 | $user1 = new stdClass(); | |
1976 | $user1->firstname = 'User'; | |
1977 | $user1->lastname = 'One'; | |
1978 | $user1 = self::getDataGenerator()->create_user($user1); | |
1979 | ||
1980 | // Set as the user performing the search. | |
1981 | $this->setUser($user1); | |
1982 | ||
1983 | $user2 = new stdClass(); | |
1984 | $user2->firstname = 'User search'; | |
1985 | $user2->lastname = 'Two'; | |
1986 | $user2 = self::getDataGenerator()->create_user($user2); | |
1987 | ||
1988 | $user3 = new stdClass(); | |
1989 | $user3->firstname = 'User search'; | |
1990 | $user3->lastname = 'Three'; | |
1991 | $user3 = self::getDataGenerator()->create_user($user3); | |
1992 | ||
1993 | $user4 = new stdClass(); | |
1994 | $user4->firstname = 'User'; | |
1995 | $user4->lastname = 'Four'; | |
1996 | $user4 = self::getDataGenerator()->create_user($user4); | |
1997 | ||
1998 | $user5 = new stdClass(); | |
1999 | $user5->firstname = 'User search'; | |
2000 | $user5->lastname = 'Five'; | |
2001 | $user5 = self::getDataGenerator()->create_user($user5); | |
2002 | ||
2003 | $user6 = new stdClass(); | |
2004 | $user6->firstname = 'User'; | |
2005 | $user6->lastname = 'Six'; | |
2006 | $user6 = self::getDataGenerator()->create_user($user6); | |
2007 | ||
2008 | // Create some courses. | |
2009 | $course1 = new stdClass(); | |
2010 | $course1->fullname = 'Course search'; | |
2011 | $course1->shortname = 'One'; | |
2012 | $course1 = $this->getDataGenerator()->create_course($course1); | |
2013 | ||
2014 | $course2 = new stdClass(); | |
2015 | $course2->fullname = 'Course'; | |
2016 | $course2->shortname = 'Two'; | |
2017 | $course2 = $this->getDataGenerator()->create_course($course2); | |
2018 | ||
2019 | $course3 = new stdClass(); | |
2020 | $course3->fullname = 'Course'; | |
2021 | $course3->shortname = 'Three search'; | |
2022 | $course3 = $this->getDataGenerator()->create_course($course3); | |
2023 | ||
87d4ab65 AG |
2024 | $course4 = new stdClass(); |
2025 | $course4->fullname = 'Course Four'; | |
2026 | $course4->shortname = 'CF100'; | |
2027 | $course4 = $this->getDataGenerator()->create_course($course4); | |
2028 | ||
2029 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student'); | |
2030 | $this->getDataGenerator()->enrol_user($user1->id, $course2->id, 'student'); | |
2031 | $this->getDataGenerator()->enrol_user($user1->id, $course3->id, 'student'); | |
2032 | ||
6aa01968 | 2033 | // Add some users as contacts. |
f219eac7 MN |
2034 | \core_message\api::add_contact($user1->id, $user2->id); |
2035 | \core_message\api::add_contact($user1->id, $user3->id); | |
2036 | \core_message\api::add_contact($user1->id, $user4->id); | |
6aa01968 MN |
2037 | |
2038 | // Perform a search. | |
2039 | $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search'); | |
2040 | ||
2041 | // We need to execute the return values cleaning process to simulate the web service server. | |
2042 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(), | |
2043 | $result); | |
2044 | ||
2045 | // Confirm that we returns contacts, courses and non-contacts. | |
2046 | $contacts = $result['contacts']; | |
2047 | $courses = $result['courses']; | |
2048 | $noncontacts = $result['noncontacts']; | |
2049 | ||
2050 | // Check that we retrieved the correct contacts. | |
2051 | $this->assertCount(2, $contacts); | |
2052 | $this->assertEquals($user3->id, $contacts[0]['userid']); | |
2053 | $this->assertEquals($user2->id, $contacts[1]['userid']); | |
2054 | ||
2055 | // Check that we retrieved the correct courses. | |
2056 | $this->assertCount(2, $courses); | |
2057 | $this->assertEquals($course3->id, $courses[0]['id']); | |
2058 | $this->assertEquals($course1->id, $courses[1]['id']); | |
2059 | ||
2060 | // Check that we retrieved the correct non-contacts. | |
2061 | $this->assertCount(1, $noncontacts); | |
2062 | $this->assertEquals($user5->id, $noncontacts[0]['userid']); | |
2063 | } | |
2064 | ||
2065 | /** | |
2066 | * Tests searching users as another user. | |
2067 | */ | |
2068 | public function test_messagearea_search_users_as_other_user() { | |
2069 | $this->resetAfterTest(true); | |
2070 | ||
2071 | // The person doing the search. | |
2072 | $this->setAdminUser(); | |
2073 | ||
2074 | // Create some users. | |
2075 | $user1 = new stdClass(); | |
2076 | $user1->firstname = 'User'; | |
2077 | $user1->lastname = 'One'; | |
2078 | $user1 = self::getDataGenerator()->create_user($user1); | |
2079 | ||
2080 | $user2 = new stdClass(); | |
2081 | $user2->firstname = 'User search'; | |
2082 | $user2->lastname = 'Two'; | |
2083 | $user2 = self::getDataGenerator()->create_user($user2); | |
2084 | ||
2085 | $user3 = new stdClass(); | |
2086 | $user3->firstname = 'User search'; | |
2087 | $user3->lastname = 'Three'; | |
2088 | $user3 = self::getDataGenerator()->create_user($user3); | |
2089 | ||
2090 | $user4 = new stdClass(); | |
2091 | $user4->firstname = 'User'; | |
2092 | $user4->lastname = 'Four'; | |
2093 | $user4 = self::getDataGenerator()->create_user($user4); | |
2094 | ||
2095 | $user5 = new stdClass(); | |
2096 | $user5->firstname = 'User search'; | |
2097 | $user5->lastname = 'Five'; | |
2098 | $user5 = self::getDataGenerator()->create_user($user5); | |
2099 | ||
2100 | $user6 = new stdClass(); | |
2101 | $user6->firstname = 'User'; | |
2102 | $user6->lastname = 'Six'; | |
2103 | $user6 = self::getDataGenerator()->create_user($user6); | |
2104 | ||
2105 | // Create some courses. | |
2106 | $course1 = new stdClass(); | |
2107 | $course1->fullname = 'Course search'; | |
2108 | $course1->shortname = 'One'; | |
2109 | $course1 = $this->getDataGenerator()->create_course($course1); | |
2110 | ||
2111 | $course2 = new stdClass(); | |
2112 | $course2->fullname = 'Course'; | |
2113 | $course2->shortname = 'Two'; | |
2114 | $course2 = $this->getDataGenerator()->create_course($course2); | |
2115 | ||
2116 | $course3 = new stdClass(); | |
2117 | $course3->fullname = 'Course'; | |
2118 | $course3->shortname = 'Three search'; | |
2119 | $course3 = $this->getDataGenerator()->create_course($course3); | |
2120 | ||
2121 | // Add some users as contacts. | |
f219eac7 MN |
2122 | \core_message\api::add_contact($user1->id, $user2->id); |
2123 | \core_message\api::add_contact($user1->id, $user3->id); | |
2124 | \core_message\api::add_contact($user1->id, $user4->id); | |
6aa01968 MN |
2125 | |
2126 | // Perform a search. | |
2127 | $result = core_message_external::data_for_messagearea_search_users($user1->id, 'search'); | |
2128 | ||
2129 | // We need to execute the return values cleaning process to simulate the web service server. | |
2130 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_users_returns(), | |
2131 | $result); | |
2132 | ||
2133 | // Confirm that we returns contacts, courses and non-contacts. | |
2134 | $contacts = $result['contacts']; | |
2135 | $courses = $result['courses']; | |
2136 | $noncontacts = $result['noncontacts']; | |
2137 | ||
2138 | // Check that we retrieved the correct contacts. | |
2139 | $this->assertCount(2, $contacts); | |
2140 | $this->assertEquals($user3->id, $contacts[0]['userid']); | |
2141 | $this->assertEquals($user2->id, $contacts[1]['userid']); | |
2142 | ||
2143 | // Check that we retrieved the correct courses. | |
87d4ab65 | 2144 | $this->assertCount(0, $courses); |
6aa01968 MN |
2145 | |
2146 | // Check that we retrieved the correct non-contacts. | |
2147 | $this->assertCount(1, $noncontacts); | |
2148 | $this->assertEquals($user5->id, $noncontacts[0]['userid']); | |
2149 | } | |
2150 | ||
2151 | /** | |
2152 | * Tests searching users as another user without the proper capabilities. | |
2153 | */ | |
2154 | public function test_messagearea_search_users_as_other_user_without_cap() { | |
2155 | $this->resetAfterTest(true); | |
2156 | ||
2157 | // Create some users. | |
2158 | $user1 = self::getDataGenerator()->create_user(); | |
2159 | $user2 = self::getDataGenerator()->create_user(); | |
2160 | ||
2161 | // The person doing the search for another user. | |
2162 | $this->setUser($user1); | |
2163 | ||
2164 | // Ensure an exception is thrown. | |
2165 | $this->expectException('moodle_exception'); | |
2166 | core_message_external::data_for_messagearea_search_users($user2->id, 'User'); | |
2167 | } | |
2168 | ||
2169 | /** | |
2170 | * Tests searching users with messaging disabled. | |
2171 | */ | |
2172 | public function test_messagearea_search_users_messaging_disabled() { | |
2173 | global $CFG; | |
2174 | ||
2175 | $this->resetAfterTest(true); | |
2176 | ||
2177 | // Create some skeleton data just so we can call the WS. | |
2178 | $user = self::getDataGenerator()->create_user(); | |
2179 | ||
2180 | // The person doing the search. | |
2181 | $this->setUser($user); | |
2182 | ||
2183 | // Disable messaging. | |
2184 | $CFG->messaging = 0; | |
2185 | ||
2186 | // Ensure an exception is thrown. | |
2187 | $this->expectException('moodle_exception'); | |
2188 | core_message_external::data_for_messagearea_search_users($user->id, 'User'); | |
2189 | } | |
2190 | ||
2191 | /** | |
2192 | * Tests searching messages. | |
2193 | */ | |
2194 | public function test_messagearea_search_messages() { | |
2195 | $this->resetAfterTest(true); | |
2196 | ||
2197 | // Create some users. | |
2198 | $user1 = self::getDataGenerator()->create_user(); | |
2199 | $user2 = self::getDataGenerator()->create_user(); | |
2200 | ||
2201 | // The person doing the search. | |
2202 | $this->setUser($user1); | |
2203 | ||
2204 | // Send some messages back and forth. | |
2205 | $time = time(); | |
2206 | $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
2207 | $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
2208 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
2209 | $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
2210 | ||
2211 | // Perform a search. | |
2212 | $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o'); | |
2213 | ||
2214 | // We need to execute the return values cleaning process to simulate the web service server. | |
2215 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(), | |
2216 | $result); | |
2217 | ||
2218 | // Confirm the data is correct. | |
2219 | $messages = $result['contacts']; | |
2220 | $this->assertCount(2, $messages); | |
2221 | ||
2222 | $message1 = $messages[0]; | |
2223 | $message2 = $messages[1]; | |
2224 | ||
2225 | $this->assertEquals($user2->id, $message1['userid']); | |
2226 | $this->assertEquals(fullname($user2), $message1['fullname']); | |
2227 | $this->assertTrue($message1['ismessaging']); | |
2228 | $this->assertFalse($message1['sentfromcurrentuser']); | |
2229 | $this->assertEquals('Word.', $message1['lastmessage']); | |
2230 | $this->assertNotEmpty($message1['messageid']); | |
cb805753 | 2231 | $this->assertNull($message1['isonline']); |
6aa01968 MN |
2232 | $this->assertFalse($message1['isread']); |
2233 | $this->assertFalse($message1['isblocked']); | |
2234 | $this->assertNull($message1['unreadcount']); | |
2235 | ||
2236 | $this->assertEquals($user2->id, $message2['userid']); | |
2237 | $this->assertEquals(fullname($user2), $message2['fullname']); | |
2238 | $this->assertTrue($message2['ismessaging']); | |
2239 | $this->assertTrue($message2['sentfromcurrentuser']); | |
2240 | $this->assertEquals('Yo!', $message2['lastmessage']); | |
2241 | $this->assertNotEmpty($message2['messageid']); | |
cb805753 | 2242 | $this->assertNull($message2['isonline']); |
6aa01968 MN |
2243 | $this->assertTrue($message2['isread']); |
2244 | $this->assertFalse($message2['isblocked']); | |
2245 | $this->assertNull($message2['unreadcount']); | |
2246 | } | |
2247 | ||
2248 | /** | |
2249 | * Tests searching messages as another user. | |
2250 | */ | |
2251 | public function test_messagearea_search_messages_as_other_user() { | |
2252 | $this->resetAfterTest(true); | |
2253 | ||
2254 | // The person doing the search. | |
2255 | $this->setAdminUser(); | |
2256 | ||
2257 | // Create some users. | |
2258 | $user1 = self::getDataGenerator()->create_user(); | |
2259 | $user2 = self::getDataGenerator()->create_user(); | |
2260 | ||
2261 | // Send some messages back and forth. | |
2262 | $time = time(); | |
2263 | $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
2264 | $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
2265 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
2266 | $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
2267 | ||
2268 | // Perform a search. | |
2269 | $result = core_message_external::data_for_messagearea_search_messages($user1->id, 'o'); | |
2270 | ||
2271 | // We need to execute the return values cleaning process to simulate the web service server. | |
2272 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_search_messages_returns(), | |
2273 | $result); | |
2274 | ||
2275 | // Confirm the data is correct. | |
2276 | $messages = $result['contacts']; | |
2277 | $this->assertCount(2, $messages); | |
2278 | ||
2279 | $message1 = $messages[0]; | |
2280 | $message2 = $messages[1]; | |
2281 | ||
2282 | $this->assertEquals($user2->id, $message1['userid']); | |
2283 | $this->assertEquals(fullname($user2), $message1['fullname']); | |
2284 | $this->assertTrue($message1['ismessaging']); | |
2285 | $this->assertFalse($message1['sentfromcurrentuser']); | |
2286 | $this->assertEquals('Word.', $message1['lastmessage']); | |
2287 | $this->assertNotEmpty($message1['messageid']); | |
2288 | $this->assertFalse($message1['isonline']); | |
2289 | $this->assertFalse($message1['isread']); | |
2290 | $this->assertFalse($message1['isblocked']); | |
2291 | $this->assertNull($message1['unreadcount']); | |
2292 | ||
2293 | $this->assertEquals($user2->id, $message2['userid']); | |
2294 | $this->assertEquals(fullname($user2), $message2['fullname']); | |
2295 | $this->assertTrue($message2['ismessaging']); | |
2296 | $this->assertTrue($message2['sentfromcurrentuser']); | |
2297 | $this->assertEquals('Yo!', $message2['lastmessage']); | |
2298 | $this->assertNotEmpty($message2['messageid']); | |
2299 | $this->assertFalse($message2['isonline']); | |
2300 | $this->assertTrue($message2['isread']); | |
2301 | $this->assertFalse($message2['isblocked']); | |
2302 | $this->assertNull($message2['unreadcount']); | |
2303 | } | |
2304 | ||
2305 | /** | |
2306 | * Tests searching messages as another user without the proper capabilities. | |
2307 | */ | |
2308 | public function test_messagearea_search_messages_as_other_user_without_cap() { | |
2309 | $this->resetAfterTest(true); | |
2310 | ||
2311 | // Create some users. | |
2312 | $user1 = self::getDataGenerator()->create_user(); | |
2313 | $user2 = self::getDataGenerator()->create_user(); | |
2314 | ||
2315 | // The person doing the search for another user. | |
2316 | $this->setUser($user1); | |
2317 | ||
2318 | // Ensure an exception is thrown. | |
2319 | $this->expectException('moodle_exception'); | |
2320 | core_message_external::data_for_messagearea_search_messages($user2->id, 'Search'); | |
2321 | } | |
2322 | ||
2323 | /** | |
2324 | * Tests searching messages with messaging disabled | |
2325 | */ | |
2326 | public function test_messagearea_search_messages_messaging_disabled() { | |
2327 | global $CFG; | |
2328 | ||
2329 | $this->resetAfterTest(true); | |
2330 | ||
2331 | // Create some skeleton data just so we can call the WS. | |
2332 | $user = self::getDataGenerator()->create_user(); | |
2333 | ||
2334 | // The person doing the search . | |
2335 | $this->setUser($user); | |
2336 | ||
2337 | // Disable messaging. | |
2338 | $CFG->messaging = 0; | |
2339 | ||
2340 | // Ensure an exception is thrown. | |
2341 | $this->expectException('moodle_exception'); | |
2342 | core_message_external::data_for_messagearea_search_messages($user->id, 'Search'); | |
2343 | } | |
2344 | ||
2345 | /** | |
2346 | * Tests retrieving conversations. | |
2347 | */ | |
2348 | public function test_messagearea_conversations() { | |
2349 | $this->resetAfterTest(true); | |
2350 | ||
2351 | // Create some users. | |
2352 | $user1 = self::getDataGenerator()->create_user(); | |
2353 | $user2 = self::getDataGenerator()->create_user(); | |
2354 | $user3 = self::getDataGenerator()->create_user(); | |
2355 | $user4 = self::getDataGenerator()->create_user(); | |
2356 | ||
2357 | // The person retrieving the conversations. | |
2358 | $this->setUser($user1); | |
2359 | ||
2360 | // Send some messages back and forth, have some different conversations with different users. | |
2361 | $time = time(); | |
2362 | $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
2363 | $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
2364 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
4d146f1a | 2365 | $messageid1 = $this->send_message($user2, $user1, 'Word.', 0, $time + 3); |
6aa01968 MN |
2366 | |
2367 | $this->send_message($user1, $user3, 'Booyah', 0, $time + 4); | |
2368 | $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5); | |
2369 | $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6); | |
4d146f1a | 2370 | $messageid2 = $this->send_message($user3, $user1, 'Cool.', 0, $time + 7); |
6aa01968 MN |
2371 | |
2372 | $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8); | |
2373 | $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9); | |
4d146f1a | 2374 | $messageid3 = $this->send_message($user1, $user4, 'Dope.', 0, $time + 10); |
6aa01968 MN |
2375 | |
2376 | // Retrieve the conversations. | |
2377 | $result = core_message_external::data_for_messagearea_conversations($user1->id); | |
2378 | ||
2379 | // We need to execute the return values cleaning process to simulate the web service server. | |
2380 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(), | |
2381 | $result); | |
2382 | ||
2383 | // Confirm the data is correct. | |
2384 | $messages = $result['contacts']; | |
2385 | $this->assertCount(3, $messages); | |
2386 | ||
2387 | $message1 = $messages[0]; | |
2388 | $message2 = $messages[1]; | |
2389 | $message3 = $messages[2]; | |
2390 | ||
2391 | $this->assertEquals($user4->id, $message1['userid']); | |
2392 | $this->assertTrue($message1['ismessaging']); | |
2393 | $this->assertTrue($message1['sentfromcurrentuser']); | |
2394 | $this->assertEquals('Dope.', $message1['lastmessage']); | |
4d146f1a | 2395 | $this->assertEquals($messageid3, $message1['messageid']); |
cb805753 | 2396 | $this->assertNull($message1['isonline']); |
4d146f1a | 2397 | $this->assertFalse($message1['isread']); |
6aa01968 | 2398 | $this->assertFalse($message1['isblocked']); |
4d146f1a | 2399 | $this->assertEquals(1, $message1['unreadcount']); |
6aa01968 MN |
2400 | |
2401 | $this->assertEquals($user3->id, $message2['userid']); | |
2402 | $this->assertTrue($message2['ismessaging']); | |
2403 | $this->assertFalse($message2['sentfromcurrentuser']); | |
2404 | $this->assertEquals('Cool.', $message2['lastmessage']); | |
4d146f1a | 2405 | $this->assertEquals($messageid2, $message2['messageid']); |
cb805753 | 2406 | $this->assertNull($message2['isonline']); |
6aa01968 MN |
2407 | $this->assertFalse($message2['isread']); |
2408 | $this->assertFalse($message2['isblocked']); | |
2409 | $this->assertEquals(2, $message2['unreadcount']); | |
2410 | ||
2411 | $this->assertEquals($user2->id, $message3['userid']); | |
2412 | $this->assertTrue($message3['ismessaging']); | |
2413 | $this->assertFalse($message3['sentfromcurrentuser']); | |
2414 | $this->assertEquals('Word.', $message3['lastmessage']); | |
4d146f1a | 2415 | $this->assertEquals($messageid1, $message3['messageid']); |
cb805753 | 2416 | $this->assertNull($message3['isonline']); |
6aa01968 MN |
2417 | $this->assertFalse($message3['isread']); |
2418 | $this->assertFalse($message3['isblocked']); | |
2419 | $this->assertEquals(2, $message3['unreadcount']); | |
2420 | } | |
2421 | ||
2422 | /** | |
2423 | * Tests retrieving conversations as another user. | |
2424 | */ | |
2425 | public function test_messagearea_conversations_as_other_user() { | |
2426 | $this->resetAfterTest(true); | |
2427 | ||
2428 | // Set as admin. | |
2429 | $this->setAdminUser(); | |
2430 | ||
2431 | // Create some users. | |
2432 | $user1 = self::getDataGenerator()->create_user(); | |
2433 | $user2 = self::getDataGenerator()->create_user(); | |
2434 | $user3 = self::getDataGenerator()->create_user(); | |
2435 | $user4 = self::getDataGenerator()->create_user(); | |
2436 | ||
2437 | // Send some messages back and forth, have some different conversations with different users. | |
2438 | $time = time(); | |
2439 | $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
2440 | $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
2441 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
4d146f1a | 2442 | $messageid1 = $this->send_message($user2, $user1, 'Word.', 0, $time + 3); |
6aa01968 MN |
2443 | |
2444 | $this->send_message($user1, $user3, 'Booyah', 0, $time + 4); | |
2445 | $this->send_message($user3, $user1, 'Whaaat?', 0, $time + 5); | |
2446 | $this->send_message($user1, $user3, 'Nothing.', 0, $time + 6); | |
4d146f1a | 2447 | $messageid2 = $this->send_message($user3, $user1, 'Cool.', 0, $time + 7); |
6aa01968 MN |
2448 | |
2449 | $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?', 0, $time + 8); | |
2450 | $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.', 0, $time + 9); | |
4d146f1a | 2451 | $messageid3 = $this->send_message($user1, $user4, 'Dope.', 0, $time + 10); |
6aa01968 MN |
2452 | |
2453 | // Retrieve the conversations. | |
2454 | $result = core_message_external::data_for_messagearea_conversations($user1->id); | |
2455 | ||
2456 | // We need to execute the return values cleaning process to simulate the web service server. | |
2457 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_conversations_returns(), | |
2458 | $result); | |
2459 | ||
2460 | // Confirm the data is correct. | |
2461 | $messages = $result['contacts']; | |
2462 | $this->assertCount(3, $messages); | |
2463 | ||
2464 | $message1 = $messages[0]; | |
2465 | $message2 = $messages[1]; | |
2466 | $message3 = $messages[2]; | |
2467 | ||
2468 | $this->assertEquals($user4->id, $message1['userid']); | |
2469 | $this->assertTrue($message1['ismessaging']); | |
2470 | $this->assertTrue($message1['sentfromcurrentuser']); | |
2471 | $this->assertEquals('Dope.', $message1['lastmessage']); | |
4d146f1a | 2472 | $this->assertEquals($messageid3, $message1['messageid']); |
6aa01968 | 2473 | $this->assertFalse($message1['isonline']); |
4d146f1a | 2474 | $this->assertFalse($message1['isread']); |
6aa01968 | 2475 | $this->assertFalse($message1['isblocked']); |
4d146f1a | 2476 | $this->assertEquals(1, $message1['unreadcount']); |
6aa01968 MN |
2477 | |
2478 | $this->assertEquals($user3->id, $message2['userid']); | |
2479 | $this->assertTrue($message2['ismessaging']); | |
2480 | $this->assertFalse($message2['sentfromcurrentuser']); | |
2481 | $this->assertEquals('Cool.', $message2['lastmessage']); | |
4d146f1a | 2482 | $this->assertEquals($messageid2, $message2['messageid']); |
6aa01968 MN |
2483 | $this->assertFalse($message2['isonline']); |
2484 | $this->assertFalse($message2['isread']); | |
2485 | $this->assertFalse($message2['isblocked']); | |
2486 | $this->assertEquals(2, $message2['unreadcount']); | |
2487 | ||
2488 | $this->assertEquals($user2->id, $message3['userid']); | |
2489 | $this->assertTrue($message3['ismessaging']); | |
2490 | $this->assertFalse($message3['sentfromcurrentuser']); | |
2491 | $this->assertEquals('Word.', $message3['lastmessage']); | |
4d146f1a | 2492 | $this->assertEquals($messageid1, $message3['messageid']); |
6aa01968 MN |
2493 | $this->assertFalse($message3['isonline']); |
2494 | $this->assertFalse($message3['isread']); | |
2495 | $this->assertFalse($message3['isblocked']); | |
2496 | $this->assertEquals(2, $message3['unreadcount']); | |
2497 | } | |
2498 | ||
2499 | /** | |
2500 | * Tests retrieving conversations as another user without the proper capabilities. | |
2501 | */ | |
2502 | public function test_messagearea_conversations_as_other_user_without_cap() { | |
2503 | $this->resetAfterTest(true); | |
2504 | ||
2505 | // Create some users. | |
2506 | $user1 = self::getDataGenerator()->create_user(); | |
2507 | $user2 = self::getDataGenerator()->create_user(); | |
2508 | ||
2509 | // The person retrieving the conversations for another user. | |
2510 | $this->setUser($user1); | |
2511 | ||
2512 | // Ensure an exception is thrown. | |
2513 | $this->expectException('moodle_exception'); | |
2514 | core_message_external::data_for_messagearea_conversations($user2->id); | |
2515 | } | |
2516 | ||
2517 | /** | |
2518 | * Tests retrieving conversations with messaging disabled. | |
2519 | */ | |
2520 | public function test_messagearea_conversations_messaging_disabled() { | |
2521 | global $CFG; | |
2522 | ||
2523 | $this->resetAfterTest(true); | |
2524 | ||
2525 | // Create some skeleton data just so we can call the WS. | |
2526 | $user = self::getDataGenerator()->create_user(); | |
2527 | ||
2528 | // The person retrieving the conversations. | |
2529 | $this->setUser($user); | |
2530 | ||
2531 | // Disable messaging. | |
2532 | $CFG->messaging = 0; | |
2533 | ||
2534 | // Ensure an exception is thrown. | |
2535 | $this->expectException('moodle_exception'); | |
2536 | core_message_external::data_for_messagearea_conversations($user->id); | |
2537 | } | |
2538 | ||
2539 | /** | |
2540 | * Tests retrieving contacts. | |
2541 | */ | |
2542 | public function test_messagearea_contacts() { | |
2543 | $this->resetAfterTest(true); | |
2544 | ||
2545 | // Create some users. | |
2546 | $user1 = self::getDataGenerator()->create_user(); | |
2547 | ||
2548 | // Set as the user. | |
2549 | $this->setUser($user1); | |
2550 | ||
2551 | $user2 = new stdClass(); | |
2552 | $user2->firstname = 'User'; | |
2553 | $user2->lastname = 'A'; | |
2554 | $user2 = self::getDataGenerator()->create_user($user2); | |
2555 | ||
2556 | $user3 = new stdClass(); | |
2557 | $user3->firstname = 'User'; | |
2558 | $user3->lastname = 'B'; | |
2559 | $user3 = self::getDataGenerator()->create_user($user3); | |
2560 | ||
2561 | $user4 = new stdClass(); | |
2562 | $user4->firstname = 'User'; | |
2563 | $user4->lastname = 'C'; | |
2564 | $user4 = self::getDataGenerator()->create_user($user4); | |
2565 | ||
2566 | $user5 = new stdClass(); | |
2567 | $user5->firstname = 'User'; | |
2568 | $user5->lastname = 'D'; | |
2569 | $user5 = self::getDataGenerator()->create_user($user5); | |
2570 | ||
2571 | // Add some users as contacts. | |
f219eac7 MN |
2572 | \core_message\api::add_contact($user1->id, $user2->id); |
2573 | \core_message\api::add_contact($user1->id, $user3->id); | |
2574 | \core_message\api::add_contact($user1->id, $user4->id); | |
6aa01968 MN |
2575 | |
2576 | // Retrieve the contacts. | |
2577 | $result = core_message_external::data_for_messagearea_contacts($user1->id); | |
2578 | ||
2579 | // We need to execute the return values cleaning process to simulate the web service server. | |
2580 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(), | |
2581 | $result); | |
2582 | ||
2583 | // Confirm the data is correct. | |
2584 | $contacts = $result['contacts']; | |
f219eac7 | 2585 | usort($contacts, ['static', 'sort_contacts']); |
6aa01968 MN |
2586 | $this->assertCount(3, $contacts); |
2587 | ||
2588 | $contact1 = $contacts[0]; | |
2589 | $contact2 = $contacts[1]; | |
2590 | $contact3 = $contacts[2]; | |
2591 | ||
2592 | $this->assertEquals($user2->id, $contact1['userid']); | |
2593 | $this->assertFalse($contact1['ismessaging']); | |
2594 | $this->assertFalse($contact1['sentfromcurrentuser']); | |
2595 | $this->assertNull($contact1['lastmessage']); | |
2596 | $this->assertNull($contact1['messageid']); | |
cb805753 | 2597 | $this->assertNull($contact1['isonline']); |
6aa01968 MN |
2598 | $this->assertFalse($contact1['isread']); |
2599 | $this->assertFalse($contact1['isblocked']); | |
2600 | $this->assertNull($contact1['unreadcount']); | |
2601 | ||
2602 | $this->assertEquals($user3->id, $contact2['userid']); | |
2603 | $this->assertFalse($contact2['ismessaging']); | |
2604 | $this->assertFalse($contact2['sentfromcurrentuser']); | |
2605 | $this->assertNull($contact2['lastmessage']); | |
2606 | $this->assertNull($contact2['messageid']); | |
cb805753 | 2607 | $this->assertNull($contact2['isonline']); |
6aa01968 MN |
2608 | $this->assertFalse($contact2['isread']); |
2609 | $this->assertFalse($contact2['isblocked']); | |
2610 | $this->assertNull($contact2['unreadcount']); | |
2611 | ||
2612 | $this->assertEquals($user4->id, $contact3['userid']); | |
2613 | $this->assertFalse($contact3['ismessaging']); | |
2614 | $this->assertFalse($contact3['sentfromcurrentuser']); | |
2615 | $this->assertNull($contact3['lastmessage']); | |
2616 | $this->assertNull($contact3['messageid']); | |
cb805753 | 2617 | $this->assertNull($contact3['isonline']); |
6aa01968 MN |
2618 | $this->assertFalse($contact3['isread']); |
2619 | $this->assertFalse($contact3['isblocked']); | |
2620 | $this->assertNull($contact3['unreadcount']); | |
2621 | } | |
2622 | ||
2623 | /** | |
2624 | * Tests retrieving contacts as another user. | |
2625 | */ | |
2626 | public function test_messagearea_contacts_as_other_user() { | |
2627 | $this->resetAfterTest(true); | |
2628 | ||
2629 | $this->setAdminUser(); | |
2630 | ||
2631 | // Create some users. | |
2632 | $user1 = self::getDataGenerator()->create_user(); | |
2633 | ||
2634 | $user2 = new stdClass(); | |
2635 | $user2->firstname = 'User'; | |
2636 | $user2->lastname = 'A'; | |
2637 | $user2 = self::getDataGenerator()->create_user($user2); | |
2638 | ||
2639 | $user3 = new stdClass(); | |
2640 | $user3->firstname = 'User'; | |
2641 | $user3->lastname = 'B'; | |
2642 | $user3 = self::getDataGenerator()->create_user($user3); | |
2643 | ||
2644 | $user4 = new stdClass(); | |
2645 | $user4->firstname = 'User'; | |
2646 | $user4->lastname = 'C'; | |
2647 | $user4 = self::getDataGenerator()->create_user($user4); | |
2648 | ||
2649 | $user5 = new stdClass(); | |
2650 | $user5->firstname = 'User'; | |
2651 | $user5->lastname = 'D'; | |
2652 | $user5 = self::getDataGenerator()->create_user($user5); | |
2653 | ||
2654 | // Add some users as contacts. | |
f219eac7 MN |
2655 | \core_message\api::add_contact($user1->id, $user2->id); |
2656 | \core_message\api::add_contact($user1->id, $user3->id); | |
2657 | \core_message\api::add_contact($user1->id, $user4->id); | |
6aa01968 MN |
2658 | |
2659 | // Retrieve the contacts. | |
2660 | $result = core_message_external::data_for_messagearea_contacts($user1->id); | |
2661 | ||
2662 | // We need to execute the return values cleaning process to simulate the web service server. | |
2663 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_contacts_returns(), | |
2664 | $result); | |
2665 | ||
2666 | // Confirm the data is correct. | |
2667 | $contacts = $result['contacts']; | |
f219eac7 | 2668 | usort($contacts, ['static', 'sort_contacts']); |
6aa01968 MN |
2669 | $this->assertCount(3, $contacts); |
2670 | ||
2671 | $contact1 = $contacts[0]; | |
2672 | $contact2 = $contacts[1]; | |
2673 | $contact3 = $contacts[2]; | |
2674 | ||
2675 | $this->assertEquals($user2->id, $contact1['userid']); | |
2676 | $this->assertFalse($contact1['ismessaging']); | |
2677 | $this->assertFalse($contact1['sentfromcurrentuser']); | |
2678 | $this->assertNull($contact1['lastmessage']); | |
2679 | $this->assertNull($contact1['messageid']); | |
2680 | $this->assertFalse($contact1['isonline']); | |
2681 | $this->assertFalse($contact1['isread']); | |
2682 | $this->assertFalse($contact1['isblocked']); | |
2683 | $this->assertNull($contact1['unreadcount']); | |
2684 | ||
2685 | $this->assertEquals($user3->id, $contact2['userid']); | |
2686 | $this->assertFalse($contact2['ismessaging']); | |
2687 | $this->assertFalse($contact2['sentfromcurrentuser']); | |
2688 | $this->assertNull($contact2['lastmessage']); | |
2689 | $this->assertNull($contact2['messageid']); | |
2690 | $this->assertFalse($contact2['isonline']); | |
2691 | $this->assertFalse($contact2['isread']); | |
2692 | $this->assertFalse($contact2['isblocked']); | |
2693 | $this->assertNull($contact2['unreadcount']); | |
2694 | ||
2695 | $this->assertEquals($user4->id, $contact3['userid']); | |
2696 | $this->assertFalse($contact3['ismessaging']); | |
2697 | $this->assertFalse($contact3['sentfromcurrentuser']); | |
2698 | $this->assertNull($contact3['lastmessage']); | |
2699 | $this->assertNull($contact3['messageid']); | |
2700 | $this->assertFalse($contact3['isonline']); | |
2701 | $this->assertFalse($contact3['isread']); | |
2702 | $this->assertFalse($contact3['isblocked']); | |
2703 | $this->assertNull($contact3['unreadcount']); | |
2704 | } | |
2705 | ||
2706 | /** | |
2707 | * Tests retrieving contacts as another user without the proper capabilities. | |
2708 | */ | |
2709 | public function test_messagearea_contacts_as_other_user_without_cap() { | |
2710 | $this->resetAfterTest(true); | |
2711 | ||
2712 | // Create some users. | |
2713 | $user1 = self::getDataGenerator()->create_user(); | |
2714 | $user2 = self::getDataGenerator()->create_user(); | |
2715 | ||
2716 | // The person retrieving the contacts for another user. | |
2717 | $this->setUser($user1); | |
2718 | ||
2719 | // Perform the WS call and ensure an exception is thrown. | |
2720 | $this->expectException('moodle_exception'); | |
2721 | core_message_external::data_for_messagearea_contacts($user2->id); | |
2722 | } | |
2723 | ||
2724 | /** | |
2725 | * Tests retrieving contacts with messaging disabled. | |
2726 | */ | |
2727 | public function test_messagearea_contacts_messaging_disabled() { | |
2728 | global $CFG; | |
2729 | ||
2730 | $this->resetAfterTest(true); | |
2731 | ||
2732 | // Create some skeleton data just so we can call the WS. | |
2733 | $user = self::getDataGenerator()->create_user(); | |
2734 | ||
2735 | // The person retrieving the contacts. | |
2736 | $this->setUser($user); | |
2737 | ||
2738 | // Disable messaging. | |
2739 | $CFG->messaging = 0; | |
2740 | ||
2741 | // Perform the WS call and ensure we are shown that it is disabled. | |
2742 | $this->expectException('moodle_exception'); | |
2743 | core_message_external::data_for_messagearea_contacts($user->id); | |
2744 | } | |
2745 | ||
2746 | /** | |
2747 | * Tests retrieving messages. | |
2748 | */ | |
2749 | public function test_messagearea_messages() { | |
2750 | $this->resetAfterTest(true); | |
2751 | ||
2752 | // Create some users. | |
2753 | $user1 = self::getDataGenerator()->create_user(); | |
2754 | $user2 = self::getDataGenerator()->create_user(); | |
2755 | ||
2756 | // The person asking for the messages. | |
2757 | $this->setUser($user1); | |
2758 | ||
2759 | // Send some messages back and forth. | |
2760 | $time = time(); | |
2761 | $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
2762 | $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
2763 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
2764 | $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
2765 | ||
2766 | // Retrieve the messages. | |
2767 | $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id); | |
2768 | ||
2769 | // We need to execute the return values cleaning process to simulate the web service server. | |
2770 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(), | |
2771 | $result); | |
2772 | ||
2773 | // Check the results are correct. | |
2774 | $this->assertTrue($result['iscurrentuser']); | |
2775 | $this->assertEquals($user1->id, $result['currentuserid']); | |
2776 | $this->assertEquals($user2->id, $result['otheruserid']); | |
2777 | $this->assertEquals(fullname($user2), $result['otheruserfullname']); | |
cb805753 | 2778 | $this->assertNull($result['isonline']); |
6aa01968 MN |
2779 | |
2780 | // Confirm the message data is correct. | |
2781 | $messages = $result['messages']; | |
2782 | $this->assertCount(4, $messages); | |
2783 | ||
2784 | $message1 = $messages[0]; | |
2785 | $message2 = $messages[1]; | |
2786 | $message3 = $messages[2]; | |
2787 | $message4 = $messages[3]; | |
2788 | ||
2789 | $this->assertEquals($user1->id, $message1['useridfrom']); | |
2790 | $this->assertEquals($user2->id, $message1['useridto']); | |
2791 | $this->assertTrue($message1['displayblocktime']); | |
2792 | $this->assertContains('Yo!', $message1['text']); | |
2793 | ||
2794 | $this->assertEquals($user2->id, $message2['useridfrom']); | |
2795 | $this->assertEquals($user1->id, $message2['useridto']); | |
2796 | $this->assertFalse($message2['displayblocktime']); | |
2797 | $this->assertContains('Sup mang?', $message2['text']); | |
2798 | ||
2799 | $this->assertEquals($user1->id, $message3['useridfrom']); | |
2800 | $this->assertEquals($user2->id, $message3['useridto']); | |
2801 | $this->assertFalse($message3['displayblocktime']); | |
2802 | $this->assertContains('Writing PHPUnit tests!', $message3['text']); | |
2803 | ||
2804 | $this->assertEquals($user2->id, $message4['useridfrom']); | |
2805 | $this->assertEquals($user1->id, $message4['useridto']); | |
2806 | $this->assertFalse($message4['displayblocktime']); | |
2807 | $this->assertContains('Word.', $message4['text']); | |
2808 | } | |
2809 | ||
fb1469d8 RW |
2810 | /** |
2811 | * Tests retrieving messages. | |
2812 | */ | |
ffd7798c | 2813 | public function test_messagearea_messages_timefrom() { |
fb1469d8 RW |
2814 | $this->resetAfterTest(true); |
2815 | ||
2816 | // Create some users. | |
2817 | $user1 = self::getDataGenerator()->create_user(); | |
2818 | $user2 = self::getDataGenerator()->create_user(); | |
2819 | ||
2820 | // The person asking for the messages. | |
2821 | $this->setUser($user1); | |
2822 | ||
2823 | // Send some messages back and forth. | |
2824 | $time = time(); | |
2825 | $this->send_message($user1, $user2, 'Message 1', 0, $time - 4); | |
2826 | $this->send_message($user2, $user1, 'Message 2', 0, $time - 3); | |
2827 | $this->send_message($user1, $user2, 'Message 3', 0, $time - 2); | |
2828 | $this->send_message($user2, $user1, 'Message 4', 0, $time - 1); | |
2829 | ||
ffd7798c | 2830 | // Retrieve the messages from $time - 3, which should be the 3 most recent messages. |
fb1469d8 RW |
2831 | $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id, 0, 0, false, $time - 3); |
2832 | ||
2833 | // We need to execute the return values cleaning process to simulate the web service server. | |
2834 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(), | |
2835 | $result); | |
2836 | ||
2837 | // Confirm the message data is correct. We shouldn't get 'Message 1' back. | |
2838 | $messages = $result['messages']; | |
2839 | $this->assertCount(3, $messages); | |
2840 | ||
2841 | $message1 = $messages[0]; | |
2842 | $message2 = $messages[1]; | |
2843 | $message3 = $messages[2]; | |
2844 | ||
2845 | $this->assertContains('Message 2', $message1['text']); | |
2846 | $this->assertContains('Message 3', $message2['text']); | |
2847 | $this->assertContains('Message 4', $message3['text']); | |
2848 | } | |
2849 | ||
6aa01968 MN |
2850 | /** |
2851 | * Tests retrieving messages as another user. | |
2852 | */ | |
2853 | public function test_messagearea_messages_as_other_user() { | |
2854 | $this->resetAfterTest(true); | |
2855 | ||
2856 | // Set as admin. | |
2857 | $this->setAdminUser(); | |
2858 | ||
2859 | // Create some users. | |
2860 | $user1 = self::getDataGenerator()->create_user(); | |
2861 | $user2 = self::getDataGenerator()->create_user(); | |
2862 | ||
2863 | // Send some messages back and forth. | |
2864 | $time = time(); | |
2865 | $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
2866 | $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
2867 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
2868 | $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
2869 | ||
2870 | // Retrieve the messages. | |
2871 | $result = core_message_external::data_for_messagearea_messages($user1->id, $user2->id); | |
2872 | ||
2873 | // We need to execute the return values cleaning process to simulate the web service server. | |
2874 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_messages_returns(), | |
2875 | $result); | |
2876 | ||
2877 | // Check the results are correct. | |
2878 | $this->assertFalse($result['iscurrentuser']); | |
2879 | $this->assertEquals($user1->id, $result['currentuserid']); | |
2880 | $this->assertEquals($user2->id, $result['otheruserid']); | |
2881 | $this->assertEquals(fullname($user2), $result['otheruserfullname']); | |
2882 | $this->assertFalse($result['isonline']); | |
2883 | ||
2884 | // Confirm the message data is correct. | |
2885 | $messages = $result['messages']; | |
2886 | $this->assertCount(4, $messages); | |
2887 | ||
2888 | $message1 = $messages[0]; | |
2889 | $message2 = $messages[1]; | |
2890 | $message3 = $messages[2]; | |
2891 | $message4 = $messages[3]; | |
2892 | ||
2893 | $this->assertEquals($user1->id, $message1['useridfrom']); | |
2894 | $this->assertEquals($user2->id, $message1['useridto']); | |
2895 | $this->assertTrue($message1['displayblocktime']); | |
2896 | $this->assertContains('Yo!', $message1['text']); | |
2897 | ||
2898 | $this->assertEquals($user2->id, $message2['useridfrom']); | |
2899 | $this->assertEquals($user1->id, $message2['useridto']); | |
2900 | $this->assertFalse($message2['displayblocktime']); | |
2901 | $this->assertContains('Sup mang?', $message2['text']); | |
2902 | ||
2903 | $this->assertEquals($user1->id, $message3['useridfrom']); | |
2904 | $this->assertEquals($user2->id, $message3['useridto']); | |
2905 | $this->assertFalse($message3['displayblocktime']); | |
2906 | $this->assertContains('Writing PHPUnit tests!', $message3['text']); | |
2907 | ||
2908 | $this->assertEquals($user2->id, $message4['useridfrom']); | |
2909 | $this->assertEquals($user1->id, $message4['useridto']); | |
2910 | $this->assertFalse($message4['displayblocktime']); | |
2911 | $this->assertContains('Word.', $message4['text']); | |
2912 | } | |
2913 | ||
2914 | /** | |
2915 | * Tests retrieving messages as another user without the proper capabilities. | |
2916 | */ | |
2917 | public function test_messagearea_messages_as_other_user_without_cap() { | |
2918 | $this->resetAfterTest(true); | |
2919 | ||
2920 | // Create some users. | |
2921 | $user1 = self::getDataGenerator()->create_user(); | |
2922 | $user2 = self::getDataGenerator()->create_user(); | |
2923 | $user3 = self::getDataGenerator()->create_user(); | |
2924 | ||
2925 | // The person asking for the messages for another user. | |
2926 | $this->setUser($user1); | |
2927 | ||
2928 | // Ensure an exception is thrown. | |
2929 | $this->expectException('moodle_exception'); | |
2930 | core_message_external::data_for_messagearea_messages($user2->id, $user3->id); | |
2931 | } | |
2932 | ||
2933 | /** | |
2934 | * Tests retrieving messages with messaging disabled. | |
2935 | */ | |
2936 | public function test_messagearea_messages_messaging_disabled() { | |
2937 | global $CFG; | |
2938 | ||
2939 | $this->resetAfterTest(true); | |
2940 | ||
2941 | // Create some skeleton data just so we can call the WS. | |
2942 | $user1 = self::getDataGenerator()->create_user(); | |
2943 | $user2 = self::getDataGenerator()->create_user(); | |
2944 | ||
2945 | // The person asking for the messages for another user. | |
2946 | $this->setUser($user1); | |
2947 | ||
2948 | // Disable messaging. | |
2949 | $CFG->messaging = 0; | |
2950 | ||
2951 | // Ensure an exception is thrown. | |
2952 | $this->expectException('moodle_exception'); | |
2953 | core_message_external::data_for_messagearea_messages($user1->id, $user2->id); | |
2954 | } | |
2955 | ||
2956 | /** | |
2957 | * Tests retrieving most recent message. | |
2958 | */ | |
2959 | public function test_messagearea_get_most_recent_message() { | |
2960 | $this->resetAfterTest(true); | |
2961 | ||
2962 | // Create some users. | |
2963 | $user1 = self::getDataGenerator()->create_user(); | |
2964 | $user2 = self::getDataGenerator()->create_user(); | |
2965 | ||
2966 | // The person doing the search. | |
2967 | $this->setUser($user1); | |
2968 | ||
2969 | // Send some messages back and forth. | |
2970 | $time = time(); | |
2971 | $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
2972 | $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
2973 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
2974 | $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
2975 | ||
2976 | // Get the most recent message. | |
2977 | $result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id); | |
2978 | ||
2979 | // We need to execute the return values cleaning process to simulate the web service server. | |
2980 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(), | |
2981 | $result); | |
2982 | ||
2983 | // Check the results are correct. | |
2984 | $this->assertEquals($user2->id, $result['useridfrom']); | |
2985 | $this->assertEquals($user1->id, $result['useridto']); | |
2986 | $this->assertContains('Word.', $result['text']); | |
2987 | } | |
2988 | ||
2989 | /** | |
2990 | * Tests retrieving most recent message as another user. | |
2991 | */ | |
2992 | public function test_messagearea_get_most_recent_message_as_other_user() { | |
2993 | $this->resetAfterTest(true); | |
2994 | ||
2995 | // The person doing the search. | |
2996 | $this->setAdminUser(); | |
2997 | ||
2998 | // Create some users. | |
2999 | $user1 = self::getDataGenerator()->create_user(); | |
3000 | $user2 = self::getDataGenerator()->create_user(); | |
3001 | ||
3002 | // Send some messages back and forth. | |
3003 | $time = time(); | |
3004 | $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
3005 | $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
3006 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
3007 | $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
3008 | ||
3009 | // Get the most recent message. | |
3010 | $result = core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id); | |
3011 | ||
3012 | // We need to execute the return values cleaning process to simulate the web service server. | |
3013 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_most_recent_message_returns(), | |
3014 | $result); | |
3015 | ||
3016 | // Check the results are correct. | |
3017 | $this->assertEquals($user2->id, $result['useridfrom']); | |
3018 | $this->assertEquals($user1->id, $result['useridto']); | |
3019 | $this->assertContains('Word.', $result['text']); | |
3020 | } | |
3021 | ||
3022 | /** | |
3023 | * Tests retrieving most recent message as another user without the proper capabilities. | |
3024 | */ | |
3025 | public function test_messagearea_get_most_recent_message_as_other_user_without_cap() { | |
3026 | $this->resetAfterTest(true); | |
3027 | ||
3028 | // Create some users. | |
3029 | $user1 = self::getDataGenerator()->create_user(); | |
3030 | $user2 = self::getDataGenerator()->create_user(); | |
3031 | $user3 = self::getDataGenerator()->create_user(); | |
3032 | ||
3033 | // The person asking for the most recent message for another user. | |
3034 | $this->setUser($user1); | |
3035 | ||
3036 | // Ensure an exception is thrown. | |
3037 | $this->expectException('moodle_exception'); | |
3038 | core_message_external::data_for_messagearea_get_most_recent_message($user2->id, $user3->id); | |
3039 | } | |
3040 | ||
3041 | /** | |
3042 | * Tests retrieving most recent message with messaging disabled. | |
3043 | */ | |
3044 | public function test_messagearea_get_most_recent_message_messaging_disabled() { | |
3045 | global $CFG; | |
3046 | ||
3047 | $this->resetAfterTest(true); | |
3048 | ||
3049 | // Create some skeleton data just so we can call the WS. | |
3050 | $user1 = self::getDataGenerator()->create_user(); | |
3051 | $user2 = self::getDataGenerator()->create_user(); | |
3052 | ||
3053 | // The person asking for the most recent message. | |
3054 | $this->setUser($user1); | |
3055 | ||
3056 | // Disable messaging. | |
3057 | $CFG->messaging = 0; | |
3058 | ||
3059 | // Ensure an exception is thrown. | |
3060 | $this->expectException('moodle_exception'); | |
3061 | core_message_external::data_for_messagearea_get_most_recent_message($user1->id, $user2->id); | |
3062 | } | |
3063 | ||
3064 | /** | |
3065 | * Tests retrieving a user's profile. | |
3066 | */ | |
3067 | public function test_messagearea_get_profile() { | |
3068 | $this->resetAfterTest(true); | |
3069 | ||
3070 | // Create some users. | |
3071 | $user1 = self::getDataGenerator()->create_user(); | |
3072 | $user2 = self::getDataGenerator()->create_user(); | |
3073 | ||
3074 | // The person asking for the profile information. | |
3075 | $this->setUser($user1); | |
3076 | ||
3077 | // Get the profile. | |
3078 | $result = core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id); | |
3079 | ||
3080 | // We need to execute the return values cleaning process to simulate the web service server. | |
3081 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_profile_returns(), | |
3082 | $result); | |
3083 | ||
3084 | $this->assertEquals($user2->id, $result['userid']); | |
3085 | $this->assertEmpty($result['email']); | |
3086 | $this->assertEmpty($result['country']); | |
3087 | $this->assertEmpty($result['city']); | |
3088 | $this->assertEquals(fullname($user2), $result['fullname']); | |
cb805753 | 3089 | $this->assertNull($result['isonline']); |
6aa01968 MN |
3090 | $this->assertFalse($result['isblocked']); |
3091 | $this->assertFalse($result['iscontact']); | |
3092 | } | |
3093 | ||
3094 | /** | |
3095 | * Tests retrieving a user's profile as another user. | |
3096 | */ | |
3097 | public function test_messagearea_profile_as_other_user() { | |
3098 | $this->resetAfterTest(true); | |
3099 | ||
3100 | // The person asking for the profile information. | |
3101 | $this->setAdminUser(); | |
3102 | ||
3103 | // Create some users. | |
3104 | $user1 = self::getDataGenerator()->create_user(); | |
3105 | ||
3106 | $user2 = new stdClass(); | |
3107 | $user2->country = 'AU'; | |
3108 | $user2->city = 'Perth'; | |
3109 | $user2 = self::getDataGenerator()->create_user($user2); | |
3110 | ||
3111 | // Get the profile. | |
3112 | $result = core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id); | |
3113 | ||
3114 | // We need to execute the return values cleaning process to simulate the web service server. | |
3115 | $result = external_api::clean_returnvalue(core_message_external::data_for_messagearea_get_profile_returns(), | |
3116 | $result); | |
3117 | ||
3118 | $this->assertEquals($user2->id, $result['userid']); | |
3119 | $this->assertEquals($user2->email, $result['email']); | |
3120 | $this->assertEquals(get_string($user2->country, 'countries'), $result['country']); | |
3121 | $this->assertEquals($user2->city, $result['city']); | |
3122 | $this->assertEquals(fullname($user2), $result['fullname']); | |
3123 | $this->assertFalse($result['isonline']); | |
3124 | $this->assertFalse($result['isblocked']); | |
3125 | $this->assertFalse($result['iscontact']); | |
3126 | } | |
3127 | ||
3128 | /** | |
3129 | * Tests retrieving a user's profile as another user without the proper capabilities. | |
3130 | */ | |
3131 | public function test_messagearea_profile_as_other_user_without_cap() { | |
3132 | $this->resetAfterTest(true); | |
3133 | ||
3134 | // Create some users. | |
3135 | $user1 = self::getDataGenerator()->create_user(); | |
3136 | $user2 = self::getDataGenerator()->create_user(); | |
3137 | $user3 = self::getDataGenerator()->create_user(); | |
3138 | ||
3139 | // The person asking for the profile information for another user. | |
3140 | $this->setUser($user1); | |
3141 | ||
3142 | // Ensure an exception is thrown. | |
3143 | $this->expectException('moodle_exception'); | |
3144 | core_message_external::data_for_messagearea_get_profile($user2->id, $user3->id); | |
3145 | } | |
3146 | ||
3147 | /** | |
3148 | * Tests retrieving a user's profile with messaging disabled. | |
3149 | */ | |
3150 | public function test_messagearea_profile_messaging_disabled() { | |
3151 | global $CFG; | |
3152 | ||
3153 | $this->resetAfterTest(true); | |
3154 | ||
3155 | // Create some skeleton data just so we can call the WS. | |
3156 | $user1 = self::getDataGenerator()->create_user(); | |
3157 | $user2 = self::getDataGenerator()->create_user(); | |
3158 | ||
3159 | // The person asking for the profile information. | |
3160 | $this->setUser($user1); | |
3161 | ||
3162 | // Disable messaging. | |
3163 | $CFG->messaging = 0; | |
3164 | ||
3165 | // Ensure an exception is thrown. | |
3166 | $this->expectException('moodle_exception'); | |
3167 | core_message_external::data_for_messagearea_get_profile($user1->id, $user2->id); | |
3168 | } | |
3169 | ||
3170 | /** | |
3171 | * Test marking all message as read with an invalid user. | |
3172 | */ | |
3173 | public function test_mark_all_messages_as_read_invalid_user_exception() { | |
3174 | $this->resetAfterTest(true); | |
3175 | ||
3176 | $this->expectException('moodle_exception'); | |
3177 | core_message_external::mark_all_messages_as_read(-2132131, 0); | |
3178 | } | |
3179 | ||
3180 | /** | |
3181 | * Test marking all message as read without proper access. | |
3182 | */ | |
3183 | public function test_mark_all_messages_as_read_access_denied_exception() { | |
3184 | $this->resetAfterTest(true); | |
3185 | ||
3186 | $sender = $this->getDataGenerator()->create_user(); | |
3187 | $user = $this->getDataGenerator()->create_user(); | |
3188 | ||
3189 | $this->setUser($user); | |
3190 | $this->expectException('moodle_exception'); | |
3191 | core_message_external::mark_all_messages_as_read($sender->id, 0); | |
3192 | } | |
3193 | ||
3194 | /** | |
3195 | * Test marking all message as read with missing from user. | |
3196 | */ | |
3197 | public function test_mark_all_messages_as_read_missing_from_user_exception() { | |
3198 | $this->resetAfterTest(true); | |
3199 | ||
3200 | $sender = $this->getDataGenerator()->create_user(); | |
3201 | ||
3202 | $this->setUser($sender); | |
3203 | $this->expectException('moodle_exception'); | |
3204 | core_message_external::mark_all_messages_as_read($sender->id, 99999); | |
3205 | } | |
3206 | ||
3207 | /** | |
3208 | * Test marking all message as read. | |
3209 | */ | |
3210 | public function test_mark_all_messages_as_read() { | |
3211 | global $DB; | |
3212 | ||
3213 | $this->resetAfterTest(true); | |
3214 | ||
3215 | $sender1 = $this->getDataGenerator()->create_user(); | |
3216 | $sender2 = $this->getDataGenerator()->create_user(); | |
3217 | $sender3 = $this->getDataGenerator()->create_user(); | |
3218 | $recipient = $this->getDataGenerator()->create_user(); | |
3219 | ||
3220 | $this->setUser($recipient); | |
3221 | ||
3222 | $this->send_message($sender1, $recipient, 'Message'); | |
3223 | $this->send_message($sender1, $recipient, 'Message'); | |
3224 | $this->send_message($sender2, $recipient, 'Message'); | |
3225 | $this->send_message($sender2, $recipient, 'Message'); | |
3226 | $this->send_message($sender3, $recipient, 'Message'); | |
3227 | $this->send_message($sender3, $recipient, 'Message'); | |
3228 | ||
3229 | core_message_external::mark_all_messages_as_read($recipient->id, $sender1->id); | |
883ce421 | 3230 | $this->assertEquals(2, $DB->count_records('message_user_actions')); |
6aa01968 MN |
3231 | |
3232 | core_message_external::mark_all_messages_as_read($recipient->id, 0); | |
883ce421 | 3233 | $this->assertEquals(6, $DB->count_records('message_user_actions')); |
6aa01968 MN |
3234 | } |
3235 | ||
3236 | /** | |
3237 | * Test getting unread conversation count. | |
3238 | */ | |
3239 | public function test_get_unread_conversations_count() { | |
3240 | $this->resetAfterTest(true); | |
3241 | ||
3242 | // Create some users. | |
3243 | $user1 = self::getDataGenerator()->create_user(); | |
3244 | $user2 = self::getDataGenerator()->create_user(); | |
3245 | $user3 = self::getDataGenerator()->create_user(); | |
3246 | $user4 = self::getDataGenerator()->create_user(); | |
3247 | ||
3248 | // The person wanting the conversation count. | |
3249 | $this->setUser($user1); | |
3250 | ||
3251 | // Send some messages back and forth, have some different conversations with different users. | |
3252 | $this->send_message($user1, $user2, 'Yo!'); | |
3253 | $this->send_message($user2, $user1, 'Sup mang?'); | |
3254 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!'); | |
3255 | $this->send_message($user2, $user1, 'Word.'); | |
3256 | ||
3257 | $this->send_message($user1, $user3, 'Booyah'); | |
3258 | $this->send_message($user3, $user1, 'Whaaat?'); | |
3259 | $this->send_message($user1, $user3, 'Nothing.'); | |
3260 | $this->send_message($user3, $user1, 'Cool.'); | |
3261 | ||
3262 | $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?'); | |
3263 | $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.'); | |
3264 | $this->send_message($user1, $user4, 'Dope.'); | |
3265 | ||
3266 | // Get the unread conversation count. | |
3267 | $result = core_message_external::get_unread_conversations_count($user1->id); | |
3268 | ||
3269 | // We need to execute the return values cleaning process to simulate the web service server. | |
3270 | $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(), | |
3271 | $result); | |
3272 | ||
3273 | $this->assertEquals(3, $result); | |
3274 | } | |
3275 | ||
3276 | /** | |
3277 | * Test getting unread conversation count as other user. | |
3278 | */ | |
3279 | public function test_get_unread_conversations_count_as_other_user() { | |
3280 | $this->resetAfterTest(true); | |
3281 | ||
3282 | // The person wanting the conversation count. | |
3283 | $this->setAdminUser(); | |
3284 | ||
3285 | // Create some users. | |
3286 | $user1 = self::getDataGenerator()->create_user(); | |
3287 | $user2 = self::getDataGenerator()->create_user(); | |
3288 | $user3 = self::getDataGenerator()->create_user(); | |
3289 | $user4 = self::getDataGenerator()->create_user(); | |
3290 | ||
3291 | // Send some messages back and forth, have some different conversations with different users. | |
3292 | $this->send_message($user1, $user2, 'Yo!'); | |
3293 | $this->send_message($user2, $user1, 'Sup mang?'); | |
3294 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!'); | |
3295 | $this->send_message($user2, $user1, 'Word.'); | |
3296 | ||
3297 | $this->send_message($user1, $user3, 'Booyah'); | |
3298 | $this->send_message($user3, $user1, 'Whaaat?'); | |
3299 | $this->send_message($user1, $user3, 'Nothing.'); | |
3300 | $this->send_message($user3, $user1, 'Cool.'); | |
3301 | ||
3302 | $this->send_message($user1, $user4, 'Hey mate, you see the new messaging UI in Moodle?'); | |
3303 | $this->send_message($user4, $user1, 'Yah brah, it\'s pretty rad.'); | |
3304 | $this->send_message($user1, $user4, 'Dope.'); | |
3305 | ||
3306 | // Get the unread conversation count. | |
3307 | $result = core_message_external::get_unread_conversations_count($user1->id); | |
3308 | ||
3309 | // We need to execute the return values cleaning process to simulate the web service server. | |
3310 | $result = external_api::clean_returnvalue(core_message_external::get_unread_conversations_count_returns(), | |
3311 | $result); | |
3312 | ||
3313 | $this->assertEquals(3, $result); | |
3314 | } | |
3315 | ||
3316 | /** | |
3317 | * Test getting unread conversation count as other user without proper capability. | |
3318 | */ | |
3319 | public function test_get_unread_conversations_count_as_other_user_without_cap() { | |
3320 | $this->resetAfterTest(true); | |
3321 | ||
3322 | // Create some users. | |
3323 | $user1 = self::getDataGenerator()->create_user(); | |
3324 | $user2 = self::getDataGenerator()->create_user(); | |
3325 | ||
3326 | // The person wanting the conversation count. | |
3327 | $this->setUser($user1); | |
3328 | ||
3329 | // Ensure an exception is thrown. | |
3330 | $this->expectException('moodle_exception'); | |
3331 | core_message_external::get_unread_conversations_count($user2->id); | |
3332 | } | |
3333 | ||
3334 | /** | |
3335 | * Test deleting conversation. | |
3336 | */ | |
3337 | public function test_delete_conversation() { | |
3338 | global $DB; | |
3339 | ||
3340 | $this->resetAfterTest(true); | |
3341 | ||
3342 | // Create some users. | |
3343 | $user1 = self::getDataGenerator()->create_user(); | |
3344 | $user2 = self::getDataGenerator()->create_user(); | |
3345 | ||
3346 | // The person wanting to delete the conversation. | |
3347 | $this->setUser($user1); | |
3348 | ||
3349 | // Send some messages back and forth. | |
3350 | $time = time(); | |
883ce421 MN |
3351 | $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time); |
3352 | $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
3353 | $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
3354 | $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
6aa01968 MN |
3355 | |
3356 | // Delete the conversation. | |
3357 | core_message_external::delete_conversation($user1->id, $user2->id); | |
3358 | ||
883ce421 MN |
3359 | $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC'); |
3360 | $this->assertCount(4, $muas); | |
3361 | // Sort by id. | |
3362 | ksort($muas); | |
6aa01968 | 3363 | |
883ce421 MN |
3364 | $mua1 = array_shift($muas); |
3365 | $mua2 = array_shift($muas); | |
3366 | $mua3 = array_shift($muas); | |
3367 | $mua4 = array_shift($muas); | |
6aa01968 | 3368 | |
883ce421 MN |
3369 | $this->assertEquals($user1->id, $mua1->userid); |
3370 | $this->assertEquals($m1id, $mua1->messageid); | |
3371 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action); | |
6aa01968 | 3372 | |
883ce421 MN |
3373 | $this->assertEquals($user1->id, $mua2->userid); |
3374 | $this->assertEquals($m2id, $mua2->messageid); | |
3375 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action); | |
6aa01968 | 3376 | |
883ce421 MN |
3377 | $this->assertEquals($user1->id, $mua3->userid); |
3378 | $this->assertEquals($m3id, $mua3->messageid); | |
3379 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action); | |
6aa01968 | 3380 | |
883ce421 MN |
3381 | $this->assertEquals($user1->id, $mua4->userid); |
3382 | $this->assertEquals($m4id, $mua4->messageid); | |
3383 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action); | |
6aa01968 MN |
3384 | } |
3385 | ||
3386 | /** | |
3387 | * Test deleting conversation as other user. | |
3388 | */ | |
3389 | public function test_delete_conversation_as_other_user() { | |
3390 | global $DB; | |
3391 | ||
3392 | $this->resetAfterTest(true); | |
3393 | ||
3394 | $this->setAdminUser(); | |
3395 | ||
3396 | // Create some users. | |
3397 | $user1 = self::getDataGenerator()->create_user(); | |
3398 | $user2 = self::getDataGenerator()->create_user(); | |
3399 | ||
3400 | // Send some messages back and forth. | |
3401 | $time = time(); | |
883ce421 MN |
3402 | $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time); |
3403 | $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
3404 | $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
3405 | $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
6aa01968 MN |
3406 | |
3407 | // Delete the conversation. | |
3408 | core_message_external::delete_conversation($user1->id, $user2->id); | |
3409 | ||
883ce421 MN |
3410 | $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC'); |
3411 | $this->assertCount(4, $muas); | |
3412 | // Sort by id. | |
3413 | ksort($muas); | |
6aa01968 | 3414 | |
883ce421 MN |
3415 | $mua1 = array_shift($muas); |
3416 | $mua2 = array_shift($muas); | |
3417 | $mua3 = array_shift($muas); | |
3418 | $mua4 = array_shift($muas); | |
6aa01968 | 3419 | |
883ce421 MN |
3420 | $this->assertEquals($user1->id, $mua1->userid); |
3421 | $this->assertEquals($m1id, $mua1->messageid); | |
3422 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action); | |
6aa01968 | 3423 | |
883ce421 MN |
3424 | $this->assertEquals($user1->id, $mua2->userid); |
3425 | $this->assertEquals($m2id, $mua2->messageid); | |
3426 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action); | |
6aa01968 | 3427 | |
883ce421 MN |
3428 | $this->assertEquals($user1->id, $mua3->userid); |
3429 | $this->assertEquals($m3id, $mua3->messageid); | |
3430 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action); | |
6aa01968 | 3431 | |
883ce421 MN |
3432 | $this->assertEquals($user1->id, $mua4->userid); |
3433 | $this->assertEquals($m4id, $mua4->messageid); | |
3434 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action); | |
6aa01968 MN |
3435 | } |
3436 | ||
3437 | /** | |
3438 | * Test deleting conversation as other user without proper capability. | |
3439 | */ | |
3440 | public function test_delete_conversation_as_other_user_without_cap() { | |
3441 | $this->resetAfterTest(true); | |
3442 | ||
3443 | // Create some users. | |
3444 | $user1 = self::getDataGenerator()->create_user(); | |
3445 | $user2 = self::getDataGenerator()->create_user(); | |
3446 | $user3 = self::getDataGenerator()->create_user(); | |
3447 | ||
15663b0b MN |
3448 | // Send some messages back and forth. |
3449 | $time = time(); | |
3450 | $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
3451 | $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
3452 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
3453 | $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
3454 | ||
6aa01968 MN |
3455 | // The person wanting to delete the conversation. |
3456 | $this->setUser($user3); | |
3457 | ||
3458 | // Ensure an exception is thrown. | |
3459 | $this->expectException('moodle_exception'); | |
3460 | core_message_external::delete_conversation($user1->id, $user2->id); | |
3461 | } | |
3462 | ||
3463 | /** | |
3464 | * Test deleting conversation with messaging disabled. | |
3465 | */ | |
3466 | public function test_delete_conversation_messaging_disabled() { | |
3467 | global $CFG; | |
3468 | ||
3469 | $this->resetAfterTest(true); | |
3470 | ||
3471 | // Create some users. | |
3472 | $user1 = self::getDataGenerator()->create_user(); | |
3473 | $user2 = self::getDataGenerator()->create_user(); | |
3474 | ||
3475 | // The person wanting to delete the conversation. | |
3476 | $this->setUser($user1); | |
3477 | ||
3478 | // Disable messaging. | |
3479 | $CFG->messaging = 0; | |
3480 | ||
3481 | // Ensure an exception is thrown. | |
3482 | $this->expectException('moodle_exception'); | |
3483 | core_message_external::delete_conversation($user1->id, $user2->id); | |
3484 | } | |
3485 | ||
26f39c88 MN |
3486 | /** |
3487 | * Test deleting conversation. | |
3488 | */ | |
3489 | public function test_delete_conversation_by_id() { | |
3490 | global $DB; | |
3491 | ||
3492 | $this->resetAfterTest(true); | |
3493 | ||
3494 | // Create some users. | |
3495 | $user1 = self::getDataGenerator()->create_user(); | |
3496 | $user2 = self::getDataGenerator()->create_user(); | |
3497 | ||
3498 | // The person wanting to delete the conversation. | |
3499 | $this->setUser($user1); | |
3500 | ||
3501 | // Send some messages back and forth. | |
3502 | $time = time(); | |
3503 | $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
3504 | $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
3505 | $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
3506 | $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
3507 | ||
3508 | $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]); | |
3509 | ||
3510 | // Delete the conversation. | |
3511 | core_message_external::delete_conversation_by_id($user1->id, $conversationid); | |
3512 | ||
3513 | $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC'); | |
3514 | $this->assertCount(4, $muas); | |
3515 | // Sort by id. | |
3516 | ksort($muas); | |
3517 | ||
3518 | $mua1 = array_shift($muas); | |
3519 | $mua2 = array_shift($muas); | |
3520 | $mua3 = array_shift($muas); | |
3521 | $mua4 = array_shift($muas); | |
3522 | ||
3523 | $this->assertEquals($user1->id, $mua1->userid); | |
3524 | $this->assertEquals($m1id, $mua1->messageid); | |
3525 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action); | |
3526 | ||
3527 | $this->assertEquals($user1->id, $mua2->userid); | |
3528 | $this->assertEquals($m2id, $mua2->messageid); | |
3529 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action); | |
3530 | ||
3531 | $this->assertEquals($user1->id, $mua3->userid); | |
3532 | $this->assertEquals($m3id, $mua3->messageid); | |
3533 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action); | |
3534 | ||
3535 | $this->assertEquals($user1->id, $mua4->userid); | |
3536 | $this->assertEquals($m4id, $mua4->messageid); | |
3537 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action); | |
3538 | } | |
3539 | ||
3540 | /** | |
3541 | * Test deleting conversation as other user. | |
3542 | */ | |
3543 | public function test_delete_conversation_by_id_as_other_user() { | |
3544 | global $DB; | |
3545 | ||
3546 | $this->resetAfterTest(true); | |
3547 | ||
3548 | $this->setAdminUser(); | |
3549 | ||
3550 | // Create some users. | |
3551 | $user1 = self::getDataGenerator()->create_user(); | |
3552 | $user2 = self::getDataGenerator()->create_user(); | |
3553 | ||
3554 | // Send some messages back and forth. | |
3555 | $time = time(); | |
3556 | $m1id = $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
3557 | $m2id = $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
3558 | $m3id = $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
3559 | $m4id = $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
3560 | ||
3561 | $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]); | |
3562 | ||
3563 | // Delete the conversation. | |
3564 | core_message_external::delete_conversation_by_id($user1->id, $conversationid); | |
3565 | ||
3566 | $muas = $DB->get_records('message_user_actions', array(), 'timecreated ASC'); | |
3567 | $this->assertCount(4, $muas); | |
3568 | // Sort by id. | |
3569 | ksort($muas); | |
3570 | ||
3571 | $mua1 = array_shift($muas); | |
3572 | $mua2 = array_shift($muas); | |
3573 | $mua3 = array_shift($muas); | |
3574 | $mua4 = array_shift($muas); | |
3575 | ||
3576 | $this->assertEquals($user1->id, $mua1->userid); | |
3577 | $this->assertEquals($m1id, $mua1->messageid); | |
3578 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua1->action); | |
3579 | ||
3580 | $this->assertEquals($user1->id, $mua2->userid); | |
3581 | $this->assertEquals($m2id, $mua2->messageid); | |
3582 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua2->action); | |
3583 | ||
3584 | $this->assertEquals($user1->id, $mua3->userid); | |
3585 | $this->assertEquals($m3id, $mua3->messageid); | |
3586 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua3->action); | |
3587 | ||
3588 | $this->assertEquals($user1->id, $mua4->userid); | |
3589 | $this->assertEquals($m4id, $mua4->messageid); | |
3590 | $this->assertEquals(\core_message\api::MESSAGE_ACTION_DELETED, $mua4->action); | |
3591 | } | |
3592 | ||
3593 | /** | |
3594 | * Test deleting conversation as other user without proper capability. | |
3595 | */ | |
3596 | public function test_delete_conversation_by_id_as_other_user_without_cap() { | |
3597 | $this->resetAfterTest(true); | |
3598 | ||
3599 | // Create some users. | |
3600 | $user1 = self::getDataGenerator()->create_user(); | |
3601 | $user2 = self::getDataGenerator()->create_user(); | |
3602 | $user3 = self::getDataGenerator()->create_user(); | |
3603 | ||
3604 | // Send some messages back and forth. | |
3605 | $time = time(); | |
3606 | $this->send_message($user1, $user2, 'Yo!', 0, $time); | |
3607 | $this->send_message($user2, $user1, 'Sup mang?', 0, $time + 1); | |
3608 | $this->send_message($user1, $user2, 'Writing PHPUnit tests!', 0, $time + 2); | |
3609 | $this->send_message($user2, $user1, 'Word.', 0, $time + 3); | |
3610 | ||
3611 | $conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id]); | |
3612 | ||
3613 | // The person wanting to delete the conversation. | |
3614 | $this->setUser($user3); | |
3615 | ||
3616 | // Ensure an exception is thrown. | |
3617 | $this->expectException('moodle_exception'); | |
3618 | core_message_external::delete_conversation_by_id($user1->id, $conversationid); | |