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 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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