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.
   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   * Tests the \core\event\grade_deleted event.
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2014 Mark Nelson <markn@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  /**
  29   * Class core_event_grade_deleted_testcase
  30   *
  31   * Tests for event \core\event\grade_deleted
  32   *
  33   * @package    core
  34   * @category   test
  35   * @copyright  2014 Mark Nelson <markn@moodle.com>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class core_event_grade_deleted_testcase extends advanced_testcase {
  39  
  40      /**
  41       * Tests the event details.
  42       */
  43      public function test_event() {
  44          global $CFG;
  45          require_once("$CFG->libdir/gradelib.php");
  46  
  47          $this->resetAfterTest();
  48  
  49          $course = $this->getDataGenerator()->create_course();
  50          $user = $this->getDataGenerator()->create_user();
  51          $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
  52  
  53          // Create a grade item for the quiz.
  54          $grade = array();
  55          $grade['userid'] = $user->id;
  56          $grade['rawgrade'] = 50;
  57          grade_update('mod/quiz', $course->id, 'mod', 'quiz', $quiz->id, 0, $grade);
  58  
  59          // Get the grade item and override it.
  60          $gradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id,
  61              'courseid' => $course->id));
  62          $gradeitem->update_final_grade($user->id, 10, 'gradebook');
  63  
  64          // Get the grade_grade object.
  65          $gradegrade = new grade_grade(array('userid' => $user->id, 'itemid' => $gradeitem->id), true);
  66          $gradegrade->grade_item = $gradeitem;
  67  
  68          // Trigger the event.
  69          $sink = $this->redirectEvents();
  70          course_delete_module($quiz->cmid);
  71          $events = $sink->get_events();
  72          $event = $events[1];
  73          $sink->close();
  74  
  75          // Check the event details are correct.
  76          $grade = $event->get_grade();
  77          $this->assertInstanceOf('grade_grade', $grade);
  78          $this->assertInstanceOf('\core\event\grade_deleted', $event);
  79          $this->assertEquals(context_course::instance($course->id), $event->get_context());
  80          $this->assertSame($event->objecttable, 'grade_grades');
  81          $this->assertEquals($event->objectid, $gradegrade->id);
  82          $this->assertEquals($event->other['itemid'], $gradeitem->id);
  83          $this->assertTrue($event->other['overridden']);
  84          $this->assertEquals(10, $event->other['finalgrade']);
  85          $this->assertEventContextNotUsed($event);
  86          $this->assertEquals($gradegrade->id, $grade->id);
  87      }
  88  }