PageRenderTime 31ms CodeModel.GetById 2ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
PHP | 471 lines | 322 code | 82 blank | 67 comment | 1 complexity | 7552eeea561fbbd12e95c045a4998b72 MD5 | raw file
  1<?php // $Id: testgradecategory.php,v 1.7.2.3 2009/04/29 13:44:17 skodak 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_category 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
 40class grade_category_test extends grade_test {
 41
 42    function test_grade_category_construct() {
 43        $course_category = grade_category::fetch_course_category($this->courseid);
 44
 45        $params = new stdClass();
 46
 47        $params->courseid = $this->courseid;
 48        $params->fullname = 'unittestcategory4';
 49
 50        $grade_category = new grade_category($params, false);
 51        $grade_category->insert();
 52
 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;
 58
 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();
 64
 65        $this->assertEqual(3, $grade_category->depth);
 66        $this->assertEqual($parentpath.$grade_category->id."/", $grade_category->path);
 67        $parentpath = $grade_category->path;
 68
 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);
 76    }
 77
 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);
 83    }
 84
 85    function test_grade_category_fetch() {
 86        $grade_category = new grade_category();
 87        $this->assertTrue(method_exists($grade_category, 'fetch'));
 88
 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);
 92    }
 93
 94    function test_grade_category_fetch_all() {
 95        $grade_category = new grade_category();
 96        $this->assertTrue(method_exists($grade_category, 'fetch_all'));
 97
 98        $grade_categories = grade_category::fetch_all(array('courseid'=>$this->courseid));
 99        $this->assertEqual(count($this->grade_categories), count($grade_categories)-1);
100    }
101
102    function test_grade_category_update() {
103        $grade_category = new grade_category($this->grade_categories[0]);
104        $this->assertTrue(method_exists($grade_category, 'update'));
105
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
110
111        $grade_item = $grade_category->get_grade_item();
112        $this->assertEqual(0, $grade_item->needsupdate);
113
114        $this->assertTrue($grade_category->update());
115
116        $fullname = get_field('grade_categories', 'fullname', 'id', $this->grade_categories[0]->id);
117        $this->assertEqual($grade_category->fullname, $fullname);
118
119        $path = get_field('grade_categories', 'path', 'id', $this->grade_categories[0]->id);
120        $this->assertEqual($grade_category->path, $path);
121
122        $depth = get_field('grade_categories', 'depth', 'id', $this->grade_categories[0]->id);
123        $this->assertEqual($grade_category->depth, $depth);
124
125        $grade_item = $grade_category->get_grade_item();
126        $this->assertEqual(1, $grade_item->needsupdate);
127    }
128
129    function test_grade_category_delete() {
130        $grade_category = new grade_category($this->grade_categories[0]);
131        $this->assertTrue(method_exists($grade_category, 'delete'));
132
133        $this->assertTrue($grade_category->delete());
134        $this->assertFalse(get_record('grade_categories', 'id', $grade_category->id));
135    }
136
137    function test_grade_category_insert() {
138        $course_category = grade_category::fetch_course_category($this->courseid);
139
140        $grade_category = new grade_category();
141        $this->assertTrue(method_exists($grade_category, 'insert'));
142
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;
151
152        $grade_category->insert();
153
154        $this->assertEqual('/'.$course_category->id.'/'.$this->grade_categories[0]->id.'/'.$grade_category->id.'/', $grade_category->path);
155        $this->assertEqual(3, $grade_category->depth);
156
157        $last_grade_category = end($this->grade_categories);
158
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);
162
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));
166    }
167
168    function test_grade_category_insert_course_category() {
169        $grade_category = new grade_category();
170        $this->assertTrue(method_exists($grade_category, 'insert_course_category'));
171
172        $id = $grade_category->insert_course_category($this->courseid);
173        $this->assertNotNull($id);
174        $this->assertEqual('?', $grade_category->fullname);
175        $this->assertEqual(GRADE_AGGREGATE_WEIGHTED_MEAN2, $grade_category->aggregation);
176        $this->assertEqual("/$id/", $grade_category->path);
177        $this->assertEqual(1, $grade_category->depth);
178        $this->assertNull($grade_category->parent);
179    }
180
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'));
184
185        $this->assertFalse($grade_category->qualifies_for_regrading());
186
187        $grade_category->aggregation = GRADE_AGGREGATE_MAX;
188        $this->assertTrue($grade_category->qualifies_for_regrading());
189
190        $grade_category = new grade_category($this->grade_categories[0]);
191        $grade_category->droplow = 99;
192        $this->assertTrue($grade_category->qualifies_for_regrading());
193
194        $grade_category = new grade_category($this->grade_categories[0]);
195        $grade_category->keephigh = 99;
196        $this->assertTrue($grade_category->qualifies_for_regrading());
197    }
198
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'));
202
203        $grade_category->load_grade_item();
204        $this->assertEqual(0, $grade_category->grade_item->needsupdate);
205
206        $grade_category->force_regrading();
207
208        $grade_category->grade_item = null;
209        $grade_category->load_grade_item();
210
211        $this->assertEqual(1, $grade_category->grade_item->needsupdate);
212    }
213
214    /*
215     * I am disabling this test until we implement proper mock objects. This is meant
216     * to be a private method called from within a grade_item method.
217
218    function test_grade_category_generate_grades() {
219        $category = new grade_category($this->grade_categories[3]);
220        $this->assertTrue(method_exists($category, 'generate_grades'));
221        $category->load_grade_item();
222
223        $grades = get_records('grade_grades', 'itemid', $category->grade_item->id);
224        $this->assertFalse($grades);
225
226        $category->generate_grades();
227        $grades = get_records('grade_grades', 'itemid', $category->grade_item->id);
228        $this->assertEqual(3, count($grades));
229
230        $rawvalues = array();
231        foreach ($grades as $grade) {
232            $this->assertWithinMargin($grade->rawgrade, $grade->rawgrademin, $grade->rawgrademax);
233            $rawvalues[] = (int)$grade->rawgrade;
234        }
235        sort($rawvalues);
236        // calculated mean results
237        $this->assertEqual($rawvalues, array(20,50,100));
238    }
239
240    */
241
242    function test_grade_category_aggregate_grades() {
243        $category = new grade_category($this->grade_categories[0]);
244        $this->assertTrue(method_exists($category, 'aggregate_grades'));
245        // tested above in test_grade_category_generate_grades()
246    }
247
248    function test_grade_category_apply_limit_rules() {
249        $items[$this->grade_items[0]->id] = new grade_item($this->grade_items[0], false);
250        $items[$this->grade_items[1]->id] = new grade_item($this->grade_items[1], false);
251        $items[$this->grade_items[2]->id] = new grade_item($this->grade_items[2], false);
252        $items[$this->grade_items[4]->id] = new grade_item($this->grade_items[4], false);
253
254        $category = new grade_category();
255        $category->droplow = 2;
256        $grades = array($this->grade_items[0]->id=>5.374,
257                        $this->grade_items[1]->id=>9.4743,
258                        $this->grade_items[2]->id=>2.5474,
259                        $this->grade_items[4]->id=>7.3754);
260        $category->apply_limit_rules($grades, $items);
261        $this->assertEqual(count($grades), 2);
262        $this->assertEqual($grades[$this->grade_items[1]->id], 9.4743);
263        $this->assertEqual($grades[$this->grade_items[4]->id], 7.3754);
264
265        $category = new grade_category();
266        $category->keephigh = 1;
267        $category->droplow = 0;
268        $grades = array($this->grade_items[0]->id=>5.374,
269                        $this->grade_items[1]->id=>9.4743,
270                        $this->grade_items[2]->id=>2.5474,
271                        $this->grade_items[4]->id=>7.3754);
272        $category->apply_limit_rules($grades, $items);
273        $this->assertEqual(count($grades), 1);
274        $grade = reset($grades);
275        $this->assertEqual(9.4743, $grade);
276
277        $category = new grade_category();
278        $category->droplow     = 2;
279        $category->aggregation = GRADE_AGGREGATE_SUM;
280        $items[$this->grade_items[2]->id]->aggregationcoef = 1;
281        $grades = array($this->grade_items[0]->id=>5.374,
282                        $this->grade_items[1]->id=>9.4743,
283                        $this->grade_items[2]->id=>2.5474,
284                        $this->grade_items[4]->id=>7.3754);
285
286        $category->apply_limit_rules($grades, $items);
287        $this->assertEqual(count($grades), 2);
288        $this->assertEqual($grades[$this->grade_items[1]->id], 9.4743);
289        $this->assertEqual($grades[$this->grade_items[2]->id], 2.5474);
290
291        $category = new grade_category();
292        $category->keephigh = 1;
293        $category->droplow = 0;
294        $category->aggregation = GRADE_AGGREGATE_SUM;
295        $items[$this->grade_items[2]->id]->aggregationcoef = 1;
296        $grades = array($this->grade_items[0]->id=>5.374,
297                        $this->grade_items[1]->id=>9.4743,
298                        $this->grade_items[2]->id=>2.5474,
299                        $this->grade_items[4]->id=>7.3754);
300        $category->apply_limit_rules($grades, $items);
301        $this->assertEqual(count($grades), 2);
302        $this->assertEqual($grades[$this->grade_items[1]->id], 9.4743);
303        $this->assertEqual($grades[$this->grade_items[2]->id], 2.5474);
304    }
305
306    /**
307     * TODO implement
308     */
309    function test_grade_category_is_aggregationcoef_used() {
310
311    }
312
313    function test_grade_category_fetch_course_tree() {
314        $category = new grade_category();
315        $this->assertTrue(method_exists($category, 'fetch_course_tree'));
316        //TODO: add some tests
317    }
318
319    function test_grade_category_get_children() {
320        $course_category = grade_category::fetch_course_category($this->courseid);
321
322        $category = new grade_category($this->grade_categories[0]);
323        $this->assertTrue(method_exists($category, 'get_children'));
324
325        $children_array = $category->get_children(0);
326
327        $this->assertTrue(is_array($children_array));
328        $this->assertFalse(empty($children_array[2]));
329        $this->assertFalse(empty($children_array[2]['object']));
330        $this->assertFalse(empty($children_array[2]['children']));
331        $this->assertEqual($this->grade_categories[1]->id, $children_array[2]['object']->id);
332        $this->assertEqual($this->grade_categories[2]->id, $children_array[5]['object']->id);
333        $this->assertEqual($this->grade_items[0]->id, $children_array[2]['children'][3]['object']->id);
334        $this->assertEqual($this->grade_items[1]->id, $children_array[2]['children'][4]['object']->id);
335        $this->assertEqual($this->grade_items[2]->id, $children_array[5]['children'][6]['object']->id);
336    }
337
338    function test_grade_category_load_grade_item() {
339        $category = new grade_category($this->grade_categories[0]);
340        $this->assertTrue(method_exists($category, 'load_grade_item'));
341        $this->assertEqual(null, $category->grade_item);
342        $category->load_grade_item();
343        $this->assertEqual($this->grade_items[3]->id, $category->grade_item->id);
344    }
345
346    function test_grade_category_get_grade_item() {
347        $category = new grade_category($this->grade_categories[0]);
348        $this->assertTrue(method_exists($category, 'get_grade_item'));
349        $grade_item = $category->get_grade_item();
350        $this->assertEqual($this->grade_items[3]->id, $grade_item->id);
351    }
352
353    function test_grade_category_load_parent_category() {
354        $category = new grade_category($this->grade_categories[1]);
355        $this->assertTrue(method_exists($category, 'load_parent_category'));
356        $this->assertEqual(null, $category->parent_category);
357        $category->load_parent_category();
358        $this->assertEqual($this->grade_categories[0]->id, $category->parent_category->id);
359    }
360
361    function test_grade_category_get_parent_category() {
362        $category = new grade_category($this->grade_categories[1]);
363        $this->assertTrue(method_exists($category, 'get_parent_category'));
364        $parent_category = $category->get_parent_category();
365        $this->assertEqual($this->grade_categories[0]->id, $parent_category->id);
366    }
367
368    function test_grade_category_get_name() {
369        $category = new grade_category($this->grade_categories[0]);
370        $this->assertTrue(method_exists($category, 'get_name'));
371        $this->assertEqual($this->grade_categories[0]->fullname, $category->get_name());
372    }
373
374    function test_grade_category_set_parent() {
375        $category = new grade_category($this->grade_categories[1]);
376        $this->assertTrue(method_exists($category, 'set_parent'));
377        // TODO: implement detailed tests
378
379        $course_category = grade_category::fetch_course_category($this->courseid);
380        $this->assertTrue($category->set_parent($course_category->id));
381        $this->assertEqual($course_category->id, $category->parent);
382    }
383
384    function test_grade_category_get_final() {
385        $category = new grade_category($this->grade_categories[0]);
386        $this->assertTrue(method_exists($category, 'get_final'));
387        $category->load_grade_item();
388        $this->assertEqual($category->get_final(), $category->grade_item->get_final());
389    }
390
391    function test_grade_category_get_sortorder() {
392        $category = new grade_category($this->grade_categories[0]);
393        $this->assertTrue(method_exists($category, 'get_sortorder'));
394        $category->load_grade_item();
395        $this->assertEqual($category->get_sortorder(), $category->grade_item->get_sortorder());
396    }
397
398    function test_grade_category_set_sortorder() {
399        $category = new grade_category($this->grade_categories[0]);
400        $this->assertTrue(method_exists($category, 'set_sortorder'));
401        $category->load_grade_item();
402        $this->assertEqual($category->set_sortorder(10), $category->grade_item->set_sortorder(10));
403    }
404
405    function test_grade_category_move_after_sortorder() {
406        $category = new grade_category($this->grade_categories[0]);
407        $this->assertTrue(method_exists($category, 'move_after_sortorder'));
408        $category->load_grade_item();
409        $this->assertEqual($category->move_after_sortorder(10), $category->grade_item->move_after_sortorder(10));
410    }
411
412    function test_grade_category_is_course_category() {
413        $category = grade_category::fetch_course_category($this->courseid);
414        $this->assertTrue(method_exists($category, 'is_course_category'));
415        $this->assertTrue($category->is_course_category());
416    }
417
418    function test_grade_category_fetch_course_category() {
419        $category = new grade_category();
420        $this->assertTrue(method_exists($category, 'fetch_course_category'));
421        $category = grade_category::fetch_course_category($this->courseid);
422        $this->assertTrue(empty($category->parent));
423    }
424    /**
425     * TODO implement
426     */
427    function test_grade_category_is_editable() {
428
429    }
430
431    function test_grade_category_is_locked() {
432        $category = new grade_category($this->grade_categories[0]);
433        $this->assertTrue(method_exists($category, 'is_locked'));
434        $category->load_grade_item();
435        $this->assertEqual($category->is_locked(), $category->grade_item->is_locked());
436    }
437
438    function test_grade_category_set_locked() {
439        $category = new grade_category($this->grade_categories[0]);
440        $this->assertTrue(method_exists($category, 'set_locked'));
441        $this->assertTrue($category->set_locked(1));
442    }
443
444    function test_grade_category_is_hidden() {
445        $category = new grade_category($this->grade_categories[0]);
446        $this->assertTrue(method_exists($category, 'is_hidden'));
447        $category->load_grade_item();
448        $this->assertEqual($category->is_hidden(), $category->grade_item->is_hidden());
449    }
450
451    function test_grade_category_set_hidden() {
452        $category = new grade_category($this->grade_categories[0]);
453        $this->assertTrue(method_exists($category, 'set_hidden'));
454        $category->set_hidden(1);
455        $category->load_grade_item();
456        $this->assertEqual(true, $category->grade_item->is_hidden());
457    }
458
459    function generate_random_raw_grade($item, $userid) {
460        $grade = new grade_grade();
461        $grade->itemid = $item->id;
462        $grade->userid = $userid;
463        $grade->grademin = 0;
464        $grade->grademax = 1;
465        $valuetype = "grade$item->gradetype";
466        $grade->rawgrade = rand(0, 1000) / 1000;
467        $grade->insert();
468        return $grade->rawgrade;
469    }
470}
471?>