Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [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_workshop\dates.
  19   *
  20   * @package   mod_workshop
  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_workshop;
  29  
  30  use advanced_testcase;
  31  use cm_info;
  32  use core\activity_dates;
  33  
  34  /**
  35   * Class for unit testing mod_workshop\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          $earliest = $earlier - DAYSECS;
  51          $after = $now + DAYSECS;
  52          $later = $after + DAYSECS;
  53          $latest = $later + DAYSECS;
  54  
  55          return [
  56              'without any dates' => [
  57                  null, null, null, null, []
  58              ],
  59              'only with start time for submissions' => [
  60                  $after, null, null, null, [
  61                      ['label' => 'Submissions open:', 'timestamp' => $after, 'dataid' => 'submissionstart'],
  62                  ]
  63              ],
  64              'only with end time for submissions' => [
  65                  null, $after, null, null, [
  66                      ['label' => 'Submissions close:', 'timestamp' => $after, 'dataid' => 'submissionend'],
  67                  ]
  68              ],
  69              'only with start time for assessments' => [
  70                  null, null, $after, null, [
  71                      ['label' => 'Assessments open:', 'timestamp' => $after, 'dataid' => 'assessmentstart'],
  72                  ]
  73              ],
  74              'only with end time for assessments' => [
  75                  null, null, null, $after, [
  76                      ['label' => 'Assessments close:', 'timestamp' => $after, 'dataid' => 'assessmentend'],
  77                  ]
  78              ],
  79              'all times in future' => [
  80                  $after, $later, $latest, $latest + DAYSECS, [
  81                      ['label' => 'Submissions open:', 'timestamp' => $after, 'dataid' => 'submissionstart'],
  82                      ['label' => 'Submissions close:', 'timestamp' => $later, 'dataid' => 'submissionend'],
  83                      ['label' => 'Assessments open:', 'timestamp' => $latest, 'dataid' => 'assessmentstart'],
  84                      ['label' => 'Assessments close:', 'timestamp' => $latest + DAYSECS, 'dataid' => 'assessmentend'],
  85                  ]
  86              ],
  87              'all times in the past' => [
  88                  $earliest - DAYSECS, $earliest, $earlier, $before, [
  89                      ['label' => 'Submissions opened:', 'timestamp' => $earliest - DAYSECS, 'dataid' => 'submissionstart'],
  90                      ['label' => 'Submissions closed:', 'timestamp' => $earliest, 'dataid' => 'submissionend'],
  91                      ['label' => 'Assessments opened:', 'timestamp' => $earlier, 'dataid' => 'assessmentstart'],
  92                      ['label' => 'Assessments closed:', 'timestamp' => $before, 'dataid' => 'assessmentend'],
  93                  ]
  94              ],
  95              'between submission and assessment' => [
  96                  $earlier, $before, $after, $later, [
  97                      ['label' => 'Submissions opened:', 'timestamp' => $earlier, 'dataid' => 'submissionstart'],
  98                      ['label' => 'Submissions closed:', 'timestamp' => $before, 'dataid' => 'submissionend'],
  99                      ['label' => 'Assessments open:', 'timestamp' => $after, 'dataid' => 'assessmentstart'],
 100                      ['label' => 'Assessments close:', 'timestamp' => $later, 'dataid' => 'assessmentend'],
 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 $submissionstart The 'Open for submissions from' value of the workshop.
 111       * @param int|null $submissionend The 'Submissions deadline' value of the workshop.
 112       * @param int|null $assessmentstart The 'Open for assessment from' value of the workshop.
 113       * @param int|null $assessmentend The 'Deadline for assessment' value of the workshop.
 114       * @param array $expected The expected value of calling get_dates_for_module()
 115       */
 116      public function test_get_dates_for_module(?int $submissionstart, ?int $submissionend,
 117              ?int $assessmentstart, ?int $assessmentend,
 118              array $expected) {
 119  
 120          $this->resetAfterTest();
 121  
 122          $course = $this->getDataGenerator()->create_course();
 123          $user = $this->getDataGenerator()->create_user();
 124          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 125  
 126          $data = ['course' => $course->id];
 127          if ($submissionstart) {
 128              $data['submissionstart'] = $submissionstart;
 129          }
 130          if ($submissionend) {
 131              $data['submissionend'] = $submissionend;
 132          }
 133          if ($assessmentstart) {
 134              $data['assessmentstart'] = $assessmentstart;
 135          }
 136          if ($assessmentend) {
 137              $data['assessmentend'] = $assessmentend;
 138          }
 139          $this->setAdminUser();
 140          $workshop = $this->getDataGenerator()->create_module('workshop', $data);
 141  
 142          $this->setUser($user);
 143  
 144          $cm = get_coursemodule_from_instance('workshop', $workshop->id);
 145          // Make sure we're using a cm_info object.
 146          $cm = cm_info::create($cm);
 147  
 148          $dates = activity_dates::get_dates_for_module($cm, (int) $user->id);
 149  
 150          $this->assertEquals($expected, $dates);
 151      }
 152  }