MDL-49294 logging: Improve cleanup tests
[moodle.git] / lib / tests / event_user_graded_test.php
blob293b00e1109627b386ac72a4b569278d21a15fa3
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.
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/>.
17 /**
18 * Tests for base course module viewed event.
20 * @package core
21 * @category phpunit
22 * @copyright 2014 Petr Skoda
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 /**
29 * Class core_event_user_graded_testcase
31 * Tests for event \core\event\user_graded
33 * @package core
34 * @category phpunit
35 * @copyright 2014 Petr Skoda
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_event_user_graded_testcase extends advanced_testcase {
39 /**
40 * Test the event.
42 public function test_event() {
43 global $CFG;
44 require_once("$CFG->libdir/gradelib.php");
46 $this->resetAfterTest();
48 $course = $this->getDataGenerator()->create_course();
49 $user = $this->getDataGenerator()->create_user();
50 $this->getDataGenerator()->enrol_user($user->id, $course->id);
52 $grade_category = grade_category::fetch_course_category($course->id);
53 $grade_category->load_grade_item();
54 $grade_item = $grade_category->grade_item;
56 $grade_item->update_final_grade($user->id, 10, 'gradebook');
58 $grade_grade = new grade_grade(array('userid' => $user->id, 'itemid' => $grade_item->id), true);
59 $grade_grade->grade_item = $grade_item;
61 $event = \core\event\user_graded::create_from_grade($grade_grade);
63 $this->assertEventLegacyLogData(
64 array($course->id, 'grade', 'update', '/report/grader/index.php?id=' . $course->id, $grade_item->itemname . ': ' . fullname($user)),
65 $event
67 $this->assertEquals(context_course::instance($course->id), $event->get_context());
68 $this->assertSame($event->objecttable, 'grade_grades');
69 $this->assertEquals($event->objectid, $grade_grade->id);
70 $this->assertEquals($event->other['itemid'], $grade_item->id);
71 $this->assertTrue($event->other['overridden']);
72 $this->assertEquals(10, $event->other['finalgrade']);
74 // Trigger the events.
75 $sink = $this->redirectEvents();
76 $event->trigger();
77 $result = $sink->get_events();
78 $sink->close();
80 $this->assertCount(1, $result);
82 $event = reset($result);
83 $this->assertEventContextNotUsed($event);
85 $grade = $event->get_grade();
86 $this->assertInstanceOf('grade_grade', $grade);
87 $this->assertEquals($grade_grade->id, $grade->id);