2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 declare(strict_types
=1);
21 use advanced_testcase
;
23 use core\activity_dates
;
26 * Class for unit testing mod_chat\dates.
30 * @copyright 2021 Dongsheng Cai
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 final class dates_test
extends advanced_testcase
{
38 public function setUp(): void
{
40 // Chat module is disabled by default, enable it for testing.
41 $manager = \core_plugin_manager
::resolve_plugininfo_class('mod');
42 $manager::enable_plugin('chat', 1);
46 * Data provider for get_dates_for_module().
49 public static function get_dates_for_module_provider(): array {
51 require_once($CFG->dirroot
. '/mod/chat/lib.php');
54 $past = $now - DAYSECS
;
55 $future = $now + DAYSECS
;
57 $dailynextchattime = $past +
2 * DAYSECS
;
58 $weeklynextchattime = $past +
7 * DAYSECS
;
59 $label = get_string('nextchattime', 'mod_chat');
61 'chattime in the past (no schedule)' => [
62 $past, CHAT_SCHEDULE_NONE
, []
64 'chattime in the past (single schedule)' => [
65 $past, CHAT_SCHEDULE_SINGLE
, []
67 'chattime in the future' => [
68 $future, CHAT_SCHEDULE_SINGLE
, [
71 'timestamp' => $future,
72 'dataid' => 'chattime',
76 'future chattime weekly' => [
77 $future, CHAT_SCHEDULE_WEEKLY
, [
80 'timestamp' => $future,
81 'dataid' => 'chattime',
85 'future chattime daily' => [
86 $future, CHAT_SCHEDULE_DAILY
, [
89 'timestamp' => $future,
90 'dataid' => 'chattime',
94 'past chattime daily' => [
95 $past, CHAT_SCHEDULE_DAILY
, [
98 'timestamp' => $dailynextchattime,
99 'dataid' => 'chattime',
103 'past chattime weekly' => [
104 $past, CHAT_SCHEDULE_WEEKLY
, [
107 'timestamp' => $weeklynextchattime,
108 'dataid' => 'chattime',
116 * Test for get_dates_for_module().
118 * @dataProvider get_dates_for_module_provider
119 * @param int|null $chattime
120 * @param int|null $schedule
121 * @param array $expected The expected value of calling get_dates_for_module()
123 public function test_get_dates_for_module(?
int $chattime, ?
int $schedule, array $expected): void
{
124 $this->resetAfterTest();
125 $course = $this->getDataGenerator()->create_course();
126 $user = $this->getDataGenerator()->create_user();
127 $this->getDataGenerator()->enrol_user($user->id
, $course->id
);
128 $chat = ['course' => $course->id
];
129 $chat['chattime'] = $chattime;
130 $chat['schedule'] = $schedule;
131 $modchat = $this->getDataGenerator()->create_module('chat', $chat);
132 $this->setUser($user);
133 $cm = get_coursemodule_from_instance('chat', $modchat->id
);
134 $cminfo = cm_info
::create($cm);
135 $dates = activity_dates
::get_dates_for_module($cminfo, (int) $user->id
);
136 $this->assertEquals($expected, $dates);