From 168a941104ede7540c5ace198784c033eb8f5b65 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Tue, 6 Nov 2012 11:02:18 +0800 Subject: [PATCH] MDL-36061 core_grade: added some unit tests related to refresh_grades() --- lib/grade/grade_item.php | 11 ++++-- lib/grade/tests/grade_item_test.php | 13 +++++++ lib/gradelib.php | 2 +- lib/tests/gradelib_test.php | 59 +++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 lib/tests/gradelib_test.php diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 6ce395f3f02..8378b78c1f4 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -1423,30 +1423,33 @@ class grade_item extends grade_object { * Refetch grades from modules, plugins. * * @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) { global $DB; if ($this->itemtype == 'mod') { if ($this->is_outcome_item()) { //nothing to do - return; + return true; } if (!$activity = $DB->get_record($this->itemmodule, array('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)) { debugging('Can not find course module'); - return; + return false; } $activity->modname = $this->itemmodule; $activity->cmidnumber = $cm->idnumber; - grade_update_mod_grades($activity, $userid); + return grade_update_mod_grades($activity, $userid); } + + return true; } /** diff --git a/lib/grade/tests/grade_item_test.php b/lib/grade/tests/grade_item_test.php index dab82c27ad6..35d527b4fcc 100644 --- a/lib/grade/tests/grade_item_test.php +++ b/lib/grade/tests/grade_item_test.php @@ -58,6 +58,7 @@ class grade_item_testcase extends grade_base_testcase { $this->sub_test_grade_item_is_course_item(); $this->sub_test_grade_item_fetch_course_item(); $this->sub_test_grade_item_depends_on(); + $this->sub_test_refresh_grades(); $this->sub_test_grade_item_is_calculated(); $this->sub_test_grade_item_set_calculation(); $this->sub_test_grade_item_get_calculation(); @@ -483,6 +484,18 @@ class grade_item_testcase extends grade_base_testcase { $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() { $grade_item = new grade_item($this->grade_items[1], false); $this->assertTrue(method_exists($grade_item, 'is_calculated')); diff --git a/lib/gradelib.php b/lib/gradelib.php index 8de67cf103d..c59bd1c2915 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -1190,7 +1190,7 @@ function grade_update_mod_grades($modinstance, $userid=0) { $updategradesfunc($modinstance, $userid); } else { - // mudule does not support grading?? + // Module does not support grading? } return true; diff --git a/lib/tests/gradelib_test.php b/lib/tests/gradelib_test.php new file mode 100644 index 00000000000..14173cfeeff --- /dev/null +++ b/lib/tests/gradelib_test.php @@ -0,0 +1,59 @@ +. + +/** + * 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)); + } +} -- 2.43.0