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_timelimit;
  18  
  19  use quiz;
  20  use quizaccess_timelimit;
  21  
  22  defined('MOODLE_INTERNAL') || die();
  23  
  24  global $CFG;
  25  require_once($CFG->dirroot . '/mod/quiz/accessrule/timelimit/rule.php');
  26  
  27  
  28  /**
  29   * Unit tests for the quizaccess_timelimit plugin.
  30   *
  31   * @package    quizaccess_timelimit
  32   * @copyright  2008 The Open University
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class rule_test extends \basic_testcase {
  36      public function test_time_limit_access_rule() {
  37          $quiz = new \stdClass();
  38          $quiz->timeclose = 0;
  39          $quiz->timelimit = 3600;
  40          $cm = new \stdClass();
  41          $cm->id = 0;
  42          $quizobj = new quiz($quiz, $cm, null);
  43          $rule = new quizaccess_timelimit($quizobj, 10000);
  44          $attempt = new \stdClass();
  45  
  46          $this->assertEquals($rule->description(),
  47              get_string('quiztimelimit', 'quizaccess_timelimit', format_time(3600)));
  48  
  49          $attempt->timestart = 10000;
  50          $attempt->preview = 0;
  51          $this->assertEquals(13600, $rule->end_time($attempt));
  52          $this->assertEquals(3600, $rule->time_left_display($attempt, 10000));
  53          $this->assertEquals(1600, $rule->time_left_display($attempt, 12000));
  54          $this->assertEquals(-400, $rule->time_left_display($attempt, 14000));
  55  
  56          $this->assertFalse($rule->prevent_access());
  57          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
  58          $this->assertFalse($rule->is_finished(0, $attempt));
  59      }
  60  
  61      /**
  62       * Data provider for test_time_limit_access_rule_with_time_close.
  63       *
  64       * @return array of ($timetoclose, $timelimit, $displaylimit, $actuallimit)
  65       */
  66      public function time_limit_access_rule_with_time_close_provider() {
  67          return [
  68              'Close time is earlier than time limit' => [1800, 3600, 3600, 1800],
  69              'Close time is on time limit' => [3600, 3600, 3600, 3600],
  70              'Close time is later than time limit' => [3600, 1800, 1800, 1800]
  71          ];
  72      }
  73  
  74      /**
  75       * Test the time_left_display method of the quizaccess_timelimit class.
  76       *
  77       * @param int $timetoclose  The number of seconds that is left to the quiz' closing time
  78       * @param int $timelimit    Time limit of the quiz
  79       * @param int $displaylimit The limit that is displayed on the quiz page
  80       * @param int $actuallimit  The actual limit that is being applied
  81       * @dataProvider time_limit_access_rule_with_time_close_provider
  82       */
  83      public function test_time_limit_access_rule_with_time_close($timetoclose, $timelimit, $displaylimit, $actuallimit) {
  84          $timenow = 10000;
  85  
  86          $quiz = new \stdClass();
  87          $quiz->timeclose = $timenow + $timetoclose;
  88          $quiz->timelimit = $timelimit;
  89          $cm = new \stdClass();
  90          $cm->id = 0;
  91          $quizobj = new quiz($quiz, $cm, null);
  92          $rule = new quizaccess_timelimit($quizobj, $timenow);
  93          $attempt = new \stdClass();
  94  
  95          $this->assertEquals($rule->description(),
  96              get_string('quiztimelimit', 'quizaccess_timelimit', format_time($displaylimit)));
  97  
  98          $attempt->timestart = $timenow;
  99          $attempt->preview = 0;
 100          $this->assertEquals($timenow + $actuallimit, $rule->end_time($attempt));
 101          $this->assertEquals($actuallimit, $rule->time_left_display($attempt, $timenow));
 102          $this->assertEquals($actuallimit - 1000, $rule->time_left_display($attempt, $timenow + 1000));
 103  
 104          $this->assertFalse($rule->prevent_access());
 105          $this->assertFalse($rule->prevent_new_attempt(0, $attempt));
 106          $this->assertFalse($rule->is_finished(0, $attempt));
 107      }
 108  }