Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
namespace core_calendar\output;
18
 
19
use DateTime;
20
 
21
/**
22
 * Tests for humandate class.
23
 *
24
 * @covers     \core_calendar\output\humandate
25
 * @package    core_calendar
26
 * @category   test
27
 * @copyright  2025 Sara Arjona <sara@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
30
final class humandate_test extends \advanced_testcase {
31
 
32
    /**
33
     * Initialize.
34
     */
35
    protected function setUp(): void {
36
        parent::setUp();
37
 
38
        // Mock the clock.
39
        $this->setTimezone('Australia/Perth');
40
    }
41
 
42
    /**
43
     * Test export_for_template() method.
44
     *
45
     * @dataProvider provider_export_for_template
46
     * @param int $addseconds The number of seconds to add to the current time.
47
     * @param bool $userelatives Whether to use relative dates.
48
     * @param string|null $date For relative dates, the expected string (Tomorrow, Today, Yesterday).
49
     * @param bool $ispast Whether the date is in the past.
50
     * @param bool $needtitle Whether the date needs a title.
51
     * @param bool $isnear Whether the date is near.
52
     * @param string $userdateformat The user date expected format.
53
     */
54
    public function test_export_for_template(
55
        int $addseconds,
56
        bool $userelatives,
57
        ?string $date,
58
        bool $ispast,
59
        bool $needtitle,
60
        bool $isnear,
61
        string $userdateformat,
62
    ): void {
63
        global $PAGE;
64
 
65
        $this->resetAfterTest();
66
 
67
        // 26 February 2025 15:59:59 (GMT).
68
        $clock = $this->mock_clock_with_frozen(1740585599);
69
        $renderer = $PAGE->get_renderer('core');
70
 
71
        $timestamp = $clock->time() + $addseconds;
72
        $expected = [
73
            'timestamp' => $timestamp,
74
            'date' => $date,
75
            'userdate' => userdate($timestamp, get_string($userdateformat)),
76
            'ispast' => $ispast,
77
            'needtitle' => $needtitle,
78
            'isnear' => $isnear,
79
        ];
80
        $humandate = humandate::create_from_timestamp($timestamp);
81
        $humandate->set_use_relatives($userelatives);
82
        $result = $humandate->export_for_template($renderer);
83
        $this->compare_output($expected, $result, $userelatives);
84
    }
85
 
86
    /**
87
     * Data provider.
88
     *
89
     * @return array
90
     */
