Merge branch 'MDL-66550-master' of git://github.com/andrewnicols/moodle
[moodle.git] / report / log / tests / events_test.php
blob0426838aa18f4bf22a88714c2ae29cee807d8fad
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Tests for report log events.
20 * @package report_log
21 * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
25 defined('MOODLE_INTERNAL') || die();
27 /**
28 * Class report_log_events_testcase
30 * Class for tests related to log events.
32 * @package report_log
33 * @copyright 2014 Rajesh Taneja <rajesh@moodle.com>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
36 class report_log_events_testcase extends advanced_testcase {
38 /**
39 * Setup testcase.
41 public function setUp() {
42 $this->setAdminUser();
43 $this->resetAfterTest();
46 /**
47 * Test the report viewed event.
49 * It's not possible to use the moodle API to simulate the viewing of log report, so here we
50 * simply create the event and trigger it.
52 public function test_report_viewed() {
53 $course = $this->getDataGenerator()->create_course();
54 $context = context_course::instance($course->id);
56 // Trigger event for log report viewed.
57 $event = \report_log\event\report_viewed::create(array('context' => $context,
58 'relateduserid' => 0, 'other' => array('groupid' => 0, 'date' => 0, 'modid' => 0, 'modaction' => '',
59 'logformat' => 'showashtml')));
61 // Trigger and capture the event.
62 $sink = $this->redirectEvents();
63 $event->trigger();
64 $events = $sink->get_events();
65 $event = reset($events);
67 $this->assertInstanceOf('\report_log\event\report_viewed', $event);
68 $this->assertEquals($context, $event->get_context());
69 $expected = array($course->id, "course", "report log", "report/log/index.php?id=$course->id", $course->id);
70 $this->assertEventLegacyLogData($expected, $event);
71 $this->assertEventContextNotUsed($event);
72 $url = new moodle_url('/report/log/index.php', array('id' => $event->courseid));
73 $this->assertEquals($url, $event->get_url());
76 /**
77 * Test the user report viewed event.
79 * It's not possible to use the moodle API to simulate the viewing of user log report, so here we
80 * simply create the event and trigger it.
82 public function test_user_report_viewed() {
83 $user = $this->getDataGenerator()->create_user();
84 $course = $this->getDataGenerator()->create_course();
85 $context = context_course::instance($course->id);
87 // Trigger event for user report viewed.
88 $event = \report_log\event\user_report_viewed::create(array('context' => $context,
89 'relateduserid' => $user->id, 'other' => array('mode' => 'today')));
91 // Trigger and capture the event.
92 $sink = $this->redirectEvents();
93 $event->trigger();
94 $events = $sink->get_events();
95 $event = reset($events);
97 $this->assertInstanceOf('\report_log\event\user_report_viewed', $event);
98 $this->assertEquals($context, $event->get_context());
99 $url = 'report/log/user.php?id=' . $user->id . '&course=' . $course->id . '&mode=today';
100 $expected = array($course->id, "course", "report log", $url, $course->id);
101 $this->assertEventLegacyLogData($expected, $event);
102 $this->assertEventContextNotUsed($event);
103 $url = new moodle_url('/report/log/user.php', array('course' => $course->id, 'id' => $user->id, 'mode' => 'today'));
104 $this->assertEquals($url, $event->get_url());