Skip to content
Permalink
Browse files

MDL-39959 events: Add unit tests for course module events

test_course_module_created_event()
test_course_module_created_event_exceptions()
test_course_module_updated_event()
test_course_module_updated_event_exceptions()
test_course_module_deleted_event()
test_course_module_deleted_event_exceptions()
  • Loading branch information...
ankitagarwal committed Sep 16, 2013
1 parent 0158650 commit b9b994c7d2fcaf3aa23b0f286d5f419f1cda0055
Showing with 296 additions and 0 deletions.
  1. +296 −0 course/tests/courselib_test.php
@@ -317,6 +317,7 @@ private function create_specific_module_test($modulename) {
// Test specific to the module.
$modulerunasserts = $modulename.'_create_run_asserts';
$this->$modulerunasserts($moduleinfo, $dbmodinstance);
return $moduleinfo;
}
/**
@@ -569,6 +570,7 @@ private function update_specific_module_test($modulename) {
// Test specific to the module.
$modulerunasserts = $modulename.'_update_run_asserts';
$this->$modulerunasserts($moduleinfo, $dbmodinstance);
return $moduleinfo;
}
@@ -1887,4 +1889,298 @@ public function test_course_integrity_check() {
$this->assertEquals($section0->id, $cms[$quiz->cmid]->section);
$this->assertEquals(2, count($cms));
}
/**
* Tests for event related to course module creation.
*/
public function test_course_module_created_event() {
global $USER, $DB;
$this->resetAfterTest();
// Create an assign module.
$sink = $this->redirectEvents();
$modinfo = $this->create_specific_module_test('assign');
$events = $sink->get_events();
$event = array_pop($events);
$sink->close();
$cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
$mod = $DB->get_record('assign', array('id' => $modinfo->instance), '*', MUST_EXIST);
// Validate event data.
$this->assertInstanceOf('\core\event\course_module_created', $event);
$this->assertEquals($cm->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals('course_modules', $event->objecttable);
$url = new moodle_url('/mod/assign/view.php', array('id' => $mod->id));
$this->assertEquals($url, $event->get_url());
// Test legacy data.
$this->assertSame('mod_created', $event->get_legacy_eventname());
$eventdata = new stdClass();
$eventdata->modulename = 'assign';
$eventdata->name = $mod->name;
$eventdata->cmid = $cm->id;
$eventdata->courseid = $cm->course;
$eventdata->userid = $USER->id;
$this->assertEventLegacyData($eventdata, $event);
$arr = array($cm->course, "course", "add mod", "../mod/assign/view.php?id=$mod->id", "assign $cm->instance");
$this->assertEventLegacyLogData($arr, $event);
}
/**
* Tests for event validations related to course module creation.
*/
public function test_course_module_created_event_exceptions() {
$this->resetAfterTest();
// Generate data.
$modinfo = $this->create_specific_module_test('assign');
$context = context_module::instance($modinfo->coursemodule);
// Test not setting instanceid.
try {
$event = \core\event\course_module_created::create(array(
'courseid' => $modinfo->course,
'context' => $context,
'objectid' => $modinfo->coursemodule,
'other' => array(
'modulename' => 'assign',
'name' => 'My assignment',
)
));
$this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
other['instanceid']");
} catch (coding_exception $e) {
$this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
}
// Test not setting modulename.
try {
$event = \core\event\course_module_created::create(array(
'courseid' => $modinfo->course,
'context' => $context,
'objectid' => $modinfo->coursemodule,
'other' => array(
'instanceid' => $modinfo->instance,
'name' => 'My assignment',
)
));
$this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
other['modulename']");
} catch (coding_exception $e) {
$this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
}
// Test not setting name.
try {
$event = \core\event\course_module_created::create(array(
'courseid' => $modinfo->course,
'context' => $context,
'objectid' => $modinfo->coursemodule,
'other' => array(
'modulename' => 'assign',
'instanceid' => $modinfo->instance,
)
));
$this->fail("Event validation should not allow \\core\\event\\course_module_created to be triggered without
other['name']");
} catch (coding_exception $e) {
$this->assertContains("Field other['name'] cannot be empty", $e->getMessage());
}
}
/**
* Tests for event related to course module updates.
*/
public function test_course_module_updated_event() {
global $USER, $DB;
$this->resetAfterTest();
// Update a forum module.
$sink = $this->redirectEvents();
$modinfo = $this->update_specific_module_test('forum');
$events = $sink->get_events();
$event = array_pop($events);
$sink->close();
$cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
$mod = $DB->get_record('forum', array('id' => $cm->instance), '*', MUST_EXIST);
// Validate event data.
$this->assertInstanceOf('\core\event\course_module_updated', $event);
$this->assertEquals($cm->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals('course_modules', $event->objecttable);
$url = new moodle_url('/mod/forum/view.php', array('id' => $mod->id));
$this->assertEquals($url, $event->get_url());
// Test legacy data.
$this->assertSame('mod_updated', $event->get_legacy_eventname());
$eventdata = new stdClass();
$eventdata->modulename = 'forum';
$eventdata->name = $mod->name;
$eventdata->cmid = $cm->id;
$eventdata->courseid = $cm->course;
$eventdata->userid = $USER->id;
$this->assertEventLegacyData($eventdata, $event);
$arr = array($cm->course, "course", "update mod", "../mod/forum/view.php?id=$mod->id", "forum $cm->instance");
$this->assertEventLegacyLogData($arr, $event);
}
/**
* Tests for event validations related to course module update.
*/
public function test_course_module_updated_event_exceptions() {
$this->resetAfterTest();
// Generate data.
$modinfo = $this->create_specific_module_test('assign');
$context = context_module::instance($modinfo->coursemodule);
// Test not setting instanceid.
try {
$event = \core\event\course_module_updated::create(array(
'courseid' => $modinfo->course,
'context' => $context,
'objectid' => $modinfo->coursemodule,
'other' => array(
'modulename' => 'assign',
'name' => 'My assignment',
)
));
$this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
other['instanceid']");
} catch (coding_exception $e) {
$this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
}
// Test not setting modulename.
try {
$event = \core\event\course_module_updated::create(array(
'courseid' => $modinfo->course,
'context' => $context,
'objectid' => $modinfo->coursemodule,
'other' => array(
'instanceid' => $modinfo->instance,
'name' => 'My assignment',
)
));
$this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
other['modulename']");
} catch (coding_exception $e) {
$this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
}
// Test not setting name.
try {
$event = \core\event\course_module_updated::create(array(
'courseid' => $modinfo->course,
'context' => $context,
'objectid' => $modinfo->coursemodule,
'other' => array(
'modulename' => 'assign',
'instanceid' => $modinfo->instance,
)
));
$this->fail("Event validation should not allow \\core\\event\\course_module_updated to be triggered without
other['name']");
} catch (coding_exception $e) {
$this->assertContains("Field other['name'] cannot be empty", $e->getMessage());
}
}
/**
* Tests for event related to course module delete.
*/
public function test_course_module_deleted_event() {
global $USER, $DB;
$this->resetAfterTest();
// Create and delete a module.
$sink = $this->redirectEvents();
$modinfo = $this->create_specific_module_test('forum');
$cm = $DB->get_record('course_modules', array('id' => $modinfo->coursemodule), '*', MUST_EXIST);
course_delete_module($modinfo->coursemodule);
$events = $sink->get_events();
$event = array_pop($events); // delete module event.;
$sink->close();
// Validate event data.
$this->assertInstanceOf('\core\event\course_module_deleted', $event);
$this->assertEquals($cm->id, $event->objectid);
$this->assertEquals($USER->id, $event->userid);
$this->assertEquals('course_modules', $event->objecttable);
$this->assertEquals(null, $event->get_url());
$this->assertEquals($cm, $event->get_record_snapshot('course_modules', $cm->id));
// Test legacy data.
$this->assertSame('mod_deleted', $event->get_legacy_eventname());
$eventdata = new stdClass();
$eventdata->modulename = 'forum';
$eventdata->cmid = $cm->id;
$eventdata->courseid = $cm->course;
$eventdata->userid = $USER->id;
$this->assertEventLegacyData($eventdata, $event);
$arr = array($cm->course, 'course', "delete mod", "view.php?id=$cm->course", "forum $cm->instance", $cm->id);
$this->assertEventLegacyLogData($arr, $event);
}
/**
* Tests for event validations related to course module deletion.
*/
public function test_course_module_deleted_event_exceptions() {
$this->resetAfterTest();
// Generate data.
$modinfo = $this->create_specific_module_test('assign');
$context = context_module::instance($modinfo->coursemodule);
// Test not setting instanceid.
try {
$event = \core\event\course_module_deleted::create(array(
'courseid' => $modinfo->course,
'context' => $context,
'objectid' => $modinfo->coursemodule,
'other' => array(
'modulename' => 'assign',
'name' => 'My assignment',
)
));
$this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
other['instanceid']");
} catch (coding_exception $e) {
$this->assertContains("Field other['instanceid'] cannot be empty", $e->getMessage());
}
// Test not setting modulename.
try {
$event = \core\event\course_module_deleted::create(array(
'courseid' => $modinfo->course,
'context' => $context,
'objectid' => $modinfo->coursemodule,
'other' => array(
'instanceid' => $modinfo->instance,
'name' => 'My assignment',
)
));
$this->fail("Event validation should not allow \\core\\event\\course_module_deleted to be triggered without
other['modulename']");
} catch (coding_exception $e) {
$this->assertContains("Field other['modulename'] cannot be empty", $e->getMessage());
}
}
}

0 comments on commit b9b994c

Please sign in to comment.
You can’t perform that action at this time.