MDL-36061 - remove master-only unit tests
[moodle.git] / lib / grade / tests / grade_item_test.php
CommitLineData
948d46da
PS
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 * @package core_grades
19 * @category phpunit
20 * @copyright nicolas@moodle.com
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24defined('MOODLE_INTERNAL') || die();
25
26require_once(__DIR__.'/fixtures/lib.php');
27
28class grade_item_testcase extends grade_base_testcase {
29 public function test_grade_item() {
30 $this->sub_test_grade_item_construct();
31 $this->sub_test_grade_item_insert();
32 $this->sub_test_grade_item_delete();
33 $this->sub_test_grade_item_update();
34 $this->sub_test_grade_item_load_scale();
35 $this->sub_test_grade_item_load_outcome();
36 $this->sub_test_grade_item_qualifies_for_regrading();
37 $this->sub_test_grade_item_force_regrading();
38 $this->sub_test_grade_item_fetch();
39 $this->sub_test_grade_item_fetch_all();
40 $this->sub_test_grade_item_get_all_finals();
41 $this->sub_test_grade_item_get_final();
42 $this->sub_test_grade_item_get_sortorder();
43 $this->sub_test_grade_item_set_sortorder();
44 $this->sub_test_grade_item_move_after_sortorder();
45 $this->sub_test_grade_item_get_name();
46 $this->sub_test_grade_item_set_parent();
47 $this->sub_test_grade_item_get_parent_category();
48 $this->sub_test_grade_item_load_parent_category();
49 $this->sub_test_grade_item_get_item_category();
50 $this->sub_test_grade_item_load_item_category();
51 $this->sub_test_grade_item_regrade_final_grades();
52 $this->sub_test_grade_item_adjust_raw_grade();
53 $this->sub_test_grade_item_set_locked();
54 $this->sub_test_grade_item_is_locked();
55 $this->sub_test_grade_item_set_hidden();
56 $this->sub_test_grade_item_is_hidden();
57 $this->sub_test_grade_item_is_category_item();
58 $this->sub_test_grade_item_is_course_item();
59 $this->sub_test_grade_item_fetch_course_item();
60 $this->sub_test_grade_item_depends_on();
168a9411 61 $this->sub_test_refresh_grades();
948d46da
PS
62 $this->sub_test_grade_item_is_calculated();
63 $this->sub_test_grade_item_set_calculation();
64 $this->sub_test_grade_item_get_calculation();
65 $this->sub_test_grade_item_compute();
66 }
67
68 protected function sub_test_grade_item_construct() {
69 $params = new stdClass();
70
71 $params->courseid = $this->courseid;
72 $params->categoryid = $this->grade_categories[1]->id;
73 $params->itemname = 'unittestgradeitem4';
74 $params->itemtype = 'mod';
75 $params->itemmodule = 'database';
76 $params->iteminfo = 'Grade item used for unit testing';
77
78 $grade_item = new grade_item($params, false);
79
80 $this->assertEquals($params->courseid, $grade_item->courseid);
81 $this->assertEquals($params->categoryid, $grade_item->categoryid);
82 $this->assertEquals($params->itemmodule, $grade_item->itemmodule);
83 }
84
85 protected function sub_test_grade_item_insert() {
86 $grade_item = new grade_item();
87 $this->assertTrue(method_exists($grade_item, 'insert'));
88
89 $grade_item->courseid = $this->courseid;
90 $grade_item->categoryid = $this->grade_categories[1]->id;
91 $grade_item->itemname = 'unittestgradeitem4';
92 $grade_item->itemtype = 'mod';
93 $grade_item->itemmodule = 'quiz';
94 $grade_item->iteminfo = 'Grade item used for unit testing';
95
96 $grade_item->insert();
97
98 $last_grade_item = end($this->grade_items);
99
100 $this->assertEquals($grade_item->id, $last_grade_item->id + 1);
101 $this->assertEquals(11, $grade_item->sortorder);
102
103 //keep our reference collection the same as what is in the database
104 $this->grade_items[] = $grade_item;
105 }
106
107 protected function sub_test_grade_item_delete() {
108 global $DB;
bd42a265 109 $grade_item = new grade_item($this->grade_items[7],false);//use a grade item not touched by previous (or future) tests
948d46da
PS
110 $this->assertTrue(method_exists($grade_item, 'delete'));
111
112 $this->assertTrue($grade_item->delete());
113
114 $this->assertFalse($DB->get_record('grade_items', array('id' => $grade_item->id)));
115
116 //keep our reference collection the same as the database
117 unset($this->grade_items[7]);
118 }
119
120 protected function sub_test_grade_item_update() {
121 global $DB;
bd42a265 122 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
123 $this->assertTrue(method_exists($grade_item, 'update'));
124
125 $grade_item->iteminfo = 'Updated info for this unittest grade_item';
126
127 $this->assertTrue($grade_item->update());
128
129 $grade_item->grademin = 14;
130 $this->assertTrue($grade_item->qualifies_for_regrading());
131 $this->assertTrue($grade_item->update());
132
133 $iteminfo = $DB->get_field('grade_items', 'iteminfo', array('id' => $this->grade_items[0]->id));
134 $this->assertEquals($grade_item->iteminfo, $iteminfo);
135 }
136
137 protected function sub_test_grade_item_load_scale() {
bd42a265 138 $grade_item = new grade_item($this->grade_items[2], false);
948d46da
PS
139 $this->assertTrue(method_exists($grade_item, 'load_scale'));
140 $scale = $grade_item->load_scale();
141 $this->assertFalse(empty($grade_item->scale));
142 $this->assertEquals($scale->id, $this->grade_items[2]->scaleid);
143 }
144
145 protected function sub_test_grade_item_load_outcome() {
bd42a265 146 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
147 $this->assertTrue(method_exists($grade_item, 'load_outcome'));
148 //TODO: add tests
149 }
150
151 protected function sub_test_grade_item_qualifies_for_regrading() {
bd42a265 152 $grade_item = new grade_item($this->grade_items[3], false);//use a grade item not touched by previous tests
948d46da
PS
153 $this->assertTrue(method_exists($grade_item, 'qualifies_for_regrading'));
154
155 $this->assertFalse($grade_item->qualifies_for_regrading());
156
157 $grade_item->iteminfo = 'Updated info for this unittest grade_item';
158
159 $this->assertFalse($grade_item->qualifies_for_regrading());
160
161 $grade_item->grademin = 14;
162
163 $this->assertTrue($grade_item->qualifies_for_regrading());
164 }
165
166 protected function sub_test_grade_item_force_regrading() {
bd42a265 167 $grade_item = new grade_item($this->grade_items[3], false);//use a grade item not touched by previous tests
948d46da
PS
168 $this->assertTrue(method_exists($grade_item, 'force_regrading'));
169
170 $this->assertEquals(0, $grade_item->needsupdate);
171
172 $grade_item->force_regrading();
173 $this->assertEquals(1, $grade_item->needsupdate);
174 $grade_item->update_from_db();
175 $this->assertEquals(1, $grade_item->needsupdate);
176 }
177
178 protected function sub_test_grade_item_fetch() {
179 $grade_item = new grade_item();
180 $this->assertTrue(method_exists($grade_item, 'fetch'));
181
182 //not using $this->grade_items[0] as it's iteminfo was modified by sub_test_grade_item_qualifies_for_regrading()
183 $grade_item = grade_item::fetch(array('id'=>$this->grade_items[1]->id));
184 $this->assertEquals($this->grade_items[1]->id, $grade_item->id);
185 $this->assertEquals($this->grade_items[1]->iteminfo, $grade_item->iteminfo);
186
187 $grade_item = grade_item::fetch(array('itemtype'=>$this->grade_items[1]->itemtype, 'itemmodule'=>$this->grade_items[1]->itemmodule));
188 $this->assertEquals($this->grade_items[1]->id, $grade_item->id);
189 $this->assertEquals($this->grade_items[1]->iteminfo, $grade_item->iteminfo);
190 }
191
192 protected function sub_test_grade_item_fetch_all() {
193 $grade_item = new grade_item();
194 $this->assertTrue(method_exists($grade_item, 'fetch_all'));
195
196 $grade_items = grade_item::fetch_all(array('courseid'=>$this->courseid));
197 $this->assertEquals(count($this->grade_items), count($grade_items)-1);//-1 to account for the course grade item
198 }
199
200 // Retrieve all final scores for a given grade_item.
201 protected function sub_test_grade_item_get_all_finals() {
bd42a265 202 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
203 $this->assertTrue(method_exists($grade_item, 'get_final'));
204
205 $final_grades = $grade_item->get_final();
206 $this->assertEquals(3, count($final_grades));
207 }
208
209
210 // Retrieve all final scores for a specific userid.
211 protected function sub_test_grade_item_get_final() {
bd42a265 212 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
213 $this->assertTrue(method_exists($grade_item, 'get_final'));
214 $final_grade = $grade_item->get_final($this->user[1]->id);
215 $this->assertEquals($this->grade_grades[0]->finalgrade, $final_grade->finalgrade);
216 }
217
218 protected function sub_test_grade_item_get_sortorder() {
bd42a265 219 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
220 $this->assertTrue(method_exists($grade_item, 'get_sortorder'));
221 $sortorder = $grade_item->get_sortorder();
222 $this->assertEquals($this->grade_items[0]->sortorder, $sortorder);
223 }
224
225 protected function sub_test_grade_item_set_sortorder() {
bd42a265 226 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
227 $this->assertTrue(method_exists($grade_item, 'set_sortorder'));
228 $grade_item->set_sortorder(999);
229 $this->assertEquals($grade_item->sortorder, 999);
230 }
231
232 protected function sub_test_grade_item_move_after_sortorder() {
bd42a265 233 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
234 $this->assertTrue(method_exists($grade_item, 'move_after_sortorder'));
235 $grade_item->move_after_sortorder(5);
236 $this->assertEquals($grade_item->sortorder, 6);
237
238 $grade_item = grade_item::fetch(array('id'=>$this->grade_items[0]->id));
239 $this->assertEquals($grade_item->sortorder, 6);
240
241 $after = grade_item::fetch(array('id'=>$this->grade_items[6]->id));
242 $this->assertEquals($after->sortorder, 8);
243 }
244
245 protected function sub_test_grade_item_get_name() {
bd42a265 246 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
247 $this->assertTrue(method_exists($grade_item, 'get_name'));
248
249 $name = $grade_item->get_name();
250 $this->assertEquals($this->grade_items[0]->itemname, $name);
251 }
252
253 protected function sub_test_grade_item_set_parent() {
bd42a265 254 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
255 $this->assertTrue(method_exists($grade_item, 'set_parent'));
256
257 $old = $grade_item->get_parent_category();
bd42a265 258 $new = new grade_category($this->grade_categories[3], false);
948d46da
PS
259 $new_item = $new->get_grade_item();
260
261 $this->assertTrue($grade_item->set_parent($new->id));
262
263 $new_item->update_from_db();
264 $grade_item->update_from_db();
265
266 $this->assertEquals($grade_item->categoryid, $new->id);
267 }
268
269 protected function sub_test_grade_item_get_parent_category() {
bd42a265 270 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
271 $this->assertTrue(method_exists($grade_item, 'get_parent_category'));
272
273 $category = $grade_item->get_parent_category();
274 $this->assertEquals($this->grade_categories[1]->fullname, $category->fullname);
275 }
276
277 protected function sub_test_grade_item_load_parent_category() {
bd42a265 278 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
279 $this->assertTrue(method_exists($grade_item, 'load_parent_category'));
280
281 $category = $grade_item->load_parent_category();
282 $this->assertEquals($this->grade_categories[1]->fullname, $category->fullname);
283 $this->assertEquals($this->grade_categories[1]->fullname, $grade_item->parent_category->fullname);
284 }
285
286 protected function sub_test_grade_item_get_item_category() {
bd42a265 287 $grade_item = new grade_item($this->grade_items[3], false);
948d46da
PS
288 $this->assertTrue(method_exists($grade_item, 'get_item_category'));
289
290 $category = $grade_item->get_item_category();
291 $this->assertEquals($this->grade_categories[0]->fullname, $category->fullname);
292 }
293
294 protected function sub_test_grade_item_load_item_category() {
bd42a265 295 $grade_item = new grade_item($this->grade_items[3], false);
948d46da
PS
296 $this->assertTrue(method_exists($grade_item, 'load_item_category'));
297
298 $category = $grade_item->load_item_category();
299 $this->assertEquals($this->grade_categories[0]->fullname, $category->fullname);
300 $this->assertEquals($this->grade_categories[0]->fullname, $grade_item->item_category->fullname);
301 }
302
303 // Test update of all final grades
304 protected function sub_test_grade_item_regrade_final_grades() {
bd42a265 305 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
306 $this->assertTrue(method_exists($grade_item, 'regrade_final_grades'));
307 $this->assertEquals(true, $grade_item->regrade_final_grades());
308 //TODO: add more tests
309 }
310
311 // Test the adjust_raw_grade method
312 protected function sub_test_grade_item_adjust_raw_grade() {
bd42a265 313 $grade_item = new grade_item($this->grade_items[2], false); // anything but assignment module!
948d46da
PS
314 $this->assertTrue(method_exists($grade_item, 'adjust_raw_grade'));
315
316 $grade_raw = new stdClass();
317 $grade_raw->rawgrade = 40;
318 $grade_raw->grademax = 100;
319 $grade_raw->grademin = 0;
320
321 $grade_item->gradetype = GRADE_TYPE_VALUE;
322 $grade_item->multfactor = 1;
323 $grade_item->plusfactor = 0;
324 $grade_item->grademax = 50;
325 $grade_item->grademin = 0;
326
327 $original_grade_raw = clone($grade_raw);
328 $original_grade_item = clone($grade_item);
329
330 $this->assertEquals(20, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
331
332 // Try a larger maximum grade
333 $grade_item->grademax = 150;
334 $grade_item->grademin = 0;
335 $this->assertEquals(60, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
336
337 // Try larger minimum grade
338 $grade_item->grademin = 50;
339
340 $this->assertEquals(90, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
341
342 // Rescaling from a small scale (0-50) to a larger scale (0-100)
343 $grade_raw->grademax = 50;
344 $grade_raw->grademin = 0;
345 $grade_item->grademax = 100;
346 $grade_item->grademin = 0;
347
348 $this->assertEquals(80, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
349
350 // Rescaling from a small scale (0-50) to a larger scale with offset (40-100)
351 $grade_item->grademax = 100;
352 $grade_item->grademin = 40;
353
354 $this->assertEquals(88, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
355
356 // Try multfactor and plusfactor
357 $grade_raw = clone($original_grade_raw);
358 $grade_item = clone($original_grade_item);
359 $grade_item->multfactor = 1.23;
360 $grade_item->plusfactor = 3;
361
362 $this->assertEquals(27.6, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
363
364 // Try multfactor below 0 and a negative plusfactor
365 $grade_raw = clone($original_grade_raw);
366 $grade_item = clone($original_grade_item);
367 $grade_item->multfactor = 0.23;
368 $grade_item->plusfactor = -3;
369
370 $this->assertEquals(round(1.6), round($grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax)));
371 }
372
373 // Test locking of grade items
374 protected function sub_test_grade_item_set_locked() {
375 //getting a grade_item from the DB as set_locked() will fail if the grade items needs to be updated
376 //also needs to have at least one grade_grade or $grade_item->get_final(1) returns null
377 //$grade_item = new grade_item($this->grade_items[8]);
378 $grade_item = grade_item::fetch(array('id'=>$this->grade_items[8]->id));
379
380 $this->assertTrue(method_exists($grade_item, 'set_locked'));
381
bd42a265 382 $grade_grade = new grade_grade($grade_item->get_final($this->user[1]->id), false);
948d46da
PS
383 $this->assertTrue(empty($grade_item->locked));//not locked
384 $this->assertTrue(empty($grade_grade->locked));//not locked
385
386 $this->assertTrue($grade_item->set_locked(true, true, false));
bd42a265 387 $grade_grade = new grade_grade($grade_item->get_final($this->user[1]->id), false);
948d46da
PS
388
389 $this->assertFalse(empty($grade_item->locked));//locked
390 $this->assertFalse(empty($grade_grade->locked)); // individual grades should be locked too
391
392 $this->assertTrue($grade_item->set_locked(false, true, false));
bd42a265 393 $grade = new grade_grade($grade_item->get_final($this->user[1]->id), false);
948d46da
PS
394
395 $this->assertTrue(empty($grade_item->locked));
396 $this->assertTrue(empty($grade->locked)); // individual grades should be unlocked too
397 }
398
399 protected function sub_test_grade_item_is_locked() {
bd42a265 400 $grade_item = new grade_item($this->grade_items[10], false);
948d46da
PS
401 $this->assertTrue(method_exists($grade_item, 'is_locked'));
402
403 $this->assertFalse($grade_item->is_locked());
404 $this->assertFalse($grade_item->is_locked($this->user[1]->id));
405 $this->assertTrue($grade_item->set_locked(true, true, false));
406 $this->assertTrue($grade_item->is_locked());
407 $this->assertTrue($grade_item->is_locked($this->user[1]->id));
408 }
409
410 // Test hiding of grade items
411 protected function sub_test_grade_item_set_hidden() {
bd42a265 412 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
413 $this->assertTrue(method_exists($grade_item, 'set_hidden'));
414
bd42a265 415 $grade = new grade_grade($grade_item->get_final($this->user[1]->id), false);
948d46da
PS
416 $this->assertEquals(0, $grade_item->hidden);
417 $this->assertEquals(0, $grade->hidden);
418
419 $grade_item->set_hidden(666, true);
bd42a265 420 $grade = new grade_grade($grade_item->get_final($this->user[1]->id), false);
948d46da
PS
421
422 $this->assertEquals(666, $grade_item->hidden);
423 $this->assertEquals(666, $grade->hidden);
424 }
425
426 protected function sub_test_grade_item_is_hidden() {
bd42a265 427 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
428 $this->assertTrue(method_exists($grade_item, 'is_hidden'));
429
430 $this->assertFalse($grade_item->is_hidden());
431 $this->assertFalse($grade_item->is_hidden(1));
432
433 $grade_item->set_hidden(1);
434 $this->assertTrue($grade_item->is_hidden());
435 $this->assertTrue($grade_item->is_hidden(1));
436
437 $grade_item->set_hidden(666);
438 $this->assertFalse($grade_item->is_hidden());
439 $this->assertFalse($grade_item->is_hidden(1));
440
441 $grade_item->set_hidden(time()+666);
442 $this->assertTrue($grade_item->is_hidden());
443 $this->assertTrue($grade_item->is_hidden(1));
444 }
445
446 protected function sub_test_grade_item_is_category_item() {
bd42a265 447 $grade_item = new grade_item($this->grade_items[3], false);
948d46da
PS
448 $this->assertTrue(method_exists($grade_item, 'is_category_item'));
449 $this->assertTrue($grade_item->is_category_item());
450 }
451
452 protected function sub_test_grade_item_is_course_item() {
453 $grade_item = grade_item::fetch_course_item($this->courseid);
454 $this->assertTrue(method_exists($grade_item, 'is_course_item'));
455 $this->assertTrue($grade_item->is_course_item());
456 }
457
458 protected function sub_test_grade_item_fetch_course_item() {
459 $grade_item = grade_item::fetch_course_item($this->courseid);
460 $this->assertTrue(method_exists($grade_item, 'fetch_course_item'));
461 $this->assertEquals($grade_item->itemtype, 'course');
462 }
463
464 protected function sub_test_grade_item_depends_on() {
bd42a265 465 $grade_item = new grade_item($this->grade_items[1], false);
948d46da
PS
466
467 // calculated grade dependency
468 $deps = $grade_item->depends_on();
469 sort($deps, SORT_NUMERIC); // for comparison
470 $this->assertEquals(array($this->grade_items[0]->id), $deps);
471
472 // simulate depends on returns none when locked
473 $grade_item->locked = time();
474 $grade_item->update();
475 $deps = $grade_item->depends_on();
476 sort($deps, SORT_NUMERIC); // for comparison
477 $this->assertEquals(array(), $deps);
478
479 // category dependency
bd42a265 480 $grade_item = new grade_item($this->grade_items[3], false);
948d46da
PS
481 $deps = $grade_item->depends_on();
482 sort($deps, SORT_NUMERIC); // for comparison
483 $res = array($this->grade_items[4]->id, $this->grade_items[5]->id);
484 $this->assertEquals($res, $deps);
485 }
486
168a9411
AD
487 protected function sub_test_refresh_grades() {
488 // Testing with the grade item for a mod_assignment instance.
489 $grade_item = new grade_item($this->grade_items[0], false);
490 $this->assertTrue(method_exists($grade_item, 'refresh_grades'));
491 $this->assertTrue($grade_item->refresh_grades());
168a9411
AD
492 }
493
948d46da 494 protected function sub_test_grade_item_is_calculated() {
bd42a265 495 $grade_item = new grade_item($this->grade_items[1], false);
948d46da
PS
496 $this->assertTrue(method_exists($grade_item, 'is_calculated'));
497 $this->assertTrue($grade_item->is_calculated());
498
bd42a265 499 $grade_item = new grade_item($this->grade_items[0], false);
948d46da
PS
500 $this->assertFalse($grade_item->is_calculated());
501 }
502
503 protected function sub_test_grade_item_set_calculation() {
bd42a265 504 $grade_item = new grade_item($this->grade_items[1], false);
948d46da 505 $this->assertTrue(method_exists($grade_item, 'set_calculation'));
bd42a265 506 $grade_itemsource = new grade_item($this->grade_items[0], false);
948d46da
PS
507
508 $grade_item->set_calculation('=[['.$grade_itemsource->idnumber.']]');
509
510 $this->assertTrue(!empty($grade_item->needsupdate));
511 $this->assertEquals('=##gi'.$grade_itemsource->id.'##', $grade_item->calculation);
512 }
513
514 protected function sub_test_grade_item_get_calculation() {
bd42a265 515 $grade_item = new grade_item($this->grade_items[1], false);
948d46da 516 $this->assertTrue(method_exists($grade_item, 'get_calculation'));
bd42a265 517 $grade_itemsource = new grade_item($this->grade_items[0], false);
948d46da
PS
518
519 $denormalizedformula = str_replace('##gi'.$grade_itemsource->id.'##', '[['.$grade_itemsource->idnumber.']]', $this->grade_items[1]->calculation);
520
521 $formula = $grade_item->get_calculation();
522 $this->assertTrue(!empty($grade_item->needsupdate));
523 $this->assertEquals($denormalizedformula, $formula);
524 }
525
526 public function sub_test_grade_item_compute() {
527 $grade_item = grade_item::fetch(array('id'=>$this->grade_items[1]->id));
528 $this->assertTrue(method_exists($grade_item, 'compute'));
529
530 //check the grade_grades in the array match those in the DB then delete $this->grade_items[1]'s grade_grades
531 $this->grade_grades[3] = grade_grade::fetch(array('id'=>$this->grade_grades[3]->id));
532 $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[3]->id));
533 $grade_grade->delete();
534
535 $this->grade_grades[4] = grade_grade::fetch(array('id'=>$this->grade_grades[4]->id));
536 $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[4]->id));
537 $grade_grade->delete();
538
539 $this->grade_grades[5] = grade_grade::fetch(array('id'=>$this->grade_grades[5]->id));
540 $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[5]->id));
541 $grade_grade->delete();
542
543 //recalculate the grades (its a calculation so pulls values from other grade_items) and reinsert them
544 $grade_item->compute();
545
546 $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[3]->userid, 'itemid'=>$this->grade_grades[3]->itemid));
547 $this->assertEquals($this->grade_grades[3]->finalgrade, $grade_grade->finalgrade);
548
549 $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[4]->userid, 'itemid'=>$this->grade_grades[4]->itemid));
550 $this->assertEquals($this->grade_grades[4]->finalgrade, $grade_grade->finalgrade);
551
552 $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[5]->userid, 'itemid'=>$this->grade_grades[5]->itemid));
553 $this->assertEquals($this->grade_grades[5]->finalgrade, $grade_grade->finalgrade);
554 }
555}