3 ///////////////////////////////////////////////////////////////////////////
5 // NOTICE OF COPYRIGHT //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
10 // Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
22 // http://www.gnu.org/copyleft/gpl.html //
24 ///////////////////////////////////////////////////////////////////////////
27 * Unit tests for grade_category object.
29 * @author nicolas@moodle.com
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
34 if (!defined('MOODLE_INTERNAL')) {
35 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
38 require_once($CFG->libdir
.'/simpletest/fixtures/gradetest.php');
40 class grade_category_test
extends grade_test
{
42 function test_grade_category_construct() {
43 $course_category = grade_category
::fetch_course_category($this->courseid
);
45 $params = new stdClass();
47 $params->courseid
= $this->courseid
;
48 $params->fullname
= 'unittestcategory4';
50 $grade_category = new grade_category($params, false);
51 $grade_category->insert();
53 $this->assertEqual($params->courseid
, $grade_category->courseid
);
54 $this->assertEqual($params->fullname
, $grade_category->fullname
);
55 $this->assertEqual(2, $grade_category->depth
);
56 $this->assertEqual("/$course_category->id/$grade_category->id/", $grade_category->path
);
57 $parentpath = $grade_category->path
;
59 // Test a child category
60 $params->parent
= $grade_category->id
;
61 $params->fullname
= 'unittestcategory5';
62 $grade_category = new grade_category($params, false);
63 $grade_category->insert();
65 $this->assertEqual(3, $grade_category->depth
);
66 $this->assertEqual($parentpath.$grade_category->id
."/", $grade_category->path
);
67 $parentpath = $grade_category->path
;
69 // Test a third depth category
70 $params->parent
= $grade_category->id
;
71 $params->fullname
= 'unittestcategory6';
72 $grade_category = new grade_category($params, false);
73 $grade_category->insert();
74 $this->assertEqual(4, $grade_category->depth
);
75 $this->assertEqual($parentpath.$grade_category->id
."/", $grade_category->path
);
78 function test_grade_category_build_path() {
79 $grade_category = new grade_category($this->grade_categories
[1]);
80 $this->assertTrue(method_exists($grade_category, 'build_path'));
81 $path = grade_category
::build_path($grade_category);
82 $this->assertEqual($grade_category->path
, $path);
85 function test_grade_category_fetch() {
86 $grade_category = new grade_category();
87 $this->assertTrue(method_exists($grade_category, 'fetch'));
89 $grade_category = grade_category
::fetch(array('id'=>$this->grade_categories
[0]->id
));
90 $this->assertEqual($this->grade_categories
[0]->id
, $grade_category->id
);
91 $this->assertEqual($this->grade_categories
[0]->fullname
, $grade_category->fullname
);
94 function test_grade_category_fetch_all() {
95 $grade_category = new grade_category();
96 $this->assertTrue(method_exists($grade_category, 'fetch_all'));
98 $grade_categories = grade_category
::fetch_all(array('courseid'=>$this->courseid
));
99 $this->assertEqual(count($this->grade_categories
), count($grade_categories)-1);
102 function test_grade_category_update() {
103 $grade_category = new grade_category($this->grade_categories
[0]);
104 $this->assertTrue(method_exists($grade_category, 'update'));
106 $grade_category->fullname
= 'Updated info for this unittest grade_category';
107 $grade_category->path
= null; // path must be recalculated if missing
108 $grade_category->depth
= null;
109 $grade_category->aggregation
= GRADE_AGGREGATE_MAX
; // should force regrading
111 $grade_item = $grade_category->get_grade_item();
112 $this->assertEqual(0, $grade_item->needsupdate
);
114 $this->assertTrue($grade_category->update());
116 $fullname = get_field('grade_categories', 'fullname', 'id', $this->grade_categories
[0]->id
);
117 $this->assertEqual($grade_category->fullname
, $fullname);
119 $path = get_field('grade_categories', 'path', 'id', $this->grade_categories
[0]->id
);
120 $this->assertEqual($grade_category->path
, $path);
122 $depth = get_field('grade_categories', 'depth', 'id', $this->grade_categories
[0]->id
);
123 $this->assertEqual($grade_category->depth
, $depth);
125 $grade_item = $grade_category->get_grade_item();
126 $this->assertEqual(1, $grade_item->needsupdate
);
129 function test_grade_category_delete() {
130 $grade_category = new grade_category($this->grade_categories
[0]);
131 $this->assertTrue(method_exists($grade_category, 'delete'));
133 $this->assertTrue($grade_category->delete());
134 $this->assertFalse(get_record('grade_categories', 'id', $grade_category->id
));
137 function test_grade_category_insert() {
138 $course_category = grade_category
::fetch_course_category($this->courseid
);
140 $grade_category = new grade_category();
141 $this->assertTrue(method_exists($grade_category, 'insert'));
143 $grade_category->fullname
= 'unittestcategory4';
144 $grade_category->courseid
= $this->courseid
;
145 $grade_category->aggregation
= GRADE_AGGREGATE_MEAN
;
146 $grade_category->aggregateonlygraded
= 1;
147 $grade_category->keephigh
= 100;
148 $grade_category->droplow
= 10;
149 $grade_category->hidden
= 0;
150 $grade_category->parent
= $this->grade_categories
[0]->id
;
152 $grade_category->insert();
154 $this->assertEqual('/'.$course_category->id
.'/'.$this->grade_categories
[0]->id
.'/'.$grade_category->id
.'/', $grade_category->path
);
155 $this->assertEqual(3, $grade_category->depth
);
157 $last_grade_category = end($this->grade_categories
);
159 $this->assertFalse(empty($grade_category->grade_item
));
160 $this->assertEqual($grade_category->id
, $grade_category->grade_item
->iteminstance
);
161 $this->assertEqual('category', $grade_category->grade_item
->itemtype
);
163 $this->assertEqual($grade_category->id
, $last_grade_category->id +
1);
164 $this->assertFalse(empty($grade_category->timecreated
));
165 $this->assertFalse(empty($grade_category->timemodified
));
168 function test_grade_category_insert_course_category() {
169 $grade_category = new grade_category();
170 $this->assertTrue(method_exists($grade_category, 'insert_course_category'));
172 $id = $grade_category->insert_course_category($this->courseid
);
173 $this->assertNotNull($id);
174 $this->assertEqual('Course grade category', $grade_category->fullname
);
175 $this->assertEqual(GRADE_AGGREGATE_MEAN
, $grade_category->aggregate
);
176 $this->assertEqual("/$id/", $grade_category->path
);
177 $this->assertEqual(1, $grade_category->depth
);
178 $this->assertNull($grade_category->parent
);
181 function test_grade_category_qualifies_for_regrading() {
182 $grade_category = new grade_category($this->grade_categories
[0]);
183 $this->assertTrue(method_exists($grade_category, 'qualifies_for_regrading'));
185 $this->assertFalse($grade_category->qualifies_for_regrading());
187 $grade_category->aggregation
= GRADE_AGGREGATE_MAX
;
188 $this->assertTrue($grade_category->qualifies_for_regrading());
190 $grade_category = new grade_category($this->grade_categories
[0]);
191 $grade_category->droplow
= 99;
192 $this->assertTrue($grade_category->qualifies_for_regrading());
194 $grade_category = new grade_category($this->grade_categories
[0]);
195 $grade_category->keephigh
= 99;
196 $this->assertTrue($grade_category->qualifies_for_regrading());
199 function test_grade_category_force_regrading() {
200 $grade_category = new grade_category($this->grade_categories
[1]);
201 $this->assertTrue(method_exists($grade_category, 'force_regrading'));
203 $grade_category->load_grade_item();
204 $this->assertEqual(0, $grade_category->grade_item
->needsupdate
);
206 $grade_category->force_regrading();
208 $grade_category->grade_item
= null;
209 $grade_category->load_grade_item();
211 $this->assertEqual(1, $grade_category->grade_item
->needsupdate
);
214 function test_grade_category_generate_grades() {
215 $category = new grade_category($this->grade_categories
[3]);
216 $this->assertTrue(method_exists($category, 'generate_grades'));
217 $category->load_grade_item();
219 $grades = get_records('grade_grades', 'itemid', $category->grade_item
->id
);
220 $this->assertFalse($grades);
222 $category->generate_grades();
223 $grades = get_records('grade_grades', 'itemid', $category->grade_item
->id
);
224 $this->assertEqual(3, count($grades));
226 $rawvalues = array();
227 foreach ($grades as $grade) {
228 $this->assertWithinMargin($grade->rawgrade
, $grade->rawgrademin
, $grade->rawgrademax
);
229 $rawvalues[] = (int)$grade->rawgrade
;
232 // calculated mean results
233 $this->assertEqual($rawvalues, array(20,50,100));
236 function test_grade_category_aggregate_grades() {
237 $category = new grade_category($this->grade_categories
[0]);
238 $this->assertTrue(method_exists($category, 'aggregate_grades'));
239 // tested above in test_grade_category_generate_grades()
242 function test_grade_category_apply_limit_rules() {
243 $category = new grade_category();
244 $grades = array(5.374, 9.4743, 2.5474, 7.3754);
246 $category->droplow
= 2;
247 $category->apply_limit_rules($grades);
248 sort($grades, SORT_NUMERIC
);
249 $this->assertEqual(array(7.3754, 9.4743), $grades);
251 $category = new grade_category();
252 $grades = array(5.374, 9.4743, 2.5474, 7.3754);
254 $category->keephigh
= 1;
255 $category->droplow
= 0;
256 $category->apply_limit_rules($grades);
257 $this->assertEqual(count($grades), 1);
258 $grade = reset($grades);
259 $this->assertEqual(9.4743, $grade);
265 function test_grade_category_is_aggregationcoef_used() {
269 function test_grade_category_fetch_course_tree() {
270 $category = new grade_category();
271 $this->assertTrue(method_exists($category, 'fetch_course_tree'));
272 //TODO: add some tests
275 function test_grade_category_get_children() {
276 $course_category = grade_category
::fetch_course_category($this->courseid
);
278 $category = new grade_category($this->grade_categories
[0]);
279 $this->assertTrue(method_exists($category, 'get_children'));
281 $children_array = $category->get_children(0);
283 $this->assertTrue(is_array($children_array));
284 $this->assertFalse(empty($children_array[2]));
285 $this->assertFalse(empty($children_array[2]['object']));
286 $this->assertFalse(empty($children_array[2]['children']));
287 $this->assertEqual($this->grade_categories
[1]->id
, $children_array[2]['object']->id
);
288 $this->assertEqual($this->grade_categories
[2]->id
, $children_array[5]['object']->id
);
289 $this->assertEqual($this->grade_items
[0]->id
, $children_array[2]['children'][3]['object']->id
);
290 $this->assertEqual($this->grade_items
[1]->id
, $children_array[2]['children'][4]['object']->id
);
291 $this->assertEqual($this->grade_items
[2]->id
, $children_array[5]['children'][6]['object']->id
);
294 function test_grade_category_load_grade_item() {
295 $category = new grade_category($this->grade_categories
[0]);
296 $this->assertTrue(method_exists($category, 'load_grade_item'));
297 $this->assertEqual(null, $category->grade_item
);
298 $category->load_grade_item();
299 $this->assertEqual($this->grade_items
[3]->id
, $category->grade_item
->id
);
302 function test_grade_category_get_grade_item() {
303 $category = new grade_category($this->grade_categories
[0]);
304 $this->assertTrue(method_exists($category, 'get_grade_item'));
305 $grade_item = $category->get_grade_item();
306 $this->assertEqual($this->grade_items
[3]->id
, $grade_item->id
);
309 function test_grade_category_load_parent_category() {
310 $category = new grade_category($this->grade_categories
[1]);
311 $this->assertTrue(method_exists($category, 'load_parent_category'));
312 $this->assertEqual(null, $category->parent_category
);
313 $category->load_parent_category();
314 $this->assertEqual($this->grade_categories
[0]->id
, $category->parent_category
->id
);
317 function test_grade_category_get_parent_category() {
318 $category = new grade_category($this->grade_categories
[1]);
319 $this->assertTrue(method_exists($category, 'get_parent_category'));
320 $parent_category = $category->get_parent_category();
321 $this->assertEqual($this->grade_categories
[0]->id
, $parent_category->id
);
324 function test_grade_category_get_name() {
325 $category = new grade_category($this->grade_categories
[0]);
326 $this->assertTrue(method_exists($category, 'get_name'));
327 $this->assertEqual($this->grade_categories
[0]->fullname
, $category->get_name());
330 function test_grade_category_set_parent() {
331 $category = new grade_category($this->grade_categories
[1]);
332 $this->assertTrue(method_exists($category, 'set_parent'));
333 // TODO: implement detailed tests
335 $course_category = grade_category
::fetch_course_category($this->courseid
);
336 $this->assertTrue($category->set_parent($course_category->id
));
337 $this->assertEqual($course_category->id
, $category->parent
);
340 function test_grade_category_get_final() {
341 $category = new grade_category($this->grade_categories
[0]);
342 $this->assertTrue(method_exists($category, 'get_final'));
343 $category->load_grade_item();
344 $this->assertEqual($category->get_final(), $category->grade_item
->get_final());
347 function test_grade_category_get_sortorder() {
348 $category = new grade_category($this->grade_categories
[0]);
349 $this->assertTrue(method_exists($category, 'get_sortorder'));
350 $category->load_grade_item();
351 $this->assertEqual($category->get_sortorder(), $category->grade_item
->get_sortorder());
354 function test_grade_category_set_sortorder() {
355 $category = new grade_category($this->grade_categories
[0]);
356 $this->assertTrue(method_exists($category, 'set_sortorder'));
357 $category->load_grade_item();
358 $this->assertEqual($category->set_sortorder(10), $category->grade_item
->set_sortorder(10));
361 function test_grade_category_move_after_sortorder() {
362 $category = new grade_category($this->grade_categories
[0]);
363 $this->assertTrue(method_exists($category, 'move_after_sortorder'));
364 $category->load_grade_item();
365 $this->assertEqual($category->move_after_sortorder(10), $category->grade_item
->move_after_sortorder(10));
368 function test_grade_category_is_course_category() {
369 $category = grade_category
::fetch_course_category($this->courseid
);
370 $this->assertTrue(method_exists($category, 'is_course_category'));
371 $this->assertTrue($category->is_course_category());
374 function test_grade_category_fetch_course_category() {
375 $category = new grade_category();
376 $this->assertTrue(method_exists($category, 'fetch_course_category'));
377 $category = grade_category
::fetch_course_category($this->courseid
);
378 $this->assertTrue(empty($category->parent
));
383 function test_grade_category_is_editable() {
387 function test_grade_category_is_locked() {
388 $category = new grade_category($this->grade_categories
[0]);
389 $this->assertTrue(method_exists($category, 'is_locked'));
390 $category->load_grade_item();
391 $this->assertEqual($category->is_locked(), $category->grade_item
->is_locked());
394 function test_grade_category_set_locked() {
395 $category = new grade_category($this->grade_categories
[0]);
396 $this->assertTrue(method_exists($category, 'set_locked'));
397 $this->assertTrue($category->set_locked(1));
400 function test_grade_category_is_hidden() {
401 $category = new grade_category($this->grade_categories
[0]);
402 $this->assertTrue(method_exists($category, 'is_hidden'));
403 $category->load_grade_item();
404 $this->assertEqual($category->is_hidden(), $category->grade_item
->is_hidden());
407 function test_grade_category_set_hidden() {
408 $category = new grade_category($this->grade_categories
[0]);
409 $this->assertTrue(method_exists($category, 'set_hidden'));
410 $category->set_hidden(1);
411 $category->load_grade_item();
412 $this->assertEqual(true, $category->grade_item
->is_hidden());
415 function generate_random_raw_grade($item, $userid) {
416 $grade = new grade_grade();
417 $grade->itemid
= $item->id
;
418 $grade->userid
= $userid;
419 $grade->grademin
= 0;
420 $grade->grademax
= 1;
421 $valuetype = "grade$item->gradetype";
422 $grade->rawgrade
= rand(0, 1000) / 1000;
424 return $grade->rawgrade
;