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_openclosedate;
  18  
  19  use quiz;
  20  use quizaccess_openclosedate;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/mod/quiz/accessrule/openclosedate/rule.php');
  26  
  27  
  28  /**
  29   * Unit tests for the quizaccess_openclosedate plugin.
  30   *
  31   * @package    quizaccess_openclosedate
  32   * @category   test
  33   * @copyright  2008 The Open University
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class rule_test extends \basic_testcase {
  37      public function test_no_dates() {
  38          $quiz = new \stdClass();
  39          $quiz->timeopen = 0;
  40          $quiz->timeclose = 0;
  41          $quiz->overduehandling = 'autosubmit';
  42          $cm = new \stdClass();
  43          $cm->id = 0;
  44          $quizobj = new quiz($quiz, $cm, null);
  45          $attempt = new \stdClass();
  46          $attempt->preview = 0;
  47  
  48          $rule = new quizaccess_openclosedate($quizobj, 10000);
  49          $this->assertFalse($rule->prevent_access());
  50          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
  51          $this->assertFalse($rule->is_finished(0, $attempt));
  52          $this->assertFalse($rule->end_time($attempt));
  53          $this->assertFalse($rule->time_left_display($attempt, 10000));
  54          $this->assertFalse($rule->time_left_display($attempt, 0));
  55  
  56          $rule = new quizaccess_openclosedate($quizobj, 0);
  57          $this->assertFalse($rule->prevent_access());
  58          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
  59          $this->assertFalse($rule->is_finished(0, $attempt));
  60          $this->assertFalse($rule->end_time($attempt));
  61          $this->assertFalse($rule->time_left_display($attempt, 0));
  62      }
  63  
  64      public function test_start_date() {
  65          $quiz = new \stdClass();
  66          $quiz->timeopen = 10000;
  67          $quiz->timeclose = 0;
  68          $quiz->overduehandling = 'autosubmit';
  69          $cm = new \stdClass();
  70          $cm->id = 0;
  71          $quizobj = new quiz($quiz, $cm, null);
  72          $attempt = new \stdClass();
  73          $attempt->preview = 0;
  74  
  75          $rule = new quizaccess_openclosedate($quizobj, 9999);
  76          $this->assertEquals($rule->prevent_access(),
  77              get_string('notavailable', 'quizaccess_openclosedate'));
  78          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
  79          $this->assertFalse($rule->is_finished(0, $attempt));
  80          $this->assertFalse($rule->end_time($attempt));
  81          $this->assertFalse($rule->time_left_display($attempt, 0));
  82  
  83          $rule = new quizaccess_openclosedate($quizobj, 10000);
  84          $this->assertFalse($rule->prevent_access());
  85          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
  86          $this->assertFalse($rule->is_finished(0, $attempt));
  87          $this->assertFalse($rule->end_time($attempt));
  88          $this->assertFalse($rule->time_left_display($attempt, 0));
  89      }
  90  
  91      public function test_close_date() {
  92          $quiz = new \stdClass();
  93          $quiz->timeopen = 0;
  94          $quiz->timeclose = 20000;
  95          $quiz->overduehandling = 'autosubmit';
  96          $cm = new \stdClass();
  97          $cm->id = 0;
  98          $quizobj = new quiz($quiz, $cm, null);
  99          $attempt = new \stdClass();
 100          $attempt->preview = 0;
 101  
 102          $rule = new quizaccess_openclosedate($quizobj, 20000);
 103          $this->assertFalse($rule->prevent_access());
 104          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
 105          $this->assertFalse($rule->is_finished(0, $attempt));
 106  
 107          $this->assertEquals($rule->end_time($attempt), 20000);
 108          $this->assertFalse($rule->time_left_display($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
 109          $this->assertEquals($rule->time_left_display($attempt, 19900), 100);
 110          $this->assertEquals($rule->time_left_display($attempt, 20000), 0);
 111          $this->assertEquals($rule->time_left_display($attempt, 20100), -100);
 112  
 113          $rule = new quizaccess_openclosedate($quizobj, 20001);
 114          $this->assertEquals($rule->prevent_access(),
 115              get_string('notavailable', 'quizaccess_openclosedate'));
 116          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
 117          $this->assertTrue($rule->is_finished(0, $attempt));
 118          $this->assertEquals($rule->end_time($attempt), 20000);
 119          $this->assertFalse($rule->time_left_display($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
 120          $this->assertEquals($rule->time_left_display($attempt, 19900), 100);
 121          $this->assertEquals($rule->time_left_display($attempt, 20000), 0);
 122          $this->assertEquals($rule->time_left_display($attempt, 20100), -100);
 123      }
 124  
 125      public function test_both_dates() {
 126          $quiz = new \stdClass();
 127          $quiz->timeopen = 10000;
 128          $quiz->timeclose = 20000;
 129          $quiz->overduehandling = 'autosubmit';
 130          $cm = new \stdClass();
 131          $cm->id = 0;
 132          $quizobj = new quiz($quiz, $cm, null);
 133          $attempt = new \stdClass();
 134          $attempt->preview = 0;
 135  
 136          $rule = new quizaccess_openclosedate($quizobj, 9999);
 137          $this->assertEquals($rule->prevent_access(),
 138              get_string('notavailable', 'quizaccess_openclosedate'));
 139          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
 140          $this->assertFalse($rule->is_finished(0, $attempt));
 141  
 142          $rule = new quizaccess_openclosedate($quizobj, 10000);
 143          $this->assertFalse($rule->prevent_access());
 144          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
 145          $this->assertFalse($rule->is_finished(0, $attempt));
 146  
 147          $rule = new quizaccess_openclosedate($quizobj, 20000);
 148          $this->assertFalse($rule->prevent_access());
 149          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
 150          $this->assertFalse($rule->is_finished(0, $attempt));
 151  
 152          $rule = new quizaccess_openclosedate($quizobj, 20001);
 153          $this->assertEquals($rule->prevent_access(),
 154              get_string('notavailable', 'quizaccess_openclosedate'));
 155          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
 156          $this->assertTrue($rule->is_finished(0, $attempt));
 157  
 158          $this->assertEquals($rule->end_time($attempt), 20000);
 159          $this->assertFalse($rule->time_left_display($attempt, 20000 - QUIZ_SHOW_TIME_BEFORE_DEADLINE));
 160          $this->assertEquals($rule->time_left_display($attempt, 19900), 100);
 161          $this->assertEquals($rule->time_left_display($attempt, 20000), 0);
 162          $this->assertEquals($rule->time_left_display($attempt, 20100), -100);
 163      }
 164  
 165      public function test_close_date_with_overdue() {
 166          $quiz = new \stdClass();
 167          $quiz->timeopen = 0;
 168          $quiz->timeclose = 20000;
 169          $quiz->overduehandling = 'graceperiod';
 170          $quiz->graceperiod = 1000;
 171          $cm = new \stdClass();
 172          $cm->id = 0;
 173          $quizobj = new quiz($quiz, $cm, null);
 174          $attempt = new \stdClass();
 175          $attempt->preview = 0;
 176  
 177          $rule = new quizaccess_openclosedate($quizobj, 20000);
 178          $this->assertFalse($rule->prevent_access());
 179  
 180          $rule = new quizaccess_openclosedate($quizobj, 20001);
 181          $this->assertFalse($rule->prevent_access());
 182  
 183          $rule = new quizaccess_openclosedate($quizobj, 21000);
 184          $this->assertFalse($rule->prevent_access());
 185  
 186          $rule = new quizaccess_openclosedate($quizobj, 21001);
 187          $this->assertEquals($rule->prevent_access(),
 188                  get_string('notavailable', 'quizaccess_openclosedate'));
 189      }
 190  }