Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 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;
  18  
  19  /**
  20   * PHPUnit tests for template class.
  21   *
  22   * @package   quizaccess_seb
  23   * @author    Dmitrii Metelkin <dmitriim@catalyst-au.net>
  24   * @copyright 2020 Catalyst IT
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class template_test extends \advanced_testcase {
  28  
  29      /**
  30       * Called before every test.
  31       */
  32      public function setUp(): void {
  33          parent::setUp();
  34  
  35          $this->resetAfterTest();
  36      }
  37  
  38      /**
  39       * Test that template saved with valid content.
  40       */
  41      public function test_template_is_saved() {
  42          global $DB;
  43          $data = new \stdClass();
  44          $data->name = 'Test name';
  45          $data->description = 'Test description';
  46          $data->enabled = 1;
  47          $data->content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  48  <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
  49  <plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key><false/><key>showReloadButton</key><true/>"
  50              . "<key>showTime</key><false/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
  51              . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><true/><key>audioMute</key><false/>"
  52              . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><true/>"
  53              . "<key>URLFilterEnableContentFilter</key><false/><key>hashedQuitPassword</key>"
  54              . "<string>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</string><key>URLFilterRules</key>"
  55              . "<array><dict><key>action</key><integer>1</integer><key>active</key><true/><key>expression</key>"
  56              . "<string>test.com</string><key>regex</key><false/></dict></array>"
  57              . "<key>sendBrowserExamKey</key><true/></dict></plist>\n";
  58          $template = new template(0, $data);
  59          $template->save();
  60  
  61          $actual = $DB->get_record(template::TABLE, ['id' => $template->get('id')]);
  62          $this->assertEquals($data->name, $actual->name);
  63          $this->assertEquals($data->description, $actual->description);
  64          $this->assertEquals($data->enabled, $actual->enabled);
  65          $this->assertEquals($data->content, $actual->content);
  66          $this->assertTrue($template->can_delete());
  67      }
  68  
  69      /**
  70       * Test that template is not saved with invalid content.
  71       */
  72      public function test_template_is_not_saved_with_invalid_content() {
  73          $this->expectException(\core\invalid_persistent_exception::class);
  74          $this->expectExceptionMessage('Invalid SEB config template');
  75  
  76          $data = new \stdClass();
  77          $data->name = 'Test name';
  78          $data->description = 'Test description';
  79          $data->enabled = 1;
  80          $data->content = "Invalid content";
  81          $template = new template(0, $data);
  82          $template->save();
  83      }
  84  
  85      /**
  86       * Test that a template cannot be deleted when assigned to a quiz.
  87       */
  88      public function test_cannot_delete_template_when_assigned_to_quiz() {
  89          global $DB;
  90  
  91          $data = new \stdClass();
  92          $data->name = 'Test name';
  93          $data->description = 'Test description';
  94          $data->enabled = 1;
  95          $data->content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  96  <!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
  97  <plist version=\"1.0\"><dict><key>showTaskBar</key><true/><key>allowWlan</key><false/><key>showReloadButton</key><true/>"
  98              . "<key>showTime</key><false/><key>showInputLanguage</key><true/><key>allowQuit</key><true/>"
  99              . "<key>quitURLConfirm</key><true/><key>audioControlEnabled</key><true/><key>audioMute</key><false/>"
 100              . "<key>allowSpellCheck</key><false/><key>browserWindowAllowReload</key><true/><key>URLFilterEnable</key><true/>"
 101              . "<key>URLFilterEnableContentFilter</key><false/><key>hashedQuitPassword</key>"
 102              . "<string>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08</string><key>URLFilterRules</key>"
 103              . "<array><dict><key>action</key><integer>1</integer><key>active</key><true/><key>expression</key>"
 104              . "<string>test.com</string><key>regex</key><false/></dict></array>"
 105              . "<key>sendBrowserExamKey</key><true/></dict></plist>\n";
 106          $template = new template(0, $data);
 107  
 108          $template->save();
 109          $this->assertTrue($template->can_delete());
 110  
 111          $DB->insert_record(quiz_settings::TABLE, (object) [
 112              'quizid' => 1,
 113              'cmid' => 1,
 114              'templateid' => $template->get('id'),
 115              'requiresafeexambrowser' => '1',
 116              'sebconfigfile' => '373552893',
 117              'showsebtaskbar' => '1',
 118              'showwificontrol' => '0',
 119              'showreloadbutton' => '1',
 120              'showtime' => '0',
 121              'showkeyboardlayout' => '1',
 122              'allowuserquitseb' => '1',
 123              'quitpassword' => 'test',
 124              'linkquitseb' => '',
 125              'userconfirmquit' => '1',
 126              'enableaudiocontrol' => '1',
 127              'muteonstartup' => '0',
 128              'allowspellchecking' => '0',
 129              'allowreloadinexam' => '1',
 130              'activateurlfiltering' => '1',
 131              'filterembeddedcontent' => '0',
 132              'expressionsallowed' => 'test.com',
 133              'regexallowed' => '',
 134              'expressionsblocked' => '',
 135              'regexblocked' => '',
 136              'showsebdownloadlink' => '1',
 137              'config' => '',
 138          ]);
 139  
 140          $this->assertFalse($template->can_delete());
 141      }
 142  
 143  }