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 humantimeperiod_test class.
|
|
|
23 |
*
|
|
|
24 |
* @covers \core_calendar\output\humantimeperiod
|
|
|
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 humantimeperiod_test extends \advanced_testcase {
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Test format_period() method.
|
|
|
34 |
*
|
|
|
35 |
* @dataProvider provider_format_period
|
|
|
36 |
* @param int|null $addsecondsend The number of seconds to add to the current time for the end date.
|
|
|
37 |
* @param bool $expectedendnull Whether the end date is null.
|
|
|
38 |
*/
|
|
|
39 |
public function test_format_period(
|
|
|
40 |
?int $addsecondsend,
|
|
|
41 |
bool $expectedendnull,
|
|
|
42 |
): void {
|
|
|
43 |
$this->resetAfterTest();
|
|
|
44 |
|
|
|
45 |
// 26 February 2025 15:30:00 (GMT).
|
|
|
46 |
$clock = $this->mock_clock_with_frozen(1740583800);
|
|
|
47 |
|
|
|
48 |
$timestampstart = $clock->time();
|
|
|
49 |
$timestampend = is_null($addsecondsend) ? null : $clock->time() + $addsecondsend;
|
|
|
50 |
$humandateend = null;
|
|
|
51 |
if (!$expectedendnull) {
|
|
|
52 |
$humandateend = humandate::create_from_timestamp(
|
|
|
53 |
timestamp: $timestampend,
|
|
|
54 |
timeonly: (abs($addsecondsend) < 1800),
|
|
|
55 |
);
|
|
|
56 |
}
|
|
|
57 |
$expected = [
|
|
|
58 |
'startdate' => humandate::create_from_timestamp($timestampstart),
|
|
|
59 |
'enddate' => $humandateend,
|
|
|
60 |
];
|
|
|
61 |
$humantimeperiod = humantimeperiod::create_from_timestamp($timestampstart, $timestampend);
|
|
|
62 |
|
|
|
63 |
$reflection = new \ReflectionClass($humantimeperiod);
|
|
|
64 |
$method = $reflection->getMethod('format_period');
|
|
|
65 |
$method->setAccessible(true);
|
|
|
66 |
$result = $method->invoke($humantimeperiod);
|
|
|
67 |
$this->compare_output($expected, $result);
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
/**
|
|
|
71 |
* Data provider.
|
|
|
72 |
*
|
|
|
73 |
* @return array
|
|
|
74 |
*/
|
|
|
75 |
public static function provider_format_period(): array {
|
|
|
76 |
return [
|
|
|
77 |
'Same start and end' => [
|
|
|
78 |
'addsecondsend' => 0,
|
|
|
79 |
'expectedendnull' => true,
|
|
|
80 |
],
|
|
|
81 |
'Null end date' => [
|
|
|
82 |
'addsecondsend' => null,
|
|
|
83 |
'expectedendnull' => true,
|
|
|
84 |
],
|
|
|
85 |
'1 day ahead' => [
|
|
|
86 |
'addsecondsend' => 87000,
|
|
|
87 |
'expectedendnull' => false,
|
|
|
88 |
],
|
|
|
89 |
'1 day behind' => [
|
|
|
90 |
'addsecondsend' => -87000,
|
|
|
91 |
'expectedendnull' => false,
|
|
|
92 |
],
|
|
|
93 |
'29 minutes ahead' => [
|
|
|
94 |
'addsecondsend' => 1799,
|
|
|
95 |
'expectedendnull' => false,
|
|
|
96 |
],
|
|
|
97 |
'30 minutes ahead (Midnight)' => [
|
|
|
98 |
'addsecondsend' => 1800,
|
|
|
99 |
'expectedendnull' => false,
|
|
|
100 |
],
|
|
|
101 |
'31 minutes ahead (Midnight)' => [
|
|
|
102 |
'addsecondsend' => 1801,
|
|
|
103 |
'expectedendnull' => false,
|
|
|
104 |
],
|
|
|
105 |
];
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
/**
|
|
|
109 |
* Test create_from_timestamp.
|
|
|
110 |
*/
|
|
|
111 |
public function test_create_from_timestamp(): void {
|
|
|
112 |
$this->resetAfterTest();
|
|
|
113 |
|
|
|
114 |
$clock = \core\di::get(\core\clock::class);
|
|
|
115 |
$timestamp = $clock->time();
|
|
|
116 |
$humantimeperiod = humantimeperiod::create_from_timestamp($timestamp, $timestamp + 3600);
|
|
|
117 |
$this->assertInstanceOf(humantimeperiod::class, $humantimeperiod);
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* Test create_from_timestamp with enddatetime null.
|
|
|
122 |
*/
|
|
|
123 |
public function test_create_from_timestamp_endnull(): void {
|
|
|
124 |
$this->resetAfterTest();
|
|
|
125 |
|
|
|
126 |
$clock = $this->mock_clock_with_frozen();
|
|
|
127 |
$timestamp = $clock->time();
|
|
|
128 |
$humantimeperiod = humantimeperiod::create_from_timestamp($timestamp, null);
|
|
|
129 |
$this->assertInstanceOf(humantimeperiod::class, $humantimeperiod);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Test create_from_datetime.
|
|
|
134 |
*/
|
|
|
135 |
public function test_create_from_datetime(): void {
|
|
|
136 |
$this->resetAfterTest();
|
|
|
137 |
|
|
|
138 |
$humantimeperiod = humantimeperiod::create_from_datetime(new DateTime(), new DateTime('+1 hour'));
|
|
|
139 |
$this->assertInstanceOf(humantimeperiod::class, $humantimeperiod);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
/**
|
|
|
143 |
* Test create_from_datetime with enddatetime null.
|
|
|
144 |
*/
|
|
|
145 |
public function test_create_from_datetime_endnull(): void {
|
|
|
146 |
$this->resetAfterTest();
|
|
|
147 |
|
|
|
148 |
$humantimeperiod = humantimeperiod::create_from_datetime(new DateTime(), null);
|
|
|
149 |
$this->assertInstanceOf(humantimeperiod::class, $humantimeperiod);
|
|
|
150 |
}
|
|
|
151 |
/**
|
|
|
152 |
* Compare humantimeperiod output.
|
|
|
153 |
*
|
|
|
154 |
* @param array $expected The expected output.
|
|
|
155 |
* @param array $actual The actual output.
|
|
|
156 |
*/
|
|
|
157 |
protected function compare_output(
|
|
|
158 |
array $expected,
|
|
|
159 |
array $actual,
|
|
|
160 |
): void {
|
|
|
161 |
$fields = ['startdate', 'enddate'];
|
|
|
162 |
foreach ($fields as $field) {
|
|
|
163 |
$this->assertEquals($expected[$field], $actual[$field], "Field $field does not match");
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
}
|