forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-44017 Events: Added unit test for report_viewed events
- Loading branch information
Rajesh Taneja
committed
Mar 7, 2014
1 parent
e1b16f9
commit 2cd29dd
Showing
5 changed files
with
156 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,137 +15,88 @@ | |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Tests for notes events. | ||
* Tests for report log events. | ||
* | ||
* @package core_notes | ||
* @copyright 2013 Ankit Agarwal | ||
* @package report_log | ||
* @copyright 2014 Rajesh Taneja <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
/** | ||
* Class core_notes_events_testcase | ||
* Class report_log_events_testcase | ||
* | ||
* Class for tests related to notes events. | ||
* Class for tests related to log events. | ||
* | ||
* @package core_notes | ||
* @copyright 2013 Ankit Agarwal | ||
* @package report_log | ||
* @copyright 2014 Rajesh Taneja <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. | ||
*/ | ||
class core_notes_events_testcase extends advanced_testcase { | ||
|
||
/** @var stdClass A note object. */ | ||
private $eventnote; | ||
|
||
/** @var stdClass A complete record from post table */ | ||
private $noterecord; | ||
class report_log_events_testcase extends advanced_testcase { | ||
|
||
/** | ||
* Setup testcase. | ||
*/ | ||
public function setUp() { | ||
global $DB; | ||
|
||
$this->resetAfterTest(); | ||
$this->setAdminUser(); | ||
|
||
$course = $this->getDataGenerator()->create_course(); | ||
$user = $this->getDataGenerator()->create_user(); | ||
$gen = $this->getDataGenerator()->get_plugin_generator('core_notes'); | ||
$this->eventnote = $gen->create_instance(array('courseid' => $course->id, 'userid' => $user->id)); | ||
// Get the full record, note_load doesn't return everything. | ||
$this->noterecord = $DB->get_record('post', array('id' => $this->eventnote->id), '*', MUST_EXIST); | ||
|
||
$this->resetAfterTest(); | ||
} | ||
|
||
/** | ||
* Tests for event note_deleted. | ||
* Test the report viewed event. | ||
* | ||
* It's not possible to use the moodle API to simulate the viewing of log report, so here we | ||
* simply create the event and trigger it. | ||
*/ | ||
public function test_note_deleted_event() { | ||
// Delete a note. | ||
$sink = $this->redirectEvents(); | ||
note_delete($this->eventnote); | ||
$events = $sink->get_events(); | ||
$event = array_pop($events); // Delete note event. | ||
$sink->close(); | ||
|
||
// Validate event data. | ||
$this->assertInstanceOf('\core\event\note_deleted', $event); | ||
$this->assertEquals($this->eventnote->id, $event->objectid); | ||
$this->assertEquals($this->eventnote->usermodified, $event->userid); | ||
$this->assertEquals($this->eventnote->userid, $event->relateduserid); | ||
$this->assertEquals('post', $event->objecttable); | ||
$this->assertEquals(null, $event->get_url()); | ||
$this->assertEquals($this->noterecord, $event->get_record_snapshot('post', $event->objectid)); | ||
$this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']); | ||
|
||
// Test legacy data. | ||
$logurl = new \moodle_url('index.php', | ||
array('course' => $this->eventnote->courseid, 'user' => $this->eventnote->userid)); | ||
$logurl->set_anchor('note-' . $this->eventnote->id); | ||
$arr = array($this->eventnote->courseid, 'notes', 'delete', $logurl, 'delete note'); | ||
$this->assertEventLegacyLogData($arr, $event); | ||
$this->assertEventContextNotUsed($event); | ||
} | ||
public function test_report_viewed() { | ||
$course = $this->getDataGenerator()->create_course(); | ||
$context = context_course::instance($course->id); | ||
|
||
/** | ||
* Tests for event note_created. | ||
*/ | ||
public function test_note_created_event() { | ||
// Trigger event for log report viewed. | ||
$event = \report_log\event\report_viewed::create(array('context' => $context, | ||
'relateduserid' => 0, 'other' => array('groupid' => 0, 'date' => 0, 'modid' => 0, 'modaction' => '', | ||
'logformat' => 'showashtml'))); | ||
|
||
// Delete a note. | ||
// Trigger and capture the event. | ||
$sink = $this->redirectEvents(); | ||
$note = clone $this->eventnote; | ||
unset($note->id); | ||
note_save($note); | ||
$event->trigger(); | ||
$events = $sink->get_events(); | ||
$event = array_pop($events); // Delete note event. | ||
$sink->close(); | ||
$event = reset($events); | ||
|
||
// Validate event data. | ||
$this->assertInstanceOf('\core\event\note_created', $event); | ||
$this->assertEquals($note->id, $event->objectid); | ||
$this->assertEquals($note->usermodified, $event->userid); | ||
$this->assertEquals($note->userid, $event->relateduserid); | ||
$this->assertEquals('post', $event->objecttable); | ||
$this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']); | ||
|
||
// Test legacy data. | ||
$logurl = new \moodle_url('index.php', | ||
array('course' => $note->courseid, 'user' => $note->userid)); | ||
$logurl->set_anchor('note-' . $note->id); | ||
$arr = array($note->courseid, 'notes', 'add', $logurl, 'add note'); | ||
$this->assertEventLegacyLogData($arr, $event); | ||
$this->assertInstanceOf('\report_log\event\report_viewed', $event); | ||
$this->assertEquals($context, $event->get_context()); | ||
$expected = array($course->id, "course", "report log", "report/log/index.php?id=$course->id", $course->id); | ||
$this->assertEventLegacyLogData($expected, $event); | ||
$this->assertEventContextNotUsed($event); | ||
} | ||
|
||
/** | ||
* Tests for event note_updated. | ||
* Test the user report viewed event. | ||
* | ||
* It's not possible to use the moodle API to simulate the viewing of user log report, so here we | ||
* simply create the event and trigger it. | ||
*/ | ||
public function test_note_updated_event() { | ||
public function test_user_report_viewed() { | ||
$user = $this->getDataGenerator()->create_user(); | ||
$course = $this->getDataGenerator()->create_course(); | ||
$context = context_course::instance($course->id); | ||
|
||
// Delete a note. | ||
// Trigger event for user report viewed. | ||
$event = \report_log\event\user_report_viewed::create(array('context' => $context, | ||
'relateduserid' => $user->id, 'other' => array('mode' => 'today'))); | ||
|
||
// Trigger and capture the event. | ||
$sink = $this->redirectEvents(); | ||
$note = clone $this->eventnote; | ||
$note->publishstate = NOTES_STATE_DRAFT; | ||
note_save($note); | ||
$event->trigger(); | ||
$events = $sink->get_events(); | ||
$event = array_pop($events); // Delete note event. | ||
$sink->close(); | ||
|
||
// Validate event data. | ||
$this->assertInstanceOf('\core\event\note_updated', $event); | ||
$this->assertEquals($note->id, $event->objectid); | ||
$this->assertEquals($note->usermodified, $event->userid); | ||
$this->assertEquals($note->userid, $event->relateduserid); | ||
$this->assertEquals('post', $event->objecttable); | ||
$this->assertEquals(NOTES_STATE_DRAFT, $event->other['publishstate']); | ||
$event = reset($events); | ||
|
||
// Test legacy data. | ||
$logurl = new \moodle_url('index.php', | ||
array('course' => $note->courseid, 'user' => $note->userid)); | ||
$logurl->set_anchor('note-' . $note->id); | ||
$arr = array($note->courseid, 'notes', 'update', $logurl, 'update note'); | ||
$this->assertEventLegacyLogData($arr, $event); | ||
$this->assertInstanceOf('\report_log\event\user_report_viewed', $event); | ||
$this->assertEquals($context, $event->get_context()); | ||
$url = 'report/log/user.php?id=' . $user->id . '&course=' . $course->id . '&mode=today'; | ||
$expected = array($course->id, "course", "report log", $url, $course->id); | ||
$this->assertEventLegacyLogData($expected, $event); | ||
$this->assertEventContextNotUsed($event); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,137 +15,56 @@ | |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
/** | ||
* Tests for notes events. | ||
* Tests for report log live events. | ||
* | ||
* @package core_notes | ||
* @copyright 2013 Ankit Agarwal | ||
* @package report_loglive | ||
* @copyright 2014 Rajesh Taneja <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
/** | ||
* Class core_notes_events_testcase | ||
* Class report_loglive_events_testcase | ||
* | ||
* Class for tests related to notes events. | ||
* Class for tests related to log live events. | ||
* | ||
* @package core_notes | ||
* @copyright 2013 Ankit Agarwal | ||
* @package report_loglive | ||
* @copyright 2014 Rajesh Taneja <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. | ||
*/ | ||
class core_notes_events_testcase extends advanced_testcase { | ||
|
||
/** @var stdClass A note object. */ | ||
private $eventnote; | ||
|
||
/** @var stdClass A complete record from post table */ | ||
private $noterecord; | ||
|
||
public function setUp() { | ||
global $DB; | ||
|
||
$this->resetAfterTest(); | ||
$this->setAdminUser(); | ||
|
||
$course = $this->getDataGenerator()->create_course(); | ||
$user = $this->getDataGenerator()->create_user(); | ||
$gen = $this->getDataGenerator()->get_plugin_generator('core_notes'); | ||
$this->eventnote = $gen->create_instance(array('courseid' => $course->id, 'userid' => $user->id)); | ||
// Get the full record, note_load doesn't return everything. | ||
$this->noterecord = $DB->get_record('post', array('id' => $this->eventnote->id), '*', MUST_EXIST); | ||
|
||
} | ||
|
||
/** | ||
* Tests for event note_deleted. | ||
*/ | ||
public function test_note_deleted_event() { | ||
// Delete a note. | ||
$sink = $this->redirectEvents(); | ||
note_delete($this->eventnote); | ||
$events = $sink->get_events(); | ||
$event = array_pop($events); // Delete note event. | ||
$sink->close(); | ||
|
||
// Validate event data. | ||
$this->assertInstanceOf('\core\event\note_deleted', $event); | ||
$this->assertEquals($this->eventnote->id, $event->objectid); | ||
$this->assertEquals($this->eventnote->usermodified, $event->userid); | ||
$this->assertEquals($this->eventnote->userid, $event->relateduserid); | ||
$this->assertEquals('post', $event->objecttable); | ||
$this->assertEquals(null, $event->get_url()); | ||
$this->assertEquals($this->noterecord, $event->get_record_snapshot('post', $event->objectid)); | ||
$this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']); | ||
|
||
// Test legacy data. | ||
$logurl = new \moodle_url('index.php', | ||
array('course' => $this->eventnote->courseid, 'user' => $this->eventnote->userid)); | ||
$logurl->set_anchor('note-' . $this->eventnote->id); | ||
$arr = array($this->eventnote->courseid, 'notes', 'delete', $logurl, 'delete note'); | ||
$this->assertEventLegacyLogData($arr, $event); | ||
$this->assertEventContextNotUsed($event); | ||
} | ||
class report_loglive_events_testcase extends advanced_testcase { | ||
|
||
/** | ||
* Tests for event note_created. | ||
* Setup testcase. | ||
*/ | ||
public function test_note_created_event() { | ||
|
||
// Delete a note. | ||
$sink = $this->redirectEvents(); | ||
$note = clone $this->eventnote; | ||
unset($note->id); | ||
note_save($note); | ||
$events = $sink->get_events(); | ||
$event = array_pop($events); // Delete note event. | ||
$sink->close(); | ||
|
||
// Validate event data. | ||
$this->assertInstanceOf('\core\event\note_created', $event); | ||
$this->assertEquals($note->id, $event->objectid); | ||
$this->assertEquals($note->usermodified, $event->userid); | ||
$this->assertEquals($note->userid, $event->relateduserid); | ||
$this->assertEquals('post', $event->objecttable); | ||
$this->assertEquals(NOTES_STATE_SITE, $event->other['publishstate']); | ||
|
||
// Test legacy data. | ||
$logurl = new \moodle_url('index.php', | ||
array('course' => $note->courseid, 'user' => $note->userid)); | ||
$logurl->set_anchor('note-' . $note->id); | ||
$arr = array($note->courseid, 'notes', 'add', $logurl, 'add note'); | ||
$this->assertEventLegacyLogData($arr, $event); | ||
$this->assertEventContextNotUsed($event); | ||
public function setUp() { | ||
$this->setAdminUser(); | ||
$this->resetAfterTest(); | ||
} | ||
|
||
/** | ||
* Tests for event note_updated. | ||
* Test the report viewed event. | ||
* | ||
* It's not possible to use the moodle API to simulate the viewing of log report, so here we | ||
* simply create the event and trigger it. | ||
*/ | ||
public function test_note_updated_event() { | ||
public function test_report_viewed() { | ||
$course = $this->getDataGenerator()->create_course(); | ||
$context = context_course::instance($course->id); | ||
// Trigger event for loglive report viewed. | ||
$event = \report_loglive\event\report_viewed::create(array('context' => $context)); | ||
|
||
// Delete a note. | ||
// Trigger and capture the event. | ||
$sink = $this->redirectEvents(); | ||
$note = clone $this->eventnote; | ||
$note->publishstate = NOTES_STATE_DRAFT; | ||
note_save($note); | ||
$event->trigger(); | ||
$events = $sink->get_events(); | ||
$event = array_pop($events); // Delete note event. | ||
$sink->close(); | ||
$event = reset($events); | ||
|
||
// Validate event data. | ||
$this->assertInstanceOf('\core\event\note_updated', $event); | ||
$this->assertEquals($note->id, $event->objectid); | ||
$this->assertEquals($note->usermodified, $event->userid); | ||
$this->assertEquals($note->userid, $event->relateduserid); | ||
$this->assertEquals('post', $event->objecttable); | ||
$this->assertEquals(NOTES_STATE_DRAFT, $event->other['publishstate']); | ||
|
||
// Test legacy data. | ||
$logurl = new \moodle_url('index.php', | ||
array('course' => $note->courseid, 'user' => $note->userid)); | ||
$logurl->set_anchor('note-' . $note->id); | ||
$arr = array($note->courseid, 'notes', 'update', $logurl, 'update note'); | ||
$this->assertEventLegacyLogData($arr, $event); | ||
$this->assertInstanceOf('\report_loglive\event\report_viewed', $event); | ||
$this->assertEquals($context, $event->get_context()); | ||
$expected = array($course->id, 'course', 'report live', "report/loglive/index.php?id=$course->id", $course->id); | ||
$this->assertEventLegacyLogData($expected, $event); | ||
$this->assertEventContextNotUsed($event); | ||
} | ||
} | ||
|
Oops, something went wrong.