2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Unit tests for grade/report/user/lib.php.
20 * @package core_grades
22 * @copyright 2012 Andrew Davis
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->dirroot
.'/grade/lib.php');
30 require_once($CFG->dirroot
.'/grade/report/grader/lib.php');
33 * Tests grade_report_grader (the grader report)
35 class core_grade_report_graderlib_testcase
extends advanced_testcase
{
38 * Tests grade_report_grader::process_data()
40 * process_data() processes submitted grade and feedback data
42 public function test_process_data() {
45 $this->resetAfterTest(true);
47 $course = $this->getDataGenerator()->create_course();
49 // Create and enrol a student.
50 $student = $this->getDataGenerator()->create_user(array('username' => 'Student Sam'));
51 $role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST
);
52 $this->getDataGenerator()->enrol_user($student->id
, $course->id
, $role->id
);
54 // Test with limited grades.
55 $CFG->unlimitedgrades
= 0;
58 $forum1 = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => $forummax, 'course' => $course->id
));
59 // Switch the stdClass instance for a grade item instance.
60 $forum1 = grade_item
::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum1->id
, 'courseid' => $course->id
));
62 $report = $this->create_report($course);
65 $data = new stdClass();
66 $data->id
= $course->id
;
67 $data->report
= 'grader';
68 $data->timepageload
= time();
70 $data->grade
= array();
71 $data->grade
[$student->id
] = array();
72 $data->grade
[$student->id
][$forum1->id
] = $testgrade;
74 $warnings = $report->process_data($data);
75 $this->assertEquals(count($warnings), 0);
77 $studentgrade = grade_grade
::fetch(array('itemid' => $forum1->id
, '' => $student->id
));
78 $this->assertEquals($studentgrade->finalgrade
, $testgrade);
80 // Grade above max. Should be pulled down to max.
82 $data->grade
[$student->id
][$forum1->id
] = $toobig;
83 $data->timepageload
= time();
84 $warnings = $report->process_data($data);
85 $this->assertEquals(count($warnings), 1);
87 $studentgrade = grade_grade
::fetch(array('itemid' => $forum1->id
, '' => $student->id
));
88 $this->assertEquals($studentgrade->finalgrade
, $forummax);
90 // Grade below min. Should be pulled up to min.
92 $data->grade
[$student->id
][$forum1->id
] = $toosmall;
93 $data->timepageload
= time();
94 $warnings = $report->process_data($data);
95 $this->assertEquals(count($warnings), 1);
97 $studentgrade = grade_grade
::fetch(array('itemid' => $forum1->id
, '' => $student->id
));
98 $this->assertEquals($studentgrade->finalgrade
, 0);
100 // Test unlimited grades so we can give a student a grade about max.
101 $CFG->unlimitedgrades
= 1;
103 $data->grade
[$student->id
][$forum1->id
] = $toobig;
104 $data->timepageload
= time();
105 $warnings = $report->process_data($data);
106 $this->assertEquals(count($warnings), 0);
108 $studentgrade = grade_grade
::fetch(array('itemid' => $forum1->id
, '' => $student->id
));
109 $this->assertEquals($studentgrade->finalgrade
, $toobig);
112 public function test_collapsed_preferences() {
113 $this->resetAfterTest(true);
115 $emptypreferences = array('aggregatesonly' => array(), 'gradesonly' => array());
117 $user1 = $this->getDataGenerator()->create_user();
118 $course1 = $this->getDataGenerator()->create_course();
119 $course2 = $this->getDataGenerator()->create_course();
120 $course3 = $this->getDataGenerator()->create_course();
122 $this->setUser($user1);
124 $report = $this->create_report($course1);
125 $this->assertEquals($emptypreferences, $report->collapsed
);
127 // Validating preferences set/get for one course.
128 $report->process_action('cg13', 'switch_minus');
129 $report = $this->create_report($course1);
130 $this->assertEquals(array(13), $report->collapsed
['aggregatesonly']);
131 $this->assertEmpty($report->collapsed
['gradesonly']);
133 $report->process_action('cg13', 'switch_plus');
134 $report = $this->create_report($course1);
135 $this->assertEmpty($report->collapsed
['aggregatesonly']);
136 $this->assertEquals(array(13), $report->collapsed
['gradesonly']);
138 $report->process_action('cg13', 'switch_whole');
139 $report = $this->create_report($course1);
140 $this->assertEquals($emptypreferences, $report->collapsed
);
142 // Validating preferences set/get for several courses.
144 $course1cats = $course2cats = $course3cats = array();
145 for ($i=0;$i<10;$i++
) {
146 $course1cats[] = $this->create_grade_category($course1)->id
;
147 $course2cats[] = $this->create_grade_category($course2)->id
;
148 $course3cats[] = $this->create_grade_category($course3)->id
;
151 $report1 = $this->create_report($course1);
152 foreach ($course1cats as $catid) {
153 $report1->process_action('cg'.$catid, 'switch_minus');
155 $report2 = $this->create_report($course2);
156 foreach ($course2cats as $catid) {
157 $report2->process_action('cg'.$catid, 'switch_minus');
158 $report2->process_action('cg'.$catid, 'switch_plus');
160 $report3 = $this->create_report($course3);
161 foreach ($course3cats as $catid) {
162 $report3->process_action('cg'.$catid, 'switch_minus');
164 $report3->process_action('cg'.$catid, 'switch_plus');
168 $report1 = $this->create_report($course1);
169 $this->assertEquals(10, count($report1->collapsed
['aggregatesonly']));
170 $this->assertEquals(0, count($report1->collapsed
['gradesonly']));
171 $report2 = $this->create_report($course2);
172 $this->assertEquals(0, count($report2->collapsed
['aggregatesonly']));
173 $this->assertEquals(10, count($report2->collapsed
['gradesonly']));
174 $report3 = $this->create_report($course3);
175 $this->assertEquals(5, count($report3->collapsed
['aggregatesonly']));
176 $this->assertEquals(5, count($report3->collapsed
['gradesonly']));
178 // Test upgrade script.
179 // Combine data generated for user1 and set it in the old format for user2, Try to retrieve it and make sure it is converted.
181 $user2 = $this->getDataGenerator()->create_user();
183 'aggregatesonly' => array_merge($report1->collapsed
['aggregatesonly'], $report2->collapsed
['aggregatesonly'], $report3->collapsed
['aggregatesonly']),
184 'gradesonly' => array_merge($report1->collapsed
['gradesonly'], $report2->collapsed
['gradesonly'], $report3->collapsed
['gradesonly']),
186 set_user_preference('grade_report_grader_collapsed_categories', serialize($alldata), $user2);
188 $this->setUser($user2);
189 $convertedreport1 = $this->create_report($course1);
190 $this->assertEquals($report1->collapsed
, $convertedreport1->collapsed
);
191 $convertedreport2 = $this->create_report($course2);
192 $this->assertEquals($report2->collapsed
, $convertedreport2->collapsed
);
193 $convertedreport3 = $this->create_report($course3);
194 $this->assertEquals($report3->collapsed
, $convertedreport3->collapsed
);
195 // Make sure the old style user preference is removed now.
196 $this->assertEmpty(get_user_preferences('grade_report_grader_collapsed_categories'));
198 // Test overflowing the setting with non-existing categories (only validated if new setting size exceeds 1333 chars).
200 $toobigvalue = $expectedvalue = $report1->collapsed
;
201 for ($i = 0; strlen(json_encode($toobigvalue)) < 1333; $i++
) {
202 $toobigvalue[($i < 7) ?
'gradesonly' : 'aggregatesonly'][] = $course1cats[9] +
1 +
$i;
204 $lastvalue = array_pop($toobigvalue['gradesonly']);
205 set_user_preference('grade_report_grader_collapsed_categories'.$course1->id
, json_encode($toobigvalue));
207 $report1 = $this->create_report($course1);
208 $report1->process_action('cg'.$lastvalue, 'switch_minus');
210 $report1 = $this->create_report($course1);
211 $this->assertEquals($expectedvalue, $report1->collapsed
);
213 // Test overflowing the setting with existing categories.
215 $toobigvalue = $report1->collapsed
;
216 for ($i = 0; strlen(json_encode($toobigvalue)) < 1333; $i++
) {
217 $catid = $this->create_grade_category($course1)->id
;
218 $toobigvalue[($i < 7) ?
'gradesonly' : 'aggregatesonly'][] = $catid;
220 $lastcatid = array_pop($toobigvalue['gradesonly']);
221 set_user_preference('grade_report_grader_collapsed_categories'.$course1->id
, json_encode($toobigvalue));
222 $toobigvalue['aggregatesonly'][] = $lastcatid;
224 $report1 = $this->create_report($course1);
225 $report1->process_action('cg'.$lastcatid, 'switch_minus');
227 // One last value should be removed from both arrays.
228 $report1 = $this->create_report($course1);
229 $this->assertEquals(count($toobigvalue['aggregatesonly']) - 1, count($report1->collapsed
['aggregatesonly']));
230 $this->assertEquals(count($toobigvalue['gradesonly']) - 1, count($report1->collapsed
['gradesonly']));
234 * Tests the get_right_rows function with one 'normal' and one 'ungraded' quiz.
236 * Previously, with an ungraded quiz (which results in a grade item with type GRADETYPE_NONE)
237 * there was a bug in get_right_rows in some situations.
239 public function test_get_right_rows() {
241 $this->resetAfterTest(true);
243 // Create manager and student on a course.
244 $generator = $this->getDataGenerator();
245 $manager = $generator->create_user();
246 $student = $generator->create_user();
247 $course = $generator->create_course();
248 $generator->enrol_user($manager->id
, $course->id
, 'manager');
249 $generator->enrol_user($student->id
, $course->id
, 'student');
251 // Create a couple of quizzes on the course.
252 $normalquiz = $generator->create_module('quiz', array('course' => $course->id
,
253 'name' => 'NormalQuiz'));
254 $ungradedquiz = $generator->create_module('quiz', array('course' => $course->id
,
255 'name' => 'UngradedQuiz'));
257 // Set the grade for the second one to 0 (note, you have to do this after creating it,
258 // otherwise it doesn't create an ungraded grade item).
259 $ungradedquiz->instance
= $ungradedquiz->id
;
260 quiz_set_grade(0, $ungradedquiz);
263 $this->setUser($manager);
264 $USER->gradeediting
[$course->id
] = false;
267 $report = $this->create_report($course);
268 $report->load_users();
269 $report->load_final_grades();
270 $result = $report->get_right_rows(false);
272 // There should be 3 rows (2 header rows, plus the grades for the first user).
273 $this->assertCount(3, $result);
275 // The second row should contain 2 cells - one for the graded quiz and course total.
276 $this->assertCount(2, $result[1]->cells
);
277 $this->assertContains('NormalQuiz', $result[1]->cells
[0]->text
);
278 $this->assertContains('Course total', $result[1]->cells
[1]->text
);
280 // User row should contain grade values '-'.
281 $this->assertCount(2, $result[2]->cells
);
282 $this->assertContains('>-<', $result[2]->cells
[0]->text
);
283 $this->assertContains('>-<', $result[2]->cells
[1]->text
);
285 // Supposing the user cannot view hidden grades, this shouldn't make any difference (due
286 // to a bug, it previously did).
287 $context = context_course
::instance($course->id
);
288 $managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
289 assign_capability('moodle/grade:viewhidden', CAP_PROHIBIT
, $managerroleid, $context->id
, true);
290 $this->assertFalse(has_capability('moodle/grade:viewhidden', $context));
292 // Recreate the report. Confirm it returns successfully still.
293 $report = $this->create_report($course);
294 $report->load_users();
295 $report->load_final_grades();
296 $result = $report->get_right_rows(false);
297 $this->assertCount(3, $result);
300 private function create_grade_category($course) {
303 $grade_category = new grade_category(array('courseid' => $course->id
, 'fullname' => 'Cat '.$cnt), false);
304 $grade_category->apply_default_settings();
305 $grade_category->apply_forced_settings();
306 $grade_category->insert();
307 return $grade_category;
310 private function create_report($course) {
312 $coursecontext = context_course
::instance($course->id
);
313 $gpr = new grade_plugin_return(array('type' => 'report', 'plugin'=>'grader', 'courseid' => $course->id
));
314 $report = new grade_report_grader($course->id
, $gpr, $coursecontext);