Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   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 events found in /grade/letter and /grade/scale.
  19   *
  20   * @package   core_grades
  21   * @category  test
  22   * @copyright 2017 Stephen Bourget
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU Public License
  24   */
  25  
  26  namespace core_grades\event;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->dirroot . '/grade/lib.php');
  32  
  33  /**
  34   * Unit tests for grade events.
  35   *
  36   * @package   core_grades
  37   * @category  test
  38   * @copyright 2017 Stephen Bourget
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class events_test extends \advanced_testcase {
  42  
  43      /** @var stdClass the course used for testing */
  44      private $course;
  45  
  46      /**
  47       * Test set up.
  48       *
  49       * This is executed before running any test in this file.
  50       */
  51      public function setUp(): void {
  52          $this->resetAfterTest();
  53  
  54          $this->setAdminUser();
  55          $this->course = $this->getDataGenerator()->create_course();
  56      }
  57  
  58      /**
  59       * Test the grade letter created event.
  60       *
  61       * There is no external API for triggering this event, so the unit test will simply
  62       * create and trigger the event and ensure the data is returned as expected.
  63       */
  64      public function test_grade_letter_created() {
  65          // Create a grade letter created event.
  66          $event = \core\event\grade_letter_created::create(array(
  67              'objectid' => 10,
  68              'context' => \context_course::instance($this->course->id)
  69          ));
  70  
  71          // Trigger and capture the event.
  72          $sink = $this->redirectEvents();
  73          $event->trigger();
  74          $events = $sink->get_events();
  75          $event = reset($events);
  76  
  77          // Check that the event data is valid.
  78          $this->assertInstanceOf('\core\event\grade_letter_created', $event);
  79          $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
  80      }
  81  
  82      /**
  83       * Test the grade letter deleted event.
  84       *
  85       * There is no external API for triggering this event, so the unit test will simply
  86       * create and trigger the event and ensure the data is returned as expected.
  87       */
  88      public function test_grade_letter_deleted() {
  89          // Create a grade letter deleted event.
  90          $event = \core\event\grade_letter_deleted::create(array(
  91              'objectid' => 10,
  92              'context' => \context_course::instance($this->course->id)
  93          ));
  94  
  95          // Trigger and capture the event.
  96          $sink = $this->redirectEvents();
  97          $event->trigger();
  98          $events = $sink->get_events();
  99          $event = reset($events);
 100  
 101          // Check that the event data is valid.
 102          $this->assertInstanceOf('\core\event\grade_letter_deleted', $event);
 103          $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
 104      }
 105  
 106      /**
 107       * Test the grade letter updated event.
 108       *
 109       * There is no external API for triggering this event, so the unit test will simply
 110       * create and trigger the event and ensure the data is returned as expected.
 111       */
 112      public function test_grade_letter_updated() {
 113          // Create a grade letter updated event.
 114          $event = \core\event\grade_letter_updated::create(array(
 115              'objectid' => 10,
 116              'context' => \context_course::instance($this->course->id)
 117          ));
 118  
 119          // Trigger and capture the event.
 120          $sink = $this->redirectEvents();
 121          $event->trigger();
 122          $events = $sink->get_events();
 123          $event = reset($events);
 124  
 125          // Check that the event data is valid.
 126          $this->assertInstanceOf('\core\event\grade_letter_updated', $event);
 127          $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
 128      }
 129  
 130      /**
 131       * Test the scale created event.
 132       */
 133      public function test_scale_created() {
 134          $gradescale = new \grade_scale();
 135          $gradescale->name        = 'unittestscale3';
 136          $gradescale->courseid    = $this->course->id;
 137          $gradescale->userid      = 317;
 138          $gradescale->scale       = 'Distinction, Very Good, Good, Pass, Fail';
 139          $gradescale->description = 'This scale is used to mark standard assignments.';
 140  
 141          $url = new \moodle_url('/grade/edit/scale/index.php', array('id' => $this->course->id));
 142  
 143          // Trigger and capture the event.
 144          $sink = $this->redirectEvents();
 145          $id = $gradescale->insert();
 146          $events = $sink->get_events();
 147          $event = reset($events);
 148  
 149          // Check that the event data is valid.
 150          $this->assertInstanceOf('\core\event\scale_created', $event);
 151          $this->assertEquals($id, $event->objectid);
 152          $this->assertEquals($url, $event->get_url());
 153          $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
 154      }
 155  
 156      /**
 157       * Test the scale deleted event.
 158       */
 159      public function test_scale_deleted() {
 160          $gradescale = new \grade_scale();
 161          $gradescale->name        = 'unittestscale3';
 162          $gradescale->courseid    = $this->course->id;
 163          $gradescale->userid      = 317;
 164          $gradescale->scale       = 'Distinction, Very Good, Good, Pass, Fail';
 165          $gradescale->description = 'This scale is used to mark standard assignments.';
 166          $gradescale->insert();
 167  
 168          // Trigger and capture the event.
 169          $sink = $this->redirectEvents();
 170          $gradescale->delete();
 171          $events = $sink->get_events();
 172          $event = reset($events);
 173  
 174          // Check that the event data is valid.
 175          $this->assertInstanceOf('\core\event\scale_deleted', $event);
 176          $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
 177      }
 178  
 179      /**
 180       * Test the scale updated event.
 181       */
 182      public function test_scale_updated() {
 183          $gradescale = new \grade_scale();
 184          $gradescale->name        = 'unittestscale3';
 185          $gradescale->courseid    = $this->course->id;
 186          $gradescale->userid      = 317;
 187          $gradescale->scale       = 'Distinction, Very Good, Good, Pass, Fail';
 188          $gradescale->description = 'This scale is used to mark standard assignments.';
 189          $id = $gradescale->insert();
 190  
 191          $gradescale->name = 'Updated info for this unittest grade_scale';
 192          $url = new \moodle_url('/grade/edit/scale/index.php', array('id' => $this->course->id));
 193  
 194          // Trigger and capture the event.
 195          $sink = $this->redirectEvents();
 196          $gradescale->update();
 197          $events = $sink->get_events();
 198          $event = reset($events);
 199  
 200          // Check that the event data is valid.
 201          $this->assertInstanceOf('\core\event\scale_updated', $event);
 202          $this->assertEquals($id, $event->objectid);
 203          $this->assertEquals($url, $event->get_url());
 204          $this->assertEquals(\context_course::instance($this->course->id), $event->get_context());
 205      }
 206  }