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 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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  /**
  18   * PHPUnit tests for helper class.
  19   *
  20   * @package    quizaccess_seb
  21   * @author     Dmitrii Metelkin <dmitriim@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 helper class.
  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_helper_testhelpertrait extends advanced_testcase {
  38      use quizaccess_seb_test_helper_trait;
  39  
  40      /**
  41       * Test that we can check valid seb string.
  42       */
  43      public function test_is_valid_seb_config() {
  44          $validseb = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  45  <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
  46  <plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key><false/><key>showReloadButton</key><true/>"
  47              . "<key>showTime</key><false/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
  48              . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><true/><key>audioMute</key><false/>"
  49              . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><true/>"
  50              . "<key>URLFilterEnableContentFilter</key><false/><key>hashedQuitPassword</key>"
  51              . "<string>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</string><key>URLFilterRules</key>"
  52              . "<array><dict><key>action</key><integer>1</integer><key>active</key><true/><key>expression</key>"
  53              . "<string>test.com</string><key>regex</key><false/></dict></array>"
  54              . "<key>sendBrowserExamKey</key><true/></dict></plist>\n";
  55          $invalidseb = 'Invalid seb';
  56          $emptyseb = '';
  57  
  58          $this->assertTrue(\quizaccess_seb\helper::is_valid_seb_config($validseb));
  59          $this->assertFalse(\quizaccess_seb\helper::is_valid_seb_config($invalidseb));
  60          $this->assertFalse(\quizaccess_seb\helper::is_valid_seb_config($emptyseb));
  61      }
  62  
  63      /**
  64       * Test that we can get seb file headers.
  65       */
  66      public function test_get_seb_file_headers() {
  67          $expiretime = 1582767914;
  68          $headers = \quizaccess_seb\helper::get_seb_file_headers($expiretime);
  69  
  70          $this->assertCount(5, $headers);
  71          $this->assertEquals('Cache-Control: private, max-age=1, no-transform', $headers[0]);
  72          $this->assertEquals('Expires: Thu, 27 Feb 2020 01:45:14 GMT', $headers[1]);
  73          $this->assertEquals('Pragma: no-cache', $headers[2]);
  74          $this->assertEquals('Content-Disposition: attachment; filename=config.seb', $headers[3]);
  75          $this->assertEquals('Content-Type: application/seb', $headers[4]);
  76      }
  77  
  78  
  79      /**
  80       * Test that the course module must exist to get a seb config file content.
  81       */
  82      public function test_can_not_get_config_content_with_invalid_cmid() {
  83          $this->resetAfterTest();
  84  
  85          $user = $this->getDataGenerator()->create_user();
  86          $course = $this->getDataGenerator()->create_course();
  87          $this->getDataGenerator()->enrol_user($user->id, $course->id);
  88          $this->setUser($user); // Log user in.
  89  
  90          $this->expectException(dml_exception::class);
  91          $this->expectExceptionMessage("Can't find data record in database. (SELECT cm.*, m.name, md.name AS modname \n"
  92              . "              FROM {course_modules} cm\n"
  93              . "                   JOIN {modules} md ON md.id = cm.module\n"
  94              . "                   JOIN {quiz} m ON m.id = cm.instance\n"
  95              . "                   \n"
  96              . "             WHERE cm.id = :cmid AND md.name = :modulename\n"
  97              . "                   \n"
  98              . "[array (\n"
  99              . "  'cmid' => '999',\n"
 100              . "  'modulename' => 'quiz',\n"
 101              .')])');
 102          \quizaccess_seb\helper::get_seb_config_content('999');
 103      }
 104  
 105      /**
 106       * Test that the user must be enrolled to get seb config content.
 107       */
 108      public function test_can_not_get_config_content_when_user_not_enrolled_in_course() {
 109          $this->resetAfterTest();
 110  
 111          $this->setAdminUser();
 112          $course = $this->getDataGenerator()->create_course();
 113          $quiz = $this->create_test_quiz($course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY);
 114  
 115          $user = $this->getDataGenerator()->create_user();
 116          $this->setUser($user); // Log user in.
 117  
 118          $this->expectException(moodle_exception::class);
 119          $this->expectExceptionMessage('Unsupported redirect detected, script execution terminated');
 120          \quizaccess_seb\helper::get_seb_config_content($quiz->cmid);
 121      }
 122  
 123      /**
 124       * Test that if SEB quiz settings can't be found, a seb config content won't be provided.
 125       */
 126      public function test_can_not_get_config_content_if_config_not_found_for_cmid() {
 127          $this->resetAfterTest();
 128  
 129          $this->setAdminUser();
 130          $course = $this->getDataGenerator()->create_course();
 131          $quiz = $this->create_test_quiz($course);
 132  
 133          $user = $this->getDataGenerator()->create_user();
 134          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 135          $this->setUser($user); // Log user in.
 136  
 137          $this->expectException(moodle_exception::class);
 138          $this->expectExceptionMessage("No SEB config could be found for quiz with cmid: $quiz->cmid");
 139          \quizaccess_seb\helper::get_seb_config_content($quiz->cmid);
 140      }
 141  
 142      /**
 143       * That that if config is empty for a quiz, a seb config content won't be provided.
 144       */
 145      public function test_can_not_get_config_content_if_config_empty() {
 146          $this->resetAfterTest();
 147  
 148          $this->setAdminUser();
 149  
 150          $course = $this->getDataGenerator()->create_course();
 151          $quiz = $this->create_test_quiz($course, \quizaccess_seb\settings_provider::USE_SEB_NO);
 152  
 153          $user = $this->getDataGenerator()->create_user();
 154          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 155          $this->setUser($user); // Log user in.
 156  
 157          $this->expectException(moodle_exception::class);
 158          $this->expectExceptionMessage("No SEB config could be found for quiz with cmid: $quiz->cmid");
 159          \quizaccess_seb\helper::get_seb_config_content($quiz->cmid);
 160      }
 161  
 162      /**
 163       * Test config content is provided successfully.
 164       */
 165      public function test_config_provided() {
 166          $this->resetAfterTest();
 167  
 168          $this->setAdminUser();
 169  
 170          $course = $this->getDataGenerator()->create_course();
 171          $quiz = $this->create_test_quiz($course, \quizaccess_seb\settings_provider::USE_SEB_CONFIG_MANUALLY);
 172  
 173          $user = $this->getDataGenerator()->create_user();
 174          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 175          $this->setUser($user); // Log user in.
 176  
 177          $config = \quizaccess_seb\helper::get_seb_config_content($quiz->cmid);
 178  
 179          $url = new moodle_url("/mod/quiz/view.php", ['id' => $quiz->cmid]);
 180  
 181          $this->assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
 182              . "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
 183              . "<plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key>"
 184              . "<false/><key>showReloadButton</key><true/><key>showTime</key><true/><key>showInputLanguage</key>"
 185              . "<true/><key>allowQuit</key><true/><key>quitURLConfirm</key><true/><key>audioControlEnabled</key>"
 186              . "<false/><key>audioMute</key><false/><key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key>"
 187              . "<true/><key>URLFilterEnable</key><false/><key>URLFilterEnableContentFilter</key><false/>"
 188              . "<key>URLFilterRules</key><array/><key>startURL</key><string>$url</string>"
 189              . "<key>sendBrowserExamKey</key><true/><key>examSessionClearCookiesOnStart</key><false/>"
 190              . "<key>allowPreferencesWindow</key><false/></dict></plist>\n", $config);
 191      }
 192  
 193  }