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_scorm\dates.
  19   *
  20   * @package   mod_scorm
  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_scorm;
  29  
  30  use advanced_testcase;
  31  use cm_info;
  32  use core\activity_dates;
  33  
  34  /**
  35   * Class for unit testing mod_scorm\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' => 'timeopen'],
  60                  ]
  61              ],
  62              'only with closing time' => [
  63                  null, $after, [
  64                      ['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeclose'],
  65                  ]
  66              ],
  67              'with both times' => [
  68                  $after, $later, [
  69                      ['label' => 'Opens:', 'timestamp' => $after, 'dataid' => 'timeopen'],
  70                      ['label' => 'Closes:', 'timestamp' => $later, 'dataid' => 'timeclose'],
  71                  ]
  72              ],
  73              'between the dates' => [
  74                  $before, $after, [
  75                      ['label' => 'Opened:', 'timestamp' => $before, 'dataid' => 'timeopen'],
  76                      ['label' => 'Closes:', 'timestamp' => $after, 'dataid' => 'timeclose'],
  77                  ]
  78              ],
  79              'dates are past' => [
  80                  $earlier, $before, [
  81                      ['label' => 'Opened:', 'timestamp' => $earlier, 'dataid' => 'timeopen'],
  82                      ['label' => 'Closed:', 'timestamp' => $before, 'dataid' => 'timeclose'],
  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 $timeopen The 'available from' value of the scorm.
  93       * @param int|null $timeclose The 'available to' value of the scorm.
  94       * @param array $expected The expected value of calling get_dates_for_module()
  95       */
  96      public function test_get_dates_for_module(?int $timeopen, ?int $timeclose, 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 ($timeopen) {
 105              $data['timeopen'] = $timeopen;
 106          }
 107          if ($timeclose) {
 108              $data['timeclose'] = $timeclose;
 109          }
 110          $this->setAdminUser();
 111          $scorm = $this->getDataGenerator()->create_module('scorm', $data);
 112  
 113          $this->setUser($user);
 114  
 115          $cm = get_coursemodule_from_instance('scorm', $scorm->id);
 116          // Make sure we're using a cm_info object.
 117          $cm = cm_info::create($cm);
 118  
 119          $dates = activity_dates::get_dates_for_module($cm, (int) $user->id);
 120  
 121          $this->assertEquals($expected, $dates);
 122      }
 123  }