MDL-40697 core: added more unit tests for the user_graded event
[moodle.git] / lib / tests / event_user_graded_test.php
CommitLineData
ffe60f5b
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/**
b310c9a1 18 * Tests the \core\event\user_graded event.
ffe60f5b
19 *
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
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
b310c9a1
MN
28global $CFG;
29
30require_once($CFG->libdir . '/mathslib.php');
31
ffe60f5b
32/**
33 * Class core_event_user_graded_testcase
34 *
35 * Tests for event \core\event\user_graded
36 *
37 * @package core
b310c9a1 38 * @category test
ffe60f5b
39 * @copyright 2014 Petr Skoda
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class core_event_user_graded_testcase extends advanced_testcase {
b310c9a1
MN
43
44 /**
45 * Tests set up.
46 */
47 public function setUp() {
48 $this->resetAfterTest();
49 }
50
ffe60f5b 51 /**
b310c9a1 52 * Tests the event details.
ffe60f5b
53 */
54 public function test_event() {
55 global $CFG;
56 require_once("$CFG->libdir/gradelib.php");
57
ffe60f5b
58 $course = $this->getDataGenerator()->create_course();
59 $user = $this->getDataGenerator()->create_user();
60 $this->getDataGenerator()->enrol_user($user->id, $course->id);
61
62 $grade_category = grade_category::fetch_course_category($course->id);
63 $grade_category->load_grade_item();
64 $grade_item = $grade_category->grade_item;
65
66 $grade_item->update_final_grade($user->id, 10, 'gradebook');
67
68 $grade_grade = new grade_grade(array('userid' => $user->id, 'itemid' => $grade_item->id), true);
69 $grade_grade->grade_item = $grade_item;
70
71 $event = \core\event\user_graded::create_from_grade($grade_grade);
72
73 $this->assertEventLegacyLogData(
74 array($course->id, 'grade', 'update', '/report/grader/index.php?id=' . $course->id, $grade_item->itemname . ': ' . fullname($user)),
75 $event
76 );
77 $this->assertEquals(context_course::instance($course->id), $event->get_context());
78 $this->assertSame($event->objecttable, 'grade_grades');
79 $this->assertEquals($event->objectid, $grade_grade->id);
80 $this->assertEquals($event->other['itemid'], $grade_item->id);
81 $this->assertTrue($event->other['overridden']);
82 $this->assertEquals(10, $event->other['finalgrade']);
83
84 // Trigger the events.
85 $sink = $this->redirectEvents();
86 $event->trigger();
87 $result = $sink->get_events();
88 $sink->close();
89
90 $this->assertCount(1, $result);
91
92 $event = reset($result);
93 $this->assertEventContextNotUsed($event);
94
95 $grade = $event->get_grade();
96 $this->assertInstanceOf('grade_grade', $grade);
97 $this->assertEquals($grade_grade->id, $grade->id);
98 }
b310c9a1
MN
99
100 /**
101 * Tests that the event is fired in the correct locations in core.
102 */
103 public function test_event_is_triggered() {
104 global $DB;
105
106 // Create the items we need to test with.
107 $course = $this->getDataGenerator()->create_course();
108 $user = $this->getDataGenerator()->create_user();
109 $this->getDataGenerator()->enrol_user($user->id, $course->id);
110 $quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));
111
112 // Now mark the quiz using grade_update as this is the function that modules use.
113 $grade = array();
114 $grade['userid'] = $user->id;
115 $grade['rawgrade'] = 50;
116
117 $sink = $this->redirectEvents();
118 grade_update('mod/quiz', $course->id, 'mod', 'quiz', $quiz->id, 0, $grade);
119 $events = $sink->get_events();
120 $event = reset($events);
121 $sink->close();
122
123 // Ensure we have a user_graded event.
124 $this->assertEquals(1, count($events));
125 $this->assertInstanceOf('\core\event\user_graded', $event);
126
127 // Get the grade item.
128 $gradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id,
129 'courseid' => $course->id));
130
131 // Let's alter the grade in the DB so when we call regrade_final_grades() it is changed and an event is called.
132 $sql = "UPDATE {grade_grades}
133 SET finalgrade = '2'
134 WHERE itemid = :itemid
135 AND userid = :userid";
136 $DB->execute($sql, array('itemid' => $gradeitem->id, 'userid' => $user->id));
137
138 // Now check when we regrade this that there is a user graded event.
139 $sink = $this->redirectEvents();
140 $gradeitem->regrade_final_grades();
141 $events = $sink->get_events();
142 $event = reset($events);
143 $sink->close();
144
145 // Ensure we have a user_graded event.
146 $this->assertEquals(1, count($events));
147 $this->assertInstanceOf('\core\event\user_graded', $event);
148
149 // Remove the grades.
150 $gradeitem->delete_all_grades();
151
152 // Now, create a grade using update_raw_grade().
153 $sink = $this->redirectEvents();
154 $gradeitem->update_raw_grade($user->id, 50);
155 $events = $sink->get_events();
156 $event = reset($events);
157 $sink->close();
158
159 // Ensure we have a user_graded event.
160 $this->assertEquals(1, count($events));
161 $this->assertInstanceOf('\core\event\user_graded', $event);
162
163 // Now, update this grade using update_raw_grade().
164 $sink = $this->redirectEvents();
165 $gradeitem->update_raw_grade($user->id, 100);
166 $events = $sink->get_events();
167 $event = reset($events);
168 $sink->close();
169
170 $this->assertEquals(1, count($events));
171 $this->assertInstanceOf('\core\event\user_graded', $event);
172
173 // Remove the grades.
174 $gradeitem->delete_all_grades();
175
176 // Now, create a grade using update_final_grade().
177 $sink = $this->redirectEvents();
178 $gradeitem->update_final_grade($user->id, 50);
179 $events = $sink->get_events();
180 $event = reset($events);
181 $sink->close();
182
183 // Ensure we have a user_graded event.
184 $this->assertEquals(1, count($events));
185 $this->assertInstanceOf('\core\event\user_graded', $event);
186
187 // Now, update this grade using update_final_grade().
188 $sink = $this->redirectEvents();
189 $gradeitem->update_final_grade($user->id, 100);
190 $events = $sink->get_events();
191 $event = reset($events);
192 $sink->close();
193
194 $this->assertEquals(1, count($events));
195 $this->assertInstanceOf('\core\event\user_graded', $event);
196
197 // Let's change the calculation to anything that won't cause an error.
198 $calculation = calc_formula::unlocalize("=3");
199 $gradeitem->set_calculation($calculation);
200
201 // Let's alter the grade in the DB so when we call compute() it is changed and an event is called.
202 $sql = "UPDATE {grade_grades}
203 SET finalgrade = 2, overridden = 0
204 WHERE itemid = :itemid
205 AND userid = :userid";
206 $DB->execute($sql, array('itemid' => $gradeitem->id, 'userid' => $user->id));
207
208 // Now check when we compute that there is a user graded event.
209 $sink = $this->redirectEvents();
210 $gradeitem->compute();
211 $events = $sink->get_events();
212 $event = reset($events);
213 $sink->close();
214
215 $this->assertEquals(1, count($events));
216 $this->assertInstanceOf('\core\event\user_graded', $event);
217 }
ffe60f5b 218}