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.
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 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();
48
$coursecontext = context_course::instance($course->id);
50
// Create and enrol a student.
51
$student = $this->getDataGenerator()->create_user(array('username' => 'Student Sam'));
52
$role = $DB->get_record('role', array('shortname' => 'student'), '*', MUST_EXIST);
53
$this->getDataGenerator()->enrol_user($student->id, $course->id, $role->id);
55
// Test with limited grades.
56
$CFG->unlimitedgrades = 0;
59
$forum1 = $this->getDataGenerator()->create_module('forum', array('assessed' => 1, 'scale' => $forummax, 'course' => $course->id));
60
// Switch the stdClass instance for a grade item instance.
61
$forum1 = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'forum', 'iteminstance' => $forum1->id, 'courseid' => $course->id));
63
$report = $this->create_report($course, $coursecontext);
66
$data = new stdClass();
67
$data->id = $course->id;
68
$data->report = 'grader';
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
$warnings = $report->process_data($data);
84
$this->assertEquals(count($warnings), 1);
86
$studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
87
$this->assertEquals($studentgrade->finalgrade, $forummax);
89
// Grade below min. Should be pulled up to min.
91
$data->grade[$student->id][$forum1->id] = $toosmall;
92
$warnings = $report->process_data($data);
93
$this->assertEquals(count($warnings), 1);
95
$studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
96
$this->assertEquals($studentgrade->finalgrade, 0);
98
// Test unlimited grades so we can give a student a grade about max.
99
$CFG->unlimitedgrades = 1;
101
$data->grade[$student->id][$forum1->id] = $toobig;
102
$warnings = $report->process_data($data);
103
$this->assertEquals(count($warnings), 0);
105
$studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
106
$this->assertEquals($studentgrade->finalgrade, $toobig);
109
private function create_report($course, $coursecontext) {
111
$gpr = new grade_plugin_return(array('type' => 'report', 'plugin'=>'grader', 'courseid' => $course->id));
112
$report = new grade_report_grader($course->id, $gpr, $coursecontext);