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 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_delaybetweenattempts;
  18  
  19  use quiz;
  20  use quizaccess_delaybetweenattempts;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/mod/quiz/accessrule/delaybetweenattempts/rule.php');
  26  
  27  
  28  /**
  29   * Unit tests for the quizaccess_delaybetweenattempts plugin.
  30   *
  31   * @package    quizaccess_delaybetweenattempts
  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_just_first_delay() {
  38          $quiz = new \stdClass();
  39          $quiz->attempts = 3;
  40          $quiz->timelimit = 0;
  41          $quiz->delay1 = 1000;
  42          $quiz->delay2 = 0;
  43          $quiz->timeclose = 0;
  44          $cm = new \stdClass();
  45          $cm->id = 0;
  46          $quizobj = new quiz($quiz, $cm, null);
  47          $attempt = new \stdClass();
  48          $attempt->timefinish = 10000;
  49  
  50          $rule = new quizaccess_delaybetweenattempts($quizobj, 10000);
  51          $this->assertEmpty($rule->description());
  52          $this->assertFalse($rule->prevent_access());
  53          $this->assertFalse($rule->is_finished(0, $attempt));
  54          $this->assertFalse($rule->end_time($attempt));
  55          $this->assertFalse($rule->time_left_display($attempt, 0));
  56  
  57          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
  58          $this->assertFalse($rule->prevent_new_attempt(3, $attempt));
  59          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
  60              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11000)));
  61          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
  62          $attempt->timefinish = 9000;
  63          $this->assertFalse($rule->prevent_new_attempt(1, $attempt));
  64          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
  65          $attempt->timefinish = 9001;
  66          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
  67              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
  68          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
  69      }
  70  
  71      public function test_just_second_delay() {
  72          $quiz = new \stdClass();
  73          $quiz->attempts = 5;
  74          $quiz->timelimit = 0;
  75          $quiz->delay1 = 0;
  76          $quiz->delay2 = 1000;
  77          $quiz->timeclose = 0;
  78          $cm = new \stdClass();
  79          $cm->id = 0;
  80          $quizobj = new quiz($quiz, $cm, null);
  81          $attempt = new \stdClass();
  82          $attempt->timefinish = 10000;
  83  
  84          $rule = new quizaccess_delaybetweenattempts($quizobj, 10000);
  85          $this->assertEmpty($rule->description());
  86          $this->assertFalse($rule->prevent_access());
  87          $this->assertFalse($rule->is_finished(0, $attempt));
  88          $this->assertFalse($rule->end_time($attempt));
  89          $this->assertFalse($rule->time_left_display($attempt, 0));
  90  
  91          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
  92          $this->assertFalse($rule->prevent_new_attempt(5, $attempt));
  93          $this->assertFalse($rule->prevent_new_attempt(1, $attempt));
  94          $this->assertEquals($rule->prevent_new_attempt(2, $attempt),
  95              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11000)));
  96          $this->assertEquals($rule->prevent_new_attempt(3, $attempt),
  97              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11000)));
  98          $attempt->timefinish = 9000;
  99          $this->assertFalse($rule->prevent_new_attempt(1, $attempt));
 100          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 101          $this->assertFalse($rule->prevent_new_attempt(3, $attempt));
 102          $attempt->timefinish = 9001;
 103          $this->assertFalse($rule->prevent_new_attempt(1, $attempt));
 104          $this->assertEquals($rule->prevent_new_attempt(2, $attempt),
 105              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
 106          $this->assertEquals($rule->prevent_new_attempt(4, $attempt),
 107              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
 108      }
 109  
 110      public function test_just_both_delays() {
 111          $quiz = new \stdClass();
 112          $quiz->attempts = 5;
 113          $quiz->timelimit = 0;
 114          $quiz->delay1 = 2000;
 115          $quiz->delay2 = 1000;
 116          $quiz->timeclose = 0;
 117          $cm = new \stdClass();
 118          $cm->id = 0;
 119          $quizobj = new quiz($quiz, $cm, null);
 120          $attempt = new \stdClass();
 121          $attempt->timefinish = 10000;
 122  
 123          $rule = new quizaccess_delaybetweenattempts($quizobj, 10000);
 124          $this->assertEmpty($rule->description());
 125          $this->assertFalse($rule->prevent_access());
 126          $this->assertFalse($rule->is_finished(0, $attempt));
 127          $this->assertFalse($rule->end_time($attempt));
 128          $this->assertFalse($rule->time_left_display($attempt, 0));
 129  
 130          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
 131          $this->assertFalse($rule->prevent_new_attempt(5, $attempt));
 132          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 133              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(12000)));
 134          $this->assertEquals($rule->prevent_new_attempt(2, $attempt),
 135              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11000)));
 136          $this->assertEquals($rule->prevent_new_attempt(3, $attempt),
 137              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11000)));
 138          $attempt->timefinish = 8000;
 139          $this->assertFalse($rule->prevent_new_attempt(1, $attempt));
 140          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 141          $this->assertFalse($rule->prevent_new_attempt(3, $attempt));
 142          $attempt->timefinish = 8001;
 143          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 144              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
 145          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 146          $this->assertFalse($rule->prevent_new_attempt(4, $attempt));
 147          $attempt->timefinish = 9000;
 148          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 149              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11000)));
 150          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 151          $this->assertFalse($rule->prevent_new_attempt(3, $attempt));
 152          $attempt->timefinish = 9001;
 153          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 154              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11001)));
 155          $this->assertEquals($rule->prevent_new_attempt(2, $attempt),
 156              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
 157          $this->assertEquals($rule->prevent_new_attempt(4, $attempt),
 158              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
 159      }
 160  
 161      public function test_with_close_date() {
 162          $quiz = new \stdClass();
 163          $quiz->attempts = 5;
 164          $quiz->timelimit = 0;
 165          $quiz->delay1 = 2000;
 166          $quiz->delay2 = 1000;
 167          $quiz->timeclose = 15000;
 168          $cm = new \stdClass();
 169          $cm->id = 0;
 170          $quizobj = new quiz($quiz, $cm, null);
 171          $attempt = new \stdClass();
 172          $attempt->timefinish = 13000;
 173  
 174          $rule = new quizaccess_delaybetweenattempts($quizobj, 10000);
 175          $this->assertEmpty($rule->description());
 176          $this->assertFalse($rule->prevent_access());
 177          $this->assertFalse($rule->is_finished(0, $attempt));
 178          $this->assertFalse($rule->end_time($attempt));
 179          $this->assertFalse($rule->time_left_display($attempt, 0));
 180  
 181          $attempt->timefinish = 13000;
 182          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 183              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(15000)));
 184          $attempt->timefinish = 13001;
 185          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 186              get_string('youcannotwait', 'quizaccess_delaybetweenattempts'));
 187          $attempt->timefinish = 14000;
 188          $this->assertEquals($rule->prevent_new_attempt(2, $attempt),
 189              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(15000)));
 190          $attempt->timefinish = 14001;
 191          $this->assertEquals($rule->prevent_new_attempt(2, $attempt),
 192              get_string('youcannotwait', 'quizaccess_delaybetweenattempts'));
 193  
 194          $rule = new quizaccess_delaybetweenattempts($quizobj, 15000);
 195          $attempt->timefinish = 13000;
 196          $this->assertFalse($rule->prevent_new_attempt(1, $attempt));
 197          $attempt->timefinish = 13001;
 198          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 199              get_string('youcannotwait', 'quizaccess_delaybetweenattempts'));
 200          $attempt->timefinish = 14000;
 201          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 202          $attempt->timefinish = 14001;
 203          $this->assertEquals($rule->prevent_new_attempt(2, $attempt),
 204              get_string('youcannotwait', 'quizaccess_delaybetweenattempts'));
 205  
 206          $rule = new quizaccess_delaybetweenattempts($quizobj, 15001);
 207          $attempt->timefinish = 13000;
 208          $this->assertFalse($rule->prevent_new_attempt(1, $attempt));
 209          $attempt->timefinish = 13001;
 210          $this->assertFalse($rule->prevent_new_attempt(1, $attempt));
 211          $attempt->timefinish = 14000;
 212          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 213          $attempt->timefinish = 14001;
 214          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 215      }
 216  
 217      public function test_time_limit_and_overdue() {
 218          $quiz = new \stdClass();
 219          $quiz->attempts = 5;
 220          $quiz->timelimit = 100;
 221          $quiz->delay1 = 2000;
 222          $quiz->delay2 = 1000;
 223          $quiz->timeclose = 0;
 224          $cm = new \stdClass();
 225          $cm->id = 0;
 226          $quizobj = new quiz($quiz, $cm, null);
 227          $attempt = new \stdClass();
 228          $attempt->timestart = 9900;
 229          $attempt->timefinish = 10100;
 230  
 231          $rule = new quizaccess_delaybetweenattempts($quizobj, 10000);
 232          $this->assertEmpty($rule->description());
 233          $this->assertFalse($rule->prevent_access());
 234          $this->assertFalse($rule->is_finished(0, $attempt));
 235          $this->assertFalse($rule->end_time($attempt));
 236          $this->assertFalse($rule->time_left_display($attempt, 0));
 237  
 238          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
 239          $this->assertFalse($rule->prevent_new_attempt(5, $attempt));
 240          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 241              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(12000)));
 242          $this->assertEquals($rule->prevent_new_attempt(2, $attempt),
 243              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11000)));
 244          $this->assertEquals($rule->prevent_new_attempt(3, $attempt),
 245              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11000)));
 246          $attempt->timestart = 7950;
 247          $attempt->timefinish = 8000;
 248          $this->assertFalse($rule->prevent_new_attempt(1, $attempt));
 249          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 250          $this->assertFalse($rule->prevent_new_attempt(3, $attempt));
 251          $attempt->timestart = 7950;
 252          $attempt->timefinish = 8001;
 253          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 254              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
 255          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 256          $this->assertFalse($rule->prevent_new_attempt(4, $attempt));
 257          $attempt->timestart = 8950;
 258          $attempt->timefinish = 9000;
 259          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 260              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11000)));
 261          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 262          $this->assertFalse($rule->prevent_new_attempt(3, $attempt));
 263          $attempt->timestart = 8950;
 264          $attempt->timefinish = 9001;
 265          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 266              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11001)));
 267          $this->assertEquals($rule->prevent_new_attempt(2, $attempt),
 268              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
 269          $this->assertEquals($rule->prevent_new_attempt(4, $attempt),
 270              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
 271          $attempt->timestart = 8900;
 272          $attempt->timefinish = 9100;
 273          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 274              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11000)));
 275          $this->assertFalse($rule->prevent_new_attempt(2, $attempt));
 276          $this->assertFalse($rule->prevent_new_attempt(3, $attempt));
 277          $attempt->timestart = 8901;
 278          $attempt->timefinish = 9100;
 279          $this->assertEquals($rule->prevent_new_attempt(1, $attempt),
 280              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(11001)));
 281          $this->assertEquals($rule->prevent_new_attempt(2, $attempt),
 282              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
 283          $this->assertEquals($rule->prevent_new_attempt(4, $attempt),
 284              get_string('youmustwait', 'quizaccess_delaybetweenattempts', userdate(10001)));
 285      }
 286  }