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