1 |
efrain |
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 |
namespace mod_chat;
|
|
|
18 |
|
|
|
19 |
use core_external\external_api;
|
|
|
20 |
use externallib_advanced_testcase;
|
|
|
21 |
use mod_chat_external;
|
|
|
22 |
|
|
|
23 |
defined('MOODLE_INTERNAL') || die();
|
|
|
24 |
|
|
|
25 |
global $CFG;
|
|
|
26 |
|
|
|
27 |
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* External mod_chat functions unit tests
|
|
|
31 |
*
|
|
|
32 |
* @package mod_chat
|
|
|
33 |
* @category external
|
|
|
34 |
* @copyright 2015 Juan Leyva <juan@moodle.com>
|
|
|
35 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
36 |
* @since Moodle 3.0
|
|
|
37 |
*/
|
|
|
38 |
class externallib_test extends externallib_advanced_testcase {
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Setup testcase.
|
|
|
42 |
*/
|
|
|
43 |
public function setUp(): void {
|
|
|
44 |
// Chat module is disabled by default, enable it for testing.
|
|
|
45 |
$manager = \core_plugin_manager::resolve_plugininfo_class('mod');
|
|
|
46 |
$manager::enable_plugin('chat', 1);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Test login user
|
|
|
51 |
*/
|
11 |
efrain |
52 |
public function test_login_user(): void {
|
1 |
efrain |
53 |
global $DB;
|
|
|
54 |
|
|
|
55 |
$this->resetAfterTest(true);
|
|
|
56 |
|
|
|
57 |
// Setup test data.
|
|
|
58 |
$this->setAdminUser();
|
|
|
59 |
$course = $this->getDataGenerator()->create_course();
|
|
|
60 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
|
|
|
61 |
|
|
|
62 |
$user = self::getDataGenerator()->create_user();
|
|
|
63 |
$this->setUser($user);
|
|
|
64 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
65 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
|
|
|
66 |
|
|
|
67 |
$result = mod_chat_external::login_user($chat->id);
|
|
|
68 |
$result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
|
|
|
69 |
|
|
|
70 |
// Test session started.
|
|
|
71 |
$sid = $DB->get_field('chat_users', 'sid', array('userid' => $user->id, 'chatid' => $chat->id));
|
|
|
72 |
$this->assertEquals($result['chatsid'], $sid);
|
|
|
73 |
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* Test get chat users
|
|
|
78 |
*/
|
11 |
efrain |
79 |
public function test_get_chat_users(): void {
|
1 |
efrain |
80 |
global $DB;
|
|
|
81 |
|
|
|
82 |
$this->resetAfterTest(true);
|
|
|
83 |
|
|
|
84 |
// Setup test data.
|
|
|
85 |
$this->setAdminUser();
|
|
|
86 |
$course = $this->getDataGenerator()->create_course();
|
|
|
87 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
|
|
|
88 |
|
|
|
89 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
90 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
91 |
|
|
|
92 |
$this->setUser($user1);
|
|
|
93 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
94 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id);
|
|
|
95 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id);
|
|
|
96 |
|
|
|
97 |
$result = mod_chat_external::login_user($chat->id);
|
|
|
98 |
$result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
|
|
|
99 |
|
|
|
100 |
$this->setUser($user2);
|
|
|
101 |
$result = mod_chat_external::login_user($chat->id);
|
|
|
102 |
$result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
|
|
|
103 |
|
|
|
104 |
// Get users.
|
|
|
105 |
$result = mod_chat_external::get_chat_users($result['chatsid']);
|
|
|
106 |
$result = external_api::clean_returnvalue(mod_chat_external::get_chat_users_returns(), $result);
|
|
|
107 |
|
|
|
108 |
// Check correct users.
|
|
|
109 |
$this->assertCount(2, $result['users']);
|
|
|
110 |
$found = 0;
|
|
|
111 |
foreach ($result['users'] as $user) {
|
|
|
112 |
if ($user['id'] == $user1->id or $user['id'] == $user2->id) {
|
|
|
113 |
$found++;
|
|
|
114 |
}
|
|
|
115 |
}
|
|
|
116 |
$this->assertEquals(2, $found);
|
|
|
117 |
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Test send and get chat messages
|
|
|
122 |
*/
|
11 |
efrain |
123 |
public function test_send_get_chat_message(): void {
|
1 |
efrain |
124 |
global $DB;
|
|
|
125 |
|
|
|
126 |
$this->resetAfterTest(true);
|
|
|
127 |
|
|
|
128 |
// Setup test data.
|
|
|
129 |
$this->setAdminUser();
|
|
|
130 |
$course = $this->getDataGenerator()->create_course();
|
|
|
131 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
|
|
|
132 |
|
|
|
133 |
$user = self::getDataGenerator()->create_user();
|
|
|
134 |
$this->setUser($user);
|
|
|
135 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
136 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
|
|
|
137 |
|
|
|
138 |
$result = mod_chat_external::login_user($chat->id);
|
|
|
139 |
$result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
|
|
|
140 |
$chatsid = $result['chatsid'];
|
|
|
141 |
|
|
|
142 |
$result = mod_chat_external::send_chat_message($chatsid, 'hello!');
|
|
|
143 |
$result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result);
|
|
|
144 |
|
|
|
145 |
// Test messages received.
|
|
|
146 |
|
|
|
147 |
$result = mod_chat_external::get_chat_latest_messages($chatsid, 0);
|
|
|
148 |
$result = external_api::clean_returnvalue(mod_chat_external::get_chat_latest_messages_returns(), $result);
|
|
|
149 |
|
|
|
150 |
foreach ($result['messages'] as $message) {
|
|
|
151 |
// Ommit system messages, like user just joined in.
|
|
|
152 |
if ($message['system']) {
|
|
|
153 |
continue;
|
|
|
154 |
}
|
|
|
155 |
$this->assertEquals('hello!', $message['message']);
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Test view_chat
|
|
|
161 |
*/
|
11 |
efrain |
162 |
public function test_view_chat(): void {
|
1 |
efrain |
163 |
global $DB;
|
|
|
164 |
|
|
|
165 |
$this->resetAfterTest(true);
|
|
|
166 |
|
|
|
167 |
// Setup test data.
|
|
|
168 |
$this->setAdminUser();
|
|
|
169 |
$course = $this->getDataGenerator()->create_course();
|
|
|
170 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
|
|
|
171 |
$context = \context_module::instance($chat->cmid);
|
|
|
172 |
$cm = get_coursemodule_from_instance('chat', $chat->id);
|
|
|
173 |
|
|
|
174 |
// Test invalid instance id.
|
|
|
175 |
try {
|
|
|
176 |
mod_chat_external::view_chat(0);
|
|
|
177 |
$this->fail('Exception expected due to invalid mod_chat instance id.');
|
|
|
178 |
} catch (\moodle_exception $e) {
|
|
|
179 |
$this->assertEquals('invalidrecord', $e->errorcode);
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
// Test not-enrolled user.
|
|
|
183 |
$user = self::getDataGenerator()->create_user();
|
|
|
184 |
$this->setUser($user);
|
|
|
185 |
try {
|
|
|
186 |
mod_chat_external::view_chat($chat->id);
|
|
|
187 |
$this->fail('Exception expected due to not enrolled user.');
|
|
|
188 |
} catch (\moodle_exception $e) {
|
|
|
189 |
$this->assertEquals('requireloginerror', $e->errorcode);
|
|
|
190 |
}
|
|
|
191 |
|
|
|
192 |
// Test user with full capabilities.
|
|
|
193 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
194 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
|
|
|
195 |
|
|
|
196 |
// Trigger and capture the event.
|
|
|
197 |
$sink = $this->redirectEvents();
|
|
|
198 |
|
|
|
199 |
$result = mod_chat_external::view_chat($chat->id);
|
|
|
200 |
$result = external_api::clean_returnvalue(mod_chat_external::view_chat_returns(), $result);
|
|
|
201 |
|
|
|
202 |
$events = $sink->get_events();
|
|
|
203 |
$this->assertCount(1, $events);
|
|
|
204 |
$event = array_shift($events);
|
|
|
205 |
|
|
|
206 |
// Checking that the event contains the expected values.
|
|
|
207 |
$this->assertInstanceOf('\mod_chat\event\course_module_viewed', $event);
|
|
|
208 |
$this->assertEquals($context, $event->get_context());
|
|
|
209 |
$moodlechat = new \moodle_url('/mod/chat/view.php', array('id' => $cm->id));
|
|
|
210 |
$this->assertEquals($moodlechat, $event->get_url());
|
|
|
211 |
$this->assertEventContextNotUsed($event);
|
|
|
212 |
$this->assertNotEmpty($event->get_name());
|
|
|
213 |
|
|
|
214 |
// Test user with no capabilities.
|
|
|
215 |
// We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
|
|
|
216 |
assign_capability('mod/chat:chat', CAP_PROHIBIT, $studentrole->id, $context->id);
|
|
|
217 |
accesslib_clear_all_caches_for_unit_testing();
|
|
|
218 |
|
|
|
219 |
try {
|
|
|
220 |
mod_chat_external::view_chat($chat->id);
|
|
|
221 |
$this->fail('Exception expected due to missing capability.');
|
|
|
222 |
} catch (\moodle_exception $e) {
|
|
|
223 |
$this->assertEquals('nopermissions', $e->errorcode);
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Test get_chats_by_courses
|
|
|
229 |
*/
|
11 |
efrain |
230 |
public function test_get_chats_by_courses(): void {
|
1 |
efrain |
231 |
global $DB, $CFG;
|
|
|
232 |
$this->resetAfterTest(true);
|
|
|
233 |
$this->setAdminUser();
|
|
|
234 |
|
|
|
235 |
// Set global chat method.
|
|
|
236 |
$CFG->chat_method = 'header_js';
|
|
|
237 |
|
|
|
238 |
$course1 = self::getDataGenerator()->create_course();
|
|
|
239 |
$chatoptions1 = array(
|
|
|
240 |
'course' => $course1->id,
|
|
|
241 |
'name' => 'First Chat'
|
|
|
242 |
);
|
|
|
243 |
$chat1 = self::getDataGenerator()->create_module('chat', $chatoptions1);
|
|
|
244 |
$course2 = self::getDataGenerator()->create_course();
|
|
|
245 |
$chatoptions2 = array(
|
|
|
246 |
'course' => $course2->id,
|
|
|
247 |
'name' => 'Second Chat'
|
|
|
248 |
);
|
|
|
249 |
$chat2 = self::getDataGenerator()->create_module('chat', $chatoptions2);
|
|
|
250 |
$student1 = $this->getDataGenerator()->create_user();
|
|
|
251 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
252 |
|
|
|
253 |
// Enroll Student1 in Course1.
|
|
|
254 |
self::getDataGenerator()->enrol_user($student1->id, $course1->id, $studentrole->id);
|
|
|
255 |
$this->setUser($student1);
|
|
|
256 |
|
|
|
257 |
$chats = mod_chat_external::get_chats_by_courses();
|
|
|
258 |
// We need to execute the return values cleaning process to simulate the web service server.
|
|
|
259 |
$chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
|
|
|
260 |
$this->assertCount(1, $chats['chats']);
|
|
|
261 |
$this->assertEquals('First Chat', $chats['chats'][0]['name']);
|
|
|
262 |
// We see 12 fields.
|
|
|
263 |
$this->assertCount(13, $chats['chats'][0]);
|
|
|
264 |
|
|
|
265 |
// As Student you cannot see some chat properties like 'section'.
|
|
|
266 |
$this->assertFalse(isset($chats['chats'][0]['section']));
|
|
|
267 |
|
|
|
268 |
// Student1 is not enrolled in course2. The webservice will return a warning!
|
|
|
269 |
$chats = mod_chat_external::get_chats_by_courses(array($course2->id));
|
|
|
270 |
// We need to execute the return values cleaning process to simulate the web service server.
|
|
|
271 |
$chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
|
|
|
272 |
$this->assertCount(0, $chats['chats']);
|
|
|
273 |
$this->assertEquals(1, $chats['warnings'][0]['warningcode']);
|
|
|
274 |
|
|
|
275 |
// Now as admin.
|
|
|
276 |
$this->setAdminUser();
|
|
|
277 |
// As Admin we can see this chat.
|
|
|
278 |
$chats = mod_chat_external::get_chats_by_courses(array($course2->id));
|
|
|
279 |
// We need to execute the return values cleaning process to simulate the web service server.
|
|
|
280 |
$chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
|
|
|
281 |
|
|
|
282 |
$this->assertCount(1, $chats['chats']);
|
|
|
283 |
$this->assertEquals('Second Chat', $chats['chats'][0]['name']);
|
|
|
284 |
$this->assertEquals('header_js', $chats['chats'][0]['chatmethod']);
|
|
|
285 |
// We see 17 fields.
|
|
|
286 |
$this->assertCount(18, $chats['chats'][0]);
|
|
|
287 |
// As an Admin you can see some chat properties like 'section'.
|
|
|
288 |
$this->assertEquals(0, $chats['chats'][0]['section']);
|
|
|
289 |
|
|
|
290 |
// Enrol student in the second course.
|
|
|
291 |
self::getDataGenerator()->enrol_user($student1->id, $course2->id, $studentrole->id);
|
|
|
292 |
$this->setUser($student1);
|
|
|
293 |
$chats = mod_chat_external::get_chats_by_courses();
|
|
|
294 |
$chats = external_api::clean_returnvalue(mod_chat_external::get_chats_by_courses_returns(), $chats);
|
|
|
295 |
$this->assertCount(2, $chats['chats']);
|
|
|
296 |
|
|
|
297 |
}
|
|
|
298 |
|
|
|
299 |
/**
|
|
|
300 |
* Test get_sessions_empty_chat
|
|
|
301 |
*/
|
11 |
efrain |
302 |
public function test_get_sessions_empty_chat(): void {
|
1 |
efrain |
303 |
global $DB;
|
|
|
304 |
|
|
|
305 |
$this->resetAfterTest(true);
|
|
|
306 |
|
|
|
307 |
// Setup test data.
|
|
|
308 |
$this->setAdminUser();
|
|
|
309 |
$course = $this->getDataGenerator()->create_course();
|
|
|
310 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
|
|
|
311 |
|
|
|
312 |
$result = mod_chat_external::get_sessions($chat->id);
|
|
|
313 |
$result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result);
|
|
|
314 |
$this->assertEmpty($result['sessions']);
|
|
|
315 |
$this->assertEmpty($result['warnings']);
|
|
|
316 |
}
|
|
|
317 |
|
|
|
318 |
|
|
|
319 |
/**
|
|
|
320 |
* Test get_sessions_no_permissions_for_student
|
|
|
321 |
*/
|
11 |
efrain |
322 |
public function test_get_sessions_no_permissions_for_student(): void {
|
1 |
efrain |
323 |
global $DB;
|
|
|
324 |
|
|
|
325 |
$this->resetAfterTest(true);
|
|
|
326 |
|
|
|
327 |
// Setup test data.
|
|
|
328 |
$this->setAdminUser();
|
|
|
329 |
$course = $this->getDataGenerator()->create_course();
|
|
|
330 |
// Disable logs for students.
|
|
|
331 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id, 'studentlogs' => 0));
|
|
|
332 |
// The admin has permissions to check logs.
|
|
|
333 |
$result = mod_chat_external::get_sessions($chat->id);
|
|
|
334 |
$result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result);
|
|
|
335 |
$this->assertEmpty($result['sessions']);
|
|
|
336 |
$this->assertEmpty($result['warnings']);
|
|
|
337 |
|
|
|
338 |
$user = self::getDataGenerator()->create_user();
|
|
|
339 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
340 |
unassign_capability('mod/chat:readlog', $studentrole->id);
|
|
|
341 |
accesslib_clear_all_caches_for_unit_testing();
|
|
|
342 |
|
|
|
343 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
|
|
|
344 |
$this->setUser($user);
|
|
|
345 |
// Students don't have permissions.
|
|
|
346 |
$this->expectException('moodle_exception');
|
|
|
347 |
mod_chat_external::get_sessions($chat->id);
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
/**
|
|
|
351 |
* Test get_sessions_not_completed_session
|
|
|
352 |
*/
|
11 |
efrain |
353 |
public function test_get_sessions_not_completed_session(): void {
|
1 |
efrain |
354 |
global $DB;
|
|
|
355 |
|
|
|
356 |
$this->resetAfterTest(true);
|
|
|
357 |
|
|
|
358 |
// Setup test data.
|
|
|
359 |
$this->setAdminUser();
|
|
|
360 |
$course = $this->getDataGenerator()->create_course();
|
|
|
361 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
|
|
|
362 |
|
|
|
363 |
$user = self::getDataGenerator()->create_user();
|
|
|
364 |
$this->setUser($user);
|
|
|
365 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
366 |
$this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id);
|
|
|
367 |
|
|
|
368 |
// Start a chat and send just one message.
|
|
|
369 |
$result = mod_chat_external::login_user($chat->id);
|
|
|
370 |
$result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
|
|
|
371 |
$chatsid = $result['chatsid'];
|
|
|
372 |
$result = mod_chat_external::send_chat_message($chatsid, 'hello!');
|
|
|
373 |
$result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result);
|
|
|
374 |
|
|
|
375 |
// Check session is not marked as completed so it is not returned.
|
|
|
376 |
$result = mod_chat_external::get_sessions($chat->id);
|
|
|
377 |
$result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result);
|
|
|
378 |
$this->assertEmpty($result['sessions']);
|
|
|
379 |
$this->assertEmpty($result['warnings']);
|
|
|
380 |
|
|
|
381 |
// Pass showall parameter to indicate that we want not completed sessions.
|
|
|
382 |
$result = mod_chat_external::get_sessions($chat->id, 0, true);
|
|
|
383 |
$result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result);
|
|
|
384 |
$this->assertCount(1, $result['sessions']); // One session.
|
|
|
385 |
$this->assertFalse($result['sessions'][0]['iscomplete']); // Session not complete.
|
|
|
386 |
$this->assertEmpty($result['warnings']);
|
|
|
387 |
}
|
|
|
388 |
|
|
|
389 |
/**
|
|
|
390 |
* Test get_sessions_completed_session
|
|
|
391 |
*/
|
11 |
efrain |
392 |
public function test_get_sessions_completed_session(): void {
|
1 |
efrain |
393 |
global $DB;
|
|
|
394 |
|
|
|
395 |
$this->resetAfterTest(true);
|
|
|
396 |
|
|
|
397 |
// Setup test data.
|
|
|
398 |
$this->setAdminUser();
|
|
|
399 |
$course = $this->getDataGenerator()->create_course();
|
|
|
400 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
|
|
|
401 |
|
|
|
402 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
403 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
404 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
405 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id);
|
|
|
406 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id);
|
|
|
407 |
|
|
|
408 |
// Start a chat and completeit.
|
|
|
409 |
$this->setUser($user1);
|
|
|
410 |
$result = mod_chat_external::login_user($chat->id);
|
|
|
411 |
$result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
|
|
|
412 |
$chatsid = $result['chatsid'];
|
|
|
413 |
$result = mod_chat_external::send_chat_message($chatsid, 'hello!');
|
|
|
414 |
$result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result);
|
|
|
415 |
$this->setUser($user2);
|
|
|
416 |
$result = mod_chat_external::login_user($chat->id);
|
|
|
417 |
$result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
|
|
|
418 |
$chatsid = $result['chatsid'];
|
|
|
419 |
$result = mod_chat_external::send_chat_message($chatsid, 'hello to you!');
|
|
|
420 |
$result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result);
|
|
|
421 |
// Need to change first messages and last message times to mark the session completed.
|
|
|
422 |
// We receive 4 messages (2 system messages that indicates user joined and the 2 messages sent by the users).
|
|
|
423 |
$messages = $DB->get_records('chat_messages', array('chatid' => $chat->id));
|
|
|
424 |
// Messages just one hour ago and 70 seconds between them.
|
|
|
425 |
$timegap = 0;
|
|
|
426 |
$timenow = time();
|
|
|
427 |
foreach ($messages as $message) {
|
|
|
428 |
$DB->set_field('chat_messages', 'timestamp', $timenow - HOURSECS + $timegap, array('id' => $message->id));
|
|
|
429 |
$timegap += 70;
|
|
|
430 |
}
|
|
|
431 |
// Check session is completed.
|
|
|
432 |
$result = mod_chat_external::get_sessions($chat->id);
|
|
|
433 |
$result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result);
|
|
|
434 |
$this->assertCount(1, $result['sessions']); // One session.
|
|
|
435 |
$this->assertTrue($result['sessions'][0]['iscomplete']); // Session complete.
|
|
|
436 |
// The session started when user1 entered the chat.
|
|
|
437 |
$this->assertEquals($timenow - HOURSECS, $result['sessions'][0]['sessionstart']);
|
|
|
438 |
$this->assertEmpty($result['warnings']);
|
|
|
439 |
}
|
|
|
440 |
|
|
|
441 |
/**
|
|
|
442 |
* Test get_session_messages
|
|
|
443 |
*/
|
11 |
efrain |
444 |
public function test_get_session_messages(): void {
|
1 |
efrain |
445 |
global $DB;
|
|
|
446 |
|
|
|
447 |
$this->resetAfterTest(true);
|
|
|
448 |
|
|
|
449 |
// Setup test data.
|
|
|
450 |
$this->setAdminUser();
|
|
|
451 |
$course = $this->getDataGenerator()->create_course();
|
|
|
452 |
$chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
|
|
|
453 |
|
|
|
454 |
$user1 = self::getDataGenerator()->create_user();
|
|
|
455 |
$user2 = self::getDataGenerator()->create_user();
|
|
|
456 |
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
|
|
|
457 |
$this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id);
|
|
|
458 |
$this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id);
|
|
|
459 |
|
|
|
460 |
// Start a chat and send a few messages.
|
|
|
461 |
$this->setUser($user1);
|
|
|
462 |
$result = mod_chat_external::login_user($chat->id);
|
|
|
463 |
$result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
|
|
|
464 |
$chatsid = $result['chatsid'];
|
|
|
465 |
mod_chat_external::send_chat_message($chatsid, 'hello!');
|
|
|
466 |
mod_chat_external::send_chat_message($chatsid, 'bye bye!');
|
|
|
467 |
|
|
|
468 |
$this->setUser($user2);
|
|
|
469 |
$result = mod_chat_external::login_user($chat->id);
|
|
|
470 |
$result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
|
|
|
471 |
$chatsid = $result['chatsid'];
|
|
|
472 |
mod_chat_external::send_chat_message($chatsid, 'greetings!');
|
|
|
473 |
|
|
|
474 |
// Pass showall parameter to indicate that we want not completed sessions.
|
|
|
475 |
$result = mod_chat_external::get_sessions($chat->id, 0, true);
|
|
|
476 |
$result = external_api::clean_returnvalue(mod_chat_external::get_sessions_returns(), $result);
|
|
|
477 |
$this->assertCount(1, $result['sessions']); // One session.
|
|
|
478 |
|
|
|
479 |
$sessionstart = $result['sessions'][0]['sessionstart'];
|
|
|
480 |
$sessionend = $result['sessions'][0]['sessionend'];
|
|
|
481 |
$result = mod_chat_external::get_session_messages($chat->id, $sessionstart, $sessionend);
|
|
|
482 |
$result = external_api::clean_returnvalue(mod_chat_external::get_session_messages_returns(), $result);
|
|
|
483 |
$this->assertCount(5, $result['messages']); // 2 system + 3 personal messages.
|
|
|
484 |
$found = 0;
|
|
|
485 |
foreach ($result['messages'] as $message) {
|
|
|
486 |
if (!$message['issystem']) {
|
|
|
487 |
if ($message['userid'] == $user1->id) {
|
|
|
488 |
if ($message['message'] != 'hello!') {
|
|
|
489 |
$this->assertEquals('bye bye!', $message['message']);
|
|
|
490 |
$found++;
|
|
|
491 |
}
|
|
|
492 |
} else {
|
|
|
493 |
$this->assertEquals($user2->id, $message['userid']);
|
|
|
494 |
$this->assertEquals('greetings!', $message['message']);
|
|
|
495 |
$found++;
|
|
|
496 |
}
|
|
|
497 |
}
|
|
|
498 |
}
|
|
|
499 |
$this->assertEquals(2, $found);
|
|
|
500 |
}
|
|
|
501 |
}
|