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