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 /** 18 * Events tests. 19 * 20 * @package mod_chat 21 * @copyright 2013 Frédéric Massart 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 require_once($CFG->dirroot . '/mod/chat/lib.php'); 29 30 /** 31 * Events tests class. 32 * 33 * @package mod_chat 34 * @copyright 2013 Frédéric Massart 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class mod_chat_events_testcase extends advanced_testcase { 38 39 public function test_message_sent() { 40 global $DB; 41 $this->resetAfterTest(); 42 43 $this->setAdminUser(); 44 $course = $this->getDataGenerator()->create_course(); 45 $user1 = $this->getDataGenerator()->create_user(); 46 $user2 = $this->getDataGenerator()->create_user(); 47 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); 48 $cm = $DB->get_record('course_modules', array('id' => $chat->cmid)); 49 50 // Logging in first user to the chat. 51 $this->setUser($user1->id); 52 $sid1 = chat_login_user($chat->id, 'ajax', 0, $course); 53 54 // Logging in second user to the chat. 55 $this->setUser($user2->id); 56 $sid2 = chat_login_user($chat->id, 'ajax', 0, $course); 57 58 // Getting the chatuser record. 59 $chatuser1 = $DB->get_record('chat_users', array('sid' => $sid1)); 60 $chatuser2 = $DB->get_record('chat_users', array('sid' => $sid2)); 61 62 $sink = $this->redirectEvents(); 63 64 // Send a messaging from the first user. We pass the CM to chat_send_chatmessage() this time. 65 // This ensures that the event triggered when sending a message is filled with the correct information. 66 $this->setUser($user1->id); 67 $messageid = chat_send_chatmessage($chatuser1, 'Hello!', false, $cm); 68 $events = $sink->get_events(); 69 $this->assertCount(1, $events); 70 $event = reset($events); 71 $this->assertInstanceOf('\mod_chat\event\message_sent', $event); 72 $this->assertEquals($messageid, $event->objectid); 73 $this->assertEquals($user1->id, $event->relateduserid); 74 $this->assertEquals($user1->id, $event->userid); 75 $expected = array($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id, $user1->id); 76 $this->assertEventLegacyLogData($expected, $event); 77 78 // Send a messaging from the first user. We DO NOT pass the CM to chat_send_chatmessage() this time. 79 // This ensures that the event triggered when sending a message is filled with the correct information. 80 $sink->clear(); 81 $this->setUser($user2->id); 82 $messageid = chat_send_chatmessage($chatuser2, 'Hello!'); 83 $events = $sink->get_events(); 84 $this->assertCount(1, $events); 85 $event = reset($events); 86 $this->assertInstanceOf('\mod_chat\event\message_sent', $event); 87 $this->assertEquals($messageid, $event->objectid); 88 $this->assertEquals($user2->id, $event->relateduserid); 89 $this->assertEquals($user2->id, $event->userid); 90 $expected = array($course->id, 'chat', 'talk', "view.php?id=$cm->id", $chat->id, $cm->id, $user2->id); 91 $this->assertEventLegacyLogData($expected, $event); 92 $this->assertEventContextNotUsed($event); 93 94 // Sending a message from the system should not trigger any event. 95 $sink->clear(); 96 $this->setAdminUser(); 97 chat_send_chatmessage($chatuser1, 'enter', true); 98 $this->assertEquals(0, $sink->count()); 99 100 $sink->close(); 101 } 102 103 public function test_sessions_viewed() { 104 global $USER; 105 $this->resetAfterTest(); 106 107 // Not much can be tested here as the event is only triggered on a page load, 108 // let's just check that the event contains the expected basic information. 109 $this->setAdminUser(); 110 $course = $this->getDataGenerator()->create_course(); 111 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); 112 113 $params = array( 114 'context' => context_module::instance($chat->cmid), 115 'objectid' => $chat->id, 116 'other' => array( 117 'start' => 1234, 118 'end' => 5678 119 ) 120 ); 121 $event = \mod_chat\event\sessions_viewed::create($params); 122 $event->add_record_snapshot('chat', $chat); 123 $sink = $this->redirectEvents(); 124 $event->trigger(); 125 $events = $sink->get_events(); 126 $event = reset($events); 127 $this->assertInstanceOf('\mod_chat\event\sessions_viewed', $event); 128 $this->assertEquals($USER->id, $event->userid); 129 $this->assertEquals(context_module::instance($chat->cmid), $event->get_context()); 130 $this->assertEquals(1234, $event->other['start']); 131 $this->assertEquals(5678, $event->other['end']); 132 $this->assertEquals($chat->id, $event->objectid); 133 $this->assertEquals($chat, $event->get_record_snapshot('chat', $chat->id)); 134 $expected = array($course->id, 'chat', 'report', "report.php?id=$chat->cmid", $chat->id, $chat->cmid); 135 $this->assertEventLegacyLogData($expected, $event); 136 $this->assertEventContextNotUsed($event); 137 } 138 139 public function test_course_module_instance_list_viewed() { 140 global $USER; 141 $this->resetAfterTest(); 142 143 // Not much can be tested here as the event is only triggered on a page load, 144 // let's just check that the event contains the expected basic information. 145 $this->setAdminUser(); 146 $course = $this->getDataGenerator()->create_course(); 147 148 $params = array( 149 'context' => context_course::instance($course->id) 150 ); 151 $event = \mod_chat\event\course_module_instance_list_viewed::create($params); 152 $sink = $this->redirectEvents(); 153 $event->trigger(); 154 $events = $sink->get_events(); 155 $event = reset($events); 156 $this->assertInstanceOf('\mod_chat\event\course_module_instance_list_viewed', $event); 157 $this->assertEquals($USER->id, $event->userid); 158 $this->assertEquals(context_course::instance($course->id), $event->get_context()); 159 $expected = array($course->id, 'chat', 'view all', "index.php?id=$course->id", ''); 160 $this->assertEventLegacyLogData($expected, $event); 161 $this->assertEventContextNotUsed($event); 162 } 163 164 public function test_course_module_viewed() { 165 $this->resetAfterTest(); 166 $this->setAdminUser(); 167 $course = $this->getDataGenerator()->create_course(); 168 $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id)); 169 $cm = get_coursemodule_from_instance('chat', $chat->id); 170 $context = context_module::instance($cm->id); 171 172 $params = array( 173 'objectid' => $chat->id, 174 'context' => $context 175 ); 176 $event = \mod_chat\event\course_module_viewed::create($params); 177 $event->add_record_snapshot('chat', $chat); 178 $event->trigger(); 179 180 $expected = array($course->id, 'chat', 'view', "view.php?id=$cm->id", $chat->id, $cm->id); 181 $this->assertEventLegacyLogData($expected, $event); 182 $this->assertEventContextNotUsed($event); 183 $url = new moodle_url('/mod/chat/view.php', array('id' => $cm->id)); 184 $this->assertEquals($url, $event->get_url()); 185 $event->get_name(); 186 } 187 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body