Skip to content

Commit

Permalink
MDL-40697 core: added more unit tests for the user_graded event
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Oct 8, 2014
1 parent 2e0b349 commit b310c9a
Showing 1 changed file with 134 additions and 5 deletions.
139 changes: 134 additions & 5 deletions lib/tests/event_user_graded_test.php
Expand Up @@ -15,7 +15,7 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Tests for base course module viewed event.
* Tests the \core\event\user_graded event.
*
* @package core
* @category phpunit
Expand All @@ -25,26 +25,36 @@

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

global $CFG;

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

/**
* Class core_event_user_graded_testcase
*
* Tests for event \core\event\user_graded
*
* @package core
* @category phpunit
* @category test
* @copyright 2014 Petr Skoda
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_event_user_graded_testcase extends advanced_testcase {

/**
* Tests set up.
*/
public function setUp() {
$this->resetAfterTest();
}

/**
* Test the event.
* Tests the event details.
*/
public function test_event() {
global $CFG;
require_once("$CFG->libdir/gradelib.php");

$this->resetAfterTest();

$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
Expand Down Expand Up @@ -86,4 +96,123 @@ public function test_event() {
$this->assertInstanceOf('grade_grade', $grade);
$this->assertEquals($grade_grade->id, $grade->id);
}

/**
* Tests that the event is fired in the correct locations in core.
*/
public function test_event_is_triggered() {
global $DB;

// Create the items we need to test with.
$course = $this->getDataGenerator()->create_course();
$user = $this->getDataGenerator()->create_user();
$this->getDataGenerator()->enrol_user($user->id, $course->id);
$quiz = $this->getDataGenerator()->create_module('quiz', array('course' => $course->id));

// Now mark the quiz using grade_update as this is the function that modules use.
$grade = array();
$grade['userid'] = $user->id;
$grade['rawgrade'] = 50;

$sink = $this->redirectEvents();
grade_update('mod/quiz', $course->id, 'mod', 'quiz', $quiz->id, 0, $grade);
$events = $sink->get_events();
$event = reset($events);
$sink->close();

// Ensure we have a user_graded event.
$this->assertEquals(1, count($events));
$this->assertInstanceOf('\core\event\user_graded', $event);

// Get the grade item.
$gradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'quiz', 'iteminstance' => $quiz->id,
'courseid' => $course->id));

// Let's alter the grade in the DB so when we call regrade_final_grades() it is changed and an event is called.
$sql = "UPDATE {grade_grades}
SET finalgrade = '2'
WHERE itemid = :itemid
AND userid = :userid";
$DB->execute($sql, array('itemid' => $gradeitem->id, 'userid' => $user->id));

// Now check when we regrade this that there is a user graded event.
$sink = $this->redirectEvents();
$gradeitem->regrade_final_grades();
$events = $sink->get_events();
$event = reset($events);
$sink->close();

// Ensure we have a user_graded event.
$this->assertEquals(1, count($events));
$this->assertInstanceOf('\core\event\user_graded', $event);

// Remove the grades.
$gradeitem->delete_all_grades();

// Now, create a grade using update_raw_grade().
$sink = $this->redirectEvents();
$gradeitem->update_raw_grade($user->id, 50);
$events = $sink->get_events();
$event = reset($events);
$sink->close();

// Ensure we have a user_graded event.
$this->assertEquals(1, count($events));
$this->assertInstanceOf('\core\event\user_graded', $event);

// Now, update this grade using update_raw_grade().
$sink = $this->redirectEvents();
$gradeitem->update_raw_grade($user->id, 100);
$events = $sink->get_events();
$event = reset($events);
$sink->close();

$this->assertEquals(1, count($events));
$this->assertInstanceOf('\core\event\user_graded', $event);

// Remove the grades.
$gradeitem->delete_all_grades();

// Now, create a grade using update_final_grade().
$sink = $this->redirectEvents();
$gradeitem->update_final_grade($user->id, 50);
$events = $sink->get_events();
$event = reset($events);
$sink->close();

// Ensure we have a user_graded event.
$this->assertEquals(1, count($events));
$this->assertInstanceOf('\core\event\user_graded', $event);

// Now, update this grade using update_final_grade().
$sink = $this->redirectEvents();
$gradeitem->update_final_grade($user->id, 100);
$events = $sink->get_events();
$event = reset($events);
$sink->close();

$this->assertEquals(1, count($events));
$this->assertInstanceOf('\core\event\user_graded', $event);

// Let's change the calculation to anything that won't cause an error.
$calculation = calc_formula::unlocalize("=3");
$gradeitem->set_calculation($calculation);

// Let's alter the grade in the DB so when we call compute() it is changed and an event is called.
$sql = "UPDATE {grade_grades}
SET finalgrade = 2, overridden = 0
WHERE itemid = :itemid
AND userid = :userid";
$DB->execute($sql, array('itemid' => $gradeitem->id, 'userid' => $user->id));

// Now check when we compute that there is a user graded event.
$sink = $this->redirectEvents();
$gradeitem->compute();
$events = $sink->get_events();
$event = reset($events);
$sink->close();

$this->assertEquals(1, count($events));
$this->assertInstanceOf('\core\event\user_graded', $event);
}
}

0 comments on commit b310c9a

Please sign in to comment.