Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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_data
  21   * @category test
  22   * @copyright 2014 Mark Nelson <markn@moodle.com>
  23   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_data\event;
  27  
  28  class events_test extends \advanced_testcase {
  29  
  30      /**
  31       * Test set up.
  32       *
  33       * This is executed before running any test in this file.
  34       */
  35      public function setUp(): void {
  36          $this->resetAfterTest();
  37      }
  38  
  39      /**
  40       * Test the field created event.
  41       */
  42      public function test_field_created() {
  43          $this->setAdminUser();
  44  
  45          // Create a course we are going to add a data module to.
  46          $course = $this->getDataGenerator()->create_course();
  47  
  48          // The generator used to create a data module.
  49          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  50  
  51          // Create a data module.
  52          $data = $generator->create_instance(array('course' => $course->id));
  53  
  54          // Now we want to create a field.
  55          $field = data_get_field_new('text', $data);
  56          $fielddata = new \stdClass();
  57          $fielddata->name = 'Test';
  58          $fielddata->description = 'Test description';
  59          $field->define_field($fielddata);
  60  
  61          // Trigger and capture the event for creating a field.
  62          $sink = $this->redirectEvents();
  63          $field->insert_field();
  64          $events = $sink->get_events();
  65          $event = reset($events);
  66  
  67          // Check that the event data is valid.
  68          $this->assertInstanceOf('\mod_data\event\field_created', $event);
  69          $this->assertEquals(\context_module::instance($data->cmid), $event->get_context());
  70          $expected = array($course->id, 'data', 'fields add', 'field.php?d=' . $data->id . '&amp;mode=display&amp;fid=' .
  71              $field->field->id, $field->field->id, $data->cmid);
  72          $this->assertEventLegacyLogData($expected, $event);
  73          $this->assertEventContextNotUsed($event);
  74          $url = new \moodle_url('/mod/data/field.php', array('d' => $data->id));
  75          $this->assertEquals($url, $event->get_url());
  76      }
  77  
  78      /**
  79       * Test the field updated event.
  80       */
  81      public function test_field_updated() {
  82          $this->setAdminUser();
  83  
  84          // Create a course we are going to add a data module to.
  85          $course = $this->getDataGenerator()->create_course();
  86  
  87          // The generator used to create a data module.
  88          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
  89  
  90          // Create a data module.
  91          $data = $generator->create_instance(array('course' => $course->id));
  92  
  93          // Now we want to create a field.
  94          $field = data_get_field_new('text', $data);
  95          $fielddata = new \stdClass();
  96          $fielddata->name = 'Test';
  97          $fielddata->description = 'Test description';
  98          $field->define_field($fielddata);
  99          $field->insert_field();
 100  
 101          // Trigger and capture the event for updating the field.
 102          $sink = $this->redirectEvents();
 103          $field->update_field();
 104          $events = $sink->get_events();
 105          $event = reset($events);
 106  
 107          // Check that the event data is valid.
 108          $this->assertInstanceOf('\mod_data\event\field_updated', $event);
 109          $this->assertEquals(\context_module::instance($data->cmid), $event->get_context());
 110          $expected = array($course->id, 'data', 'fields update', 'field.php?d=' . $data->id . '&amp;mode=display&amp;fid=' .
 111              $field->field->id, $field->field->id, $data->cmid);
 112          $this->assertEventLegacyLogData($expected, $event);
 113          $this->assertEventContextNotUsed($event);
 114          $url = new \moodle_url('/mod/data/field.php', array('d' => $data->id));
 115          $this->assertEquals($url, $event->get_url());
 116      }
 117  
 118      /**
 119       * Test the field deleted event.
 120       */
 121      public function test_field_deleted() {
 122          $this->setAdminUser();
 123  
 124          // Create a course we are going to add a data module to.
 125          $course = $this->getDataGenerator()->create_course();
 126  
 127          // The generator used to create a data module.
 128          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
 129  
 130          // Create a data module.
 131          $data = $generator->create_instance(array('course' => $course->id));
 132  
 133          // Now we want to create a field.
 134          $field = data_get_field_new('text', $data);
 135          $fielddata = new \stdClass();
 136          $fielddata->name = 'Test';
 137          $fielddata->description = 'Test description';
 138          $field->define_field($fielddata);
 139          $field->insert_field();
 140  
 141          // Trigger and capture the event for deleting the field.
 142          $sink = $this->redirectEvents();
 143          $field->delete_field();
 144          $events = $sink->get_events();
 145          $event = reset($events);
 146  
 147          // Check that the event data is valid.
 148          $this->assertInstanceOf('\mod_data\event\field_deleted', $event);
 149          $this->assertEquals(\context_module::instance($data->cmid), $event->get_context());
 150          $expected = array($course->id, 'data', 'fields delete', 'field.php?d=' . $data->id, $field->field->name, $data->cmid);
 151          $this->assertEventLegacyLogData($expected, $event);
 152          $this->assertEventContextNotUsed($event);
 153          $url = new \moodle_url('/mod/data/field.php', array('d' => $data->id));
 154          $this->assertEquals($url, $event->get_url());
 155      }
 156  
 157      /**
 158       * Test the record created event.
 159       */
 160      public function test_record_created() {
 161          // Create a course we are going to add a data module to.
 162          $course = $this->getDataGenerator()->create_course();
 163  
 164          // The generator used to create a data module.
 165          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
 166  
 167          // Create a data module.
 168          $data = $generator->create_instance(array('course' => $course->id));
 169  
 170          // Trigger and capture the event for creating the record.
 171          $sink = $this->redirectEvents();
 172          $recordid = data_add_record($data);
 173          $events = $sink->get_events();
 174          $event = reset($events);
 175  
 176          // Check that the event data is valid.
 177          $this->assertInstanceOf('\mod_data\event\record_created', $event);
 178          $this->assertEquals(\context_module::instance($data->cmid), $event->get_context());
 179          $expected = array($course->id, 'data', 'add', 'view.php?d=' . $data->id . '&amp;rid=' . $recordid,
 180              $data->id, $data->cmid);
 181          $this->assertEventLegacyLogData($expected, $event);
 182          $this->assertEventContextNotUsed($event);
 183          $url = new \moodle_url('/mod/data/view.php', array('d' => $data->id, 'rid' => $recordid));
 184          $this->assertEquals($url, $event->get_url());
 185      }
 186  
 187      /**
 188       * Test the record updated event.
 189       *
 190       * There is no external API for updating a record, so the unit test will simply create
 191       * and trigger the event and ensure the legacy log data is returned as expected.
 192       */
 193      public function test_record_updated() {
 194          // Create a course we are going to add a data module to.
 195          $course = $this->getDataGenerator()->create_course();
 196  
 197          // The generator used to create a data module.
 198          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
 199  
 200          // Create a data module.
 201          $data = $generator->create_instance(array('course' => $course->id));
 202  
 203          // Trigger an event for updating this record.
 204          $event = \mod_data\event\record_updated::create(array(
 205              'objectid' => 1,
 206              'context' => \context_module::instance($data->cmid),
 207              'courseid' => $course->id,
 208              'other' => array(
 209                  'dataid' => $data->id
 210              )
 211          ));
 212  
 213          // Trigger and capture the event for updating the data record.
 214          $sink = $this->redirectEvents();
 215          $event->trigger();
 216          $events = $sink->get_events();
 217          $event = reset($events);
 218  
 219          // Check that the event data is valid.
 220          $this->assertInstanceOf('\mod_data\event\record_updated', $event);
 221          $this->assertEquals(\context_module::instance($data->cmid), $event->get_context());
 222          $expected = array($course->id, 'data', 'update', 'view.php?d=' . $data->id . '&amp;rid=1', $data->id, $data->cmid);
 223          $this->assertEventLegacyLogData($expected, $event);
 224          $this->assertEventContextNotUsed($event);
 225          $url = new \moodle_url('/mod/data/view.php', array('d' => $data->id, 'rid' => $event->objectid));
 226          $this->assertEquals($url, $event->get_url());
 227      }
 228  
 229      /**
 230       * Test the record deleted event.
 231       */
 232      public function test_record_deleted() {
 233          global $DB;
 234  
 235          // Create a course we are going to add a data module to.
 236          $course = $this->getDataGenerator()->create_course();
 237  
 238          // The generator used to create a data module.
 239          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
 240  
 241          // Create a data module.
 242          $data = $generator->create_instance(array('course' => $course->id));
 243  
 244          // Now we want to create a field.
 245          $field = data_get_field_new('text', $data);
 246          $fielddata = new \stdClass();
 247          $fielddata->name = 'Test';
 248          $fielddata->description = 'Test description';
 249          $field->define_field($fielddata);
 250          $field->insert_field();
 251  
 252          // Create data record.
 253          $datarecords = new \stdClass();
 254          $datarecords->userid = '2';
 255          $datarecords->dataid = $data->id;
 256          $datarecords->id = $DB->insert_record('data_records', $datarecords);
 257  
 258          // Create data content.
 259          $datacontent = new \stdClass();
 260          $datacontent->fieldid = $field->field->id;
 261          $datacontent->recordid = $datarecords->id;
 262          $datacontent->id = $DB->insert_record('data_content', $datacontent);
 263  
 264          // Trigger and capture the event for deleting the data record.
 265          $sink = $this->redirectEvents();
 266          data_delete_record($datarecords->id, $data, $course->id, $data->cmid);
 267          $events = $sink->get_events();
 268          $event = reset($events);
 269  
 270          // Check that the event data is valid.
 271          $this->assertInstanceOf('\mod_data\event\record_deleted', $event);
 272          $this->assertEquals(\context_module::instance($data->cmid), $event->get_context());
 273          $expected = array($course->id, 'data', 'record delete', 'view.php?id=' . $data->cmid, $data->id, $data->cmid);
 274          $this->assertEventLegacyLogData($expected, $event);
 275          $this->assertEventContextNotUsed($event);
 276          $url = new \moodle_url('/mod/data/view.php', array('d' => $data->id));
 277          $this->assertEquals($url, $event->get_url());
 278      }
 279  
 280      /**
 281       * Test the template viewed event.
 282       *
 283       * There is no external API for viewing templates, so the unit test will simply create
 284       * and trigger the event and ensure the legacy log data is returned as expected.
 285       */
 286      public function test_template_viewed() {
 287          // Create a course we are going to add a data module to.
 288          $course = $this->getDataGenerator()->create_course();
 289  
 290          // The generator used to create a data module.
 291          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
 292  
 293          // Create a data module.
 294          $data = $generator->create_instance(array('course' => $course->id));
 295  
 296          // Trigger an event for updating this record.
 297          $event = \mod_data\event\template_viewed::create(array(
 298              'context' => \context_module::instance($data->cmid),
 299              'courseid' => $course->id,
 300              'other' => array(
 301                  'dataid' => $data->id
 302              )
 303          ));
 304  
 305          // Trigger and capture the event for updating the data record.
 306          $sink = $this->redirectEvents();
 307          $event->trigger();
 308          $events = $sink->get_events();
 309          $event = reset($events);
 310  
 311          // Check that the event data is valid.
 312          $this->assertInstanceOf('\mod_data\event\template_viewed', $event);
 313          $this->assertEquals(\context_module::instance($data->cmid), $event->get_context());
 314          $expected = array($course->id, 'data', 'templates view', 'templates.php?id=' . $data->cmid . '&amp;d=' .
 315              $data->id, $data->id, $data->cmid);
 316          $this->assertEventLegacyLogData($expected, $event);
 317          $this->assertEventContextNotUsed($event);
 318          $url = new \moodle_url('/mod/data/templates.php', array('d' => $data->id));
 319          $this->assertEquals($url, $event->get_url());
 320      }
 321  
 322      /**
 323       * Test the template updated event.
 324       *
 325       * There is no external API for updating a template, so the unit test will simply create
 326       * and trigger the event and ensure the legacy log data is returned as expected.
 327       */
 328      public function test_template_updated() {
 329          // Create a course we are going to add a data module to.
 330          $course = $this->getDataGenerator()->create_course();
 331  
 332          // The generator used to create a data module.
 333          $generator = $this->getDataGenerator()->get_plugin_generator('mod_data');
 334  
 335          // Create a data module.
 336          $data = $generator->create_instance(array('course' => $course->id));
 337  
 338          // Trigger an event for updating this record.
 339          $event = \mod_data\event\template_updated::create(array(
 340              'context' => \context_module::instance($data->cmid),
 341              'courseid' => $course->id,
 342              'other' => array(
 343                  'dataid' => $data->id,
 344              )
 345          ));
 346  
 347          // Trigger and capture the event for updating the data record.
 348          $sink = $this->redirectEvents();
 349          $event->trigger();
 350          $events = $sink->get_events();
 351          $event = reset($events);
 352  
 353          // Check that the event data is valid.
 354          $this->assertInstanceOf('\mod_data\event\template_updated', $event);
 355          $this->assertEquals(\context_module::instance($data->cmid), $event->get_context());
 356          $expected = array($course->id, 'data', 'templates saved', 'templates.php?id=' . $data->cmid . '&amp;d=' .
 357              $data->id, $data->id, $data->cmid);
 358          $this->assertEventLegacyLogData($expected, $event);
 359          $this->assertEventContextNotUsed($event);
 360          $url = new \moodle_url('/mod/data/templates.php', array('d' => $data->id));
 361          $this->assertEquals($url, $event->get_url());
 362      }
 363  }