2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
22 * @copyright 2013 Mark Nelson <markn@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
30 require_once($CFG->dirroot
. '/mnet/lib.php');
32 class mnet_events_testcase
extends advanced_testcase
{
34 /** @var stdClass the mnet host we are using to test */
40 * This is executed before running any test in this file.
42 public function setUp(): void
{
45 $this->resetAfterTest();
48 $this->mnethost
= new stdClass();
49 $this->mnethost
->name
= 'A mnet host';
50 $this->mnethost
->public_key
= 'A random public key!';
51 $this->mnethost
->id
= $DB->insert_record('mnet_host', $this->mnethost
);
55 * Test the mnet access control created event.
57 public function test_mnet_access_control_created() {
58 // Trigger and capture the event.
59 $sink = $this->redirectEvents();
60 mnet_update_sso_access_control('username', $this->mnethost
->id
, 'enabled');
61 $events = $sink->get_events();
62 $event = reset($events);
64 // Check that the event data is valid.
65 $this->assertInstanceOf('\core\event\mnet_access_control_created', $event);
66 $this->assertEquals(context_system
::instance(), $event->get_context());
67 $expected = array(SITEID
, 'admin/mnet', 'add', 'admin/mnet/access_control.php',
68 'SSO ACL: enabled user \'username\' from ' . $this->mnethost
->name
);
69 $this->assertEventLegacyLogData($expected, $event);
70 $this->assertEventContextNotUsed($event);
71 $url = new \
moodle_url('/admin/mnet/access_control.php');
72 $this->assertEquals($url, $event->get_url());
76 * Test the mnet access control updated event.
78 public function test_mnet_access_control_updated() {
81 // Create a mnet access control.
82 $mnetaccesscontrol = new stdClass();
83 $mnetaccesscontrol->username
= 'username';
84 $mnetaccesscontrol->mnet_host_id
= $this->mnethost
->id
;
85 $mnetaccesscontrol->accessctrl
= 'enabled';
86 $mnetaccesscontrol->id
= $DB->insert_record('mnet_sso_access_control', $mnetaccesscontrol);
88 // Trigger and capture the event.
89 $sink = $this->redirectEvents();
90 mnet_update_sso_access_control('username', $this->mnethost
->id
, 'enabled');
91 $events = $sink->get_events();
92 $event = reset($events);
94 // Check that the event data is valid.
95 $this->assertInstanceOf('\core\event\mnet_access_control_updated', $event);
96 $this->assertEquals(context_system
::instance(), $event->get_context());
97 $expected = array(SITEID
, 'admin/mnet', 'update', 'admin/mnet/access_control.php',
98 'SSO ACL: enabled user \'username\' from ' . $this->mnethost
->name
);
99 $this->assertEventLegacyLogData($expected, $event);
100 $this->assertEventContextNotUsed($event);
101 $url = new \
moodle_url('/admin/mnet/access_control.php');
102 $this->assertEquals($url, $event->get_url());