Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

MDL-36061 core_grade: added some unit tests related to refresh_grades()

  • Loading branch information...
commit 69bcca5e9c9f3c0bd3dcb40073fb4202a678887c 1 parent 0a757cf
Andrew Davis andyjdavis authored
11 lib/grade/grade_item.php
View
@@ -1423,30 +1423,33 @@ public function depends_on($reset_cache=false) {
* 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;
}
/**
13 lib/grade/tests/grade_item_test.php
View
@@ -58,6 +58,7 @@ public function test_grade_item() {
$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 @@ protected function sub_test_grade_item_depends_on() {
$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'));
2  lib/gradelib.php
View
@@ -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;
59 lib/tests/gradelib_test.php
View
@@ -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));
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.