Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

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