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