Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
declare(strict_types=1);
18
 
19
namespace mod_chat;
20
 
21
use advanced_testcase;
22
use cm_info;
23
use core\activity_dates;
24
 
25
/**
26
 * Class for unit testing mod_chat\dates.
11 efrain 27
 *
28
 * @package   mod_chat
29
 * @category  test
30
 * @copyright 2021 Dongsheng Cai
31
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
1 efrain 32
 */
11 efrain 33
final class dates_test extends advanced_testcase {
1 efrain 34
 
35
    /**
36
     * Setup testcase.
37
     */
38
    public function setUp(): void {
39
        // Chat module is disabled by default, enable it for testing.
40
        $manager = \core_plugin_manager::resolve_plugininfo_class('mod');
41
        $manager::enable_plugin('chat', 1);
42
    }
43
 
44
    /**
45
     * Data provider for get_dates_for_module().
46
     * @return array[]
47
     */
11 efrain 48
    public static function get_dates_for_module_provider(): array {
1 efrain 49
        global $CFG;
50
        require_once($CFG->dirroot . '/mod/chat/lib.php');
51
 
52
        $now = time();
53
        $past = $now - DAYSECS;
54
        $future = $now + DAYSECS;
55
 
56
        $dailynextchattime = $past + 2 * DAYSECS;
57
        $weeklynextchattime = $past + 7 * DAYSECS;
58
        $label = get_string('nextchattime', 'mod_chat');
59
        return [
11 efrain 60
            'chattime in the past (no schedule)' => [
1 efrain 61
                $past, CHAT_SCHEDULE_NONE, []
62
            ],
11 efrain 63
            'chattime in the past (single schedule)' => [
1 efrain 64
                $past, CHAT_SCHEDULE_SINGLE, []
65
            ],
66
            'chattime in the future' => [
67
                $future, CHAT_SCHEDULE_SINGLE, [
68
                    [
69
                        'label' => $label,
70
                        'timestamp' => $future,
71
                        'dataid' => 'chattime',
72
                    ],
73
                ]
74
            ],
75
            'future chattime weekly' => [
76
                $future, CHAT_SCHEDULE_WEEKLY, [
77
                    [
78
                        'label' => $label,
79
                        'timestamp' => $future,
80
                        'dataid' => 'chattime',
81
                    ]
82
                ]
83
            ],
84
            'future chattime daily' => [
85
                $future, CHAT_SCHEDULE_DAILY, [
86
                    [
87
                        'label' => $label,
88
                        'timestamp' => $future,
89
                        'dataid' => 'chattime',
90
                    ]
91
                ]
92
            ],
93
            'past chattime daily' => [
94
                $past, CHAT_SCHEDULE_DAILY, [
95
                    [
96
                        'label' => $label,
97
                        'timestamp' => $dailynextchattime,
98
                        'dataid' => 'chattime',
99
                    ],
100
                ]
101
            ],
102
            'past chattime weekly' => [
103
                $past, CHAT_SCHEDULE_WEEKLY, [
104
                    [
105
                        'label' => $label,
106
                        'timestamp' => $weeklynextchattime,
107
                        'dataid' => 'chattime',
108
                    ],
109
                ]
110
            ],
111
        ];
112
    }
113
 
114
    /**
115
     * Test for get_dates_for_module().
116
     *
117
     * @dataProvider get_dates_for_module_provider
118
     * @param int|null $chattime
119
     * @param int|null $schedule
120
     * @param array $expected The expected value of calling get_dates_for_module()
121
     */
11 efrain 122
    public function test_get_dates_for_module(?int $chattime, ?int $schedule, array $expected): void {
1 efrain 123
        $this->resetAfterTest();
124
        $course = $this->getDataGenerator()->create_course();
125
        $user = $this->getDataGenerator()->create_user();
126
        $this->getDataGenerator()->enrol_user($user->id, $course->id);
127
        $chat = ['course' => $course->id];
128
        $chat['chattime'] = $chattime;
129
        $chat['schedule'] = $schedule;
130
        $modchat = $this->getDataGenerator()->create_module('chat', $chat);
131
        $this->setUser($user);
132
        $cm = get_coursemodule_from_instance('chat', $modchat->id);
133
        $cminfo = cm_info::create($cm);
134
        $dates = activity_dates::get_dates_for_module($cminfo, (int) $user->id);
135
        $this->assertEquals($expected, $dates);
136
    }
137
}