Proyectos de Subversion Moodle

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
     * Setup testcase.
41
     */
42
    public function setUp(): void {
43
        // Chat module is disabled by default, enable it for testing.
44
        $manager = \core_plugin_manager::resolve_plugininfo_class('mod');
45
        $manager::enable_plugin('chat', 1);
46
    }
47
 
48
    /**
49
     * Data provider for get_dates_for_module().
50
     * @return array[]
51
     */
52
    public function get_dates_for_module_provider(): array {
53
        global $CFG;
54
        require_once($CFG->dirroot . '/mod/chat/lib.php');
55
 
56
        $now = time();
57
        $past = $now - DAYSECS;
58
        $future = $now + DAYSECS;
59
 
60
        $dailynextchattime = $past + 2 * DAYSECS;
61
        $weeklynextchattime = $past + 7 * DAYSECS;
62
        $label = get_string('nextchattime', 'mod_chat');
63
        return [
64
            'chattime in the past' => [
65
                $past, CHAT_SCHEDULE_NONE, []
66
            ],
67
            'chattime in the past' => [
68
                $past, CHAT_SCHEDULE_SINGLE, []
69
            ],
70
            'chattime in the future' => [
71
                $future, CHAT_SCHEDULE_SINGLE, [
72
                    [
73
                        'label' => $label,
74
                        'timestamp' => $future,
75
                        'dataid' => 'chattime',
76
                    ],
77
                ]
78
            ],
79
            'future chattime weekly' => [
80
                $future, CHAT_SCHEDULE_WEEKLY, [
81
                    [
82
                        'label' => $label,
83
                        'timestamp' => $future,
84
                        'dataid' => 'chattime',
85
                    ]
86
                ]
87
            ],
88
            'future chattime daily' => [
89
                $future, CHAT_SCHEDULE_DAILY, [
90
                    [
91
                        'label' => $label,
92
                        'timestamp' => $future,
93
                        'dataid' => 'chattime',
94
                    ]
95
                ]
96
            ],
97
            'past chattime daily' => [
98
                $past, CHAT_SCHEDULE_DAILY, [
99
                    [
100
                        'label' => $label,
101
                        'timestamp' => $dailynextchattime,
102
                        'dataid' => 'chattime',
103
                    ],
104
                ]
105
            ],
106
            'past chattime weekly' => [
107
                $past, CHAT_SCHEDULE_WEEKLY, [
108
                    [
109
                        'label' => $label,
110
                        'timestamp' => $weeklynextchattime,
111
                        'dataid' => 'chattime',
112
                    ],
113
                ]
114
            ],
115
        ];
116
    }
117
 
118
    /**
119
     * Test for get_dates_for_module().
120
     *
121
     * @dataProvider get_dates_for_module_provider
122
     * @param int|null $chattime
123
     * @param int|null $schedule
124
     * @param array $expected The expected value of calling get_dates_for_module()
125
     */
126
    public function test_get_dates_for_module(?int $chattime, ?int $schedule, array $expected) {
127
        $this->resetAfterTest();
128
        $course = $this->getDataGenerator()->create_course();
129
        $user = $this->getDataGenerator()->create_user();
130
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
131
        $chat = ['course' => $course->id];
132
        $chat['chattime'] = $chattime;
133
        $chat['schedule'] = $schedule;
134
        $modchat = $this->getDataGenerator()->create_module('chat', $chat);
135
        $this->setUser($user);
136
        $cm = get_coursemodule_from_instance('chat', $modchat->id);
137
        $cminfo = cm_info::create($cm);
138
        $dates = activity_dates::get_dates_for_module($cminfo, (int) $user->id);
139
        $this->assertEquals($expected, $dates);
140
    }
141
}