Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Events tests.
  19   *
  20   * @package   core_mnet
  21   * @category  test
  22   * @copyright 2013 Mark Nelson <markn@moodle.com>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  
  30  require_once($CFG->dirroot . '/mnet/lib.php');
  31  
  32  class mnet_events_testcase extends advanced_testcase {
  33  
  34      /** @var stdClass the mnet host we are using to test */
  35      protected $mnethost;
  36  
  37      /**
  38       * Test set up.
  39       *
  40       * This is executed before running any test in this file.
  41       */
  42      public function setUp(): void {
  43          global $DB;
  44  
  45          $this->resetAfterTest();
  46  
  47          // Add a mnet host.
  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);
  52      }
  53  
  54      /**
  55       * Test the mnet access control created event.
  56       */
  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);
  63  
  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());
  73      }
  74  
  75      /**
  76       * Test the mnet access control updated event.
  77       */
  78      public function test_mnet_access_control_updated() {
  79          global $DB;
  80  
  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);
  87  
  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);
  93  
  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());
 103      }
 104  }