Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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