MDL-40901 standardise core test case class and file names
[moodle.git] / grade / tests / report_graderlib_test.php
blobf3fd1ee6a16495a07f6d4f47b0b0155710697a19
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/user/lib.php.
20 * @package core_grade
21 * @category phpunit
22 * @copyright 2012 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/grader/lib.php');
32 /**
33 * Tests grade_report_grader (the grader report)
35 class core_grade_report_graderlib_testcase extends advanced_testcase {
37 /**
38 * Tests grade_report_grader::process_data()
40 * process_data() processes submitted grade and feedback data
42 public function test_process_data() {
43 global $DB, $CFG;
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;
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));
63 $report = $this->create_report($course, $coursecontext);
64 $testgrade = 60.00;
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.
81 $toobig = 200.00;
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.
90 $toosmall = -10.00;
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);
114 return $report;