From cdb5d405c1e3be153d4e8ac4d0862f8d98ce2264 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Thu, 16 Jul 2015 14:47:18 +0200 Subject: [PATCH] MDL-50853 mod_chat: Unit tests for the external lib --- mod/chat/tests/externallib_test.php | 222 ++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 mod/chat/tests/externallib_test.php diff --git a/mod/chat/tests/externallib_test.php b/mod/chat/tests/externallib_test.php new file mode 100644 index 00000000000..5497b24a941 --- /dev/null +++ b/mod/chat/tests/externallib_test.php @@ -0,0 +1,222 @@ +. + +/** + * External mod_chat functions unit tests + * + * @package mod_chat + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); + +/** + * External mod_chat functions unit tests + * + * @package mod_chat + * @category external + * @copyright 2015 Juan Leyva + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @since Moodle 3.0 + */ +class mod_chat_external_testcase extends externallib_advanced_testcase { + + /** + * Test login user + */ + public function test_login_user() { + global $DB; + + $this->resetAfterTest(true); + + // Setup test data. + $this->setAdminUser(); + $course = $this->getDataGenerator()->create_course(); + $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); + + $user = self::getDataGenerator()->create_user(); + $this->setUser($user); + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); + + $result = mod_chat_external::login_user($chat->id); + $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + + // Test session started. + $sid = $DB->get_field('chat_users', 'sid', array('userid' => $user->id, 'chatid' => $chat->id)); + $this->assertEquals($result['chatsid'], $sid); + + } + + /** + * Test get chat users + */ + public function test_get_chat_users() { + global $DB; + + $this->resetAfterTest(true); + + // Setup test data. + $this->setAdminUser(); + $course = $this->getDataGenerator()->create_course(); + $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); + + $user1 = self::getDataGenerator()->create_user(); + $user2 = self::getDataGenerator()->create_user(); + + $this->setUser($user1); + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $this->getDataGenerator()->enrol_user($user1->id, $course->id, $studentrole->id); + $this->getDataGenerator()->enrol_user($user2->id, $course->id, $studentrole->id); + + $result = mod_chat_external::login_user($chat->id); + $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + + $this->setUser($user2); + $result = mod_chat_external::login_user($chat->id); + $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + + // Get users. + $result = mod_chat_external::get_chat_users($result['chatsid']); + $result = external_api::clean_returnvalue(mod_chat_external::get_chat_users_returns(), $result); + + // Check correct users. + $this->assertCount(2, $result['users']); + $found = 0; + foreach ($result['users'] as $user) { + if ($user['id'] == $user1->id or $user['id'] == $user2->id) { + $found++; + } + } + $this->assertEquals(2, $found); + + } + + /** + * Test send and get chat messages + */ + public function test_send_get_chat_message() { + global $DB; + + $this->resetAfterTest(true); + + // Setup test data. + $this->setAdminUser(); + $course = $this->getDataGenerator()->create_course(); + $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); + + $user = self::getDataGenerator()->create_user(); + $this->setUser($user); + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); + + $result = mod_chat_external::login_user($chat->id); + $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result); + $chatsid = $result['chatsid']; + + $result = mod_chat_external::send_chat_message($chatsid, 'hello!'); + $result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result); + + // Test messages received. + + $result = mod_chat_external::get_chat_latest_messages($chatsid, 0); + $result = external_api::clean_returnvalue(mod_chat_external::get_chat_latest_messages_returns(), $result); + + foreach ($result['messages'] as $message) { + // Ommit system messages, like user just joined in. + if ($message['system']) { + continue; + } + $this->assertEquals('hello!', $message['message']); + } + } + + /** + * Test view_chat + */ + public function test_view_chat() { + global $DB; + + $this->resetAfterTest(true); + + // Setup test data. + $this->setAdminUser(); + $course = $this->getDataGenerator()->create_course(); + $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); + $context = context_module::instance($chat->cmid); + $cm = get_coursemodule_from_instance('chat', $chat->id); + + // Test invalid instance id. + try { + mod_chat_external::view_chat(0); + $this->fail('Exception expected due to invalid mod_chat instance id.'); + } catch (moodle_exception $e) { + $this->assertEquals('invalidrecord', $e->errorcode); + } + + // Test not-enrolled user. + $user = self::getDataGenerator()->create_user(); + $this->setUser($user); + try { + mod_chat_external::view_chat($chat->id); + $this->fail('Exception expected due to not enrolled user.'); + } catch (moodle_exception $e) { + $this->assertEquals('requireloginerror', $e->errorcode); + } + + // Test user with full capabilities. + $studentrole = $DB->get_record('role', array('shortname' => 'student')); + $this->getDataGenerator()->enrol_user($user->id, $course->id, $studentrole->id); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + + $result = mod_chat_external::view_chat($chat->id); + $result = external_api::clean_returnvalue(mod_chat_external::view_chat_returns(), $result); + + $events = $sink->get_events(); + $this->assertCount(1, $events); + $event = array_shift($events); + + // Checking that the event contains the expected values. + $this->assertInstanceOf('\mod_chat\event\course_module_viewed', $event); + $this->assertEquals($context, $event->get_context()); + $moodlechat = new \moodle_url('/mod/chat/view.php', array('id' => $cm->id)); + $this->assertEquals($moodlechat, $event->get_url()); + $this->assertEventContextNotUsed($event); + $this->assertNotEmpty($event->get_name()); + + // Test user with no capabilities. + // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles. + assign_capability('mod/chat:chat', CAP_PROHIBIT, $studentrole->id, $context->id); + accesslib_clear_all_caches_for_unit_testing(); + + try { + mod_chat_external::view_chat($chat->id); + $this->fail('Exception expected due to missing capability.'); + } catch (moodle_exception $e) { + $this->assertEquals('nopermissions', $e->errorcode); + } + + } +}