Skip to content

Commit

Permalink
MDL-44017 Events: Added unit test for report_viewed events
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Mar 7, 2014
1 parent e1b16f9 commit 2cd29dd
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 308 deletions.
1 change: 1 addition & 0 deletions course/modlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ function edit_module_post_actions($moduleinfo, $course) {
}
}

require_once($CFG->libdir.'/grade/grade_outcome.php');
// Add outcomes if requested.
if ($hasoutcomes && $outcomes = grade_outcome::fetch_all_available($course->id)) {
$grade_items = array();
Expand Down
29 changes: 28 additions & 1 deletion notes/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,5 +147,32 @@ public function test_note_updated_event() {
$this->assertEventLegacyLogData($arr, $event);
$this->assertEventContextNotUsed($event);
}
}

/**
* Test the notes viewed event.
*
* It's not possible to use the moodle API to simulate the viewing of notes, so here we
* simply create the event and trigger it.
*/
public function test_notes_viewed() {
$coursecontext = context_course::instance($this->eventnote->courseid);
// Trigger event for notes viewed.
$event = \core\event\notes_viewed::create(array(
'context' => $coursecontext,
'relateduserid' => $this->eventnote->userid
));

// Trigger and capture the event.
$sink = $this->redirectEvents();
$event->trigger();
$events = $sink->get_events();
$event = reset($events);

$this->assertInstanceOf('\core\event\notes_viewed', $event);
$this->assertEquals($coursecontext, $event->get_context());
$expected = array($this->eventnote->courseid, 'notes', 'view', 'index.php?course=' .
$this->eventnote->courseid.'&user=' . $this->eventnote->userid, 'view notes');
$this->assertEventLegacyLogData($expected, $event);
$this->assertEventContextNotUsed($event);
}
}
149 changes: 50 additions & 99 deletions report/log/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

137 changes: 28 additions & 109 deletions report/loglive/tests/events_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Loading

0 comments on commit 2cd29dd

Please sign in to comment.