MDL-30070 message: Optimised search for users over multiple courses
[moodle.git] / message / tests / externallib_test.php
CommitLineData
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
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30require_once($CFG->dirroot . '/webservice/tests/helpers.php');
31require_once($CFG->dirroot . '/message/externallib.php');
32
33class core_message_external_testcase extends externallib_advanced_testcase {
34
35 /**
36 * Test send_instant_messages
37 */
38 public function test_send_instant_messages() {
39
40 global $DB, $USER, $CFG;
41
42 $this->resetAfterTest(true);
43
44 // Turn off all message processors (so nothing is really sent)
45 require_once($CFG->dirroot . '/message/lib.php');
46 $messageprocessors = get_message_processors();
47 foreach($messageprocessors as $messageprocessor) {
48 $messageprocessor->enabled = 0;
49 $DB->update_record('message_processors', $messageprocessor);
50 }
51
52 // Set the required capabilities by the external function
53 $contextid = context_system::instance()->id;
54 $roleid = $this->assignUserCapability('moodle/site:sendmessage', $contextid);
55
56 $user1 = self::getDataGenerator()->create_user();
57
58 // Create test message data.
59 $message1 = array();
60 $message1['touserid'] = $user1->id;
61 $message1['text'] = 'the message.';
62 $message1['clientmsgid'] = 4;
63 $messages = array($message1);
64
65 $sentmessages = core_message_external::send_instant_messages($messages);
66
67 $themessage = $DB->get_record('message', array('id' => $sentmessages[0]['msgid']));
68
69 // Confirm that the message was inserted correctly.
70 $this->assertEquals($themessage->useridfrom, $USER->id);
71 $this->assertEquals($themessage->useridto, $message1['touserid']);
72 $this->assertEquals($themessage->smallmessage, $message1['text']);
73 $this->assertEquals($sentmessages[0]['clientmsgid'], $message1['clientmsgid']);
74 }
75}