From a9ef9c5e82211d7734a9972c9ef7ae3780c15ccd Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Tue, 18 Aug 2015 11:28:24 +0800 Subject: [PATCH] MDL-30315 grades: Unit tests for grade import. --- grade/tests/importlib_test.php | 208 +++++++++++++++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 grade/tests/importlib_test.php diff --git a/grade/tests/importlib_test.php b/grade/tests/importlib_test.php new file mode 100644 index 00000000000..0acea9e6dd9 --- /dev/null +++ b/grade/tests/importlib_test.php @@ -0,0 +1,208 @@ +. + +/** + * Unit tests for grade/import/lib.php. + * + * @package core_grade + * @category phpunit + * @copyright 2015 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU Public License + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/grade/import/lib.php'); + +/** + * Tests grade_import_lib functions. + */ +class core_grade_import_lib_test extends advanced_testcase { + + /** + * Import grades into 'grade_import_values' table. This is done differently in the various import plugins, + * so there is no direct API to call. + * + * @param array $data Information to be inserted into the table. + * @return int The insert ID of the sql statement. + */ + private function import_grades($data) { + global $DB, $USER; + $graderecord = new stdClass(); + $graderecord->importcode = $data['importcode']; + if (isset($data['itemid'])) { + $graderecord->itemid = $data['itemid']; + } + $graderecord->userid = $data['userid']; + if (isset($data['importer'])) { + $graderecord->importer = $data['importer']; + } else { + $graderecord->importer = $USER->id; + } + if (isset($data['finalgrade'])) { + $graderecord->finalgrade = $data['finalgrade']; + } else { + $graderecord->finalgrade = rand(0, 100); + } + if (isset($data['feedback'])) { + $graderecord->feedback = $data['feedback']; + } + if (isset($data['importonlyfeedback'])) { + $graderecord->importonlyfeedback = $data['importonlyfeedback']; + } else { + $graderecord->importonlyfeedback = false; + } + if (isset($data['newgradeitem'])) { + $graderecord->newgradeitem = $data['newgradeitem']; + } + return $DB->insert_record('grade_import_values', $graderecord); + } + + /** + * Tests for importing grades from an external source. + */ + public function test_grade_import_commit() { + global $USER, $DB, $CFG; + $this->resetAfterTest(); + + $importcode = get_new_importcode(); + $user1 = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + + $course = $this->getDataGenerator()->create_course(); + $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); + $itemname = $assign->name; + $modulecontext = context_module::instance($assign->cmid); + // The generator returns a dummy object, lets get the real assign object. + $assign = new assign($modulecontext, false, false); + $cm = $assign->get_course_module(); + + // Enrol users in the course. + $this->getDataGenerator()->enrol_user($user1->id, $course->id); + $this->getDataGenerator()->enrol_user($user2->id, $course->id); + + // Enter a new grade into an existing grade item. + $gradeitem = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod')); + + // Keep this value around for a test further down. + $originalgrade = 55; + $this->import_grades(array( + 'importcode' => $importcode, + 'itemid' => $gradeitem->id, + 'userid' => $user1->id, + 'finalgrade' => $originalgrade + )); + + $status = grade_import_commit($course->id, $importcode, false, false); + $this->assertTrue($status); + + // Get imported grade_grade. + $gradegrade = grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user1->id)); + $this->assertEquals($originalgrade, $gradegrade->finalgrade); + // Overriden field will be a timestamp and will evaluate out to true. + $this->assertTrue($gradegrade->is_overridden()); + + // Create a new grade item and import into that. + $importcode = get_new_importcode(); + $record = new stdClass(); + $record->itemname = 'New grade item'; + $record->importcode = $importcode; + $record->importer = $USER->id; + $insertid = $DB->insert_record('grade_import_newitem', $record); + + $finalgrade = 75; + $this->import_grades(array( + 'importcode' => $importcode, + 'userid' => $user1->id, + 'finalgrade' => $finalgrade, + 'newgradeitem' => $insertid + )); + + $status = grade_import_commit($course->id, $importcode, false, false); + $this->assertTrue($status); + // Check that we have a new grade_item. + $gradeitem = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'manual')); + $this->assertEquals($record->itemname, $gradeitem->itemname); + // Grades were imported. + $gradegrade = grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user1->id)); + $this->assertEquals($finalgrade, $gradegrade->finalgrade); + // As this is a new item the grade has not been overridden. + $this->assertFalse($gradegrade->is_overridden()); + + // Import feedback only. + $importcode = get_new_importcode(); + $gradeitem = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod')); + + $originalfeedback = 'feedback can be useful'; + $this->import_grades(array( + 'importcode' => $importcode, + 'userid' => $user1->id, + 'itemid' => $gradeitem->id, + 'feedback' => $originalfeedback, + 'importonlyfeedback' => true + )); + + $status = grade_import_commit($course->id, $importcode, true, false); + $this->assertTrue($status); + $gradegrade = grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user1->id)); + // The final grade should be the same as the first record further up. We are only altering the feedback. + $this->assertEquals($originalgrade, $gradegrade->finalgrade); + $this->assertTrue($gradegrade->is_overridden()); + + // Import grades only. + $importcode = get_new_importcode(); + $gradeitem = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod')); + + $finalgrade = 60; + $this->import_grades(array( + 'importcode' => $importcode, + 'userid' => $user1->id, + 'itemid' => $gradeitem->id, + 'finalgrade' => $finalgrade, + 'feedback' => 'feedback can still be useful' + )); + + $status = grade_import_commit($course->id, $importcode, false, false); + $this->assertTrue($status); + $gradegrade = grade_grade::fetch(array('itemid' => $gradeitem->id, 'userid' => $user1->id)); + $this->assertEquals($finalgrade, $gradegrade->finalgrade); + // The final feedback should not have changed. + $this->assertEquals($originalfeedback, $gradegrade->feedback); + $this->assertTrue($gradegrade->is_overridden()); + + // Check that printing of import status is correct. + $importcode = get_new_importcode(); + $gradeitem = grade_item::fetch(array('courseid' => $course->id, 'itemtype' => 'mod')); + + $this->import_grades(array( + 'importcode' => $importcode, + 'userid' => $user1->id, + 'itemid' => $gradeitem->id + )); + + $url = $CFG->wwwroot . '/grade/index.php'; + $expectedresponse = "++ Grade import success ++ +
id\" />
"; + + ob_start(); + $status = grade_import_commit($course->id, $importcode); + $output = ob_get_contents(); + ob_end_clean(); + $this->assertTrue($status); + $this->assertEquals($expectedresponse, $output); + } +}