From d85fb0c6e3397933872213ab75982f2a2ebdfc46 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Fri, 24 Jan 2014 09:59:40 +0800 Subject: [PATCH] MDL-40921 mod_workshop: Unit tests for new events system. --- mod/workshop/tests/events_test.php | 317 +++++++++++++++++++++++++++ mod/workshop/tests/locallib_test.php | 27 ++- 2 files changed, 335 insertions(+), 9 deletions(-) create mode 100644 mod/workshop/tests/events_test.php diff --git a/mod/workshop/tests/events_test.php b/mod/workshop/tests/events_test.php new file mode 100644 index 00000000000..139f2a97e28 --- /dev/null +++ b/mod/workshop/tests/events_test.php @@ -0,0 +1,317 @@ +. + +/** + * Unit tests for workshop events. + * + * @package mod_workshop + * @category phpunit + * @copyright 2013 Adrian Greeve + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/mod/workshop/lib.php'); // Include the code to test +require_once($CFG->dirroot . '/mod/workshop/locallib.php'); // Include the code to test +require_once($CFG->dirroot . '/lib/cronlib.php'); // Include the code to test + + +/** + * Test cases for the internal workshop api + */ +class mod_workshop_events_testcase extends advanced_testcase { + + /** $workshop Basic workshop data stored in an object. */ + protected $workshop; + /** $course Generated Random Course. */ + protected $course; + /** $context Course module context. */ + protected $context; + + /** + * Set up the testing environment. + */ + protected function setUp() { + parent::setUp(); + $this->setAdminUser(); + + // Create a workshop activity. + $this->course = $this->getDataGenerator()->create_course(); + $this->workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $this->course)); + $this->context = context_module::instance($this->workshop->id); + } + + protected function tearDown() { + $this->workshop = null; + parent::tearDown(); + } + + /** + * This event is triggered in view.php and workshop/lib.php through the function workshop_cron(). + */ + function test_phase_switched_event() { + $this->resetAfterTest(); + $this->setAdminUser(); + + // Add additional workshop information. + $this->workshop->phase = 20; + $this->workshop->phaseswitchassessment = 1; + $this->workshop->submissionend = time() - 1; + + $event = \mod_workshop\event\phase_switched::create(array( + 'objectid' => $this->workshop->id, + 'context' => $this->context, + 'courseid' => $this->course->id, + 'other' => array('workshopphase' => $this->workshop->phase) + )); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the legacy log data is valid. + $expected = array($this->course->id, 'workshop', 'update switch phase', 'view.php?id=' . $this->workshop->id, + $this->workshop->phase, $this->workshop->id); + $this->assertEventLegacyLogData($expected, $event); + + $sink->close(); + } + + function test_assessment_evaluated() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST); + + $workshop = new testable_workshop($this->workshop, $cm, $this->course); + + $assessments = array(); + $assessments[] = (object)array('reviewerid'=>2, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>null, + 'aggregatedgrade'=>12); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $workshop->aggregate_grading_grades_process($assessments); + $events = $sink->get_events(); + $event = reset($events); + + $this->assertInstanceOf('\mod_workshop\event\assessment_evaluated', $event); + $this->assertEquals('workshop_aggregations', $event->objecttable); + $this->assertEquals(context_module::instance($workshop->id), $event->get_context()); + + $sink->close(); + } + + function test_assessment_reevaluated() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $cm = get_coursemodule_from_instance('workshop', $this->workshop->id, $this->course->id, false, MUST_EXIST); + + $workshop = new testable_workshop($this->workshop, $cm, $this->course); + + $assessments = array(); + $assessments[] = (object)array('reviewerid'=>2, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>2, + 'aggregatedgrade'=>12); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $workshop->aggregate_grading_grades_process($assessments); + $events = $sink->get_events(); + $event = reset($events); + + $this->assertInstanceOf('\mod_workshop\event\assessment_reevaluated', $event); + $this->assertEquals('workshop_aggregations', $event->objecttable); + $this->assertEquals(context_module::instance($workshop->id), $event->get_context()); + $expected = array($this->course->id, 'workshop', 'update aggregate grade', 'view.php?id=' . $event->get_context()->instanceid, + $event->objectid, $event->get_context()->instanceid); + $this->assertEventLegacyLogData($expected, $event); + + $sink->close(); + } + + /** + * There is no api involved so the best we can do is test legacy data by triggering event manually. + */ + function test_aggregate_grades_reset_event() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $event = \mod_workshop\event\assessment_evaluations_reset::create(array( + 'objectid' => $this->workshop->id, + 'context' => $this->context, + 'courseid' => $this->course->id, + )); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the legacy log data is valid. + $expected = array($this->course->id, 'workshop', 'update clear aggregated grade', 'view.php?id=' . $this->workshop->id, + $this->workshop->id, $this->workshop->id); + $this->assertEventLegacyLogData($expected, $event); + + $sink->close(); + } + + /** + * There is no api involved so the best we can do is test legacy data by triggering event manually. + */ + function test_instances_list_viewed_event() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $context = context_course::instance($this->course->id); + + $event = \mod_workshop\event\instances_list_viewed::create(array('context' => $context)); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the legacy log data is valid. + $expected = array($this->course->id, 'workshop', 'view all', 'index.php?id=' . $this->course->id, ''); + $this->assertEventLegacyLogData($expected, $event); + + $sink->close(); + } + + /** + * There is no api involved so the best we can do is test legacy data by triggering event manually. + */ + function test_submission_created_event() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $user = $this->getDataGenerator()->create_user(); + $submissionid = 48; + + $event = \mod_workshop\event\submission_created::create(array( + 'objectid' => $submissionid, + 'context' => $this->context, + 'courseid' => $this->course->id, + 'relateduserid' => $user->id, + 'other' => array( + 'workshopid' => $this->workshop->id + ) + ) + ); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the legacy log data is valid. + $expected = array($this->course->id, 'workshop', 'add submission', + 'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->workshop->id); + $this->assertEventLegacyLogData($expected, $event); + + $sink->close(); + } + + /** + * There is no api involved so the best we can do is test legacy data by triggering event manually. + */ + function test_submission_updated_event() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $user = $this->getDataGenerator()->create_user(); + $submissionid = 48; + + $event = \mod_workshop\event\submission_updated::create(array( + 'objectid' => $submissionid, + 'context' => $this->context, + 'courseid' => $this->course->id, + 'relateduserid' => $user->id, + 'other' => array( + 'workshopid' => $this->workshop->id + ) + ) + ); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the legacy log data is valid. + $expected = array($this->course->id, 'workshop', 'update submission', + 'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->workshop->id); + $this->assertEventLegacyLogData($expected, $event); + + $sink->close(); + } + + /** + * There is no api involved so the best we can do is test legacy data by triggering event manually. + */ + function test_submission_viewed_event() { + $this->resetAfterTest(); + $this->setAdminUser(); + + $user = $this->getDataGenerator()->create_user(); + $submissionid = 48; + + $event = \mod_workshop\event\submission_viewed::create(array( + 'objectid' => $submissionid, + 'context' => $this->context, + 'courseid' => $this->course->id, + 'relateduserid' => $user->id, + 'other' => array( + 'workshopid' => $this->workshop->id + ) + ) + ); + + // Trigger and capture the event. + $sink = $this->redirectEvents(); + $event->trigger(); + $events = $sink->get_events(); + $event = reset($events); + + // Check that the legacy log data is valid. + $expected = array($this->course->id, 'workshop', 'view submission', + 'submission.php?cmid=' . $this->workshop->id . '&id=' . $submissionid, $submissionid, $this->workshop->id); + $this->assertEventLegacyLogData($expected, $event); + + $sink->close(); + } + +} + +/** + * Test subclass that makes all the protected methods we want to test public. + */ +class testable_workshop extends workshop { + + public function aggregate_grading_grades_process(array $assessments, $timegraded = null) { + parent::aggregate_grading_grades_process($assessments, $timegraded); + } + +} diff --git a/mod/workshop/tests/locallib_test.php b/mod/workshop/tests/locallib_test.php index a6321ed98bb..480316bf0fe 100644 --- a/mod/workshop/tests/locallib_test.php +++ b/mod/workshop/tests/locallib_test.php @@ -40,8 +40,11 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { /** setup testing environment */ protected function setUp() { parent::setUp(); - - $this->workshop = new testable_workshop(); + $this->setAdminUser(); + $course = $this->getDataGenerator()->create_course(); + $workshop = $this->getDataGenerator()->create_module('workshop', array('course' => $course)); + $cm = get_coursemodule_from_instance('workshop', $workshop->id, $course->id, false, MUST_EXIST); + $this->workshop = new testable_workshop($workshop, $cm, $course); } protected function tearDown() { @@ -50,6 +53,8 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { } public function test_aggregate_submission_grades_process_notgraded() { + $this->resetAfterTest(true); + // fixture set-up $batch = array(); // batch of a submission's assessments $batch[] = (object)array('submissionid' => 12, 'submissiongrade' => null, 'weight' => 1, 'grade' => null); @@ -126,6 +131,8 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { } public function test_aggregate_submission_grades_process_mean_nochange() { + $this->resetAfterTest(true); + // fixture set-up $batch = array(); // batch of a submission's assessments $batch[] = (object)array('submissionid' => 45, 'submissiongrade' => 19.67750, 'weight' => 1, 'grade' => 56.12000); @@ -167,6 +174,7 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { } public function test_aggregate_grading_grades_process_nograding() { + $this->resetAfterTest(true); // fixture set-up $batch = array(); $batch[] = (object)array('reviewerid'=>2, 'gradinggrade'=>null, 'gradinggradeover'=>null, 'aggregationid'=>null, 'aggregatedgrade'=>null); @@ -205,6 +213,7 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { } public function test_aggregate_grading_grades_process_single_grade_uptodate() { + $this->resetAfterTest(true); // fixture set-up $batch = array(); $batch[] = (object)array('reviewerid'=>3, 'gradinggrade'=>90.00000, 'gradinggradeover'=>null, 'aggregationid'=>1, 'aggregatedgrade'=>90.00000); @@ -297,6 +306,7 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { } public function test_percent_to_value() { + $this->resetAfterTest(true); // fixture setup $total = 185; $percent = 56.6543; @@ -307,6 +317,7 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { } public function test_percent_to_value_negative() { + $this->resetAfterTest(true); // fixture setup $total = 185; $percent = -7.098; @@ -317,6 +328,7 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { } public function test_percent_to_value_over_hundred() { + $this->resetAfterTest(true); // fixture setup $total = 185; $percent = 121.08; @@ -327,6 +339,7 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { } public function test_lcm() { + $this->resetAfterTest(true); // fixture setup + exercise SUT + verify in one step $this->assertEquals(workshop::lcm(1,4), 4); $this->assertEquals(workshop::lcm(2,4), 4); @@ -336,6 +349,7 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { } public function test_lcm_array() { + $this->resetAfterTest(true); // fixture setup $numbers = array(5,3,15); // excersise SUT @@ -345,6 +359,7 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { } public function test_prepare_example_assessment() { + $this->resetAfterTest(true); // fixture setup $fakerawrecord = (object)array( 'id' => 42, @@ -374,6 +389,7 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { public function test_prepare_example_reference_assessment() { global $USER; + $this->resetAfterTest(true); // fixture setup $fakerawrecord = (object)array( 'id' => 38, @@ -407,13 +423,6 @@ class mod_workshop_internal_api_testcase extends advanced_testcase { */ class testable_workshop extends workshop { - public function __construct() { - $this->id = 16; - $this->cm = new stdclass(); - $this->course = new stdclass(); - $this->context = new stdclass(); - } - public function aggregate_submission_grades_process(array $assessments) { parent::aggregate_submission_grades_process($assessments); }