MDL-62781 question/privacy: fix tests with CodeRunner is installed
[moodle.git] / my / tests / events_test.php
blob3fe006a23f684e711abc5733c6270fcf8c508ad3
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 * Events tests.
20 * @package core
21 * @category test
22 * @copyright 2016 Stephen Bourget
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 /**
30 * Unit tests for the dashboard events.
32 * @copyright 2016 Stephen Bourget
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class dashboard_events_testcase extends advanced_testcase {
37 /** @var user cobject */
38 protected $user;
40 /**
41 * Setup often used objects for the following tests.
43 protected function setup() {
44 global $USER;
46 $this->resetAfterTest();
48 // The user we are going to test this on.
49 $this->setAdminUser();
50 $this->user = $USER;
53 /**
54 * Test the dashboard viewed event.
56 * There is no external API for viewing the dashboard, so the unit test will simply
57 * create and trigger the event and ensure data is returned as expected.
59 public function test_dashboard_viewed() {
61 $user = $this->user;
62 // Trigger an event: dashboard viewed.
63 $eventparams = array(
64 'context' => $context = context_user::instance($user->id)
67 $event = \core\event\dashboard_viewed::create($eventparams);
68 // Trigger and capture the event.
69 $sink = $this->redirectEvents();
70 $event->trigger();
71 $events = $sink->get_events();
72 $event = reset($events);
74 // Check that the event data is valid.
75 $this->assertInstanceOf('\core\event\dashboard_viewed', $event);
76 $this->assertEquals($user->id, $event->userid);
77 $this->assertDebuggingNotCalled();
80 /**
81 * Test the dashboard reset event.
83 * We will reset the user dashboard to
84 * trigger the event and ensure data is returned as expected.
86 public function test_dashboard_reset() {
87 global $CFG;
88 require_once($CFG->dirroot . '/my/lib.php');
89 $user = $this->user;
90 $sink = $this->redirectEvents();
92 // Reset the dashboard.
93 my_reset_page($user->id);
95 // Trigger and capture the event.
96 $events = $sink->get_events();
97 $event = reset($events);
99 // Check that the event data is valid.
100 $this->assertInstanceOf('\core\event\dashboard_reset', $event);
101 $this->assertEquals($user->id, $event->userid);
102 $this->assertEquals(MY_PAGE_PRIVATE, $event->other['private']);
103 $this->assertEquals('my-index', $event->other['pagetype']);
104 $this->assertDebuggingNotCalled();
106 // Reset the dashboard with private parameter is set to MY_PAGE_PUBLIC and pagetype set to 'user-profile'.
107 $sink = $this->redirectEvents();
108 my_reset_page($user->id, MY_PAGE_PUBLIC, 'user-profile');
110 // Trigger and capture the event.
111 $events = $sink->get_events();
112 $event = reset($events);
113 $this->assertEquals(MY_PAGE_PUBLIC, $event->other['private']);
114 $this->assertEquals('user-profile', $event->other['pagetype']);
118 * Test the dashboards reset event.
120 * We will reset all user dashboards to
121 * trigger the event and ensure data is returned as expected.
123 public function test_dashboards_reset() {
124 global $CFG, $USER;
125 require_once($CFG->dirroot . '/my/lib.php');
127 $sink = $this->redirectEvents();
129 // Reset all dashbaords.
130 my_reset_page_for_all_users();
132 // Trigger and capture the event.
133 $events = $sink->get_events();
134 $event = reset($events);
136 // Check that the event data is valid.
137 $this->assertInstanceOf('\core\event\dashboards_reset', $event);
138 $this->assertEquals($USER->id, $event->userid);
139 $this->assertEquals(MY_PAGE_PRIVATE, $event->other['private']);
140 $this->assertEquals('my-index', $event->other['pagetype']);
141 $this->assertDebuggingNotCalled();
143 // Reset the dashboards with private parameter is set to MY_PAGE_PUBLIC and pagetype set to 'user-profile'.
144 $sink = $this->redirectEvents();
145 my_reset_page_for_all_users(MY_PAGE_PUBLIC, 'user-profile');
147 // Trigger and capture the event.
148 $events = $sink->get_events();
149 $event = reset($events);
150 $this->assertEquals(MY_PAGE_PUBLIC, $event->other['private']);
151 $this->assertEquals('user-profile', $event->other['pagetype']);