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