PageRenderTime 63ms CodeModel.GetById 14ms app.highlight 44ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/MoodleWebRole/lib/grade/simpletest/testgradeitem.php

#
PHP | 508 lines | 344 code | 118 blank | 46 comment | 1 complexity | 972e3bc633e7b0692b4af159f25ab6c6 MD5 | raw file
  1<?php // $Id: testgradeitem.php,v 1.6.2.3 2008/03/27 17:13:00 nicolasconnault Exp $
  2
  3///////////////////////////////////////////////////////////////////////////
  4//                                                                       //
  5// NOTICE OF COPYRIGHT                                                   //
  6//                                                                       //
  7// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
  8//          http://moodle.org                                            //
  9//                                                                       //
 10// Copyright (C) 1999 onwards 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///////////////////////////////////////////////////////////////////////////
 25
 26/**
 27 * Unit tests for grade_item object.
 28 *
 29 * @author nicolas@moodle.com
 30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 31 * @package moodlecore
 32 */
 33
 34if (!defined('MOODLE_INTERNAL')) {
 35    die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 36}
 37
 38require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
 39
 40Mock::generatePartial('grade_item', 'mock_grade_item_for_test_is_calculated', array('set_calculation'));
 41
 42@set_time_limit(0);
 43
 44class grade_item_test extends grade_test {
 45
 46    function test_grade_item_construct() {
 47        $params = new stdClass();
 48
 49        $params->courseid = $this->courseid;
 50        $params->categoryid = $this->grade_categories[1]->id;
 51        $params->itemname = 'unittestgradeitem4';
 52        $params->itemtype = 'mod';
 53        $params->itemmodule = 'database';
 54        $params->iteminfo = 'Grade item used for unit testing';
 55
 56        $grade_item = new grade_item($params, false);
 57
 58        $this->assertEqual($params->courseid, $grade_item->courseid);
 59        $this->assertEqual($params->categoryid, $grade_item->categoryid);
 60        $this->assertEqual($params->itemmodule, $grade_item->itemmodule);
 61    }
 62
 63    function test_grade_item_insert() {
 64        $grade_item = new grade_item();
 65        $this->assertTrue(method_exists($grade_item, 'insert'));
 66
 67        $grade_item->courseid = $this->courseid;
 68        $grade_item->categoryid = $this->grade_categories[1]->id;
 69        $grade_item->itemname = 'unittestgradeitem4';
 70        $grade_item->itemtype = 'mod';
 71        $grade_item->itemmodule = 'quiz';
 72        $grade_item->iteminfo = 'Grade item used for unit testing';
 73
 74        $grade_item->insert();
 75
 76        $last_grade_item = end($this->grade_items);
 77
 78        $this->assertEqual($grade_item->id, $last_grade_item->id + 1);
 79        $this->assertEqual(11, $grade_item->sortorder);
 80    }
 81
 82    function test_grade_item_delete() {
 83        $grade_item = new grade_item($this->grade_items[0]);
 84        $this->assertTrue(method_exists($grade_item, 'delete'));
 85
 86        $this->assertTrue($grade_item->delete());
 87
 88        $this->assertFalse(get_record('grade_items', 'id', $grade_item->id));
 89    }
 90
 91    function test_grade_item_update() {
 92        $grade_item = new grade_item($this->grade_items[0]);
 93        $this->assertTrue(method_exists($grade_item, 'update'));
 94
 95        $grade_item->iteminfo = 'Updated info for this unittest grade_item';
 96
 97        $this->assertTrue($grade_item->update());
 98
 99        $grade_item->grademin = 14;
100        $this->assertTrue($grade_item->qualifies_for_regrading());
101        $this->assertTrue($grade_item->update());
102
103        $iteminfo = get_field('grade_items', 'iteminfo', 'id', $this->grade_items[0]->id);
104        $this->assertEqual($grade_item->iteminfo, $iteminfo);
105    }
106
107    function test_grade_item_load_scale() {
108        $grade_item = new grade_item($this->grade_items[2]);
109        $this->assertTrue(method_exists($grade_item, 'load_scale'));
110        $scale = $grade_item->load_scale();
111        $this->assertFalse(empty($grade_item->scale));
112        $this->assertEqual($scale->id, $this->grade_items[2]->scaleid);
113    }
114
115    function test_grade_item_load_outcome() {
116        $grade_item = new grade_item($this->grade_items[0]);
117        $this->assertTrue(method_exists($grade_item, 'load_outcome'));
118        //TODO: add tests
119    }
120
121    function test_grade_item_qualifies_for_regrading() {
122        $grade_item = new grade_item($this->grade_items[0]);
123        $this->assertTrue(method_exists($grade_item, 'qualifies_for_regrading'));
124
125        $this->assertFalse($grade_item->qualifies_for_regrading());
126
127        $grade_item->iteminfo = 'Updated info for this unittest grade_item';
128
129        $this->assertFalse($grade_item->qualifies_for_regrading());
130
131        $grade_item->grademin = 14;
132
133        $this->assertTrue($grade_item->qualifies_for_regrading());
134    }
135
136    function test_grade_item_force_regrading() {
137        $grade_item = new grade_item($this->grade_items[0]);
138        $this->assertTrue(method_exists($grade_item, 'force_regrading'));
139
140        $this->assertEqual(0, $grade_item->needsupdate);
141
142        $grade_item->force_regrading();
143        $this->assertEqual(1, $grade_item->needsupdate);
144        $grade_item->update_from_db();
145        $this->assertEqual(1, $grade_item->needsupdate);
146    }
147
148    function test_grade_item_fetch() {
149        $grade_item = new grade_item();
150        $this->assertTrue(method_exists($grade_item, 'fetch'));
151
152        $grade_item = grade_item::fetch(array('id'=>$this->grade_items[0]->id));
153        $this->assertEqual($this->grade_items[0]->id, $grade_item->id);
154        $this->assertEqual($this->grade_items[0]->iteminfo, $grade_item->iteminfo);
155
156        $grade_item = grade_item::fetch(array('itemtype'=>$this->grade_items[1]->itemtype, 'itemmodule'=>$this->grade_items[1]->itemmodule));
157        $this->assertEqual($this->grade_items[1]->id, $grade_item->id);
158        $this->assertEqual($this->grade_items[1]->iteminfo, $grade_item->iteminfo);
159    }
160
161    function test_grade_item_fetch_all() {
162        $grade_item = new grade_item();
163        $this->assertTrue(method_exists($grade_item, 'fetch_all'));
164
165        $grade_items = grade_item::fetch_all(array('courseid'=>$this->courseid));
166        $this->assertEqual(count($this->grade_items), count($grade_items)-1);
167    }
168
169    // Retrieve all final scores for a given grade_item.
170    function test_grade_item_get_all_finals() {
171        $grade_item = new grade_item($this->grade_items[0]);
172        $this->assertTrue(method_exists($grade_item, 'get_final'));
173
174        $final_grades = $grade_item->get_final();
175        $this->assertEqual(3, count($final_grades));
176    }
177
178
179    // Retrieve all final scores for a specific userid.
180    function test_grade_item_get_final() {
181        $grade_item = new grade_item($this->grade_items[0]);
182        $this->assertTrue(method_exists($grade_item, 'get_final'));
183        $final_grade = $grade_item->get_final($this->userid);
184        $this->assertEqual($this->grade_grades[0]->finalgrade, $final_grade->finalgrade);
185    }
186
187    function test_grade_item_get_sortorder() {
188        $grade_item = new grade_item($this->grade_items[0]);
189        $this->assertTrue(method_exists($grade_item, 'get_sortorder'));
190        $sortorder = $grade_item->get_sortorder();
191        $this->assertEqual($this->grade_items[0]->sortorder, $sortorder);
192    }
193
194    function test_grade_item_set_sortorder() {
195        $grade_item = new grade_item($this->grade_items[0]);
196        $this->assertTrue(method_exists($grade_item, 'set_sortorder'));
197        $grade_item->set_sortorder(999);
198        $this->assertEqual($grade_item->sortorder, 999);
199    }
200
201    function test_grade_item_move_after_sortorder() {
202        $grade_item = new grade_item($this->grade_items[0]);
203        $this->assertTrue(method_exists($grade_item, 'move_after_sortorder'));
204        $grade_item->move_after_sortorder(5);
205        $this->assertEqual($grade_item->sortorder, 6);
206
207        $grade_item = grade_item::fetch(array('id'=>$this->grade_items[0]->id));
208        $this->assertEqual($grade_item->sortorder, 6);
209
210        $after = grade_item::fetch(array('id'=>$this->grade_items[6]->id));
211        $this->assertEqual($after->sortorder, 8);
212    }
213
214    function test_grade_item_get_name() {
215        $grade_item = new grade_item($this->grade_items[0]);
216        $this->assertTrue(method_exists($grade_item, 'get_name'));
217
218        $name = $grade_item->get_name();
219        $this->assertEqual($this->grade_items[0]->itemname, $name);
220    }
221
222    function test_grade_item_set_parent() {
223        $grade_item = new grade_item($this->grade_items[0]);
224        $this->assertTrue(method_exists($grade_item, 'set_parent'));
225
226        $old = $grade_item->get_parent_category();
227        $new = new grade_category($this->grade_categories[3]);
228        $new_item = $new->get_grade_item();
229
230        $this->assertTrue($grade_item->set_parent($new->id));
231
232        $new_item->update_from_db();
233        $grade_item->update_from_db();
234
235        $this->assertEqual($grade_item->categoryid, $new->id);
236    }
237
238    function test_grade_item_get_parent_category() {
239        $grade_item = new grade_item($this->grade_items[0]);
240        $this->assertTrue(method_exists($grade_item, 'get_parent_category'));
241
242        $category = $grade_item->get_parent_category();
243        $this->assertEqual($this->grade_categories[1]->fullname, $category->fullname);
244    }
245
246    function test_grade_item_load_parent_category() {
247        $grade_item = new grade_item($this->grade_items[0]);
248        $this->assertTrue(method_exists($grade_item, 'load_parent_category'));
249
250        $category = $grade_item->load_parent_category();
251        $this->assertEqual($this->grade_categories[1]->fullname, $category->fullname);
252        $this->assertEqual($this->grade_categories[1]->fullname, $grade_item->parent_category->fullname);
253    }
254
255    function test_grade_item_get_item_category() {
256        $grade_item = new grade_item($this->grade_items[3]);
257        $this->assertTrue(method_exists($grade_item, 'get_item_category'));
258
259        $category = $grade_item->get_item_category();
260        $this->assertEqual($this->grade_categories[0]->fullname, $category->fullname);
261    }
262
263    function test_grade_item_load_item_category() {
264        $grade_item = new grade_item($this->grade_items[3]);
265        $this->assertTrue(method_exists($grade_item, 'load_item_category'));
266
267        $category = $grade_item->load_item_category();
268        $this->assertEqual($this->grade_categories[0]->fullname, $category->fullname);
269        $this->assertEqual($this->grade_categories[0]->fullname, $grade_item->item_category->fullname);
270    }
271
272    // Test update of all final grades
273    function test_grade_item_regrade_final_grades() {
274        $grade_item = new grade_item($this->grade_items[0]);
275        $this->assertTrue(method_exists($grade_item, 'regrade_final_grades'));
276        $this->assertEqual(true, $grade_item->regrade_final_grades());
277        //TODO: add more tests
278    }
279
280    // Test the adjust_raw_grade method
281    function test_grade_item_adjust_raw_grade() {
282        $grade_item = new grade_item($this->grade_items[0]);
283        $this->assertTrue(method_exists($grade_item, 'adjust_raw_grade'));
284        $grade_raw = new stdClass();
285
286        $grade_raw->rawgrade = 40;
287        $grade_raw->grademax = 100;
288        $grade_raw->grademin = 0;
289
290        $grade_item->multfactor = 1;
291        $grade_item->plusfactor = 0;
292        $grade_item->grademax = 50;
293        $grade_item->grademin = 0;
294
295        $original_grade_raw  = clone($grade_raw);
296        $original_grade_item = clone($grade_item);
297
298        $this->assertEqual(20, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
299
300        // Try a larger maximum grade
301        $grade_item->grademax = 150;
302        $grade_item->grademin = 0;
303        $this->assertEqual(60, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
304
305        // Try larger minimum grade
306        $grade_item->grademin = 50;
307
308        $this->assertEqual(90, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
309
310        // Rescaling from a small scale (0-50) to a larger scale (0-100)
311        $grade_raw->grademax = 50;
312        $grade_raw->grademin = 0;
313        $grade_item->grademax = 100;
314        $grade_item->grademin = 0;
315
316        $this->assertEqual(80, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
317
318        // Rescaling from a small scale (0-50) to a larger scale with offset (40-100)
319        $grade_item->grademax = 100;
320        $grade_item->grademin = 40;
321
322        $this->assertEqual(88, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
323
324        // Try multfactor and plusfactor
325        $grade_raw = clone($original_grade_raw);
326        $grade_item = clone($original_grade_item);
327        $grade_item->multfactor = 1.23;
328        $grade_item->plusfactor = 3;
329
330        $this->assertEqual(27.6, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
331
332        // Try multfactor below 0 and a negative plusfactor
333        $grade_raw = clone($original_grade_raw);
334        $grade_item = clone($original_grade_item);
335        $grade_item->multfactor = 0.23;
336        $grade_item->plusfactor = -3;
337
338        $this->assertEqual(round(1.6), round($grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax)));
339    }
340
341    // Test locking of grade items
342    function test_grade_item_set_locked() {
343        $grade_item = new grade_item($this->grade_items[0]);
344        $this->assertTrue(method_exists($grade_item, 'set_locked'));
345
346        $grade = new grade_grade($grade_item->get_final(1));
347        $this->assertTrue(empty($grade_item->locked));
348        $this->assertTrue(empty($grade->locked));
349
350        $this->assertTrue($grade_item->set_locked(true, true, false));
351        $grade = new grade_grade($grade_item->get_final(1));
352
353        $this->assertFalse(empty($grade_item->locked));
354        $this->assertFalse(empty($grade->locked)); // individual grades should be locked too
355
356        $this->assertTrue($grade_item->set_locked(false, true, false));
357        $grade = new grade_grade($grade_item->get_final(1));
358
359        $this->assertTrue(empty($grade_item->locked));
360        $this->assertTrue(empty($grade->locked)); // individual grades should be unlocked too
361    }
362
363    function test_grade_item_is_locked() {
364        $grade_item = new grade_item($this->grade_items[0]);
365        $this->assertTrue(method_exists($grade_item, 'is_locked'));
366
367        $this->assertFalse($grade_item->is_locked());
368        $this->assertFalse($grade_item->is_locked(1));
369        $this->assertTrue($grade_item->set_locked(true, true, false));
370        $this->assertTrue($grade_item->is_locked());
371        $this->assertTrue($grade_item->is_locked(1));
372    }
373
374    // Test hiding of grade items
375    function test_grade_item_set_hidden() {
376        $grade_item = new grade_item($this->grade_items[0]);
377        $this->assertTrue(method_exists($grade_item, 'set_hidden'));
378
379        $grade = new grade_grade($grade_item->get_final(1));
380        $this->assertEqual(0, $grade_item->hidden);
381        $this->assertEqual(0, $grade->hidden);
382
383        $grade_item->set_hidden(666, true);
384        $grade = new grade_grade($grade_item->get_final(1));
385
386        $this->assertEqual(666, $grade_item->hidden);
387        $this->assertEqual(666, $grade->hidden);
388    }
389
390    function test_grade_item_is_hidden() {
391        $grade_item = new grade_item($this->grade_items[0]);
392        $this->assertTrue(method_exists($grade_item, 'is_hidden'));
393
394        $this->assertFalse($grade_item->is_hidden());
395        $this->assertFalse($grade_item->is_hidden(1));
396
397        $grade_item->set_hidden(1);
398        $this->assertTrue($grade_item->is_hidden());
399        $this->assertTrue($grade_item->is_hidden(1));
400
401        $grade_item->set_hidden(666);
402        $this->assertFalse($grade_item->is_hidden());
403        $this->assertFalse($grade_item->is_hidden(1));
404
405        $grade_item->set_hidden(time()+666);
406        $this->assertTrue($grade_item->is_hidden());
407        $this->assertTrue($grade_item->is_hidden(1));
408    }
409
410    function test_grade_item_is_category_item() {
411        $grade_item = new grade_item($this->grade_items[3]);
412        $this->assertTrue(method_exists($grade_item, 'is_category_item'));
413        $this->assertTrue($grade_item->is_category_item());
414    }
415
416    function test_grade_item_is_course_item() {
417        $grade_item = grade_item::fetch_course_item($this->courseid);
418        $this->assertTrue(method_exists($grade_item, 'is_course_item'));
419        $this->assertTrue($grade_item->is_course_item());
420    }
421
422    function test_grade_item_fetch_course_item() {
423        $grade_item = grade_item::fetch_course_item($this->courseid);
424        $this->assertTrue(method_exists($grade_item, 'fetch_course_item'));
425        $this->assertTrue($grade_item->itemtype, 'course');
426    }
427
428    function test_grade_item_depends_on() {
429        $grade_item = new grade_item($this->grade_items[1]);
430
431        // calculated grade dependency
432        $deps = $grade_item->depends_on();
433        sort($deps, SORT_NUMERIC); // for comparison
434        $this->assertEqual(array($this->grade_items[0]->id), $deps);
435
436        // simulate depends on returns none when locked
437        $grade_item->locked = time();
438        $grade_item->update();
439        $deps = $grade_item->depends_on();
440        sort($deps, SORT_NUMERIC); // for comparison
441        $this->assertEqual(array(), $deps);
442
443        // category dependency
444        $grade_item = new grade_item($this->grade_items[3]);
445        $deps = $grade_item->depends_on();
446        sort($deps, SORT_NUMERIC); // for comparison
447        $res = array($this->grade_items[4]->id, $this->grade_items[5]->id);
448        $this->assertEqual($res, $deps);
449    }
450
451    function test_grade_item_is_calculated() {
452        $grade_item = new mock_grade_item_for_test_is_calculated($this);
453        $grade_item->set_properties($grade_item, $this->grade_items[1]);
454        $this->assertTrue(method_exists($grade_item, 'is_calculated'));
455        $grade_itemsource = new grade_item($this->grade_items[0]);
456        $normalizedformula = str_replace("[[$grade_itemsource->idnumber]]", "##gi$grade_itemsource->id##", $this->grade_items[1]->calculation);
457
458        $grade_item->expectOnce('set_calculation', array($grade_item->calculation));
459        $grade_item->setReturnValue('set_calculation', $normalizedformula);
460        $this->assertTrue($grade_item->is_calculated());
461    }
462
463    function test_grade_item_set_calculation() {
464        $grade_item = new grade_item($this->grade_items[1]);
465        $this->assertTrue(method_exists($grade_item, 'set_calculation'));
466        $grade_itemsource = new grade_item($this->grade_items[0]);
467
468        $grade_item->set_calculation('=[['.$grade_itemsource->idnumber.']]');
469
470        $this->assertTrue(!empty($grade_item->needsupdate));
471        $this->assertEqual('=##gi'.$grade_itemsource->id.'##', $grade_item->calculation);
472    }
473
474    function test_grade_item_get_calculation() {
475        $grade_item = new grade_item($this->grade_items[1]);
476        $this->assertTrue(method_exists($grade_item, 'get_calculation'));
477        $grade_itemsource = new grade_item($this->grade_items[0]);
478
479        $denormalizedformula = str_replace('##gi'.$grade_itemsource->id.'##', '[['.$grade_itemsource->idnumber.']]', $this->grade_items[1]->calculation);
480
481        $formula = $grade_item->get_calculation();
482        $this->assertTrue(!empty($grade_item->needsupdate));
483        $this->assertEqual($denormalizedformula, $formula);
484    }
485
486    function test_grade_item_compute() {
487        $grade_item = new grade_item($this->grade_items[1]);
488        $this->assertTrue(method_exists($grade_item, 'compute'));
489
490        $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[3]->id));
491        $grade_grade->delete();
492        $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[4]->id));
493        $grade_grade->delete();
494        $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[5]->id));
495        $grade_grade->delete();
496
497        $grade_item->compute();
498
499        $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[3]->userid, 'itemid'=>$this->grade_grades[3]->itemid));
500        $this->assertEqual($this->grade_grades[3]->finalgrade, $grade_grade->finalgrade);
501        $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[4]->userid, 'itemid'=>$this->grade_grades[4]->itemid));
502        $this->assertEqual($this->grade_grades[4]->finalgrade, $grade_grade->finalgrade);
503        $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[5]->userid, 'itemid'=>$this->grade_grades[5]->itemid));
504        $this->assertEqual($this->grade_grades[5]->finalgrade, $grade_grade->finalgrade);
505    }
506
507}
508?>