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_assign\dates.
  19   *
  20   * @package   mod_assign
  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_assign;
  29  
  30  use advanced_testcase;
  31  use cm_info;
  32  use core\activity_dates;
  33  
  34  /**
  35   * Class for unit testing mod_assign\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:submissionsopen', 'mod_assign'), 'timestamp' => $after],
  60                  ]
  61              ],
  62              'only with closing time' => [
  63                  null, $after, null, null, null, null, [
  64                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $after],
  65                  ]
  66              ],
  67              'with both times' => [
  68                  $after, $later, null, null, null, null, [
  69                      ['label' => get_string('activitydate:submissionsopen', 'mod_assign'), 'timestamp' => $after],
  70                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later],
  71                  ]
  72              ],
  73              'between the dates' => [
  74                  $before, $after, null, null, null, null, [
  75                      ['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $before],
  76                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $after],
  77                  ]
  78              ],
  79              'dates are past' => [
  80                  $earlier, $before, null, null, null, null, [
  81                      ['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier],
  82                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $before],
  83                  ]
  84              ],
  85              'with user override' => [
  86                  $before, $after, $earlier, $later, null, null, [
  87                      ['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier],
  88                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later],
  89                  ]
  90              ],
  91              'with group override' => [
  92                  $before, $after, null, null, $earlier, $later, [
  93                      ['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier],
  94                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), '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:submissionsopened', 'mod_assign'), 'timestamp' => $earlier],
 100                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), '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 $from Time of opening submissions in the assignment.
 111       * @param int|null $due Assignment's due date.
 112       * @param int|null $userfrom The user override for opening submissions.
 113       * @param int|null $userdue The user override for due date.
 114       * @param int|null $groupfrom The group override for opening submissions.
 115       * @param int|null $groupdue The group override for due date.
 116       * @param array $expected The expected value of calling get_dates_for_module()
 117       */
 118      public function test_get_dates_for_module(?int $from, ?int $due,
 119              ?int $userfrom, ?int $userdue,
 120              ?int $groupfrom, ?int $groupdue,
 121              array $expected) {
 122  
 123          $this->resetAfterTest();
 124          $generator = $this->getDataGenerator();
 125          /** @var \mod_assign_generator $assigngenerator */
 126          $assigngenerator = $generator->get_plugin_generator('mod_assign');
 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 ($from) {
 134              $data['allowsubmissionsfromdate'] = $from;
 135          }
 136          if ($due) {
 137              $data['duedate'] = $due;
 138          }
 139          $assign = $assigngenerator->create_instance($data);
 140  
 141          if ($userfrom || $userdue || $groupfrom || $groupdue) {
 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 ($userfrom || $userdue) {
 147                  $assigngenerator->create_override([
 148                      'assignid' => $assign->id,
 149                      'userid' => $user->id,
 150                      'allowsubmissionsfromdate' => $userfrom,
 151                      'duedate' => $userdue,
 152                  ]);
 153              }
 154  
 155              if ($groupfrom || $groupdue) {
 156                  $assigngenerator->create_override([
 157                      'assignid' => $assign->id,
 158                      'groupid' => $group->id,
 159                      'allowsubmissionsfromdate' => $groupfrom,
 160                      'duedate' => $groupdue,
 161                  ]);
 162              }
 163          }
 164  
 165          $this->setUser($user);
 166  
 167          $cm = get_coursemodule_from_instance('assign', $assign->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  }