MDL-30085 core_grades functions moved to correct location
[moodle.git] / grade / tests / report_graderlib_test.php
CommitLineData
01da2d36
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/user/lib.php.
19 *
20 * @package core_grade
21 * @category phpunit
22 * @copyright 2012 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/grader/lib.php');
31
32/**
33 * Tests grade_report_grader (the grader report)
34 */
8252b7c2 35class core_grade_report_graderlib_testcase extends advanced_testcase {
01da2d36
AD
36
37 /**
38 * Tests grade_report_grader::process_data()
39 *
40 * process_data() processes submitted grade and feedback data
41 */
42 public function test_process_data() {
43 global $DB, $CFG;
44
45 $this->resetAfterTest(true);
46
47 $course = $this->getDataGenerator()->create_course();
48 $coursecontext = context_course::instance($course->id);
49
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);
54
55 // Test with limited grades.
56 $CFG->unlimitedgrades = 0;
57
58 $forummax = 80;
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));
62
63 $report = $this->create_report($course, $coursecontext);
64 $testgrade = 60.00;
65
66 $data = new stdClass();
67 $data->id = $course->id;
68 $data->report = 'grader';
69
70 $data->grade = array();
71 $data->grade[$student->id] = array();
72 $data->grade[$student->id][$forum1->id] = $testgrade;
73
74 $warnings = $report->process_data($data);
75 $this->assertEquals(count($warnings), 0);
76
77 $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
78 $this->assertEquals($studentgrade->finalgrade, $testgrade);
79
80 // Grade above max. Should be pulled down to max.
81 $toobig = 200.00;
82 $data->grade[$student->id][$forum1->id] = $toobig;
83 $warnings = $report->process_data($data);
84 $this->assertEquals(count($warnings), 1);
85
86 $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
87 $this->assertEquals($studentgrade->finalgrade, $forummax);
88
89 // Grade below min. Should be pulled up to min.
90 $toosmall = -10.00;
91 $data->grade[$student->id][$forum1->id] = $toosmall;
92 $warnings = $report->process_data($data);
93 $this->assertEquals(count($warnings), 1);
94
95 $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
96 $this->assertEquals($studentgrade->finalgrade, 0);
97
98 // Test unlimited grades so we can give a student a grade about max.
99 $CFG->unlimitedgrades = 1;
100
101 $data->grade[$student->id][$forum1->id] = $toobig;
102 $warnings = $report->process_data($data);
103 $this->assertEquals(count($warnings), 0);
104
105 $studentgrade = grade_grade::fetch(array('itemid' => $forum1->id, '' => $student->id));
106 $this->assertEquals($studentgrade->finalgrade, $toobig);
107 }
108
109 private function create_report($course, $coursecontext) {
110
111 $gpr = new grade_plugin_return(array('type' => 'report', 'plugin'=>'grader', 'courseid' => $course->id));
112 $report = new grade_report_grader($course->id, $gpr, $coursecontext);
113
114 return $report;
115 }
116}