91
    public static function provider_export_for_template(): array {
92
        return [
93
            'Now with relatives' => [
94
                'addseconds' => 0,
95
                'userelatives' => true,
96
                'date' => 'Today',
97
                'ispast' => false,
98
                'needtitle' => true,
99
                'isnear' => false,
100
                'userdateformat' => 'strftimedayshort',
101
            ],
102
            'Tomorrow with relatives' => [
103
                'addseconds' => DAYSECS,
104
                'userelatives' => true,
105
                'date' => 'Tomorrow',
106
                'ispast' => false,
107
                'needtitle' => true,
108
                'isnear' => false,
109
                'userdateformat' => 'strftimedayshort',
110
            ],
111
            'Yesterday with relatives' => [
112
                'addseconds' => -DAYSECS,
113
                'userelatives' => true,
114
                'date' => 'Yesterday',
115
                'ispast' => true,
116
                'needtitle' => true,
117
                'isnear' => false,
118
                'userdateformat' => 'strftimedayshort',
119
            ],
120
            'One hour future with relatives' => [
121
                'addseconds' => HOURSECS,
122
                'userelatives' => true,
123
                'date' => 'Tomorrow',
124
                'ispast' => false,
125
                'needtitle' => true,
126
                'isnear' => true,
127
                'userdateformat' => 'strftimedayshort',
128
            ],
129
            'One hour past with relatives' => [
130
                'addseconds' => -HOURSECS,
131
                'userelatives' => true,
132
                'date' => 'Today',
133
                'ispast' => true,
134
                'needtitle' => true,
135
                'isnear' => false,
136
                'userdateformat' => 'strftimedayshort',
137
            ],
138
            'Now without relatives' => [
139
                'addseconds' => 0,
140
                'userelatives' => false,
141
                'date' => 'Today',
142
                'ispast' => false,
143
                'needtitle' => false,
144
                'isnear' => false,
145
                'userdateformat' => 'strftimedayshort',
146
            ],
147
            'Tomorrow without relatives' => [
148
                'addseconds' => DAYSECS,
149
                'userelatives' => false,
150
                'date' => 'Tomorrow',
151
                'ispast' => false,
152
                'needtitle' => false,
153
                'isnear' => false,
154
                'userdateformat' => 'strftimedayshort',
155
            ],
156
            'Yesterday without relatives' => [
157
                'addseconds' => -DAYSECS,
158
                'userelatives' => false,
159
                'date' => 'Yesterday',
160
                'ispast' => true,
161
                'needtitle' => false,
162
                'isnear' => false,
163
                'userdateformat' => 'strftimedayshort',
164
            ],
165
            'One hour future without relatives' => [
166
                'addseconds' => HOURSECS,
167
                'userelatives' => false,
168
                'date' => null,
169
                'ispast' => false,
170
                'needtitle' => false,
171
                'isnear' => true,
172
                'userdateformat' => 'strftimedayshort',
173
            ],
174
            'One hour past without relatives' => [
175
                'addseconds' => -HOURSECS,
176
                'userelatives' => false,
177
                'date' => null,
178
                'ispast' => true,
179
                'needtitle' => false,
180
                'isnear' => false,
181
                'userdateformat' => 'strftimedayshort',
182
            ],
183
            'one year from now' => [
184
                'addseconds' => YEARSECS,
185
                'userelatives' => false,
186
                'date' => null,
187
                'ispast' => false,
188
                'needtitle' => false,
189
                'isnear' => false,
190
                'userdateformat' => 'strftimedaydate',
191
            ],
192
            'one year in the past' => [
193
                'addseconds' => -YEARSECS,
194
                'userelatives' => false,
195
                'date' => null,
196
                'ispast' => true,
197
                'needtitle' => false,
198
                'isnear' => false,
199
                'userdateformat' => 'strftimedaydate',
200
            ],
201
        ];
202
    }
203
 
204
    public function test_create_from_timestamp(): void {
205
        $this->resetAfterTest();
206
 
207
        $clock = $this->mock_clock_with_frozen();
208
        $timestamp = $clock->time();
209
        $humandate = humandate::create_from_timestamp($timestamp);
210
        $this->assertInstanceOf(humandate::class, $humandate);
211
    }
212
 
213
    public function test_create_from_datetime(): void {
214
        $this->resetAfterTest();
215
 
216
        $humandate = humandate::create_from_datetime(new DateTime());
217
        $this->assertInstanceOf(humandate::class, $humandate);
218
    }
219
 
220
    /**
221
     * Compare humandate output.
222
     *
223
     * @param array $expected The expected output.
224
     * @param array $actual The actual output.
225
     * @param bool $userelatives Whether to use relative dates.
226
     */
227
    protected function compare_output(
228
        array $expected,
229
        array $actual,
230
        bool $userelatives,
231
    ): void {
232
        $fields = ['timestamp', 'userdate', 'ispast', 'needtitle'];
233
        foreach ($fields as $field) {
234
            $this->assertEquals($expected[$field], $actual[$field], "Field $field does not match");
235
        }
236
 
237
        if ($expected['isnear']) {
238
            $this->assertEquals($expected[$field], $actual[$field], "Field isnear does not match");
239
        } else {
240
            $this->assertArrayNotHasKey('isnear', $actual);
241
        }
242
 
243
        if (!is_null($expected['date'])) {
244
            if ($userelatives) {
245
                $this->assertStringContainsString($expected['date'], $actual['date']);
246
            } else {
247
                $this->assertStringNotContainsString($expected['date'], $actual['date']);
248
            }
249
        } else {
250
            $this->assertStringNotContainsString('Today', $actual['date']);
251
            $this->assertStringNotContainsString('Yesterday', $actual['date']);
252
            $this->assertStringNotContainsString('Tomorrow', $actual['date']);
253
        }
254
    }
255
}