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_chat\dates.
  19   *
  20   * @package   mod_chat
  21   * @category  test
  22   * @copyright 2021 Dongsheng Cai
  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_chat;
  29  
  30  use advanced_testcase;
  31  use cm_info;
  32  use core\activity_dates;
  33  
  34  /**
  35   * Class for unit testing mod_chat\dates.
  36   */
  37  class dates_test extends advanced_testcase {
  38  
  39      /**
  40       * Data provider for get_dates_for_module().
  41       * @return array[]
  42       */
  43      public function get_dates_for_module_provider(): array {
  44          global $CFG;
  45          require_once($CFG->dirroot . '/mod/chat/lib.php');
  46  
  47          $now = time();
  48          $past = $now - DAYSECS;
  49          $future = $now + DAYSECS;
  50  
  51          $dailynextchattime = $past + 2 * DAYSECS;
  52          $weeklynextchattime = $past + 7 * DAYSECS;
  53          $label = get_string('nextchattime', 'mod_chat');
  54          return [
  55              'chattime in the past' => [
  56                  $past, CHAT_SCHEDULE_NONE, []
  57              ],
  58              'chattime in the past' => [
  59                  $past, CHAT_SCHEDULE_SINGLE, []
  60              ],
  61              'chattime in the future' => [
  62                  $future, CHAT_SCHEDULE_SINGLE, [
  63                      [
  64                          'label' => $label,
  65                          'timestamp' => $future,
  66                          'dataid' => 'chattime',
  67                      ],
  68                  ]
  69              ],
  70              'future chattime weekly' => [
  71                  $future, CHAT_SCHEDULE_WEEKLY, [
  72                      [
  73                          'label' => $label,
  74                          'timestamp' => $future,
  75                          'dataid' => 'chattime',
  76                      ]
  77                  ]
  78              ],
  79              'future chattime daily' => [
  80                  $future, CHAT_SCHEDULE_DAILY, [
  81                      [
  82                          'label' => $label,
  83                          'timestamp' => $future,
  84                          'dataid' => 'chattime',
  85                      ]
  86                  ]
  87              ],
  88              'past chattime daily' => [
  89                  $past, CHAT_SCHEDULE_DAILY, [
  90                      [
  91                          'label' => $label,
  92                          'timestamp' => $dailynextchattime,
  93                          'dataid' => 'chattime',
  94                      ],
  95                  ]
  96              ],
  97              'past chattime weekly' => [
  98                  $past, CHAT_SCHEDULE_WEEKLY, [
  99                      [
 100                          'label' => $label,
 101                          'timestamp' => $weeklynextchattime,
 102                          'dataid' => 'chattime',
 103                      ],
 104                  ]
 105              ],
 106          ];
 107      }
 108  
 109      /**
 110       * Test for get_dates_for_module().
 111       *
 112       * @dataProvider get_dates_for_module_provider
 113       * @param int|null $chattime
 114       * @param int|null $schedule
 115       * @param array $expected The expected value of calling get_dates_for_module()
 116       */
 117      public function test_get_dates_for_module(?int $chattime, ?int $schedule, array $expected) {
 118          $this->resetAfterTest();
 119          $course = $this->getDataGenerator()->create_course();
 120          $user = $this->getDataGenerator()->create_user();
 121          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 122          $chat = ['course' => $course->id];
 123          $chat['chattime'] = $chattime;
 124          $chat['schedule'] = $schedule;
 125          $modchat = $this->getDataGenerator()->create_module('chat', $chat);
 126          $this->setUser($user);
 127          $cm = get_coursemodule_from_instance('chat', $modchat->id);
 128          $cminfo = cm_info::create($cm);
 129          $dates = activity_dates::get_dates_for_module($cminfo, (int) $user->id);
 130          $this->assertEquals($expected, $dates);
 131      }
 132  }