Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  
  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  
  91          // Sending a message from the system should not trigger any event.
  92          $sink->clear();
  93          $this->setAdminUser();
  94          chat_send_chatmessage($chatuser1, 'enter', true);
  95          $this->assertEquals(0, $sink->count());
  96  
  97          $sink->close();
  98      }
  99  
 100      public function test_sessions_viewed() {
 101          global $USER;
 102          $this->resetAfterTest();
 103  
 104          // Not much can be tested here as the event is only triggered on a page load,
 105          // let's just check that the event contains the expected basic information.
 106          $this->setAdminUser();
 107          $course = $this->getDataGenerator()->create_course();
 108          $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
 109  
 110          $params = array(
 111              'context' => \context_module::instance($chat->cmid),
 112              'objectid' => $chat->id,
 113              'other' => array(
 114                  'start' => 1234,
 115                  'end' => 5678
 116              )
 117          );
 118          $event = \mod_chat\event\sessions_viewed::create($params);
 119          $event->add_record_snapshot('chat', $chat);
 120          $sink = $this->redirectEvents();
 121          $event->trigger();
 122          $events = $sink->get_events();
 123          $event = reset($events);
 124          $this->assertInstanceOf('\mod_chat\event\sessions_viewed', $event);
 125          $this->assertEquals($USER->id, $event->userid);
 126          $this->assertEquals(\context_module::instance($chat->cmid), $event->get_context());
 127          $this->assertEquals(1234, $event->other['start']);
 128          $this->assertEquals(5678, $event->other['end']);
 129          $this->assertEquals($chat->id, $event->objectid);
 130          $this->assertEquals($chat, $event->get_record_snapshot('chat', $chat->id));
 131      }
 132  
 133      public function test_course_module_instance_list_viewed() {
 134          global $USER;
 135          $this->resetAfterTest();
 136  
 137          // Not much can be tested here as the event is only triggered on a page load,
 138          // let's just check that the event contains the expected basic information.
 139          $this->setAdminUser();
 140          $course = $this->getDataGenerator()->create_course();
 141  
 142          $params = array(
 143              'context' => \context_course::instance($course->id)
 144          );
 145          $event = \mod_chat\event\course_module_instance_list_viewed::create($params);
 146          $sink = $this->redirectEvents();
 147          $event->trigger();
 148          $events = $sink->get_events();
 149          $event = reset($events);
 150          $this->assertInstanceOf('\mod_chat\event\course_module_instance_list_viewed', $event);
 151          $this->assertEquals($USER->id, $event->userid);
 152          $this->assertEquals(\context_course::instance($course->id), $event->get_context());
 153      }
 154  
 155      public function test_course_module_viewed() {
 156          $this->resetAfterTest();
 157          $this->setAdminUser();
 158          $course = $this->getDataGenerator()->create_course();
 159          $chat = $this->getDataGenerator()->create_module('chat', array('course' => $course->id));
 160          $cm = get_coursemodule_from_instance('chat', $chat->id);
 161          $context = \context_module::instance($cm->id);
 162  
 163          $params = array(
 164              'objectid' => $chat->id,
 165              'context' => $context
 166          );
 167          $event = \mod_chat\event\course_module_viewed::create($params);
 168          $event->add_record_snapshot('chat', $chat);
 169          $event->trigger();
 170  
 171          $url = new \moodle_url('/mod/chat/view.php', array('id' => $cm->id));
 172          $this->assertEquals($url, $event->get_url());
 173          $event->get_name();
 174      }
 175  }