2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * External mod_chat functions unit tests
22 * @copyright 2015 Juan Leyva <juan@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
34 * External mod_chat functions unit tests
38 * @copyright 2015 Juan Leyva <juan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 class mod_chat_external_testcase extends externallib_advanced_testcase {
47 public function test_login_user() {
50 $this->resetAfterTest(true);
53 $this->setAdminUser();
54 $course = $this->getDataGenerator()->create_course();
55 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
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);
62 $result = mod_chat_external::login_user($chat->id);
63 $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
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);
74 public function test_get_chat_users() {
77 $this->resetAfterTest(true);
80 $this->setAdminUser();
81 $course = $this->getDataGenerator()->create_course();
82 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
84 $user1 = self::getDataGenerator()->create_user();
85 $user2 = self::getDataGenerator()->create_user();
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);
92 $result = mod_chat_external::login_user($chat->id);
93 $result = external_api::clean_returnvalue(mod_chat_external::login_user_returns(), $result);
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);
100 $result = mod_chat_external::get_chat_users($result['chatsid']);
101 $result = external_api::clean_returnvalue(mod_chat_external::get_chat_users_returns(), $result);
103 // Check correct users.
104 $this->assertCount(2, $result['users']);
106 foreach ($result['users'] as $user) {
107 if ($user['id'] == $user1->id or $user['id'] == $user2->id) {
111 $this->assertEquals(2, $found);
116 * Test send and get chat messages
118 public function test_send_get_chat_message() {
121 $this->resetAfterTest(true);
124 $this->setAdminUser();
125 $course = $this->getDataGenerator()->create_course();
126 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
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);
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'];
137 $result = mod_chat_external::send_chat_message($chatsid, 'hello!');
138 $result = external_api::clean_returnvalue(mod_chat_external::send_chat_message_returns(), $result);
140 // Test messages received.
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);
145 foreach ($result['messages'] as $message) {
146 // Ommit system messages, like user just joined in.
147 if ($message['system']) {
150 $this->assertEquals('hello!', $message['message']);
157 public function test_view_chat() {
160 $this->resetAfterTest(true);
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);
169 // Test invalid instance id.
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);
177 // Test not-enrolled user.
178 $user = self::getDataGenerator()->create_user();
179 $this->setUser($user);
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);
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);
191 // Trigger and capture the event.
192 $sink = $this->redirectEvents();
194 $result = mod_chat_external::view_chat($chat->id);
195 $result = external_api::clean_returnvalue(mod_chat_external::view_chat_returns(), $result);
197 $events = $sink->get_events();
198 $this->assertCount(1, $events);
199 $event = array_shift($events);
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());
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();
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);
223 * Test get_chats_by_courses
225 public function test_get_chats_by_courses() {
227 $this->resetAfterTest(true);
228 $this->setAdminUser();
229 $course1 = self::getDataGenerator()->create_course();
230 $chatoptions1 = array(
231 'course' => $course1->id,
232 'name' => 'First Chat'
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'
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']);
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']);