MDL-10817, adding unique key courseid-coutcomeid during creation of grade_outcomes_co...
[moodle-pu.git] / lib / grade / simpletest / testgradeitem.php
blobbef65f70137bbdc2fae141276023ffd78675a83e
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
9 // //
10 // Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com //
11 // //
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. //
16 // //
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: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27 * Unit tests for grade_item object.
29 * @author nicolas@moodle.com
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31 * @package moodlecore
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 @set_time_limit(0);
42 class grade_item_test extends grade_test {
44 function test_grade_item_construct() {
45 $params = new stdClass();
47 $params->courseid = $this->courseid;
48 $params->categoryid = $this->grade_categories[1]->id;
49 $params->itemname = 'unittestgradeitem4';
50 $params->itemtype = 'mod';
51 $params->itemmodule = 'database';
52 $params->iteminfo = 'Grade item used for unit testing';
54 $grade_item = new grade_item($params, false);
56 $this->assertEqual($params->courseid, $grade_item->courseid);
57 $this->assertEqual($params->categoryid, $grade_item->categoryid);
58 $this->assertEqual($params->itemmodule, $grade_item->itemmodule);
61 function test_grade_item_insert() {
62 $grade_item = new grade_item();
63 $this->assertTrue(method_exists($grade_item, 'insert'));
65 $grade_item->courseid = $this->courseid;
66 $grade_item->categoryid = $this->grade_categories[1]->id;
67 $grade_item->itemname = 'unittestgradeitem4';
68 $grade_item->itemtype = 'mod';
69 $grade_item->itemmodule = 'quiz';
70 $grade_item->iteminfo = 'Grade item used for unit testing';
72 $grade_item->insert();
74 $last_grade_item = end($this->grade_items);
76 $this->assertEqual($grade_item->id, $last_grade_item->id + 1);
77 $this->assertEqual(11, $grade_item->sortorder);
80 function test_grade_item_generate_itemnumber_for_manual() {
81 $grade_item = new grade_item($this->grade_items[10]);
82 $copy_grade_item = fullclone($grade_item);
83 $copy_grade_item->itemnumber = null;
84 unset($copy_grade_item->id);
85 $result_id = $copy_grade_item->insert();
86 $this->assertEqual($grade_item->itemnumber+1, $copy_grade_item->itemnumber);
89 function test_grade_item_generate_idnumber() {
93 function test_grade_item_delete() {
94 $grade_item = new grade_item($this->grade_items[0]);
95 $this->assertTrue(method_exists($grade_item, 'delete'));
97 $this->assertTrue($grade_item->delete());
99 $this->assertFalse(get_record('grade_items', 'id', $grade_item->id));
102 function test_grade_item_update() {
103 $grade_item = new grade_item($this->grade_items[0]);
104 $this->assertTrue(method_exists($grade_item, 'update'));
106 $grade_item->iteminfo = 'Updated info for this unittest grade_item';
108 $this->assertTrue($grade_item->update());
110 $grade_item->grademin = 14;
111 $this->assertTrue($grade_item->qualifies_for_regrading());
112 $this->assertTrue($grade_item->update());
114 $iteminfo = get_field('grade_items', 'iteminfo', 'id', $this->grade_items[0]->id);
115 $this->assertEqual($grade_item->iteminfo, $iteminfo);
118 function test_grade_item_load_scale() {
119 $grade_item = new grade_item($this->grade_items[2]);
120 $this->assertTrue(method_exists($grade_item, 'load_scale'));
121 $scale = $grade_item->load_scale();
122 $this->assertFalse(empty($grade_item->scale));
123 $this->assertEqual($scale->id, $this->grade_items[2]->scaleid);
126 function test_grade_item_load_outcome() {
127 $grade_item = new grade_item($this->grade_items[0]);
128 $this->assertTrue(method_exists($grade_item, 'load_outcome'));
129 //TODO: add tests
132 function test_grade_item_qualifies_for_regrading() {
133 $grade_item = new grade_item($this->grade_items[0]);
134 $this->assertTrue(method_exists($grade_item, 'qualifies_for_regrading'));
136 $this->assertFalse($grade_item->qualifies_for_regrading());
138 $grade_item->iteminfo = 'Updated info for this unittest grade_item';
140 $this->assertFalse($grade_item->qualifies_for_regrading());
142 $grade_item->grademin = 14;
144 $this->assertTrue($grade_item->qualifies_for_regrading());
147 function test_grade_item_force_regrading() {
148 $grade_item = new grade_item($this->grade_items[0]);
149 $this->assertTrue(method_exists($grade_item, 'force_regrading'));
151 $this->assertEqual(0, $grade_item->needsupdate);
153 $grade_item->force_regrading();
154 $this->assertEqual(1, $grade_item->needsupdate);
155 $grade_item->update_from_db();
156 $this->assertEqual(1, $grade_item->needsupdate);
159 function test_grade_item_fetch() {
160 $grade_item = new grade_item();
161 $this->assertTrue(method_exists($grade_item, 'fetch'));
163 $grade_item = grade_item::fetch(array('id'=>$this->grade_items[0]->id));
164 $this->assertEqual($this->grade_items[0]->id, $grade_item->id);
165 $this->assertEqual($this->grade_items[0]->iteminfo, $grade_item->iteminfo);
167 $grade_item = grade_item::fetch(array('itemtype'=>$this->grade_items[1]->itemtype, 'itemmodule'=>$this->grade_items[1]->itemmodule));
168 $this->assertEqual($this->grade_items[1]->id, $grade_item->id);
169 $this->assertEqual($this->grade_items[1]->iteminfo, $grade_item->iteminfo);
172 function test_grade_item_fetch_all() {
173 $grade_item = new grade_item();
174 $this->assertTrue(method_exists($grade_item, 'fetch_all'));
176 $grade_items = grade_item::fetch_all(array('courseid'=>$this->courseid));
177 $this->assertEqual(count($this->grade_items), count($grade_items)-1);
181 * Retrieve all final scores for a given grade_item.
183 function test_grade_item_get_all_finals() {
184 $grade_item = new grade_item($this->grade_items[0]);
185 $this->assertTrue(method_exists($grade_item, 'get_final'));
187 $final_grades = $grade_item->get_final();
188 $this->assertEqual(3, count($final_grades));
193 * Retrieve all final scores for a specific userid.
195 function test_grade_item_get_final() {
196 $grade_item = new grade_item($this->grade_items[0]);
197 $this->assertTrue(method_exists($grade_item, 'get_final'));
198 $final_grade = $grade_item->get_final($this->userid);
199 $this->assertEqual($this->grade_grades[0]->finalgrade, $final_grade->finalgrade);
202 function test_grade_item_get_sortorder() {
203 $grade_item = new grade_item($this->grade_items[0]);
204 $this->assertTrue(method_exists($grade_item, 'get_sortorder'));
205 $sortorder = $grade_item->get_sortorder();
206 $this->assertEqual($this->grade_items[0]->sortorder, $sortorder);
209 function test_grade_item_set_sortorder() {
210 $grade_item = new grade_item($this->grade_items[0]);
211 $this->assertTrue(method_exists($grade_item, 'set_sortorder'));
212 $grade_item->set_sortorder(999);
213 $this->assertEqual($grade_item->sortorder, 999);
216 function test_grade_item_move_after_sortorder() {
217 $grade_item = new grade_item($this->grade_items[0]);
218 $this->assertTrue(method_exists($grade_item, 'move_after_sortorder'));
219 $grade_item->move_after_sortorder(5);
220 $this->assertEqual($grade_item->sortorder, 6);
222 $grade_item = grade_item::fetch(array('id'=>$this->grade_items[0]->id));
223 $this->assertEqual($grade_item->sortorder, 6);
225 $after = grade_item::fetch(array('id'=>$this->grade_items[6]->id));
226 $this->assertEqual($after->sortorder, 8);
229 function test_grade_item_get_name() {
230 $grade_item = new grade_item($this->grade_items[0]);
231 $this->assertTrue(method_exists($grade_item, 'get_name'));
233 $name = $grade_item->get_name();
234 $this->assertEqual($this->grade_items[0]->itemname, $name);
237 function test_grade_item_set_parent() {
238 $grade_item = new grade_item($this->grade_items[0]);
239 $this->assertTrue(method_exists($grade_item, 'set_parent'));
241 $old = $grade_item->get_parent_category();
242 $new = new grade_category($this->grade_categories[3]);
243 $new_item = $new->get_grade_item();
245 $this->assertTrue($grade_item->set_parent($new->id));
247 $new_item->update_from_db();
248 $grade_item->update_from_db();
250 $this->assertEqual($grade_item->categoryid, $new->id);
253 function test_grade_item_get_parent_category() {
254 $grade_item = new grade_item($this->grade_items[0]);
255 $this->assertTrue(method_exists($grade_item, 'get_parent_category'));
257 $category = $grade_item->get_parent_category();
258 $this->assertEqual($this->grade_categories[1]->fullname, $category->fullname);
261 function test_grade_item_load_parent_category() {
262 $grade_item = new grade_item($this->grade_items[0]);
263 $this->assertTrue(method_exists($grade_item, 'load_parent_category'));
265 $category = $grade_item->load_parent_category();
266 $this->assertEqual($this->grade_categories[1]->fullname, $category->fullname);
267 $this->assertEqual($this->grade_categories[1]->fullname, $grade_item->parent_category->fullname);
270 function test_grade_item_get_item_category() {
271 $grade_item = new grade_item($this->grade_items[3]);
272 $this->assertTrue(method_exists($grade_item, 'get_item_category'));
274 $category = $grade_item->get_item_category();
275 $this->assertEqual($this->grade_categories[0]->fullname, $category->fullname);
278 function test_grade_item_load_item_category() {
279 $grade_item = new grade_item($this->grade_items[3]);
280 $this->assertTrue(method_exists($grade_item, 'load_item_category'));
282 $category = $grade_item->load_item_category();
283 $this->assertEqual($this->grade_categories[0]->fullname, $category->fullname);
284 $this->assertEqual($this->grade_categories[0]->fullname, $grade_item->item_category->fullname);
288 * Test update of all final grades
290 function test_grade_item_regrade_final_grades() {
291 $grade_item = new grade_item($this->grade_items[0]);
292 $this->assertTrue(method_exists($grade_item, 'regrade_final_grades'));
293 $this->assertEqual(true, $grade_item->regrade_final_grades());
294 //TODO: add more tests
298 * Test the adjust_grade method
300 function test_grade_item_adjust_grade() {
301 $grade_item = new grade_item($this->grade_items[0]);
302 $this->assertTrue(method_exists($grade_item, 'adjust_grade'));
303 $grade_raw = new stdClass();
305 $grade_raw->rawgrade = 40;
306 $grade_raw->grademax = 100;
307 $grade_raw->grademin = 0;
309 $grade_item->multfactor = 1;
310 $grade_item->plusfactor = 0;
311 $grade_item->grademax = 50;
312 $grade_item->grademin = 0;
314 $original_grade_raw = clone($grade_raw);
315 $original_grade_item = clone($grade_item);
317 $this->assertEqual(20, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
319 // Try a larger maximum grade
320 $grade_item->grademax = 150;
321 $grade_item->grademin = 0;
322 $this->assertEqual(60, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
324 // Try larger minimum grade
325 $grade_item->grademin = 50;
327 $this->assertEqual(90, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
329 // Rescaling from a small scale (0-50) to a larger scale (0-100)
330 $grade_raw->grademax = 50;
331 $grade_raw->grademin = 0;
332 $grade_item->grademax = 100;
333 $grade_item->grademin = 0;
335 $this->assertEqual(80, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
337 // Rescaling from a small scale (0-50) to a larger scale with offset (40-100)
338 $grade_item->grademax = 100;
339 $grade_item->grademin = 40;
341 $this->assertEqual(88, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
343 // Try multfactor and plusfactor
344 $grade_raw = clone($original_grade_raw);
345 $grade_item = clone($original_grade_item);
346 $grade_item->multfactor = 1.23;
347 $grade_item->plusfactor = 3;
349 $this->assertEqual(27.6, $grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
351 // Try multfactor below 0 and a negative plusfactor
352 $grade_raw = clone($original_grade_raw);
353 $grade_item = clone($original_grade_item);
354 $grade_item->multfactor = 0.23;
355 $grade_item->plusfactor = -3;
357 $this->assertEqual(round(1.6), round($grade_item->adjust_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax)));
361 * Test locking of grade items
363 function test_grade_item_set_locked() {
364 $grade_item = new grade_item($this->grade_items[0]);
365 $this->assertTrue(method_exists($grade_item, 'set_locked'));
367 $grade = new grade_grade($grade_item->get_final(1));
368 $this->assertTrue(empty($grade_item->locked));
369 $this->assertTrue(empty($grade->locked));
371 $this->assertTrue($grade_item->set_locked(true));
372 $grade = new grade_grade($grade_item->get_final(1));
374 $this->assertFalse(empty($grade_item->locked));
375 $this->assertFalse(empty($grade->locked)); // individual grades should be locked too
377 $this->assertTrue($grade_item->set_locked(false));
378 $grade = new grade_grade($grade_item->get_final(1));
380 $this->assertTrue(empty($grade_item->locked));
381 $this->assertTrue(empty($grade->locked)); // individual grades should be unlocked too
384 function test_grade_item_is_locked() {
385 $grade_item = new grade_item($this->grade_items[0]);
386 $this->assertTrue(method_exists($grade_item, 'is_locked'));
388 $this->assertFalse($grade_item->is_locked());
389 $this->assertFalse($grade_item->is_locked(1));
390 $this->assertTrue($grade_item->set_locked(true));
391 $this->assertTrue($grade_item->is_locked());
392 $this->assertTrue($grade_item->is_locked(1));
396 * Test hiding of grade items
398 function test_grade_item_set_hidden() {
399 $grade_item = new grade_item($this->grade_items[0]);
400 $this->assertTrue(method_exists($grade_item, 'set_hidden'));
402 $grade = new grade_grade($grade_item->get_final(1));
403 $this->assertEqual(0, $grade_item->hidden);
404 $this->assertEqual(0, $grade->hidden);
406 $grade_item->set_hidden(666);
407 $grade = new grade_grade($grade_item->get_final(1));
409 $this->assertEqual(666, $grade_item->hidden);
410 $this->assertEqual(666, $grade->hidden);
413 function test_grade_item_is_hidden() {
414 $grade_item = new grade_item($this->grade_items[0]);
415 $this->assertTrue(method_exists($grade_item, 'is_hidden'));
417 $this->assertFalse($grade_item->is_hidden());
418 $this->assertFalse($grade_item->is_hidden(1));
420 $grade_item->set_hidden(1);
421 $this->assertTrue($grade_item->is_hidden());
422 $this->assertTrue($grade_item->is_hidden(1));
424 $grade_item->set_hidden(666);
425 $this->assertFalse($grade_item->is_hidden());
426 $this->assertFalse($grade_item->is_hidden(1));
428 $grade_item->set_hidden(time()+666);
429 $this->assertTrue($grade_item->is_hidden());
430 $this->assertTrue($grade_item->is_hidden(1));
433 function test_grade_item_is_category_item() {
434 $grade_item = new grade_item($this->grade_items[3]);
435 $this->assertTrue(method_exists($grade_item, 'is_category_item'));
436 $this->assertTrue($grade_item->is_category_item());
439 function test_grade_item_is_course_item() {
440 $grade_item = grade_item::fetch_course_item($this->courseid);
441 $this->assertTrue(method_exists($grade_item, 'is_course_item'));
442 $this->assertTrue($grade_item->is_course_item());
445 function test_grade_item_fetch_course_item() {
446 $grade_item = grade_item::fetch_course_item($this->courseid);
447 $this->assertTrue(method_exists($grade_item, 'fetch_course_item'));
448 $this->assertTrue($grade_item->itemtype, 'course');
451 function test_grade_item_depends_on() {
452 $grade_item = new grade_item($this->grade_items[1]);
454 // calculated grade dependency
455 $deps = $grade_item->depends_on();
456 sort($deps, SORT_NUMERIC); // for comparison
457 $this->assertEqual(array($this->grade_items[0]->id), $deps);
459 // simulate depends on returns none when locked
460 $grade_item->locked = time();
461 $grade_item->update();
462 $deps = $grade_item->depends_on();
463 sort($deps, SORT_NUMERIC); // for comparison
464 $this->assertEqual(array(), $deps);
466 // category dependency
467 $grade_item = new grade_item($this->grade_items[3]);
468 $deps = $grade_item->depends_on();
469 sort($deps, SORT_NUMERIC); // for comparison
470 $res = array($this->grade_items[4]->id, $this->grade_items[5]->id);
471 $this->assertEqual($res, $deps);
474 function test_grade_item_is_calculated() {
475 $grade_item = new grade_item($this->grade_items[1]);
476 $this->assertTrue(method_exists($grade_item, 'is_calculated'));
477 $grade_itemsource = new grade_item($this->grade_items[0]);
478 $normalizedformula = str_replace('[['.$grade_itemsource->idnumber.']]', '##gi'.$grade_itemsource->id.'##', $this->grade_items[1]->calculation);
480 $this->assertTrue($grade_item->is_calculated());
481 $this->assertEqual($normalizedformula, $grade_item->calculation);
484 function test_grade_item_set_calculation() {
485 $grade_item = new grade_item($this->grade_items[1]);
486 $this->assertTrue(method_exists($grade_item, 'set_calculation'));
487 $grade_itemsource = new grade_item($this->grade_items[0]);
489 $grade_item->set_calculation('=[['.$grade_itemsource->idnumber.']]');
491 $this->assertTrue(!empty($grade_item->needsupdate));
492 $this->assertEqual('=##gi'.$grade_itemsource->id.'##', $grade_item->calculation);
495 function test_grade_item_get_calculation() {
496 $grade_item = new grade_item($this->grade_items[1]);
497 $this->assertTrue(method_exists($grade_item, 'get_calculation'));
498 $grade_itemsource = new grade_item($this->grade_items[0]);
500 $denormalizedformula = str_replace('##gi'.$grade_itemsource->id.'##', '[['.$grade_itemsource->idnumber.']]', $this->grade_items[1]->calculation);
502 $formula = $grade_item->get_calculation();
503 $this->assertTrue(!empty($grade_item->needsupdate));
504 $this->assertEqual($denormalizedformula, $formula);
507 function test_grade_item_compute() {
508 $grade_item = new grade_item($this->grade_items[1]);
509 $this->assertTrue(method_exists($grade_item, 'compute'));
511 $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[3]->id));
512 $grade_grade->delete();
513 $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[4]->id));
514 $grade_grade->delete();
515 $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[5]->id));
516 $grade_grade->delete();
518 $grade_item->compute();
520 $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[3]->userid, 'itemid'=>$this->grade_grades[3]->itemid));
521 $this->assertEqual($this->grade_grades[3]->finalgrade, $grade_grade->finalgrade);
522 $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[4]->userid, 'itemid'=>$this->grade_grades[4]->itemid));
523 $this->assertEqual($this->grade_grades[4]->finalgrade, $grade_grade->finalgrade);
524 $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[5]->userid, 'itemid'=>$this->grade_grades[5]->itemid));
525 $this->assertEqual($this->grade_grades[5]->finalgrade, $grade_grade->finalgrade);