1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of the customcert module for 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 |
* Test datarange element.
|
|
|
19 |
*
|
|
|
20 |
* @package customcertelement_daterange
|
|
|
21 |
* @copyright 2018 Dmitrii Metelkin <dmitriim@catalyst-au.net>
|
|
|
22 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
23 |
*/
|
|
|
24 |
|
|
|
25 |
namespace customcertelement_daterange;
|
|
|
26 |
|
|
|
27 |
use stdClass;
|
|
|
28 |
use advanced_testcase;
|
|
|
29 |
use fake_datarange_element;
|
|
|
30 |
|
|
|
31 |
defined('MOODLE_INTERNAL') || die();
|
|
|
32 |
|
|
|
33 |
global $CFG;
|
|
|
34 |
|
|
|
35 |
require_once($CFG->dirroot . '/mod/customcert/element/daterange/tests/fixtures/fake_datarange_element.php');
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* Test datarange element.
|
|
|
39 |
*
|
|
|
40 |
* @package customcertelement_daterange
|
|
|
41 |
* @copyright 2018 Dmitrii Metelkin <dmitriim@catalyst-au.net>
|
|
|
42 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
43 |
*/
|
|
|
44 |
class element_test extends advanced_testcase {
|
|
|
45 |
|
|
|
46 |
/**
|
|
|
47 |
* Helper function to build element data.
|
|
|
48 |
*
|
|
|
49 |
* @param stdClass $data Element data.
|
|
|
50 |
*
|
|
|
51 |
* @return object
|
|
|
52 |
*/
|
|
|
53 |
protected function build_element_data(stdClass $data) {
|
|
|
54 |
return (object) [
|
|
|
55 |
'id' => 1,
|
|
|
56 |
'pageid' => 1,
|
|
|
57 |
'name' => 'Test',
|
|
|
58 |
'data' => json_encode($data),
|
|
|
59 |
'font' => 'Font',
|
|
|
60 |
'fontsize' => 1,
|
|
|
61 |
'colour' => '#EEE',
|
|
|
62 |
'posx' => 0,
|
|
|
63 |
'posy' => 0,
|
|
|
64 |
'width' => 100,
|
|
|
65 |
'refpoint' => 1,
|
|
|
66 |
];
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
* Helper function to build datarange data.
|
|
|
71 |
*
|
|
|
72 |
* @param array $dataranges A list of dataranges.
|
|
|
73 |
* @param string $fallbackstring Fall back string.
|
|
|
74 |
*
|
|
|
75 |
* @return object
|
|
|
76 |
*/
|
|
|
77 |
protected function build_datarange_data(array $dataranges, $fallbackstring = '') {
|
|
|
78 |
return (object) [
|
|
|
79 |
'dateitem' => 1,
|
|
|
80 |
'fallbackstring' => $fallbackstring,
|
|
|
81 |
'numranges' => count($dataranges),
|
|
|
82 |
'dateranges' => $dataranges,
|
|
|
83 |
];
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
/**
|
|
|
87 |
* A helper function to get datarange element for testing.
|
|
|
88 |
*
|
|
|
89 |
* @param array $dataranges A list of dataranges.
|
|
|
90 |
* @param string $fallbackstring Fall back strin
|
|
|
91 |
*
|
|
|
92 |
* @return fake_datarange_element
|
|
|
93 |
*/
|
|
|
94 |
protected function get_datarange_element(array $dataranges, $fallbackstring = '') {
|
|
|
95 |
$datarangedata = $this->build_datarange_data($dataranges, $fallbackstring);
|
|
|
96 |
$elementdata = $this->build_element_data($datarangedata);
|
|
|
97 |
|
|
|
98 |
return new fake_datarange_element($elementdata);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Data provider for test_get_daterange_string_for_recurring_ranges.
|
|
|
103 |
* @return array
|
|
|
104 |
*/
|
|
|
105 |
public static function get_test_get_daterange_string_for_recurring_ranges_data_provider(): array {
|
|
|
106 |
return [
|
|
|
107 |
['1.11.2016', 'WS 2016/2017'],
|
|
|
108 |
['1.11.2017', 'WS 2017/2018'],
|
|
|
109 |
['1.11.2018', 'WS 2018/2019'],
|
|
|
110 |
['1.11.2019', 'WS 2019/2020'],
|
|
|
111 |
['1.02.2017', 'WS 2016/2017'],
|
|
|
112 |
['1.02.2018', 'WS 2017/2018'],
|
|
|
113 |
['1.02.2019', 'WS 2018/2019'],
|
|
|
114 |
['1.02.2020', 'WS 2019/2020'],
|
|
|
115 |
['1.05.2016', 'SS 2016'],
|
|
|
116 |
['1.05.2017', 'SS 2017'],
|
|
|
117 |
['1.05.2018', 'SS 2018'],
|
|
|
118 |
['1.05.2019', 'SS 2019'],
|
|
|
119 |
];
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
/**
|
|
|
123 |
* Test get correct strings for recurring ranges.
|
|
|
124 |
*
|
|
|
125 |
* @dataProvider get_test_get_daterange_string_for_recurring_ranges_data_provider
|
|
|
126 |
* @covers \element::get_daterange_string
|
|
|
127 |
*
|
|
|
128 |
* @param string $date Date to test.
|
|
|
129 |
* @param string $expected Expected result.
|
|
|
130 |
*/
|
|
|
131 |
public function test_get_daterange_string_for_recurring_ranges($date, $expected) {
|
|
|
132 |
$dateranges = [
|
|
|
133 |
(object)[
|
|
|
134 |
'startdate' => strtotime('01.04.2017'),
|
|
|
135 |
'enddate' => strtotime('30.09.2017'),
|
|
|
136 |
'datestring' => 'SS {{date_year}}',
|
|
|
137 |
'recurring' => true,
|
|
|
138 |
],
|
|
|
139 |
(object)[
|
|
|
140 |
'startdate' => strtotime('01.10.2017'),
|
|
|
141 |
'enddate' => strtotime('31.03.2018'),
|
|
|
142 |
'datestring' => 'WS {{recurring_range_first_year}}/{{recurring_range_last_year}}',
|
|
|
143 |
'recurring' => true,
|
|
|
144 |
],
|
|
|
145 |
];
|
|
|
146 |
|
|
|
147 |
$element = $this->get_datarange_element($dateranges);
|
|
|
148 |
$date = strtotime($date);
|
|
|
149 |
$this->assertEquals($expected, $element->get_daterange_string($date));
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Test that first found element matched.
|
|
|
154 |
*
|
|
|
155 |
* @covers \element::get_daterange_string
|
|
|
156 |
*/
|
|
|
157 |
public function test_that_first_matched_range_applied_first() {
|
|
|
158 |
$dateranges = [
|
|
|
159 |
(object)[
|
|
|
160 |
'startdate' => strtotime('01.04.2017'),
|
|
|
161 |
'enddate' => strtotime('30.09.2017'),
|
|
|
162 |
'datestring' => 'First range',
|
|
|
163 |
'recurring' => false,
|
|
|
164 |
],
|
|
|
165 |
(object)[
|
|
|
166 |
'startdate' => strtotime('01.05.2017'),
|
|
|
167 |
'enddate' => strtotime('01.07.2018'),
|
|
|
168 |
'datestring' => 'Second range',
|
|
|
169 |
'recurring' => false,
|
|
|
170 |
],
|
|
|
171 |
];
|
|
|
172 |
|
|
|
173 |
$element = $this->get_datarange_element($dateranges);
|
|
|
174 |
$date = strtotime('1.06.2017');
|
|
|
175 |
$this->assertEquals('First range', $element->get_daterange_string($date));
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
/**
|
|
|
179 |
* Test that placeholders correctly applied to matched range and fall back string.
|
|
|
180 |
*
|
|
|
181 |
* @covers \element::get_daterange_string
|
|
|
182 |
*/
|
|
|
183 |
public function test_placeholders_and_fall_back_string() {
|
|
|
184 |
$dateranges = [
|
|
|
185 |
(object)[
|
|
|
186 |
'startdate' => strtotime('01.04.2017'),
|
|
|
187 |
'enddate' => strtotime('30.09.2018'),
|
|
|
188 |
'datestring' => '{{current_year}} - {{range_first_year}} - {{range_last_year}} - {{date_year}}',
|
|
|
189 |
'recurring' => false,
|
|
|
190 |
],
|
|
|
191 |
];
|
|
|
192 |
|
|
|
193 |
$fallbackstring = '{{current_year}} - {{range_first_year}} - {{range_last_year}} - {{date_year}}';
|
|
|
194 |
$element = $this->get_datarange_element($dateranges, $fallbackstring);
|
|
|
195 |
|
|
|
196 |
$date = strtotime('1.01.2000');
|
|
|
197 |
$expected = date('Y', time()) . ' - {{range_first_year}} - {{range_last_year}} - 2000';
|
|
|
198 |
$this->assertEquals($expected, $element->get_daterange_string($date));
|
|
|
199 |
|
|
|
200 |
$date = strtotime('1.07.2017');
|
|
|
201 |
$expected = date('Y', time()) . ' - 2017 - 2018 - 2017';
|
|
|
202 |
$this->assertEquals($expected, $element->get_daterange_string($date));
|
|
|
203 |
}
|
|
|
204 |
|
|
|
205 |
/**
|
|
|
206 |
* Test that nothing will be displayed if not matched and empty fall back string.
|
|
|
207 |
*
|
|
|
208 |
* @covers \element::get_daterange_string
|
|
|
209 |
*/
|
|
|
210 |
public function test_nothing_will_be_displayed_if_empty_fallback_string() {
|
|
|
211 |
$dateranges = [
|
|
|
212 |
(object)[
|
|
|
213 |
'startdate' => strtotime('01.04.2017'),
|
|
|
214 |
'enddate' => strtotime('30.09.2018'),
|
|
|
215 |
'datestring' => '{{current_year}} - {{range_first_year}} - {{range_last_year}} - {{date_year}}',
|
|
|
216 |
'recurring' => false,
|
|
|
217 |
],
|
|
|
218 |
];
|
|
|
219 |
|
|
|
220 |
$fallbackstring = '';
|
|
|
221 |
$element = $this->get_datarange_element($dateranges, $fallbackstring);
|
|
|
222 |
|
|
|
223 |
$date = strtotime('1.07.2011');
|
|
|
224 |
$this->assertEquals($fallbackstring, $element->get_daterange_string($date));
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* Test that display recurring_range_first_year and recurring_range_last_year placeholders.
|
|
|
229 |
*
|
|
|
230 |
* @covers \element::get_daterange_string
|
|
|
231 |
*/
|
|
|
232 |
public function test_recurring_range_first_year_and_recurring_range_last_year_placeholders() {
|
|
|
233 |
$datestring = '{{range_first_year}}-{{range_last_year}}-{{recurring_range_first_year}}-{{recurring_range_last_year}}';
|
|
|
234 |
$dateranges = [
|
|
|
235 |
(object) [
|
|
|
236 |
'startdate' => strtotime('01.04.2017'),
|
|
|
237 |
'enddate' => strtotime('30.09.2017'),
|
|
|
238 |
'datestring' => $datestring,
|
|
|
239 |
'recurring' => true,
|
|
|
240 |
],
|
|
|
241 |
(object)[
|
|
|
242 |
'startdate' => strtotime('01.10.2017'),
|
|
|
243 |
'enddate' => strtotime('31.03.2018'),
|
|
|
244 |
'datestring' => $datestring,
|
|
|
245 |
'recurring' => true,
|
|
|
246 |
],
|
|
|
247 |
];
|
|
|
248 |
|
|
|
249 |
$element = $this->get_datarange_element($dateranges);
|
|
|
250 |
|
|
|
251 |
$date = strtotime('1.05.2020');
|
|
|
252 |
$this->assertEquals('2017-2017-2020-2020', $element->get_daterange_string($date));
|
|
|
253 |
|
|
|
254 |
$date = strtotime('1.05.2024');
|
|
|
255 |
$this->assertEquals('2017-2017-2024-2024', $element->get_daterange_string($date));
|
|
|
256 |
|
|
|
257 |
$date = strtotime('1.02.2020');
|
|
|
258 |
$this->assertEquals('2017-2018-2019-2020', $element->get_daterange_string($date));
|
|
|
259 |
|
|
|
260 |
$date = strtotime('1.02.2024');
|
|
|
261 |
$this->assertEquals('2017-2018-2023-2024', $element->get_daterange_string($date));
|
|
|
262 |
}
|
|
|
263 |
|
|
|
264 |
}
|