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 310 and 401]

   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   * Grade item deleted event tests.
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2020 Tom Dickman <tomdickman@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace core\event;
  27  
  28  /**
  29   * Test for grade item deleted event.
  30   *
  31   * @package    core
  32   * @category   test
  33   * @copyright  2020 Tom Dickman <tomdickman@catalyst-au.net>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   * @coversDefaultClass \core\event\grade_item_deleted
  36   */
  37  class grade_item_deleted_test extends \advanced_testcase {
  38  
  39      /**
  40       * Test the grade item deleted event.
  41       *
  42       * @covers ::create_from_grade_item
  43       */
  44      public function test_grade_item_deleted() {
  45          global $DB;
  46  
  47          $this->resetAfterTest();
  48  
  49          $course = $this->getDataGenerator()->create_course();
  50          $gradeitemrecord = $this->getDataGenerator()->create_grade_item(['courseid' => $course->id]);
  51          $gradeitem = \grade_item::fetch(['id' => $gradeitemrecord->id, 'courseid' => $course->id]);
  52  
  53          $countgradeitems = $DB->count_records('grade_items');
  54  
  55          // Trigger and capture the event for deleting a grade item.
  56          $sink = $this->redirectEvents();
  57          $gradeitem->delete();
  58          $events = $sink->get_events();
  59          $sink->close();
  60  
  61          // Event should only be triggered once.
  62          $this->assertCount(1, $events);
  63          $event = reset($events);
  64  
  65          // Expect that the grade item was deleted and the event data is valid.
  66          $this->assertEquals($countgradeitems - 1, $DB->count_records('grade_items'));
  67          $this->assertInstanceOf('\core\event\grade_item_deleted', $event);
  68          $eventdata = $event->get_data();
  69          $this->assertEquals($gradeitem->id, $eventdata['objectid']);
  70          $this->assertEquals($gradeitem->courseid, $eventdata['courseid']);
  71          $this->assertEquals(\context_course::instance($gradeitem->courseid)->id, $eventdata['contextid']);
  72          $this->assertEquals($gradeitem->itemname, $eventdata['other']['itemname']);
  73          $this->assertEquals($gradeitem->itemtype, $eventdata['other']['itemtype']);
  74          $this->assertEquals($gradeitem->itemmodule, $eventdata['other']['itemmodule']);
  75      }
  76  }