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/import/lib.php.
20 * @package core_grades
22 * @copyright 2015 Adrian Greeve <adrian@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->dirroot
. '/grade/import/lib.php');
32 * Tests grade_import_lib functions.
34 class core_grade_import_lib_test
extends advanced_testcase
{
37 * Import grades into 'grade_import_values' table. This is done differently in the various import plugins,
38 * so there is no direct API to call.
40 * @param array $data Information to be inserted into the table.
41 * @return int The insert ID of the sql statement.
43 private function import_grades($data) {
45 $graderecord = new stdClass();
46 $graderecord->importcode
= $data['importcode'];
47 if (isset($data['itemid'])) {
48 $graderecord->itemid
= $data['itemid'];
50 $graderecord->userid
= $data['userid'];
51 if (isset($data['importer'])) {
52 $graderecord->importer
= $data['importer'];
54 $graderecord->importer
= $USER->id
;
56 if (isset($data['finalgrade'])) {
57 $graderecord->finalgrade
= $data['finalgrade'];
59 $graderecord->finalgrade
= rand(0, 100);
61 if (isset($data['feedback'])) {
62 $graderecord->feedback
= $data['feedback'];
64 if (isset($data['importonlyfeedback'])) {
65 $graderecord->importonlyfeedback
= $data['importonlyfeedback'];
67 $graderecord->importonlyfeedback
= false;
69 if (isset($data['newgradeitem'])) {
70 $graderecord->newgradeitem
= $data['newgradeitem'];
72 return $DB->insert_record('grade_import_values', $graderecord);
76 * Tests for importing grades from an external source.
78 public function test_grade_import_commit() {
79 global $USER, $DB, $CFG;
80 $this->resetAfterTest();
82 $importcode = get_new_importcode();
83 $user1 = $this->getDataGenerator()->create_user();
84 $user2 = $this->getDataGenerator()->create_user();
86 $course = $this->getDataGenerator()->create_course();
87 $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id
));
88 $itemname = $assign->name
;
89 $modulecontext = context_module
::instance($assign->cmid
);
90 // The generator returns a dummy object, lets get the real assign object.
91 $assign = new assign($modulecontext, false, false);
92 $cm = $assign->get_course_module();
94 // Enrol users in the course.
95 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
);
96 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
);
98 // Enter a new grade into an existing grade item.
99 $gradeitem = grade_item
::fetch(array('courseid' => $course->id
, 'itemtype' => 'mod'));
101 // Keep this value around for a test further down.
103 $this->import_grades(array(
104 'importcode' => $importcode,
105 'itemid' => $gradeitem->id
,
106 'userid' => $user1->id
,
107 'finalgrade' => $originalgrade
110 $status = grade_import_commit($course->id
, $importcode, false, false);
111 $this->assertTrue($status);
113 // Get imported grade_grade.
114 $gradegrade = grade_grade
::fetch(array('itemid' => $gradeitem->id
, 'userid' => $user1->id
));
115 $this->assertEquals($originalgrade, $gradegrade->finalgrade
);
116 // Overriden field will be a timestamp and will evaluate out to true.
117 $this->assertTrue($gradegrade->is_overridden());
119 // Create a new grade item and import into that.
120 $importcode = get_new_importcode();
121 $record = new stdClass();
122 $record->itemname
= 'New grade item';
123 $record->importcode
= $importcode;
124 $record->importer
= $USER->id
;
125 $insertid = $DB->insert_record('grade_import_newitem', $record);
128 $this->import_grades(array(
129 'importcode' => $importcode,
130 'userid' => $user1->id
,
131 'finalgrade' => $finalgrade,
132 'newgradeitem' => $insertid
135 $status = grade_import_commit($course->id
, $importcode, false, false);
136 $this->assertTrue($status);
137 // Check that we have a new grade_item.
138 $gradeitem = grade_item
::fetch(array('courseid' => $course->id
, 'itemtype' => 'manual'));
139 $this->assertEquals($record->itemname
, $gradeitem->itemname
);
140 // Grades were imported.
141 $gradegrade = grade_grade
::fetch(array('itemid' => $gradeitem->id
, 'userid' => $user1->id
));
142 $this->assertEquals($finalgrade, $gradegrade->finalgrade
);
143 // As this is a new item the grade has not been overridden.
144 $this->assertFalse($gradegrade->is_overridden());
146 // Import feedback only.
147 $importcode = get_new_importcode();
148 $gradeitem = grade_item
::fetch(array('courseid' => $course->id
, 'itemtype' => 'mod'));
150 $originalfeedback = 'feedback can be useful';
151 $this->import_grades(array(
152 'importcode' => $importcode,
153 'userid' => $user1->id
,
154 'itemid' => $gradeitem->id
,
155 'feedback' => $originalfeedback,
156 'importonlyfeedback' => true
159 $status = grade_import_commit($course->id
, $importcode, true, false);
160 $this->assertTrue($status);
161 $gradegrade = grade_grade
::fetch(array('itemid' => $gradeitem->id
, 'userid' => $user1->id
));
162 // The final grade should be the same as the first record further up. We are only altering the feedback.
163 $this->assertEquals($originalgrade, $gradegrade->finalgrade
);
164 $this->assertTrue($gradegrade->is_overridden());
166 // Import grades only.
167 $importcode = get_new_importcode();
168 $gradeitem = grade_item
::fetch(array('courseid' => $course->id
, 'itemtype' => 'mod'));
171 $this->import_grades(array(
172 'importcode' => $importcode,
173 'userid' => $user1->id
,
174 'itemid' => $gradeitem->id
,
175 'finalgrade' => $finalgrade,
176 'feedback' => 'feedback can still be useful'
179 $status = grade_import_commit($course->id
, $importcode, false, false);
180 $this->assertTrue($status);
181 $gradegrade = grade_grade
::fetch(array('itemid' => $gradeitem->id
, 'userid' => $user1->id
));
182 $this->assertEquals($finalgrade, $gradegrade->finalgrade
);
183 // The final feedback should not have changed.
184 $this->assertEquals($originalfeedback, $gradegrade->feedback
);
185 $this->assertTrue($gradegrade->is_overridden());
187 // Check that printing of import status is correct.
188 $importcode = get_new_importcode();
189 $gradeitem = grade_item
::fetch(array('courseid' => $course->id
, 'itemtype' => 'mod'));
191 $this->import_grades(array(
192 'importcode' => $importcode,
193 'userid' => $user1->id
,
194 'itemid' => $gradeitem->id
197 $url = $CFG->wwwroot
. '/grade/index.php';
198 $expectedresponse = "++ Grade import success ++
199 <div class=\"continuebutton\"><form method=\"get\" action=\"$url\"><div><input type=\"submit\" value=\"Continue\" /><input type=\"hidden\" name=\"id\" value=\"$course->id\" /></div></form></div>";
202 $status = grade_import_commit($course->id
, $importcode);
203 $output = ob_get_contents();
205 $this->assertTrue($status);
206 $this->assertEquals($expectedresponse, $output);