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_data\dates.
  19   *
  20   * @package   mod_data
  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_data;
  29  
  30  use advanced_testcase;
  31  use cm_info;
  32  use core\activity_dates;
  33  
  34  /**
  35   * Class for unit testing mod_data\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, []
  56              ],
  57              'only with opening time' => [
  58                  $after, null, [
  59                      ['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeavailablefrom'],
  60                  ]
  61              ],
  62              'only with closing time' => [
  63                  null, $after, [
  64                      ['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeavailableto'],
  65                  ]
  66              ],
  67              'with both times' => [
  68                  $after, $later, [
  69                      ['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeavailablefrom'],
  70                      ['label' => 'Closes:', 'timestamp' => $later, 'dataid' => 'timeavailableto'],
  71                  ]
  72              ],
  73              'between the dates' => [
  74                  $before, $after, [
  75                      ['label' => 'Opened:', 'timestamp' => $before, 'dataid' => 'timeavailablefrom'],
  76                      ['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeavailableto'],
  77                  ]
  78              ],
  79              'dates are past' => [
  80                  $earlier, $before, [
  81                      ['label' => 'Opened:', 'timestamp' => $earlier, 'dataid' => 'timeavailablefrom'],
  82                      ['label' => 'Closed:', 'timestamp' => $before, 'dataid' => 'timeavailableto'],
  83                  ]
  84              ],
  85          ];
  86      }
  87  
  88      /**
  89       * Test for get_dates_for_module().
  90       *
  91       * @dataProvider get_dates_for_module_provider
  92       * @param int|null $availablefrom The "available from" time in the database activity.
  93       * @param int|null $availableto The "available to" time in the database activity.
  94       * @param array $expected The expected value of calling get_dates_for_module()
  95       */
  96      public function test_get_dates_for_module(?int $availablefrom, ?int $availableto, array $expected) {
  97          $this->resetAfterTest();
  98  
  99          $course = $this->getDataGenerator()->create_course();
 100          $user = $this->getDataGenerator()->create_user();
 101          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 102  
 103          $data = ['course' => $course->id];
 104          if ($availablefrom) {
 105              $data['timeavailablefrom'] = $availablefrom;
 106          }
 107          if ($availableto) {
 108              $data['timeavailableto'] = $availableto;
 109          }
 110          $moddata = $this->getDataGenerator()->create_module('data', $data);
 111  
 112          $this->setUser($user);
 113  
 114          $cm = get_coursemodule_from_instance('data', $moddata->id);
 115          // Make sure we're using a cm_info object.
 116          $cm = cm_info::create($cm);
 117  
 118          $dates = activity_dates::get_dates_for_module($cm, (int) $user->id);
 119  
 120          $this->assertEquals($expected, $dates);
 121      }
 122  }