1 |
efrain |
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 |
* File contains the unit tests for the element helper class.
|
|
|
19 |
*
|
|
|
20 |
* @package mod_customcert
|
|
|
21 |
* @category test
|
|
|
22 |
* @copyright 2017 Mark Nelson <markn@moodle.com>
|
|
|
23 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
24 |
*/
|
|
|
25 |
|
|
|
26 |
namespace mod_customcert;
|
|
|
27 |
|
|
|
28 |
use grade_item;
|
|
|
29 |
use grade_grade;
|
|
|
30 |
use context_module;
|
|
|
31 |
use context_system;
|
|
|
32 |
use advanced_testcase;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Unit tests for the element helper class.
|
|
|
36 |
*
|
|
|
37 |
* @package mod_customcert
|
|
|
38 |
* @category test
|
|
|
39 |
* @copyright 2017 Mark Nelson <markn@moodle.com>
|
|
|
40 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
41 |
*/
|
|
|
42 |
class element_helper_test extends advanced_testcase {
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Test set up.
|
|
|
46 |
*/
|
|
|
47 |
public function setUp(): void {
|
|
|
48 |
$this->resetAfterTest();
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* Tests we are returning the correct course id for an element in a course customcert activity.
|
|
|
53 |
*
|
|
|
54 |
* @covers \element_helper::get_courseid
|
|
|
55 |
*/
|
|
|
56 |
public function test_get_courseid_element_in_course_certificate() {
|
|
|
57 |
global $DB;
|
|
|
58 |
|
|
|
59 |
// Create a course.
|
|
|
60 |
$course = $this->getDataGenerator()->create_course();
|
|
|
61 |
|
|
|
62 |
// Create a custom certificate in the course.
|
|
|
63 |
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
|
|
|
64 |
|
|
|
65 |
// Get the template to add elements to.
|
|
|
66 |
$template = $DB->get_record('customcert_templates', ['contextid' => context_module::instance($customcert->cmid)->id]);
|
|
|
67 |
$template = new template($template);
|
|
|
68 |
|
|
|
69 |
// Add a page to the template.
|
|
|
70 |
$pageid = $template->add_page();
|
|
|
71 |
|
|
|
72 |
// Add an element to this page.
|
|
|
73 |
$element = new \stdClass();
|
|
|
74 |
$element->name = 'Test element';
|
|
|
75 |
$element->element = 'testelement';
|
|
|
76 |
$element->pageid = $pageid;
|
|
|
77 |
$element->sequence = element_helper::get_element_sequence($element->pageid);
|
|
|
78 |
$element->timecreated = time();
|
|
|
79 |
$element->id = $DB->insert_record('customcert_elements', $element);
|
|
|
80 |
|
|
|
81 |
// Confirm the correct course id is returned.
|
|
|
82 |
$this->assertEquals($course->id, element_helper::get_courseid($element->id));
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* Tests we are returning the correct course id for an element in a site template.
|
|
|
87 |
*
|
|
|
88 |
* @covers \element_helper::get_courseid
|
|
|
89 |
*/
|
|
|
90 |
public function test_get_courseid_element_in_site_template() {
|
|
|
91 |
global $DB, $SITE;
|
|
|
92 |
|
|
|
93 |
// Add a template to the site.
|
|
|
94 |
$template = template::create('Site template', context_system::instance()->id);
|
|
|
95 |
|
|
|
96 |
// Add a page to the template.
|
|
|
97 |
$pageid = $template->add_page();
|
|
|
98 |
|
|
|
99 |
// Add an element to this page.
|
|
|
100 |
$element = new \stdClass();
|
|
|
101 |
$element->name = 'Test element';
|
|
|
102 |
$element->element = 'testelement';
|
|
|
103 |
$element->pageid = $pageid;
|
|
|
104 |
$element->sequence = element_helper::get_element_sequence($element->pageid);
|
|
|
105 |
$element->timecreated = time();
|
|
|
106 |
$element->id = $DB->insert_record('customcert_elements', $element);
|
|
|
107 |
|
|
|
108 |
// Confirm the correct course id is returned.
|
|
|
109 |
$this->assertEquals($SITE->id, element_helper::get_courseid($element->id));
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
/**
|
|
|
113 |
* Tests we are returning the correct course module id for an element in a course customcert activity.
|
|
|
114 |
*
|
|
|
115 |
* @covers \element_helper::get_context
|
|
|
116 |
*/
|
|
|
117 |
public function test_get_context_element_in_course_certificate() {
|
|
|
118 |
global $DB;
|
|
|
119 |
|
|
|
120 |
// Create a course.
|
|
|
121 |
$course = $this->getDataGenerator()->create_course();
|
|
|
122 |
|
|
|
123 |
// Create a custom certificate in the course.
|
|
|
124 |
$customcert = $this->getDataGenerator()->create_module('customcert', ['course' => $course->id]);
|
|
|
125 |
|
|
|
126 |
// Get the template to add elements to.
|
|
|
127 |
$template = $DB->get_record('customcert_templates', ['contextid' => context_module::instance($customcert->cmid)->id]);
|
|
|
128 |
$template = new template($template);
|
|
|
129 |
|
|
|
130 |
// Add a page to the template.
|
|
|
131 |
$pageid = $template->add_page();
|
|
|
132 |
|
|
|
133 |
// Add an element to this page.
|
|
|
134 |
$element = new \stdClass();
|
|
|
135 |
$element->name = 'Test element';
|
|
|
136 |
$element->element = 'testelement';
|
|
|
137 |
$element->pageid = $pageid;
|
|
|
138 |
$element->sequence = element_helper::get_element_sequence($element->pageid);
|
|
|
139 |
$element->timecreated = time();
|
|
|
140 |
$element->id = $DB->insert_record('customcert_elements', $element);
|
|
|
141 |
|
|
|
142 |
// Confirm the correct course module id is returned.
|
|
|
143 |
$this->assertEquals(context_module::instance($customcert->cmid),
|
|
|
144 |
element_helper::get_context($element->id));
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Tests we are returning the correct course module id for an element in a site template.
|
|
|
149 |
*
|
|
|
150 |
* @covers \element_helper::get_context
|
|
|
151 |
*/
|
|
|
152 |
public function test_get_context_element_in_site_template() {
|
|
|
153 |
global $DB;
|
|
|
154 |
|
|
|
155 |
// Add a template to the site.
|
|
|
156 |
$template = template::create('Site template', context_system::instance()->id);
|
|
|
157 |
|
|
|
158 |
// Add a page to the template.
|
|
|
159 |
$pageid = $template->add_page();
|
|
|
160 |
|
|
|
161 |
// Add an element to this page.
|
|
|
162 |
$element = new \stdClass();
|
|
|
163 |
$element->name = 'Test element';
|
|
|
164 |
$element->element = 'testelement';
|
|
|
165 |
$element->pageid = $pageid;
|
|
|
166 |
$element->sequence = element_helper::get_element_sequence($element->pageid);
|
|
|
167 |
$element->timecreated = time();
|
|
|
168 |
$element->id = $DB->insert_record('customcert_elements', $element);
|
|
|
169 |
|
|
|
170 |
// Confirm the correct course module id is returned.
|
|
|
171 |
$this->assertEquals(context_system::instance(), element_helper::get_context($element->id));
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
/**
|
|
|
175 |
* Test we return the correct grade items in a course.
|
|
|
176 |
*
|
|
|
177 |
* @covers \element_helper::get_grade_items
|
|
|
178 |
*/
|
|
|
179 |
public function test_get_grade_items() {
|
|
|
180 |
global $DB;
|
|
|
181 |
|
|
|
182 |
// Create a course.
|
|
|
183 |
$course = $this->getDataGenerator()->create_course();
|
|
|
184 |
|
|
|
185 |
// Create a few gradeable items.
|
|
|
186 |
$assign1 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
|
|
|
187 |
$assign2 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
|
|
|
188 |
$assign3 = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
|
|
|
189 |
|
|
|
190 |
// Create a manual grade item.
|
|
|
191 |
$gi = $this->getDataGenerator()->create_grade_item(['courseid' => $course->id]);
|
|
|
192 |
|
|
|
193 |
// Create a category grade item.
|
|
|
194 |
$gc = $this->getDataGenerator()->create_grade_category(['courseid' => $course->id]);
|
|
|
195 |
$gc = $DB->get_record('grade_items', ['itemtype' => 'category', 'iteminstance' => $gc->id]);
|
|
|
196 |
|
|
|
197 |
// Create an item attached to an outcome.
|
|
|
198 |
$outcome = $this->getDataGenerator()->create_grade_outcome(['courseid' => $course->id, 'shortname' => 'outcome']);
|
|
|
199 |
$go = $this->getDataGenerator()->create_grade_item(
|
|
|
200 |
[
|
|
|
201 |
'courseid' => $course->id,
|
|
|
202 |
'outcomeid' => $outcome->id,
|
|
|
203 |
]
|
|
|
204 |
);
|
|
|
205 |
|
|
|
206 |
// Confirm the function returns the correct number of grade items.
|
|
|
207 |
$gradeitems = element_helper::get_grade_items($course);
|
|
|
208 |
$this->assertCount(6, $gradeitems);
|
|
|
209 |
$this->assertArrayHasKey($assign1->cmid, $gradeitems);
|
|
|
210 |
$this->assertArrayHasKey($assign2->cmid, $gradeitems);
|
|
|
211 |
$this->assertArrayHasKey($assign3->cmid, $gradeitems);
|
|
|
212 |
$this->assertArrayHasKey('gradeitem:' . $gi->id, $gradeitems);
|
|
|
213 |
$this->assertArrayHasKey('gradeitem:' . $gc->id, $gradeitems);
|
|
|
214 |
$this->assertArrayHasKey('gradeitem:' . $go->id, $gradeitems);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
/**
|
|
|
218 |
* Test we return the correct grade information for an activity.
|
|
|
219 |
*
|
|
|
220 |
* @covers \element_helper::get_mod_grade_info
|
|
|
221 |
*/
|
|
|
222 |
public function test_get_mod_grade_info() {
|
|
|
223 |
// Create a course.
|
|
|
224 |
$course = $this->getDataGenerator()->create_course();
|
|
|
225 |
|
|
|
226 |
// Set that we want 3 decimals to display.
|
|
|
227 |
grade_set_setting($course->id, 'decimalpoints', 3);
|
|
|
228 |
|
|
|
229 |
// Create two users.
|
|
|
230 |
$student1 = $this->getDataGenerator()->create_user();
|
|
|
231 |
$student2 = $this->getDataGenerator()->create_user();
|
|
|
232 |
|
|
|
233 |
// Enrol them into the course.
|
|
|
234 |
$this->getDataGenerator()->enrol_user($student1->id, $course->id);
|
|
|
235 |
$this->getDataGenerator()->enrol_user($student2->id, $course->id);
|
|
|
236 |
|
|
|
237 |
// Create a gradeable item.
|
|
|
238 |
$assign = $this->getDataGenerator()->create_module('assign', ['course' => $course->id]);
|
|
|
239 |
|
|
|
240 |
// Give a grade to the student.
|
|
|
241 |
$gi = grade_item::fetch(
|
|
|
242 |
[
|
|
|
243 |
'itemtype' => 'mod',
|
|
|
244 |
'itemmodule' => 'assign',
|
|
|
245 |
'iteminstance' => $assign->id,
|
|
|
246 |
'courseid' => $course->id,
|
|
|
247 |
]
|
|
|
248 |
);
|
|
|
249 |
$datagrade = 50;
|
|
|
250 |
$time = time();
|
|
|
251 |
$grade = new grade_grade();
|
|
|
252 |
$grade->itemid = $gi->id;
|
|
|
253 |
$grade->userid = $student1->id;
|
|
|
254 |
$grade->rawgrade = $datagrade;
|
|
|
255 |
$grade->finalgrade = $datagrade;
|
|
|
256 |
$grade->rawgrademax = 100;
|
|
|
257 |
$grade->rawgrademin = 0;
|
|
|
258 |
$grade->timecreated = $time;
|
|
|
259 |
$grade->timemodified = $time;
|
|
|
260 |
$grade->insert();
|
|
|
261 |
|
|
|
262 |
// Check that the user received the grade.
|
|
|
263 |
$grade = element_helper::get_mod_grade_info(
|
|
|
264 |
$assign->cmid,
|
|
|
265 |
GRADE_DISPLAY_TYPE_PERCENTAGE,
|
|
|
266 |
$student1->id
|
|
|
267 |
);
|
|
|
268 |
|
|
|
269 |
$this->assertEquals($assign->name, $grade->get_name());
|
|
|
270 |
$this->assertEquals('50.00000', $grade->get_grade());
|
|
|
271 |
$this->assertEquals('50.000 %', $grade->get_displaygrade());
|
|
|
272 |
$this->assertEquals($time, $grade->get_dategraded());
|
|
|
273 |
|
|
|
274 |
// Check that the user we did not grade has no grade.
|
|
|
275 |
$grade = element_helper::get_mod_grade_info(
|
|
|
276 |
$assign->cmid,
|
|
|
277 |
GRADE_DISPLAY_TYPE_PERCENTAGE,
|
|
|
278 |
$student2->id
|
|
|
279 |
);
|
|
|
280 |
$this->assertEquals($assign->name, $grade->get_name());
|
|
|
281 |
$this->assertEquals(null, $grade->get_grade());
|
|
|
282 |
$this->assertEquals('-', $grade->get_displaygrade());
|
|
|
283 |
$this->assertEquals(null, $grade->get_dategraded());
|
|
|
284 |
|
|
|
285 |
grade_get_setting($course->id, null, null, true);
|
|
|
286 |
}
|
|
|
287 |
|
|
|
288 |
/**
|
|
|
289 |
* Test we return the correct grade information for a course.
|
|
|
290 |
*
|
|
|
291 |
* @covers \element_helper::get_course_grade_info
|
|
|
292 |
*/
|
|
|
293 |
public function test_get_course_grade_info() {
|
|
|
294 |
// Create a course.
|
|
|
295 |
$course = $this->getDataGenerator()->create_course();
|
|
|
296 |
|
|
|
297 |
// Set that we want 3 decimals to display.
|
|
|
298 |
grade_set_setting($course->id, 'decimalpoints', 3);
|
|
|
299 |
|
|
|
300 |
// Create two users.
|
|
|
301 |
$student1 = $this->getDataGenerator()->create_user();
|
|
|
302 |
$student2 = $this->getDataGenerator()->create_user();
|
|
|
303 |
|
|
|
304 |
// Enrol them into the course.
|
|
|
305 |
$this->getDataGenerator()->enrol_user($student1->id, $course->id);
|
|
|
306 |
$this->getDataGenerator()->enrol_user($student2->id, $course->id);
|
|
|
307 |
|
|
|
308 |
// Get the course item.
|
|
|
309 |
$coursegradeitem = grade_item::fetch_course_item($course->id);
|
|
|
310 |
|
|
|
311 |
$datagrade = 50;
|
|
|
312 |
$time = time();
|
|
|
313 |
$grade = new grade_grade();
|
|
|
314 |
$grade->itemid = $coursegradeitem->id;
|
|
|
315 |
$grade->userid = $student1->id;
|
|
|
316 |
$grade->rawgrade = $datagrade;
|
|
|
317 |
$grade->finalgrade = $datagrade;
|
|
|
318 |
$grade->rawgrademax = 100;
|
|
|
319 |
$grade->rawgrademin = 0;
|
|
|
320 |
$grade->timecreated = $time;
|
|
|
321 |
$grade->timemodified = $time;
|
|
|
322 |
$grade->insert();
|
|
|
323 |
|
|
|
324 |
// Check that the user received the grade.
|
|
|
325 |
$grade = element_helper::get_course_grade_info(
|
|
|
326 |
$course->id,
|
|
|
327 |
GRADE_DISPLAY_TYPE_PERCENTAGE,
|
|
|
328 |
$student1->id
|
|
|
329 |
);
|
|
|
330 |
|
|
|
331 |
$this->assertEquals(get_string('coursetotal', 'grades'), $grade->get_name());
|
|
|
332 |
$this->assertEquals('50.00000', $grade->get_grade());
|
|
|
333 |
$this->assertEquals('50.000 %', $grade->get_displaygrade());
|
|
|
334 |
$this->assertEquals($time, $grade->get_dategraded());
|
|
|
335 |
|
|
|
336 |
// Check that the user we did not grade has no grade.
|
|
|
337 |
$grade = element_helper::get_course_grade_info(
|
|
|
338 |
$course->id,
|
|
|
339 |
GRADE_DISPLAY_TYPE_PERCENTAGE,
|
|
|
340 |
$student2->id
|
|
|
341 |
);
|
|
|
342 |
$this->assertEquals(get_string('coursetotal', 'grades'), $grade->get_name());
|
|
|
343 |
$this->assertEquals(null, $grade->get_grade());
|
|
|
344 |
$this->assertEquals('-', $grade->get_displaygrade());
|
|
|
345 |
$this->assertEquals(null, $grade->get_dategraded());
|
|
|
346 |
|
|
|
347 |
grade_get_setting($course->id, null, null, true);
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
/**
|
|
|
351 |
* Test we return the correct grade information for a grade item.
|
|
|
352 |
*
|
|
|
353 |
* @covers \element_helper::get_grade_item_info
|
|
|
354 |
*/
|
|
|
355 |
public function test_get_grade_item_info() {
|
|
|
356 |
// Create a course.
|
|
|
357 |
$course = $this->getDataGenerator()->create_course();
|
|
|
358 |
|
|
|
359 |
// Set that we want 3 decimals to display.
|
|
|
360 |
grade_set_setting($course->id, 'decimalpoints', 3);
|
|
|
361 |
|
|
|
362 |
// Create two users.
|
|
|
363 |
$student1 = $this->getDataGenerator()->create_user();
|
|
|
364 |
$student2 = $this->getDataGenerator()->create_user();
|
|
|
365 |
|
|
|
366 |
// Enrol them into the course.
|
|
|
367 |
$this->getDataGenerator()->enrol_user($student1->id, $course->id);
|
|
|
368 |
$this->getDataGenerator()->enrol_user($student2->id, $course->id);
|
|
|
369 |
|
|
|
370 |
// Create a manual grade item.
|
|
|
371 |
$gi = $this->getDataGenerator()->create_grade_item(['itemname' => 'Grade item yo', 'courseid' => $course->id]);
|
|
|
372 |
|
|
|
373 |
// Give a grade to the student.
|
|
|
374 |
$gi = grade_item::fetch(['id' => $gi->id]);
|
|
|
375 |
$datagrade = 50;
|
|
|
376 |
$time = time();
|
|
|
377 |
$grade = new grade_grade();
|
|
|
378 |
$grade->itemid = $gi->id;
|
|
|
379 |
$grade->userid = $student1->id;
|
|
|
380 |
$grade->rawgrade = $datagrade;
|
|
|
381 |
$grade->finalgrade = $datagrade;
|
|
|
382 |
$grade->rawgrademax = 100;
|
|
|
383 |
$grade->rawgrademin = 0;
|
|
|
384 |
$grade->timecreated = $time;
|
|
|
385 |
$grade->timemodified = $time;
|
|
|
386 |
$grade->insert();
|
|
|
387 |
|
|
|
388 |
// Check that the user received the grade.
|
|
|
389 |
$grade = element_helper::get_grade_item_info(
|
|
|
390 |
$gi->id,
|
|
|
391 |
GRADE_DISPLAY_TYPE_PERCENTAGE,
|
|
|
392 |
$student1->id
|
|
|
393 |
);
|
|
|
394 |
|
|
|
395 |
$this->assertEquals('Grade item yo', $grade->get_name());
|
|
|
396 |
$this->assertEquals('50.00000', $grade->get_grade());
|
|
|
397 |
$this->assertEquals('50.000 %', $grade->get_displaygrade());
|
|
|
398 |
$this->assertEquals($time, $grade->get_dategraded());
|
|
|
399 |
|
|
|
400 |
// Check that the user we did not grade has no grade.
|
|
|
401 |
$grade = element_helper::get_grade_item_info(
|
|
|
402 |
$gi->id,
|
|
|
403 |
GRADE_DISPLAY_TYPE_PERCENTAGE,
|
|
|
404 |
$student2->id
|
|
|
405 |
);
|
|
|
406 |
$this->assertEquals('Grade item yo', $grade->get_name());
|
|
|
407 |
$this->assertEquals(null, $grade->get_grade());
|
|
|
408 |
$this->assertEquals('-', $grade->get_displaygrade());
|
|
|
409 |
$this->assertEquals(null, $grade->get_dategraded());
|
|
|
410 |
|
|
|
411 |
grade_get_setting($course->id, null, null, true);
|
|
|
412 |
}
|
|
|
413 |
}
|