Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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 quiz;
  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      public function test_event_access_prevented() {
  50          $this->resetAfterTest();
  51  
  52          $this->setAdminUser();
  53          $quiz = $this->create_test_quiz($this->course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY);
  54          $accessmanager = new \quizaccess_seb\access_manager(new quiz($quiz,
  55              get_coursemodule_from_id('quiz', $quiz->cmid), $this->course));
  56  
  57          // Set up event with data.
  58          $user = $this->getDataGenerator()->create_user();
  59          $this->setUser($user);
  60  
  61          $_SERVER['HTTP_X_SAFEEXAMBROWSER_CONFIGKEYHASH'] = 'configkey';
  62          $_SERVER['HTTP_X_SAFEEXAMBROWSER_REQUESTHASH'] = 'browserexamkey';
  63  
  64          $event = \quizaccess_seb\event\access_prevented::create_strict($accessmanager, 'Because I said so.');
  65  
  66          // Create an event sink, trigger event and retrieve event.
  67          $sink = $this->redirectEvents();
  68          $event->trigger();
  69          $events = $sink->get_events();
  70          $this->assertEquals(1, count($events));
  71          $event = reset($events);
  72  
  73          $expectedconfigkey = $accessmanager->get_valid_config_key();
  74  
  75          // Test that the event data is as expected.
  76          $this->assertInstanceOf('\quizaccess_seb\event\access_prevented', $event);
  77          $this->assertEquals('Quiz access was prevented', $event->get_name());
  78          $this->assertEquals(
  79              "The user with id '$user->id' has been prevented from accessing quiz with id '$quiz->id' by the "
  80              . "Safe Exam Browser access plugin. The reason was 'Because I said so.'. "
  81              . "Expected config key: '$expectedconfigkey'. "
  82              . "Received config key: 'configkey'. Received browser exam key: 'browserexamkey'.",
  83              $event->get_description());
  84          $this->assertEquals(\context_module::instance($quiz->cmid), $event->get_context());
  85          $this->assertEquals($user->id, $event->userid);
  86          $this->assertEquals($quiz->id, $event->objectid);
  87          $this->assertEquals($this->course->id, $event->courseid);
  88          $this->assertEquals('Because I said so.', $event->other['reason']);
  89          $this->assertEquals($expectedconfigkey, $event->other['savedconfigkey']);
  90          $this->assertEquals('configkey', $event->other['receivedconfigkey']);
  91          $this->assertEquals('browserexamkey', $event->other['receivedbrowserexamkey']);
  92      }
  93  
  94      /**
  95       * Test creating the template_created event.
  96       */
  97      public function test_event_create_template() {
  98          $this->resetAfterTest();
  99          // Set up event with data.
 100          $user = $this->getDataGenerator()->create_user();
 101          $this->setUser($user);
 102  
 103          $template = $this->create_template();
 104  
 105          $event = \quizaccess_seb\event\template_created::create_strict(
 106              $template,
 107              \context_system::instance());
 108  
 109          // Create an event sink, trigger event and retrieve event.
 110          $sink = $this->redirectEvents();
 111          $event->trigger();
 112          $events = $sink->get_events();
 113          $this->assertEquals(1, count($events));
 114          $event = reset($events);
 115  
 116          // Test that the event data is as expected.
 117          $this->assertInstanceOf('\quizaccess_seb\event\template_created', $event);
 118          $this->assertEquals('SEB template was created', $event->get_name());
 119          $this->assertEquals(
 120              "The user with id '$user->id' has created a template with id '{$template->get('id')}'.",
 121              $event->get_description()
 122          );
 123          $this->assertEquals(\context_system::instance(), $event->get_context());
 124          $this->assertEquals($user->id, $event->userid);
 125          $this->assertEquals($template->get('id'), $event->objectid);
 126      }
 127  }