MDL-39801 navigation_node::remove does not allow to insert nodes afterwards
[moodle.git] / grade / tests / reportlib_test.php
blobaf3f7915b878e4df26576d900ab3545e796f5a15
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 * Unit tests for grade/report/lib.php.
20 * @pacakge core_grade
21 * @category phpunit
22 * @author Andrew Davis
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot.'/grade/lib.php');
30 require_once($CFG->dirroot.'/grade/report/lib.php');
32 /**
33 * A test class used to test grade_report, the abstract grade report parent class
35 class grade_report_test extends grade_report {
36 public function __construct($courseid, $gpr, $context, $user) {
37 parent::__construct($courseid, $gpr, $context);
38 $this->user = $user;
41 /**
42 * A wrapper around blank_hidden_total() to allow test code to call it directly
44 public function blank_hidden_total($courseid, $courseitem, $finalgrade) {
45 return parent::blank_hidden_total($courseid, $courseitem, $finalgrade);
48 /**
49 * Implementation of the abstract method process_data()
51 public function process_data($data) {
54 /**
55 * Implementation of the abstract method process_action()
57 public function process_action($target, $action) {
61 /**
62 * Tests grade_report, the parent class for all grade reports.
64 class gradereportlib_testcase extends advanced_testcase {
66 /**
67 * Tests grade_report::blank_hidden_total()
69 public function test_blank_hidden_total() {
70 global $DB;
72 $this->resetAfterTest(true);
74 $student = $this->getDataGenerator()->create_user();
75 $this->setUser($student);
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);
83 $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
84 $datacm = get_coursemodule_from_id('data', $data->cmid);
86 $forum = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
87 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
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();
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();
116 // Hide the database activity.
117 set_coursemodule_visible($datacm->id, 0);
119 $gpr = new grade_plugin_return(array('type' => 'report', 'courseid' => $course->id));
120 $report = new grade_report_test($course->id, $gpr, $coursecontext, $student);
122 // Should return the supplied student total grade regardless of hiding.
123 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN);
124 $this->assertEquals($datagrade + $forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
126 // Should blank the student total as course grade depends on a hidden item.
127 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN);
128 $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
130 // Should return the course total minus the hidden database activity grade.
131 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN);
132 $this->assertEquals($forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
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.
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);
145 $data = $this->getDataGenerator()->create_module('data', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
146 $datacm = get_coursemodule_from_id('data', $data->cmid);
148 $forum = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => 100, 'course' => $course->id));
149 $forumcm = get_coursemodule_from_id('forum', $forum->cmid);
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();
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();
177 // Hide both activities.
178 set_coursemodule_visible($datacm->id, 0);
179 set_coursemodule_visible($forumcm->id, 0);
181 $gpr = new grade_plugin_return(array('type' => 'report', 'courseid' => $course->id));
182 $report = new grade_report_test($course->id, $gpr, $coursecontext, $student);
184 // Should return the supplied student total grade regardless of hiding.
185 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN);
186 $this->assertEquals($datagrade + $forumgrade, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
188 // Should blank the student total as course grade depends on a hidden item.
189 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN);
190 $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));
192 // Should return the course total minus the hidden activity grades.
193 // They are both hidden so should return null.
194 $report->showtotalsifcontainhidden = array($course->id => GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN);
195 $this->assertEquals(null, $report->blank_hidden_total($course->id, $coursegradeitem, $datagrade + $forumgrade));