Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 311 and 400]

   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                          'dataid' => 'allowsubmissionsfromdate'],
  61                  ]
  62              ],
  63              'only with closing time' => [
  64                  null, $after, null, null, null, null, [
  65                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $after,
  66                          'dataid' => 'duedate'],
  67                  ]
  68              ],
  69              'with both times' => [
  70                  $after, $later, null, null, null, null, [
  71                      ['label' => get_string('activitydate:submissionsopen', 'mod_assign'), 'timestamp' => $after,
  72                          'dataid' => 'allowsubmissionsfromdate'],
  73                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later,
  74                          'dataid' => 'duedate'],
  75                  ]
  76              ],
  77              'between the dates' => [
  78                  $before, $after, null, null, null, null, [
  79                      ['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $before,
  80                          'dataid' => 'allowsubmissionsfromdate'],
  81                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $after,
  82                          'dataid' => 'duedate'],
  83                  ]
  84              ],
  85              'dates are past' => [
  86                  $earlier, $before, null, null, null, null, [
  87                      ['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier,
  88                          'dataid' => 'allowsubmissionsfromdate'],
  89                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $before,
  90                          'dataid' => 'duedate'],
  91                  ]
  92              ],
  93              'with user override' => [
  94                  $before, $after, $earlier, $later, null, null, [
  95                      ['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier,
  96                          'dataid' => 'allowsubmissionsfromdate'],
  97                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later,
  98                          'dataid' => 'duedate'],
  99                  ]
 100              ],
 101              'with group override' => [
 102                  $before, $after, null, null, $earlier, $later, [
 103                      ['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier,
 104                          'dataid' => 'allowsubmissionsfromdate'],
 105                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later,
 106                          'dataid' => 'duedate'],
 107                  ]
 108              ],
 109              'with both user and group overrides' => [
 110                  $before, $after, $earlier, $later, $earlier - DAYSECS, $later + DAYSECS, [
 111                      ['label' => get_string('activitydate:submissionsopened', 'mod_assign'), 'timestamp' => $earlier,
 112                          'dataid' => 'allowsubmissionsfromdate'],
 113                      ['label' => get_string('activitydate:submissionsdue', 'mod_assign'), 'timestamp' => $later,
 114                          'dataid' => 'duedate'],
 115                  ]
 116              ],
 117          ];
 118      }
 119  
 120      /**
 121       * Test for get_dates_for_module().
 122       *
 123       * @dataProvider get_dates_for_module_provider
 124       * @param int|null $from Time of opening submissions in the assignment.
 125       * @param int|null $due Assignment's due date.
 126       * @param int|null $userfrom The user override for opening submissions.
 127       * @param int|null $userdue The user override for due date.
 128       * @param int|null $groupfrom The group override for opening submissions.
 129       * @param int|null $groupdue The group override for due date.
 130       * @param array $expected The expected value of calling get_dates_for_module()
 131       */
 132      public function test_get_dates_for_module(?int $from, ?int $due,
 133              ?int $userfrom, ?int $userdue,
 134              ?int $groupfrom, ?int $groupdue,
 135              array $expected) {
 136  
 137          $this->resetAfterTest();
 138          $generator = $this->getDataGenerator();
 139          /** @var \mod_assign_generator $assigngenerator */
 140          $assigngenerator = $generator->get_plugin_generator('mod_assign');
 141  
 142          $course = $generator->create_course();
 143          $user = $generator->create_user();
 144          $generator->enrol_user($user->id, $course->id);
 145  
 146          $data = ['course' => $course->id];
 147          if ($from) {
 148              $data['allowsubmissionsfromdate'] = $from;
 149          }
 150          if ($due) {
 151              $data['duedate'] = $due;
 152          }
 153          $assign = $assigngenerator->create_instance($data);
 154  
 155          if ($userfrom || $userdue || $groupfrom || $groupdue) {
 156              $generator->enrol_user($user->id, $course->id);
 157              $group = $generator->create_group(['courseid' => $course->id]);
 158              $generator->create_group_member(['groupid' => $group->id, 'userid' => $user->id]);
 159  
 160              if ($userfrom || $userdue) {
 161                  $assigngenerator->create_override([
 162                      'assignid' => $assign->id,
 163                      'userid' => $user->id,
 164                      'allowsubmissionsfromdate' => $userfrom,
 165                      'duedate' => $userdue,
 166                  ]);
 167              }
 168  
 169              if ($groupfrom || $groupdue) {
 170                  $assigngenerator->create_override([
 171                      'assignid' => $assign->id,
 172                      'groupid' => $group->id,
 173                      'allowsubmissionsfromdate' => $groupfrom,
 174                      'duedate' => $groupdue,
 175                  ]);
 176              }
 177          }
 178  
 179          $this->setUser($user);
 180  
 181          $cm = get_coursemodule_from_instance('assign', $assign->id);
 182          // Make sure we're using a cm_info object.
 183          $cm = cm_info::create($cm);
 184  
 185          $dates = activity_dates::get_dates_for_module($cm, (int) $user->id);
 186  
 187          $this->assertEquals($expected, $dates);
 188      }
 189  }