MDL-50537 mod_chat: New WS mod_chat_get_chats_by_courses
[moodle.git] / mod / chat / tests / externallib_test.php
CommitLineData
cdb5d405
JL
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 mod_chat functions unit tests
19 *
20 * @package mod_chat
21 * @category external
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @since Moodle 3.0
25 */
26
27defined('MOODLE_INTERNAL') || die();
28
29global $CFG;
30
31require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32
33/**
34 * External mod_chat functions unit tests
35 *
36 * @package mod_chat
37 * @category external
38 * @copyright 2015 Juan Leyva <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 * @since Moodle 3.0
41 */
42class mod_chat_external_testcase extends externallib_advanced_testcase {
43
44 /**
45 * Test login user
46 */
47 public function test_login_user() {
48 global $DB;
49
50 $this->resetAfterTest(true);
51
52 // Setup test data.
53 $this->setAdminUser();
54 $course = $this->getDataGenerator()->create_course();
55 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
56
57 $user = self::getDataGenerator()->create_user();
58 $this->setUser($user);
59 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
60 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
61
62 $result = mod_chat_external::login_user($chat->id);
63 $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
64
65 // Test session started.
66 $sid = $DB->get_field('chat_users', 'sid', array('userid' => $user->id, 'chatid' => $chat->id));
67 $this->assertEquals($result['chatsid'], $sid);
68
69 }
70
71 /**
72 * Test get chat users
73 */
74 public function test_get_chat_users() {
75 global $DB;
76
77 $this->resetAfterTest(true);
78
79 // Setup test data.
80 $this->setAdminUser();
81 $course = $this->getDataGenerator()->create_course();
82 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
83
84 $user1 = self::getDataGenerator()->create_user();
85 $user2 = self::getDataGenerator()->create_user();
86
87 $this->setUser($user1);
88 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
89 $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id);
90 $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id);
91
92 $result = mod_chat_external::login_user($chat->id);
93 $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
94
95 $this->setUser($user2);
96 $result = mod_chat_external::login_user($chat->id);
97 $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
98
99 // Get users.
100 $result = mod_chat_external::get_chat_users($result['chatsid']);
101 $result = external_api::clean_returnvalue(mod_chat_external::get_chat_users_returns(), $result);
102
103 // Check correct users.
104 $this->assertCount(2, $result['users']);
105 $found = 0;
106 foreach ($result['users'] as $user) {
107 if ($user['id'] == $user1->id or $user['id'] == $user2->id) {
108 $found++;
109 }
110 }
111 $this->assertEquals(2, $found);
112
113 }
114
115 /**
116 * Test send and get chat messages
117 */
118 public function test_send_get_chat_message() {
119 global $DB;
120
121 $this->resetAfterTest(true);
122
123 // Setup test data.
124 $this->setAdminUser();
125 $course = $this->getDataGenerator()->create_course();
126 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
127
128 $user = self::getDataGenerator()->create_user();
129 $this->setUser($user);
130 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
131 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
132
133 $result = mod_chat_external::login_user($chat->id);
134 $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
135 $chatsid = $result['chatsid'];
136
137 $result = mod_chat_external::send_chat_message($chatsid, 'hello!');
138 $result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result);
139
140 // Test messages received.
141
142 $result = mod_chat_external::get_chat_latest_messages($chatsid, 0);
143 $result = external_api::clean_returnvalue(mod_chat_external::get_chat_latest_messages_returns(), $result);
144
145 foreach ($result['messages'] as $message) {
146 // Ommit system messages, like user just joined in.
147 if ($message['system']) {
148 continue;
149 }
150 $this->assertEquals('hello!', $message['message']);
151 }
152 }
153
154 /**
155 * Test view_chat
156 */
157 public function test_view_chat() {
158 global $DB;
159
160 $this->resetAfterTest(true);
161
162 // Setup test data.
163 $this->setAdminUser();
164 $course = $this->getDataGenerator()->create_course();
165 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
166 $context = context_module::instance($chat->cmid);
167 $cm = get_coursemodule_from_instance('chat', $chat->id);
168
169 // Test invalid instance id.
170 try {
171 mod_chat_external::view_chat(0);
172 $this->fail('Exception expected due to invalid mod_chat instance id.');
173 } catch (moodle_exception $e) {
174 $this->assertEquals('invalidrecord', $e->errorcode);
175 }
176
177 // Test not-enrolled user.
178 $user = self::getDataGenerator()->create_user();
179 $this->setUser($user);
180 try {
181 mod_chat_external::view_chat($chat->id);
182 $this->fail('Exception expected due to not enrolled user.');
183 } catch (moodle_exception $e) {
184 $this->assertEquals('requireloginerror', $e->errorcode);
185 }
186
187 // Test user with full capabilities.
188 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
189 $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
190
191 // Trigger and capture the event.
192 $sink = $this->redirectEvents();
193
194 $result = mod_chat_external::view_chat($chat->id);
195 $result = external_api::clean_returnvalue(mod_chat_external::view_chat_returns(), $result);
196
197 $events = $sink->get_events();
198 $this->assertCount(1, $events);
199 $event = array_shift($events);
200
201 // Checking that the event contains the expected values.
202 $this->assertInstanceOf('\mod_chat\event\course_module_viewed', $event);
203 $this->assertEquals($context, $event->get_context());
204 $moodlechat = new \moodle_url('/mod/chat/view.php', array('id' => $cm->id));
205 $this->assertEquals($moodlechat, $event->get_url());
206 $this->assertEventContextNotUsed($event);
207 $this->assertNotEmpty($event->get_name());
208
209 // Test user with no capabilities.
210 // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
211 assign_capability('mod/chat:chat', CAP_PROHIBIT, $studentrole->id, $context->id);
212 accesslib_clear_all_caches_for_unit_testing();
213
214 try {
215 mod_chat_external::view_chat($chat->id);
216 $this->fail('Exception expected due to missing capability.');
217 } catch (moodle_exception $e) {
218 $this->assertEquals('nopermissions', $e->errorcode);
219 }
9f7de631 220 }
cdb5d405 221
9f7de631
CC
222 /**
223 * Test get_chats_by_courses
224 */
225 public function test_get_chats_by_courses() {
226 global $DB, $USER;
227 $this->resetAfterTest(true);
228 $this->setAdminUser();
229 $course1 = self::getDataGenerator()->create_course();
230 $chatoptions1 = array(
231 'course' => $course1->id,
232 'name' => 'First Chat'
233 );
234 $chat1 = self::getDataGenerator()->create_module('chat', $chatoptions1);
235 $course2 = self::getDataGenerator()->create_course();
236 $chatoptions2 = array(
237 'course' => $course2->id,
238 'name' => 'Second Chat'
239 );
240 $chat2 = self::getDataGenerator()->create_module('chat', $chatoptions2);
241 $student1 = $this->getDataGenerator()->create_user();
242 $studentrole = $DB->get_record('role', array('shortname' => 'student'));
243 // Enroll Student1 in Course1.
244 self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
245 $this->setUser($student1);
246 $chats = mod_chat_external::get_chats_by_courses(array());
247 // We need to execute the return values cleaning process to simulate the web service server.
248 $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
249 $this->assertCount(1, $chats['chats']);
250 $this->assertEquals('First Chat', $chats['chats'][0]['name']);
251 // As Student you cannot see some chat properties like 'showunanswered'.
252 $this->assertFalse(isset($chats['chats'][0]['section']));
253 // Student1 is not enrolled in this Course.
254 // The webservice will give a warning!
255 $chats = mod_chat_external::get_chats_by_courses(array($course2->id));
256 // We need to execute the return values cleaning process to simulate the web service server.
257 $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
258 $this->assertCount(0, $chats['chats']);
259 $this->assertEquals(1, $chats['warnings'][0]['warningcode']);
260 // Now as admin.
261 $this->setAdminUser();
262 // As Admin we can see this chat.
263 $chats = mod_chat_external::get_chats_by_courses(array($course2->id));
264 // We need to execute the return values cleaning process to simulate the web service server.
265 $chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
266 $this->assertCount(1, $chats['chats']);
267 $this->assertEquals('Second Chat', $chats['chats'][0]['name']);
268 // As an Admin you can see some chat properties like 'section'.
269 $this->assertEquals(0, $chats['chats'][0]['section']);
cdb5d405
JL
270 }
271}