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 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Contains unit tests for mod_quiz\dates.
  19   *
  20   * @package   mod_quiz
  21   * @category  test
  22   * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  declare(strict_types=1);
  27  
  28  namespace mod_quiz;
  29  
  30  use advanced_testcase;
  31  use cm_info;
  32  use core\activity_dates;
  33  
  34  /**
  35   * Class for unit testing mod_quiz\dates.
  36   *
  37   * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
  38   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class dates_test extends advanced_testcase {
  41  
  42      /**
  43       * Data provider for get_dates_for_module().
  44       * @return array[]
  45       */
  46      public function get_dates_for_module_provider(): array {
  47          $now = time();
  48          $before = $now - DAYSECS;
  49          $earlier = $before - DAYSECS;
  50          $after = $now + DAYSECS;
  51          $later = $after + DAYSECS;
  52  
  53          return [
  54              'without any dates' => [
  55                  null, null, null, null, null, null, []
  56              ],
  57              'only with opening time' => [
  58                  $after, null, null, null, null, null, [
  59                      ['label' => get_string('activitydate:opens', 'course'), 'timestamp' => $after],
  60                  ]
  61              ],
  62              'only with closing time' => [
  63                  null, $after, null, null, null, null, [
  64                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $after],
  65                  ]
  66              ],
  67              'with both times' => [
  68                  $after, $later, null, null, null, null, [
  69                      ['label' => get_string('activitydate:opens', 'course'), 'timestamp' => $after],
  70                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later],
  71                  ]
  72              ],
  73              'between the dates' => [
  74                  $before, $after, null, null, null, null, [
  75                      ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $before],
  76                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $after],
  77                  ]
  78              ],
  79              'dates are past' => [
  80                  $earlier, $before, null, null, null, null, [
  81                      ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier],
  82                      ['label' => get_string('activitydate:closed', 'course'), 'timestamp' => $before],
  83                  ]
  84              ],
  85              'with user override' => [
  86                  $before, $after, $earlier, $later, null, null, [
  87                      ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier],
  88                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later],
  89                  ]
  90              ],
  91              'with group override' => [
  92                  $before, $after, null, null, $earlier, $later, [
  93                      ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier],
  94                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later],
  95                  ]
  96              ],
  97              'with both user and group overrides' => [
  98                  $before, $after, $earlier, $later, $earlier - DAYSECS, $later + DAYSECS, [
  99                      ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier],
 100                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later],
 101                  ]
 102              ],
 103          ];
 104      }
 105  
 106      /**
 107       * Test for get_dates_for_module().
 108       *
 109       * @dataProvider get_dates_for_module_provider
 110       * @param int|null $timeopen Time of opening the quiz.
 111       * @param int|null $timeclose Time of closing the quiz.
 112       * @param int|null $usertimeopen The user override for opening the quiz.
 113       * @param int|null $usertimeclose The user override for closing the quiz.
 114       * @param int|null $grouptimeopen The group override for opening the quiz.
 115       * @param int|null $grouptimeclose The group override for closing the quiz.
 116       * @param array $expected The expected value of calling get_dates_for_module()
 117       */
 118      public function test_get_dates_for_module(?int $timeopen, ?int $timeclose,
 119              ?int $usertimeopen, ?int $usertimeclose,
 120              ?int $grouptimeopen, ?int $grouptimeclose,
 121              array $expected) {
 122  
 123          $this->resetAfterTest();
 124          $generator = $this->getDataGenerator();
 125          /** @var \mod_quiz_generator $quizgenerator */
 126          $quizgenerator = $generator->get_plugin_generator('mod_quiz');
 127  
 128          $course = $generator->create_course();
 129          $user = $generator->create_user();
 130          $generator->enrol_user($user->id, $course->id);
 131  
 132          $data = ['course' => $course->id];
 133          if ($timeopen) {
 134              $data['timeopen'] = $timeopen;
 135          }
 136          if ($timeclose) {
 137              $data['timeclose'] = $timeclose;
 138          }
 139          $quiz = $quizgenerator->create_instance($data);
 140  
 141          if ($usertimeopen || $usertimeclose || $grouptimeopen || $grouptimeclose) {
 142              $generator->enrol_user($user->id, $course->id);
 143              $group = $generator->create_group(['courseid' => $course->id]);
 144              $generator->create_group_member(['groupid' => $group->id, 'userid' => $user->id]);
 145  
 146              if ($usertimeopen || $usertimeclose) {
 147                  $quizgenerator->create_override([
 148                      'quiz' => $quiz->id,
 149                      'userid' => $user->id,
 150                      'timeopen' => $usertimeopen,
 151                      'timeclose' => $usertimeclose,
 152                  ]);
 153              }
 154  
 155              if ($grouptimeopen || $grouptimeclose) {
 156                  $quizgenerator->create_override([
 157                      'quiz' => $quiz->id,
 158                      'groupid' => $group->id,
 159                      'timeopen' => $grouptimeopen,
 160                      'timeclose' => $grouptimeclose,
 161                  ]);
 162              }
 163          }
 164  
 165          $this->setUser($user);
 166  
 167          $cm = get_coursemodule_from_instance('quiz', $quiz->id);
 168          // Make sure we're using a cm_info object.
 169          $cm = cm_info::create($cm);
 170  
 171          $dates = activity_dates::get_dates_for_module($cm, (int) $user->id);
 172  
 173          $this->assertEquals($expected, $dates);
 174      }
 175  }