Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 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   * Unit tests for workshop events.
  19   *
  20   * @package    mod_workshop
  21   * @category   phpunit
  22   * @copyright  2013 Adrian Greeve <adrian@moodle.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace mod_workshop\event;
  27  
  28  use testable_workshop;
  29  
  30  defined('MOODLE_INTERNAL') || die();
  31  
  32  global $CFG;
  33  require_once($CFG->dirroot . '/mod/workshop/lib.php'); // Include the code to test.
  34  require_once($CFG->dirroot . '/mod/workshop/locallib.php'); // Include the code to test.
  35  require_once($CFG->dirroot . '/lib/cronlib.php'); // Include the code to test.
  36  require_once (__DIR__ . '/../fixtures/testable.php');
  37  
  38  
  39  /**
  40   * Test cases for the internal workshop api
  41   */
  42  class events_test extends \advanced_testcase {
  43  
  44      /** @var \stdClass $workshop Basic workshop data stored in an object. */
  45      protected $workshop;
  46      /** @var \stdClass $course Generated Random Course. */
  47      protected $course;
  48      /** @var stdClass mod info */
  49      protected $cm;
  50      /** @var context $context Course module context. */
  51      protected $context;
  52  
  53      /**
  54       * Set up the testing environment.
  55       */
  56      protected function setUp(): void {
  57          parent::setUp();
  58          $this->setAdminUser();
  59  
  60          // Create a workshop activity.
  61          $this->course = $this->getDataGenerator()->create_course();
  62          $this->workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $this->course));
  63          $this->cm = get_coursemodule_from_instance('workshop', $this->workshop->id);
  64          $this->context = \context_module::instance($this->cm->id);
  65      }
  66  
  67      protected function tearDown(): void {
  68          $this->workshop = null;
  69          $this->course = null;
  70          $this->cm = null;
  71          $this->context = null;
  72          parent::tearDown();
  73      }
  74  
  75      /**
  76       * This event is triggered in view.php and workshop/lib.php through the function workshop_cron().
  77       */
  78      public function test_phase_switched_event() {
  79          $this->resetAfterTest();
  80          $this->setAdminUser();
  81  
  82          // Add additional workshop information.
  83          $this->workshop->phase = 20;
  84          $this->workshop->phaseswitchassessment = 1;
  85          $this->workshop->submissionend = time() - 1;
  86  
  87          $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
  88          $workshop = new testable_workshop($this->workshop, $cm, $this->course);
  89  
  90          // The phase that we are switching to.
  91          $newphase = 30;
  92          // Trigger and capture the event.
  93          $sink = $this->redirectEvents();
  94          $workshop->switch_phase($newphase);
  95          $events = $sink->get_events();
  96          $event = reset($events);
  97  
  98          // Check that the legacy log data is valid.
  99          $expected = array($this->course->id, 'workshop', 'update switch phase', 'view.php?id=' . $this->cm->id,
 100              $newphase, $this->cm->id);
 101          $this->assertEventLegacyLogData($expected, $event);
 102          $this->assertEventContextNotUsed($event);
 103  
 104          $sink->close();
 105      }
 106  
 107      public function test_assessment_evaluated() {
 108          $this->resetAfterTest();
 109          $this->setAdminUser();
 110  
 111          $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
 112  
 113          $workshop = new testable_workshop($this->workshop, $cm, $this->course);
 114  
 115          $assessments = array();
 116          $assessments[] = (object)array('reviewerid' => 2, 'gradinggrade' => null,
 117              'gradinggradeover' => null, 'aggregationid' => null, 'aggregatedgrade' => 12);
 118  
 119          // Trigger and capture the event.
 120          $sink = $this->redirectEvents();
 121          $workshop->aggregate_grading_grades_process($assessments);
 122          $events = $sink->get_events();
 123          $event = reset($events);
 124  
 125          $this->assertInstanceOf('\mod_workshop\event\assessment_evaluated', $event);
 126          $this->assertEquals('workshop_aggregations', $event->objecttable);
 127          $this->assertEquals(\context_module::instance($cm->id), $event->get_context());
 128          $this->assertEventContextNotUsed($event);
 129  
 130          $sink->close();
 131      }
 132  
 133      public function test_assessment_reevaluated() {
 134          $this->resetAfterTest();
 135          $this->setAdminUser();
 136  
 137          $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
 138  
 139          $workshop = new testable_workshop($this->workshop, $cm, $this->course);
 140  
 141          $assessments = array();
 142          $assessments[] = (object)array('reviewerid' => 2, 'gradinggrade' => null, 'gradinggradeover' => null,
 143              'aggregationid' => 2, 'aggregatedgrade' => 12);
 144  
 145          // Trigger and capture the event.
 146          $sink = $this->redirectEvents();
 147          $workshop->aggregate_grading_grades_process($assessments);
 148          $events = $sink->get_events();
 149          $event = reset($events);
 150  
 151          $this->assertInstanceOf('\mod_workshop\event\assessment_reevaluated', $event);
 152          $this->assertEquals('workshop_aggregations', $event->objecttable);
 153          $this->assertEquals(\context_module::instance($cm->id), $event->get_context());
 154          $expected = array($this->course->id, 'workshop', 'update aggregate grade',
 155              'view.php?id=' . $event->get_context()->instanceid, $event->objectid, $event->get_context()->instanceid);
 156          $this->assertEventLegacyLogData($expected, $event);
 157          $this->assertEventContextNotUsed($event);
 158  
 159          $sink->close();
 160      }
 161  
 162      /**
 163       * There is no api involved so the best we can do is test legacy data by triggering event manually.
 164       */
 165      public function test_aggregate_grades_reset_event() {
 166          $this->resetAfterTest();
 167          $this->setAdminUser();
 168  
 169          $event = \mod_workshop\event\assessment_evaluations_reset::create(array(
 170              'context'  => $this->context,
 171              'courseid' => $this->course->id,
 172              'other' => array('workshopid' => $this->workshop->id)
 173          ));
 174  
 175          // Trigger and capture the event.
 176          $sink = $this->redirectEvents();
 177          $event->trigger();
 178          $events = $sink->get_events();
 179          $event = reset($events);
 180  
 181          // Check that the legacy log data is valid.
 182          $expected = array($this->course->id, 'workshop', 'update clear aggregated grade', 'view.php?id=' . $this->cm->id,
 183              $this->workshop->id, $this->cm->id);
 184          $this->assertEventLegacyLogData($expected, $event);
 185  
 186          $sink->close();
 187      }
 188  
 189      /**
 190       * There is no api involved so the best we can do is test legacy data by triggering event manually.
 191       */
 192      public function test_instances_list_viewed_event() {
 193          $this->resetAfterTest();
 194          $this->setAdminUser();
 195  
 196          $context = \context_course::instance($this->course->id);
 197  
 198          $event = \mod_workshop\event\course_module_instance_list_viewed::create(array('context' => $context));
 199  
 200          // Trigger and capture the event.
 201          $sink = $this->redirectEvents();
 202          $event->trigger();
 203          $events = $sink->get_events();
 204          $event = reset($events);
 205  
 206          // Check that the legacy log data is valid.
 207          $expected = array($this->course->id, 'workshop', 'view all', 'index.php?id=' . $this->course->id, '');
 208          $this->assertEventLegacyLogData($expected, $event);
 209          $this->assertEventContextNotUsed($event);
 210  
 211          $sink->close();
 212      }
 213  
 214      /**
 215       * There is no api involved so the best we can do is test legacy data by triggering event manually.
 216       */
 217      public function test_submission_created_event() {
 218          $this->resetAfterTest();
 219          $this->setAdminUser();
 220  
 221          $user = $this->getDataGenerator()->create_user();
 222          $submissionid = 48;
 223  
 224          $event = \mod_workshop\event\submission_created::create(array(
 225                  'objectid'      => $submissionid,
 226                  'context'       => $this->context,
 227                  'courseid'      => $this->course->id,
 228                  'relateduserid' => $user->id,
 229                  'other'         => array(
 230                      'submissiontitle' => 'The submission title'
 231                  )
 232              )
 233          );
 234  
 235          // Trigger and capture the event.
 236          $sink = $this->redirectEvents();
 237          $event->trigger();
 238          $events = $sink->get_events();
 239          $event = reset($events);
 240  
 241          // Check that the legacy log data is valid.
 242          $expected = array($this->course->id, 'workshop', 'add submission',
 243              'submission.php?cmid=' . $this->cm->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
 244          $this->assertEventLegacyLogData($expected, $event);
 245          $this->assertEventContextNotUsed($event);
 246  
 247          $sink->close();
 248      }
 249  
 250      /**
 251       * There is no api involved so the best we can do is test legacy data by triggering event manually.
 252       */
 253      public function test_submission_updated_event() {
 254          $this->resetAfterTest();
 255          $this->setAdminUser();
 256  
 257          $user = $this->getDataGenerator()->create_user();
 258          $submissionid = 48;
 259  
 260          $event = \mod_workshop\event\submission_updated::create(array(
 261                  'objectid'      => $submissionid,
 262                  'context'       => $this->context,
 263                  'courseid'      => $this->course->id,
 264                  'relateduserid' => $user->id,
 265                  'other'         => array(
 266                      'submissiontitle' => 'The submission title'
 267                  )
 268              )
 269          );
 270  
 271          // Trigger and capture the event.
 272          $sink = $this->redirectEvents();
 273          $event->trigger();
 274          $events = $sink->get_events();
 275          $event = reset($events);
 276  
 277          // Check that the legacy log data is valid.
 278          $expected = array($this->course->id, 'workshop', 'update submission',
 279              'submission.php?cmid=' . $this->cm->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
 280          $this->assertEventLegacyLogData($expected, $event);
 281          $this->assertEventContextNotUsed($event);
 282  
 283          $sink->close();
 284      }
 285  
 286      /**
 287       * There is no api involved so the best we can do is test legacy data by triggering event manually.
 288       */
 289      public function test_submission_viewed_event() {
 290          $this->resetAfterTest();
 291          $this->setAdminUser();
 292  
 293          $user = $this->getDataGenerator()->create_user();
 294          $submissionid = 48;
 295  
 296          $event = \mod_workshop\event\submission_viewed::create(array(
 297                  'objectid'      => $submissionid,
 298                  'context'       => $this->context,
 299                  'courseid'      => $this->course->id,
 300                  'relateduserid' => $user->id,
 301                  'other'         => array(
 302                      'workshopid' => $this->workshop->id
 303                  )
 304              )
 305          );
 306  
 307          // Trigger and capture the event.
 308          $sink = $this->redirectEvents();
 309          $event->trigger();
 310          $events = $sink->get_events();
 311          $event = reset($events);
 312  
 313          // Check that the legacy log data is valid.
 314          $expected = array($this->course->id, 'workshop', 'view submission',
 315              'submission.php?cmid=' . $this->cm->id . '&id=' . $submissionid, $submissionid, $this->cm->id);
 316          $this->assertEventLegacyLogData($expected, $event);
 317          $this->assertEventContextNotUsed($event);
 318  
 319          $sink->close();
 320      }
 321  }