Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 400 and 402] [Versions 401 and 402]

   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  namespace quizaccess_seb\event;
  18  
  19  use mod_quiz\quiz_settings;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  require_once (__DIR__ . '/..//test_helper_trait.php');
  24  
  25  /**
  26   * PHPUnit tests for all plugin events.
  27   *
  28   * @package    quizaccess_seb
  29   * @author     Andrew Madden <andrewmadden@catalyst-au.net>
  30   * @copyright  2020 Catalyst IT
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class events_test extends \advanced_testcase {
  34      use \quizaccess_seb_test_helper_trait;
  35  
  36      /**
  37       * Called before every test.
  38       */
  39      public function setUp(): void {
  40          parent::setUp();
  41  
  42          $this->resetAfterTest();
  43          $this->course = $this->getDataGenerator()->create_course();
  44      }
  45  
  46      /**
  47       * Test creating the access_prevented event.
  48       *
  49       * @covers \quizaccess_seb\event\access_prevented
  50       */
  51      public function test_event_access_prevented() {
  52          $this->resetAfterTest();
  53  
  54          $this->setAdminUser();
  55          $quiz = $this->create_test_quiz($this->course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY);
  56          $accessmanager = new \quizaccess_seb\seb_access_manager(new quiz_settings($quiz,
  57              get_coursemodule_from_id('quiz', $quiz->cmid), $this->course));
  58  
  59          // Set up event with data.
  60          $user = $this->getDataGenerator()->create_user();
  61          $this->setUser($user);
  62  
  63          $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'configkey';
  64          $_SERVER['HTTP_X_SAFEEXAMBROWSER_REQUESTHASH'] = 'browserexamkey';
  65  
  66          $event = \quizaccess_seb\event\access_prevented::create_strict($accessmanager, 'Because I said so.');
  67  
  68          // Create an event sink, trigger event and retrieve event.
  69          $sink = $this->redirectEvents();
  70          $event->trigger();
  71          $events = $sink->get_events();
  72          $this->assertEquals(1, count($events));
  73          $event = reset($events);
  74  
  75          $expectedconfigkey = $accessmanager->get_valid_config_key();
  76  
  77          // Test that the event data is as expected.
  78          $this->assertInstanceOf('\quizaccess_seb\event\access_prevented', $event);
  79          $this->assertEquals('Quiz access was prevented', $event->get_name());
  80          $this->assertEquals(
  81              "The user with id '$user->id' has been prevented from accessing quiz with id '$quiz->id' by the "
  82              . "Safe Exam Browser access plugin. The reason was 'Because I said so.'. "
  83              . "Expected config key: '$expectedconfigkey'. "
  84              . "Received config key: 'configkey'. Received browser exam key: 'browserexamkey'.",
  85              $event->get_description());
  86          $this->assertEquals(\context_module::instance($quiz->cmid), $event->get_context());
  87          $this->assertEquals($user->id, $event->userid);
  88          $this->assertEquals($quiz->id, $event->objectid);
  89          $this->assertEquals($this->course->id, $event->courseid);
  90          $this->assertEquals('Because I said so.', $event->other['reason']);
  91          $this->assertEquals($expectedconfigkey, $event->other['savedconfigkey']);
  92          $this->assertEquals('configkey', $event->other['receivedconfigkey']);
  93          $this->assertEquals('browserexamkey', $event->other['receivedbrowserexamkey']);
  94      }
  95  
  96      /**
  97       * Test creating the access_prevented event with provided SEB keys.
  98       *
  99       * @covers \quizaccess_seb\event\access_prevented
 100       */
 101      public function test_event_access_prevented_with_keys() {
 102          $this->resetAfterTest();
 103  
 104          $this->setAdminUser();
 105          $quiz = $this->create_test_quiz($this->course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY);
 106          $accessmanager = new \quizaccess_seb\seb_access_manager(new quiz_settings($quiz,
 107              get_coursemodule_from_id('quiz', $quiz->cmid), $this->course));
 108  
 109          // Set up event with data.
 110          $user = $this->getDataGenerator()->create_user();
 111          $this->setUser($user);
 112  
 113          $event = \quizaccess_seb\event\access_prevented::create_strict($accessmanager, 'Because I said so.',
 114                  'configkey', 'browserexamkey');
 115  
 116          // Create an event sink, trigger event and retrieve event.
 117          $sink = $this->redirectEvents();
 118          $event->trigger();
 119          $events = $sink->get_events();
 120          $this->assertEquals(1, count($events));
 121          $event = reset($events);
 122  
 123          $expectedconfigkey = $accessmanager->get_valid_config_key();
 124  
 125          // Test that the event data is as expected.
 126          $this->assertInstanceOf('\quizaccess_seb\event\access_prevented', $event);
 127          $this->assertEquals('Quiz access was prevented', $event->get_name());
 128          $this->assertEquals(
 129              "The user with id '$user->id' has been prevented from accessing quiz with id '$quiz->id' by the "
 130              . "Safe Exam Browser access plugin. The reason was 'Because I said so.'. "
 131              . "Expected config key: '$expectedconfigkey'. "
 132              . "Received config key: 'configkey'. Received browser exam key: 'browserexamkey'.",
 133              $event->get_description());
 134          $this->assertEquals(\context_module::instance($quiz->cmid), $event->get_context());
 135          $this->assertEquals($user->id, $event->userid);
 136          $this->assertEquals($quiz->id, $event->objectid);
 137          $this->assertEquals($this->course->id, $event->courseid);
 138          $this->assertEquals('Because I said so.', $event->other['reason']);
 139          $this->assertEquals($expectedconfigkey, $event->other['savedconfigkey']);
 140          $this->assertEquals('configkey', $event->other['receivedconfigkey']);
 141          $this->assertEquals('browserexamkey', $event->other['receivedbrowserexamkey']);
 142      }
 143  
 144      /**
 145       * Test creating the template_created event.
 146       *
 147       * @covers \quizaccess_seb\event\template_created
 148       */
 149      public function test_event_create_template() {
 150          $this->resetAfterTest();
 151          // Set up event with data.
 152          $user = $this->getDataGenerator()->create_user();
 153          $this->setUser($user);
 154  
 155          $template = $this->create_template();
 156  
 157          $event = \quizaccess_seb\event\template_created::create_strict(
 158              $template,
 159              \context_system::instance());
 160  
 161          // Create an event sink, trigger event and retrieve event.
 162          $sink = $this->redirectEvents();
 163          $event->trigger();
 164          $events = $sink->get_events();
 165          $this->assertEquals(1, count($events));
 166          $event = reset($events);
 167  
 168          // Test that the event data is as expected.
 169          $this->assertInstanceOf('\quizaccess_seb\event\template_created', $event);
 170          $this->assertEquals('SEB template was created', $event->get_name());
 171          $this->assertEquals(
 172              "The user with id '$user->id' has created a template with id '{$template->get('id')}'.",
 173              $event->get_description()
 174          );
 175          $this->assertEquals(\context_system::instance(), $event->get_context());
 176          $this->assertEquals($user->id, $event->userid);
 177          $this->assertEquals($template->get('id'), $event->objectid);
 178      }
 179  }