MDL-30085 core_grades functions moved to correct location
[moodle.git] / grade / tests / reportlib_test.php
CommitLineData
30dd9930
AD
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/**
18 * Unit tests for grade/report/lib.php.
19 *
20 * @pacakge core_grade
21 * @category phpunit
22 * @author Andrew Davis
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->dirroot.'/grade/lib.php');
30require_once($CFG->dirroot.'/grade/report/lib.php');
31
32/**
33 * A test class used to test grade_report, the abstract grade report parent class
34 */
35class grade_report_test extends grade_report {
36 public function __construct($courseid, $gpr, $context, $user) {
37 parent::__construct($courseid, $gpr, $context);
38 $this->user = $user;
39 }
40
41 /**
42 * A wrapper around blank_hidden_total() to allow test code to call it directly
43 */
44 public function blank_hidden_total($courseid, $courseitem, $finalgrade) {
45 return parent::blank_hidden_total($courseid, $courseitem, $finalgrade);
46 }
47
48 /**
49 * Implementation of the abstract method process_data()
50 */
51 public function process_data($data) {
52 }
53
54 /**
55 * Implementation of the abstract method process_action()
56 */
57 public function process_action($target, $action) {
58 }
59}
60
61/**
62 * Tests grade_report, the parent class for all grade reports.
63 */
8252b7c2 64class core_grade_reportlib_testcase extends advanced_testcase {
30dd9930
AD
65
66 /**
67 * Tests grade_report::blank_hidden_total()
68 */
69 public function test_blank_hidden_total() {
70 global $DB;
71
72 $this->resetAfterTest(true);
73
74 $student = $this->getDataGenerator()->create_user();
75 $this->setUser($student);
76
77 // Create a course and two activities.
78 // One activity will be hidden.
79 $course = $this->getDataGenerator()->create_course();
80 $coursegradeitem = grade_item::fetch_course_item($course->id);
81 $coursecontext = context_course::instance($course->id);
82
83 $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
84 $datacm = get_coursemodule_from_id('data', $data->cmid);
85
86 $forum = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
87 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
88
89 // Insert student grades for the two activities.
90 $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'data', 'iteminstance' => $data->id, 'courseid' => $course->id));
91 $datagrade = 50;
92 $grade_grade = new grade_grade();
93 $grade_grade->itemid = $gi->id;
94 $grade_grade->userid = $student->id;
95 $grade_grade->rawgrade = $datagrade;
96 $grade_grade->finalgrade = $datagrade;
97 $grade_grade->rawgrademax = 100;
98 $grade_grade->rawgrademin = 0;
99 $grade_grade->timecreated = time();
100 $grade_grade->timemodified = time();
101 $grade_grade->insert();
102
103 $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum->id, 'courseid' => $course->id));
104 $forumgrade = 70;
105 $grade_grade = new grade_grade();
106 $grade_grade->itemid = $gi->id;
107 $grade_grade->userid = $student->id;
108 $grade_grade->rawgrade = $forumgrade;
109 $grade_grade->finalgrade = $forumgrade;
110 $grade_grade->rawgrademax = 100;
111 $grade_grade->rawgrademin = 0;
112 $grade_grade->timecreated = time();
113 $grade_grade->timemodified = time();
114 $grade_grade->insert();
115
116 // Hide the database activity.
117 set_coursemodule_visible($datacm->id, 0);
118
119 $gpr = new grade_plugin_return(array('type' => 'report', 'courseid' => $course->id));
120 $report = new grade_report_test($course->id, $gpr, $coursecontext, $student);
121
122 // Should return the supplied student total grade regardless of hiding.
5df9bc39 123 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN);
30dd9930
AD
124 $this->assertEquals($datagrade + $forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
125
126 // Should blank the student total as course grade depends on a hidden item.
5df9bc39 127 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN);
30dd9930
AD
128 $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
129
130 // Should return the course total minus the hidden database activity grade.
5df9bc39 131 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN);
30dd9930
AD
132 $this->assertEquals($forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
133
134 // Note: we cannot simply hide modules and call $report->blank_hidden_total() again.
135 // It stores grades in a static variable so $report->blank_hidden_total() will return incorrect totals
136 // In practice this isn't a problem. Grade visibility isn't altered mid-request outside of the unit tests.
137
138 // Add a second course to test:
139 // 1) How a course with no visible activities behaves.
140 // 2) That $report->blank_hidden_total() correctly moves on to the new course.
141 $course = $this->getDataGenerator()->create_course();
142 $coursegradeitem = grade_item::fetch_course_item($course->id);
143 $coursecontext = context_course::instance($course->id);
144
145 $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
146 $datacm = get_coursemodule_from_id('data', $data->cmid);
147
148 $forum = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
149 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
150
151 $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'data', 'iteminstance' => $data->id, 'courseid' => $course->id));
152 $datagrade = 50;
153 $grade_grade = new grade_grade();
154 $grade_grade->itemid = $gi->id;
155 $grade_grade->userid = $student->id;
156 $grade_grade->rawgrade = $datagrade;
157 $grade_grade->finalgrade = $datagrade;
158 $grade_grade->rawgrademax = 100;
159 $grade_grade->rawgrademin = 0;
160 $grade_grade->timecreated = time();
161 $grade_grade->timemodified = time();
162 $grade_grade->insert();
163
164 $gi = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum->id, 'courseid' => $course->id));
165 $forumgrade = 70;
166 $grade_grade = new grade_grade();
167 $grade_grade->itemid = $gi->id;
168 $grade_grade->userid = $student->id;
169 $grade_grade->rawgrade = $forumgrade;
170 $grade_grade->finalgrade = $forumgrade;
171 $grade_grade->rawgrademax = 100;
172 $grade_grade->rawgrademin = 0;
173 $grade_grade->timecreated = time();
174 $grade_grade->timemodified = time();
175 $grade_grade->insert();
176
177 // Hide both activities.
178 set_coursemodule_visible($datacm->id, 0);
179 set_coursemodule_visible($forumcm->id, 0);
180
181 $gpr = new grade_plugin_return(array('type' => 'report', 'courseid' => $course->id));
182 $report = new grade_report_test($course->id, $gpr, $coursecontext, $student);
183
184 // Should return the supplied student total grade regardless of hiding.
5df9bc39 185 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN);
30dd9930
AD
186 $this->assertEquals($datagrade + $forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
187
188 // Should blank the student total as course grade depends on a hidden item.
5df9bc39 189 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN);
30dd9930
AD
190 $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
191
192 // Should return the course total minus the hidden activity grades.
193 // They are both hidden so should return null.
5df9bc39 194 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN);
30dd9930
AD
195 $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
196 }
197}