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 311 and 401]

   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_lesson\dates.
  19   *
  20   * @package   mod_lesson
  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_lesson;
  29  
  30  use advanced_testcase;
  31  use cm_info;
  32  use core\activity_dates;
  33  
  34  /**
  35   * Class for unit testing mod_lesson\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, 'dataid' => 'available'],
  60                  ]
  61              ],
  62              'only with closing time' => [
  63                  null, $after, null, null, null, null, [
  64                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $after, 'dataid' => 'deadline'],
  65                  ]
  66              ],
  67              'with both times' => [
  68                  $after, $later, null, null, null, null, [
  69                      ['label' => get_string('activitydate:opens', 'course'), 'timestamp' => $after, 'dataid' => 'available'],
  70                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later, 'dataid' => 'deadline'],
  71                  ]
  72              ],
  73              'between the dates' => [
  74                  $before, $after, null, null, null, null, [
  75                      ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $before, 'dataid' => 'available'],
  76                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $after, 'dataid' => 'deadline'],
  77                  ]
  78              ],
  79              'dates are past' => [
  80                  $earlier, $before, null, null, null, null, [
  81                      ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier, 'dataid' => 'available'],
  82                      ['label' => get_string('activitydate:closed', 'course'), 'timestamp' => $before, 'dataid' => 'deadline'],
  83                  ]
  84              ],
  85              'with user override' => [
  86                  $before, $after, $earlier, $later, null, null, [
  87                      ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier, 'dataid' => 'available'],
  88                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later, 'dataid' => 'deadline'],
  89                  ]
  90              ],
  91              'with group override' => [
  92                  $before, $after, null, null, $earlier, $later, [
  93                      ['label' => get_string('activitydate:opened', 'course'), 'timestamp' => $earlier, 'dataid' => 'available'],
  94                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later, 'dataid' => 'deadline'],
  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, 'dataid' => 'available'],
 100                      ['label' => get_string('activitydate:closes', 'course'), 'timestamp' => $later, 'dataid' => 'deadline'],
 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 $available The 'available from' value of the lesson.
 111       * @param int|null $deadline The lesson's deadline.
 112       * @param int|null $useravailable The user override for opening the lesson.
 113       * @param int|null $userdeadline The user override for deadline of the lesson.
 114       * @param int|null $groupavailable The group override for opening the lesson.
 115       * @param int|null $groupuserdeadline The group override for deadline of the lesson.
 116       * @param array $expected The expected value of calling get_dates_for_module()
 117       */
 118      public function test_get_dates_for_module(?int $available, ?int $deadline,
 119              ?int $useravailable, ?int $userdeadline,
 120              ?int $groupavailable, ?int $groupuserdeadline,
 121              array $expected) {
 122          $this->resetAfterTest();
 123          $generator = $this->getDataGenerator();
 124          /** @var \mod_lesson_generator $lessongenerator */
 125          $lessongenerator = $generator->get_plugin_generator('mod_lesson');
 126  
 127          $course = $generator->create_course();
 128          $user = $generator->create_user();
 129          $generator->enrol_user($user->id, $course->id);
 130  
 131          $data = ['course' => $course->id];
 132          if ($available) {
 133              $data['available'] = $available;
 134          }
 135          if ($deadline) {
 136              $data['deadline'] = $deadline;
 137          }
 138          $this->setAdminUser();
 139          $lesson = $lessongenerator->create_instance($data);
 140  
 141          if ($useravailable || $userdeadline || $groupavailable || $groupuserdeadline) {
 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 ($useravailable || $userdeadline) {
 147                  $lessongenerator->create_override([
 148                      'lessonid' => $lesson->id,
 149                      'userid' => $user->id,
 150                      'available' => $useravailable,
 151                      'deadline' => $userdeadline,
 152                  ]);
 153              }
 154  
 155              if ($groupavailable || $groupuserdeadline) {
 156                  $lessongenerator->create_override([
 157                      'lessonid' => $lesson->id,
 158                      'groupid' => $group->id,
 159                      'available' => $groupavailable,
 160                      'deadline' => $groupuserdeadline,
 161                  ]);
 162              }
 163          }
 164  
 165          $this->setUser($user);
 166  
 167          $cm = get_coursemodule_from_instance('lesson', $lesson->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  }