MDL-40059 glossary: unittests for events

This commit is contained in:
Marina Glancy 2014-02-24 15:40:30 +08:00
parent 7c92f9be80
commit 2d4d742f71

View File

@ -35,6 +35,10 @@ defined('MOODLE_INTERNAL') || die();
*/
class glossary_event_testcase extends advanced_testcase {
public function setUp() {
$this->resetAfterTest();
}
/**
* Test comment_created event.
*/
@ -42,8 +46,6 @@ class glossary_event_testcase extends advanced_testcase {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
$this->resetAfterTest();
// Create a record for adding comment.
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
@ -53,7 +55,7 @@ class glossary_event_testcase extends advanced_testcase {
$entry = $glossarygenerator->create_content($glossary);
$context = context_module::instance($glossary->cmid);
$cm = get_coursemodule_from_instance('data', $glossary->id, $course->id);
$cm = get_coursemodule_from_instance('glossary', $glossary->id, $course->id);
$cmt = new stdClass();
$cmt->component = 'mod_glossary';
$cmt->context = $context;
@ -86,8 +88,6 @@ class glossary_event_testcase extends advanced_testcase {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
$this->resetAfterTest();
// Create a record for deleting comment.
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
@ -97,7 +97,7 @@ class glossary_event_testcase extends advanced_testcase {
$entry = $glossarygenerator->create_content($glossary);
$context = context_module::instance($glossary->cmid);
$cm = get_coursemodule_from_instance('data', $glossary->id, $course->id);
$cm = get_coursemodule_from_instance('glossary', $glossary->id, $course->id);
$cmt = new stdClass();
$cmt->component = 'mod_glossary';
$cmt->context = $context;
@ -123,4 +123,393 @@ class glossary_event_testcase extends advanced_testcase {
$this->assertEquals($url, $event->get_url());
$this->assertEventContextNotUsed($event);
}
public function test_course_module_viewed() {
global $DB;
// There is no proper API to call to trigger this event, so what we are
// doing here is simply making sure that the events returns the right information.
$course = $this->getDataGenerator()->create_course();
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
$dbcourse = $DB->get_record('course', array('id' => $course->id));
$dbglossary = $DB->get_record('glossary', array('id' => $glossary->id));
$context = context_module::instance($glossary->cmid);
$mode = 'letter';
$event = \mod_glossary\event\course_module_viewed::create(array(
'objectid' => $dbglossary->id,
'context' => $context,
'other' => array('mode' => $mode)
));
$event->add_record_snapshot('course', $dbcourse);
$event->add_record_snapshot('glossary', $dbglossary);
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\course_module_viewed', $event);
$this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
$this->assertEquals($glossary->cmid, $event->contextinstanceid);
$this->assertEquals($glossary->id, $event->objectid);
$expected = array($course->id, 'glossary', 'view', 'view.php?id=' . $glossary->cmid . '&tab=-1',
$glossary->id, $glossary->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEquals(new moodle_url('/mod/glossary/view.php', array('id' => $glossary->cmid, 'mode' => $mode)), $event->get_url());
$this->assertEventContextNotUsed($event);
}
public function test_course_module_instance_list_viewed() {
// There is no proper API to call to trigger this event, so what we are
// doing here is simply making sure that the events returns the right information.
$course = $this->getDataGenerator()->create_course();
$event = \mod_glossary\event\course_module_instance_list_viewed::create(array(
'context' => context_course::instance($course->id)
));
// Triggering and capturing the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\course_module_instance_list_viewed', $event);
$this->assertEquals(CONTEXT_COURSE, $event->contextlevel);
$this->assertEquals($course->id, $event->contextinstanceid);
$expected = array($course->id, 'glossary', 'view all', 'index.php?id='.$course->id, '');
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
public function test_entry_created() {
// There is no proper API to call to trigger this event, so what we are
// doing here is simply making sure that the events returns the right information.
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
$context = context_module::instance($glossary->cmid);
$glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
$entry = $glossarygenerator->create_content($glossary);
$eventparams = array(
'context' => $context,
'objectid' => $entry->id,
'other' => array('concept' => $entry->concept)
);
$event = \mod_glossary\event\entry_created::create($eventparams);
$event->add_record_snapshot('glossary_entries', $entry);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\entry_created', $event);
$this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
$this->assertEquals($glossary->cmid, $event->contextinstanceid);
$expected = array($course->id, "glossary", "add entry",
"view.php?id={$glossary->cmid}&mode=entry&hook={$entry->id}", $entry->id, $glossary->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
public function test_entry_updated() {
// There is no proper API to call to trigger this event, so what we are
// doing here is simply making sure that the events returns the right information.
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
$context = context_module::instance($glossary->cmid);
$glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
$entry = $glossarygenerator->create_content($glossary);
$eventparams = array(
'context' => $context,
'objectid' => $entry->id,
'other' => array('concept' => $entry->concept)
);
$event = \mod_glossary\event\entry_updated::create($eventparams);
$event->add_record_snapshot('glossary_entries', $entry);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\entry_updated', $event);
$this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
$this->assertEquals($glossary->cmid, $event->contextinstanceid);
$expected = array($course->id, "glossary", "update entry",
"view.php?id={$glossary->cmid}&mode=entry&hook={$entry->id}", $entry->id, $glossary->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
public function test_entry_deleted() {
global $DB;
// There is no proper API to call to trigger this event, so what we are
// doing here is simply making sure that the events returns the right information.
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
$context = context_module::instance($glossary->cmid);
$prevmode = 'view';
$hook = 'ALL';
$glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
$entry = $glossarygenerator->create_content($glossary);
$DB->delete_records('glossary_entries', array('id' => $entry->id));
$eventparams = array(
'context' => $context,
'objectid' => $entry->id,
'other' => array(
'mode' => $prevmode,
'hook' => $hook,
'concept' => $entry->concept
)
);
$event = \mod_glossary\event\entry_deleted::create($eventparams);
$event->add_record_snapshot('glossary_entries', $entry);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\entry_deleted', $event);
$this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
$this->assertEquals($glossary->cmid, $event->contextinstanceid);
$expected = array($course->id, "glossary", "delete entry",
"view.php?id={$glossary->cmid}&mode={$prevmode}&hook={$hook}", $entry->id, $glossary->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
public function test_category_created() {
global $DB;
// There is no proper API to call to trigger this event, so what we are
// doing here is simply making sure that the events returns the right information.
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
$context = context_module::instance($glossary->cmid);
// Create category and trigger event.
$category = new stdClass();
$category->name = 'New category';
$category->usedynalink = 0;
$category->id = $DB->insert_record('glossary_categories', $category);
$event = \mod_glossary\event\category_created::create(array(
'context' => $context,
'objectid' => $category->id
));
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\category_created', $event);
$this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
$this->assertEquals($glossary->cmid, $event->contextinstanceid);
//add_to_log($course->id, "glossary", "add category", "editcategories.php?id=$cm->id", $cat->id,$cm->id);
$expected = array($course->id, "glossary", "add category",
"editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
// Update category and trigger event.
$category->name = 'Updated category';
$DB->update_record('glossary_categories', $category);
$event = \mod_glossary\event\category_updated::create(array(
'context' => $context,
'objectid' => $category->id
));
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\category_updated', $event);
$this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
$this->assertEquals($glossary->cmid, $event->contextinstanceid);
//add_to_log($course->id, "glossary", "edit category", "editcategories.php?id=$cm->id", $hook,$cm->id);
$expected = array($course->id, "glossary", "edit category",
"editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
// Delete category and trigger event.
$category = $DB->get_record('glossary_categories', array('id' => $category->id));
$DB->delete_records('glossary_categories', array('id' => $category->id));
$event = \mod_glossary\event\category_deleted::create(array(
'context' => $context,
'objectid' => $category->id
));
$event->add_record_snapshot('glossary_categories', $category);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\category_deleted', $event);
$this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
$this->assertEquals($glossary->cmid, $event->contextinstanceid);
//add_to_log($course->id, "glossary", "delete category", "editcategories.php?id=$cm->id", $hook,$cm->id);
$expected = array($course->id, "glossary", "delete category",
"editcategories.php?id={$glossary->cmid}", $category->id, $glossary->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
public function test_entry_approved() {
global $DB;
// There is no proper API to call to trigger this event, so what we are
// doing here is simply making sure that the events returns the right information.
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$student = $this->getDataGenerator()->create_user();
$rolestudent = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $rolestudent->id);
$teacher = $this->getDataGenerator()->create_user();
$roleteacher = $DB->get_record('role', array('shortname' => 'teacher'));
$this->getDataGenerator()->enrol_user($teacher->id, $course->id, $roleteacher->id);
$this->setUser($teacher);
$glossary = $this->getDataGenerator()->create_module('glossary',
array('course' => $course, 'defaultapproval' => 0));
$context = context_module::instance($glossary->cmid);
$this->setUser($student);
$glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
$entry = $glossarygenerator->create_content($glossary);
$this->assertEquals(0, $entry->approved);
// Approve entry, trigger and validate event.
$this->setUser($teacher);
$newentry = new stdClass();
$newentry->id = $entry->id;
$newentry->approved = true;
$newentry->timemodified = time();
$DB->update_record("glossary_entries", $newentry);
$params = array(
'context' => $context,
'objectid' => $entry->id
);
$event = \mod_glossary\event\entry_approved::create($params);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\entry_approved', $event);
$this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
$this->assertEquals($glossary->cmid, $event->contextinstanceid);
$expected = array($course->id, "glossary", "approve entry",
"showentry.php?id={$glossary->cmid}&eid={$entry->id}", $entry->id, $glossary->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
// Disapprove entry, trigger and validate event.
$this->setUser($teacher);
$newentry = new stdClass();
$newentry->id = $entry->id;
$newentry->approved = false;
$newentry->timemodified = time();
$DB->update_record("glossary_entries", $newentry);
$params = array(
'context' => $context,
'objectid' => $entry->id
);
$event = \mod_glossary\event\entry_disapproved::create($params);
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\entry_disapproved', $event);
$this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
$this->assertEquals($glossary->cmid, $event->contextinstanceid);
$expected = array($course->id, "glossary", "disapprove entry",
"showentry.php?id={$glossary->cmid}&eid={$entry->id}", $entry->id, $glossary->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
public function test_entry_viewed() {
// There is no proper API to call to trigger this event, so what we are
// doing here is simply making sure that the events returns the right information.
$this->setAdminUser();
$course = $this->getDataGenerator()->create_course();
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course));
$context = context_module::instance($glossary->cmid);
$glossarygenerator = $this->getDataGenerator()->get_plugin_generator('mod_glossary');
$entry = $glossarygenerator->create_content($glossary);
$event = \mod_glossary\event\entry_viewed::create(array(
'objectid' => $entry->id,
'context' => $context
));
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$this->assertCount(1, $events);
$event = reset($events);
// Checking that the event contains the expected values.
$this->assertInstanceOf('\mod_glossary\event\entry_viewed', $event);
$this->assertEquals(CONTEXT_MODULE, $event->contextlevel);
$this->assertEquals($glossary->cmid, $event->contextinstanceid);
$expected = array($course->id, "glossary", "view entry",
"showentry.php?eid={$entry->id}", $entry->id, $glossary->cmid);
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}