forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreportuserlib_test.php
185 lines (149 loc) · 8.37 KB
/
reportuserlib_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Unit tests for grade/report/user/lib.php.
*
* @package core_grade
* @category phpunit
* @copyright 2012 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot.'/grade/lib.php');
require_once($CFG->dirroot.'/grade/report/user/lib.php');
/**
* Tests grade_report_user (the gradebook's user report)
*/
class core_grade_reportuserlib_testcase extends advanced_testcase {
/**
* Tests grade_report_user::inject_rowspans()
*
* inject_rowspans() returns the count of the number of elements, sets maxdepth on the
* report object and sets the rowspan property on any element that has children.
*/
public function test_inject_rowspans() {
global $CFG, $USER, $DB;
parent::setUp();
$this->resetAfterTest(true);
$CFG->enableavailability = 1;
$CFG->enablecompletion = 1;
// Create a course.
$course = $this->getDataGenerator()->create_course();
$coursecategory = grade_category::fetch_course_category($course->id);
$coursecontext = context_course::instance($course->id);
// Create and enrol test users.
$student = $this->getDataGenerator()->create_user(array('username' => 'Student Sam'));
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
$this->getDataGenerator()->enrol_user($student->id, $course->id, $role->id);
$teacher = $this->getDataGenerator()->create_user(array('username' => 'Teacher T'));
$role = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST);
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $role->id);
// An array so we can test with both users in a loop.
$users = array($student, $teacher);
// Make the student the current user.
$this->setUser($student);
// Test an empty course.
$report = $this->create_report($course, $student, $coursecontext);
// a lead column that spans all children + course grade item = 2
$this->assertEquals(2, $report->inject_rowspans($report->gtree->top_element));
$this->assertEquals(2, $report->gtree->top_element['rowspan']);
$this->assertEquals(2, $report->maxdepth);
// Only elements with children should have rowspan set.
if (array_key_exists('rowspan', $report->gtree->top_element['children'][1])) {
$this->fail('Elements without children should not have rowspan set');
}
// Add 2 activities.
$data1 = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
$forum1 = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
$forum1cm = get_coursemodule_from_id('forum', $forum1->cmid);
// Switch the stdClass instance for a grade item instance so grade_item::set_parent() is available.
$forum1 = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum1->id, 'courseid' => $course->id));
$report = $this->create_report($course, $student, $coursecontext);
// Lead column + course + (2 x activity) = 4
$this->assertEquals(4, $report->inject_rowspans($report->gtree->top_element));
$this->assertEquals(4, $report->gtree->top_element['rowspan']);
// Lead column + 1 level (course + 2 activities) = 2
$this->assertEquals(2, $report->maxdepth);
// Only elements with children should have rowspan set.
if (array_key_exists('rowspan', $report->gtree->top_element['children'][1])) {
$this->fail('Elements without children should not have rowspan set');
}
// Hide the forum activity.
set_coursemodule_visible($forum1cm->id, 0);
foreach ($users as $user) {
$this->setUser($user);
$message = 'Testing with ' . $user->username;
accesslib_clear_all_caches_for_unit_testing();
$report = $this->create_report($course, $user, $coursecontext);
// Lead column + course + (2 x activity) = 4 (element count isn't affected by hiding)
$this->assertEquals(4, $report->inject_rowspans($report->gtree->top_element), $message);
$this->assertEquals(4, $report->gtree->top_element['rowspan'], $message);
// Lead column -> 1 level containing the course + 2 activities = 2
$this->assertEquals(2, $report->maxdepth, $message);
}
// Unhide the forum activity.
set_coursemodule_visible($forum1cm->id, 1);
// Create a category and put the forum in it.
$params = new stdClass();
$params->courseid = $course->id;
$params->fullname = 'unittestcategory';
$params->parent = $coursecategory->id;
$gradecategory = new grade_category($params, false);
$gradecategory->insert();
$forum1->set_parent($gradecategory->id);
$report = $this->create_report($course, $student, $coursecontext);
// Lead column + course + (category + category grade item) + (2 x activity) = 6
$this->assertEquals(6, $report->inject_rowspans($report->gtree->top_element));
$this->assertEquals(6, $report->gtree->top_element['rowspan']);
// Lead column -> the category -> the forum activity = 3
$this->assertEquals(3, $report->maxdepth);
// Check rowspan on the category. The category itself + category grade item + forum = 3
$this->assertEquals(3, $report->gtree->top_element['children'][4]['rowspan']);
// check the forum doesn't have rowspan set
if (array_key_exists('rowspan', $report->gtree->top_element['children'][4]['children'][3])) {
$this->fail('The forum has no children so should not have rowspan set');
}
// Conditional activity tests.
// Note: I have ported this test to the new conditional availability
// system, but it does not appear to actually test anything - in fact,
// if you remove the code that sets the condition, it still passes
// because it apparently is intended to have the same number of rows
// even when some are hidden. The same is true of the
// set_coursemodule_visible test above. I don't feel this is a very
// good test; somebody with more knowledge of this report might want to
// fix it to check that the row actually is being hidden.
$DB->set_field('course_modules', 'availability', '{"op":"|","show":false,"c":[' .
'{"type":"grade","min":5.5,"id":37}]}', array('id' => $forum1cm->id));
get_fast_modinfo($course->id, 0, true);
foreach ($users as $user) {
$this->setUser($user);
$message = 'Testing with ' . $user->username;
accesslib_clear_all_caches_for_unit_testing();
$report = $this->create_report($course, $user, $coursecontext);
// Lead column + course + (category + category grade item) + (2 x activity) = 6
$this->assertEquals(6, $report->inject_rowspans($report->gtree->top_element), $message);
$this->assertEquals(6, $report->gtree->top_element['rowspan'], $message);
// Lead column -> the category -> the forum activity = 3
$this->assertEquals(3, $report->maxdepth, $message);
}
}
private function create_report($course, $user, $coursecontext) {
$gpr = new grade_plugin_return(array('type' => 'report', 'plugin'=>'user', 'courseid' => $course->id, 'userid' => $user->id));
$report = new grade_report_user($course->id, $gpr, $coursecontext, $user->id);
return $report;
}
}