Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [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_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                      ],
  67                  ]
  68              ],
  69              'future chattime weekly' => [
  70                  $future, CHAT_SCHEDULE_WEEKLY, [
  71                      [
  72                          'label' => $label,
  73                          'timestamp' => $future
  74                      ]
  75                  ]
  76              ],
  77              'future chattime daily' => [
  78                  $future, CHAT_SCHEDULE_DAILY, [
  79                      [
  80                          'label' => $label,
  81                          'timestamp' => $future
  82                      ]
  83                  ]
  84              ],
  85              'past chattime daily' => [
  86                  $past, CHAT_SCHEDULE_DAILY, [
  87                      [
  88                          'label' => $label,
  89                          'timestamp' => $dailynextchattime
  90                      ],
  91                  ]
  92              ],
  93              'past chattime weekly' => [
  94                  $past, CHAT_SCHEDULE_WEEKLY, [
  95                      [
  96                          'label' => $label,
  97                          'timestamp' => $weeklynextchattime
  98                      ],
  99                  ]
 100              ],
 101          ];
 102      }
 103  
 104      /**
 105       * Test for get_dates_for_module().
 106       *
 107       * @dataProvider get_dates_for_module_provider
 108       * @param int|null $chattime
 109       * @param int|null $schedule
 110       * @param array $expected The expected value of calling get_dates_for_module()
 111       */
 112      public function test_get_dates_for_module(?int $chattime, ?int $schedule, array $expected) {
 113          $this->resetAfterTest();
 114          $course = $this->getDataGenerator()->create_course();
 115          $user = $this->getDataGenerator()->create_user();
 116          $this->getDataGenerator()->enrol_user($user->id, $course->id);
 117          $chat = ['course' => $course->id];
 118          $chat['chattime'] = $chattime;
 119          $chat['schedule'] = $schedule;
 120          $modchat = $this->getDataGenerator()->create_module('chat', $chat);
 121          $this->setUser($user);
 122          $cm = get_coursemodule_from_instance('chat', $modchat->id);
 123          $cminfo = cm_info::create($cm);
 124          $dates = activity_dates::get_dates_for_module($cminfo, (int) $user->id);
 125          $this->assertEquals($expected, $dates);
 126      }
 127  }