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   * 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 (__DIR__ . '/../fixtures/testable.php');
  36  
  37  
  38  /**
  39   * Test cases for the internal workshop api
  40   */
  41  class events_test extends \advanced_testcase {
  42  
  43      /** @var \stdClass $workshop Basic workshop data stored in an object. */
  44      protected $workshop;
  45      /** @var \stdClass $course Generated Random Course. */
  46      protected $course;
  47      /** @var stdClass mod info */
  48      protected $cm;
  49      /** @var context $context Course module context. */
  50      protected $context;
  51  
  52      /**
  53       * Set up the testing environment.
  54       */
  55      protected function setUp(): void {
  56          parent::setUp();
  57          $this->setAdminUser();
  58  
  59          // Create a workshop activity.
  60          $this->course = $this->getDataGenerator()->create_course();
  61          $this->workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $this->course));
  62          $this->cm = get_coursemodule_from_instance('workshop', $this->workshop->id);
  63          $this->context = \context_module::instance($this->cm->id);
  64      }
  65  
  66      protected function tearDown(): void {
  67          $this->workshop = null;
  68          $this->course = null;
  69          $this->cm = null;
  70          $this->context = null;
  71          parent::tearDown();
  72      }
  73  
  74      /**
  75       * This event is triggered in view.php and workshop/lib.php through the function workshop_cron().
  76       */
  77      public function test_phase_switched_event() {
  78          $this->resetAfterTest();
  79          $this->setAdminUser();
  80  
  81          // Add additional workshop information.
  82          $this->workshop->phase = 20;
  83          $this->workshop->phaseswitchassessment = 1;
  84          $this->workshop->submissionend = time() - 1;
  85  
  86          $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
  87          $workshop = new testable_workshop($this->workshop, $cm, $this->course);
  88  
  89          // The phase that we are switching to.
  90          $newphase = 30;
  91          // Trigger and capture the event.
  92          $sink = $this->redirectEvents();
  93          $workshop->switch_phase($newphase);
  94          $events = $sink->get_events();
  95          $event = reset($events);
  96  
  97          $this->assertEventContextNotUsed($event);
  98  
  99          $sink->close();
 100      }
 101  
 102      public function test_assessment_evaluated() {
 103          $this->resetAfterTest();
 104          $this->setAdminUser();
 105  
 106          $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
 107  
 108          $workshop = new testable_workshop($this->workshop, $cm, $this->course);
 109  
 110          $assessments = array();
 111          $assessments[] = (object)array('reviewerid' => 2, 'gradinggrade' => null,
 112              'gradinggradeover' => null, 'aggregationid' => null, 'aggregatedgrade' => 12);
 113  
 114          // Trigger and capture the event.
 115          $sink = $this->redirectEvents();
 116          $workshop->aggregate_grading_grades_process($assessments);
 117          $events = $sink->get_events();
 118          $event = reset($events);
 119  
 120          $this->assertInstanceOf('\mod_workshop\event\assessment_evaluated', $event);
 121          $this->assertEquals('workshop_aggregations', $event->objecttable);
 122          $this->assertEquals(\context_module::instance($cm->id), $event->get_context());
 123          $this->assertEventContextNotUsed($event);
 124  
 125          $sink->close();
 126      }
 127  
 128      public function test_assessment_reevaluated() {
 129          $this->resetAfterTest();
 130          $this->setAdminUser();
 131  
 132          $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST);
 133  
 134          $workshop = new testable_workshop($this->workshop, $cm, $this->course);
 135  
 136          $assessments = array();
 137          $assessments[] = (object)array('reviewerid' => 2, 'gradinggrade' => null, 'gradinggradeover' => null,
 138              'aggregationid' => 2, 'aggregatedgrade' => 12);
 139  
 140          // Trigger and capture the event.
 141          $sink = $this->redirectEvents();
 142          $workshop->aggregate_grading_grades_process($assessments);
 143          $events = $sink->get_events();
 144          $event = reset($events);
 145  
 146          $this->assertInstanceOf('\mod_workshop\event\assessment_reevaluated', $event);
 147          $this->assertEquals('workshop_aggregations', $event->objecttable);
 148          $this->assertEquals(\context_module::instance($cm->id), $event->get_context());
 149          $this->assertEventContextNotUsed($event);
 150  
 151          $sink->close();
 152      }
 153  
 154      /**
 155       * There is no api involved so the best we can do is test legacy data by triggering event manually.
 156       */
 157      public function test_aggregate_grades_reset_event() {
 158          $this->resetAfterTest();
 159          $this->setAdminUser();
 160  
 161          $event = \mod_workshop\event\assessment_evaluations_reset::create(array(
 162              'context'  => $this->context,
 163              'courseid' => $this->course->id,
 164              'other' => array('workshopid' => $this->workshop->id)
 165          ));
 166  
 167          // Trigger and capture the event.
 168          $sink = $this->redirectEvents();
 169          $event->trigger();
 170          $events = $sink->get_events();
 171          $event = reset($events);
 172  
 173          $sink->close();
 174      }
 175  
 176      /**
 177       * There is no api involved so the best we can do is test legacy data by triggering event manually.
 178       */
 179      public function test_instances_list_viewed_event() {
 180          $this->resetAfterTest();
 181          $this->setAdminUser();
 182  
 183          $context = \context_course::instance($this->course->id);
 184  
 185          $event = \mod_workshop\event\course_module_instance_list_viewed::create(array('context' => $context));
 186  
 187          // Trigger and capture the event.
 188          $sink = $this->redirectEvents();
 189          $event->trigger();
 190          $events = $sink->get_events();
 191          $event = reset($events);
 192  
 193          $this->assertEventContextNotUsed($event);
 194  
 195          $sink->close();
 196      }
 197  
 198      /**
 199       * There is no api involved so the best we can do is test legacy data by triggering event manually.
 200       */
 201      public function test_submission_created_event() {
 202          $this->resetAfterTest();
 203          $this->setAdminUser();
 204  
 205          $user = $this->getDataGenerator()->create_user();
 206          $submissionid = 48;
 207  
 208          $event = \mod_workshop\event\submission_created::create(array(
 209                  'objectid'      => $submissionid,
 210                  'context'       => $this->context,
 211                  'courseid'      => $this->course->id,
 212                  'relateduserid' => $user->id,
 213                  'other'         => array(
 214                      'submissiontitle' => 'The submission title'
 215                  )
 216              )
 217          );
 218  
 219          // Trigger and capture the event.
 220          $sink = $this->redirectEvents();
 221          $event->trigger();
 222          $events = $sink->get_events();
 223          $event = reset($events);
 224  
 225          $this->assertEventContextNotUsed($event);
 226  
 227          $sink->close();
 228      }
 229  }