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