Skip to content

Commit

Permalink
MDL-36061 core_grade: added some unit tests related to refresh_grades()
Browse files Browse the repository at this point in the history
  • Loading branch information
andyjdavis committed Nov 15, 2012
1 parent 76ab64b commit 168a941
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lib/grade/grade_item.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1423,30 +1423,33 @@ public function depends_on($reset_cache=false) {
* Refetch grades from modules, plugins. * Refetch grades from modules, plugins.
* *
* @param int $userid optional, limit the refetch to a single user * @param int $userid optional, limit the refetch to a single user
* @return bool Returns true on success or if there is nothing to do
*/ */
public function refresh_grades($userid=0) { public function refresh_grades($userid=0) {
global $DB; global $DB;
if ($this->itemtype == 'mod') { if ($this->itemtype == 'mod') {
if ($this->is_outcome_item()) { if ($this->is_outcome_item()) {
//nothing to do //nothing to do
return; return true;
} }


if (!$activity = $DB->get_record($this->itemmodule, array('id' => $this->iteminstance))) { if (!$activity = $DB->get_record($this->itemmodule, array('id' => $this->iteminstance))) {
debugging("Can not find $this->itemmodule activity with id $this->iteminstance"); debugging("Can not find $this->itemmodule activity with id $this->iteminstance");
return; return false;
} }


if (!$cm = get_coursemodule_from_instance($this->itemmodule, $activity->id, $this->courseid)) { if (!$cm = get_coursemodule_from_instance($this->itemmodule, $activity->id, $this->courseid)) {
debugging('Can not find course module'); debugging('Can not find course module');
return; return false;
} }


$activity->modname = $this->itemmodule; $activity->modname = $this->itemmodule;
$activity->cmidnumber = $cm->idnumber; $activity->cmidnumber = $cm->idnumber;


grade_update_mod_grades($activity, $userid); return grade_update_mod_grades($activity, $userid);
} }

return true;
} }


/** /**
Expand Down
13 changes: 13 additions & 0 deletions lib/grade/tests/grade_item_test.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public function test_grade_item() {
$this->sub_test_grade_item_is_course_item(); $this->sub_test_grade_item_is_course_item();
$this->sub_test_grade_item_fetch_course_item(); $this->sub_test_grade_item_fetch_course_item();
$this->sub_test_grade_item_depends_on(); $this->sub_test_grade_item_depends_on();
$this->sub_test_refresh_grades();
$this->sub_test_grade_item_is_calculated(); $this->sub_test_grade_item_is_calculated();
$this->sub_test_grade_item_set_calculation(); $this->sub_test_grade_item_set_calculation();
$this->sub_test_grade_item_get_calculation(); $this->sub_test_grade_item_get_calculation();
Expand Down Expand Up @@ -483,6 +484,18 @@ protected function sub_test_grade_item_depends_on() {
$this->assertEquals($res, $deps); $this->assertEquals($res, $deps);
} }


protected function sub_test_refresh_grades() {
// Testing with the grade item for a mod_assignment instance.
$grade_item = new grade_item($this->grade_items[0], false);
$this->assertTrue(method_exists($grade_item, 'refresh_grades'));
$this->assertTrue($grade_item->refresh_grades());

// Break the grade item and check error handling.
$grade_item->iteminstance = 123456789;
$this->assertFalse($grade_item->refresh_grades());
$this->assertDebuggingCalled();
}

protected function sub_test_grade_item_is_calculated() { protected function sub_test_grade_item_is_calculated() {
$grade_item = new grade_item($this->grade_items[1], false); $grade_item = new grade_item($this->grade_items[1], false);
$this->assertTrue(method_exists($grade_item, 'is_calculated')); $this->assertTrue(method_exists($grade_item, 'is_calculated'));
Expand Down
2 changes: 1 addition & 1 deletion lib/gradelib.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ function grade_update_mod_grades($modinstance, $userid=0) {
$updategradesfunc($modinstance, $userid); $updategradesfunc($modinstance, $userid);


} else { } else {
// mudule does not support grading?? // Module does not support grading?
} }


return true; return true;
Expand Down
59 changes: 59 additions & 0 deletions lib/tests/gradelib_test.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for /lib/gradelib.php.
*
* @package core_grade
* @category phpunit
* @copyright 2012 Andrew Davis
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->libdir . '/gradelib.php');

class gradelib_testcase extends advanced_testcase {

public function test_grade_update_mod_grades() {

$this->resetAfterTest(true);

// Create a broken module instance.
$modinstance = new stdClass();
$modinstance->modname = 'doesntexist';

$this->assertFalse(grade_update_mod_grades($modinstance));
// A debug message should have been generated.
$this->assertDebuggingCalled();

// Create a course and instance of mod_assign.
$course = $this->getDataGenerator()->create_course();

$assigndata['course'] = $course->id;
$assigndata['name'] = 'lightwork assignment';
$modinstance = self::getDataGenerator()->create_module('assign', $assigndata);

// grade_update_mod_grades() requires 2 additional properties, cmidnumber and modname.
$cm = get_coursemodule_from_instance('assign', $modinstance->id, 0, false, MUST_EXIST);
$modinstance->cmidnumber = $cm->id;
$modinstance->modname = 'assign';

$this->assertTrue(grade_update_mod_grades($modinstance));
}
}

0 comments on commit 168a941

Please sign in to comment.