MDL-69507 duration form field: modernise coding style
[moodle.git] / lib / form / tests / duration_test.php
CommitLineData
a3d5830a
PS
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
a3d5830a
PS
17/**
18 * Unit tests for forms lib.
19 *
20 * This file contains all unit test related to forms library.
21 *
22 * @package core_form
23 * @category phpunit
24 * @copyright 2009 Tim Hunt
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28defined('MOODLE_INTERNAL') || die();
29
30global $CFG;
31require_once($CFG->libdir . '/form/duration.php');
32
33/**
34 * Unit tests for MoodleQuickForm_duration
35 *
36 * Contains test cases for testing MoodleQuickForm_duration
37 *
38 * @package core_form
1eb13556 39 * @category phpunit
a3d5830a
PS
40 * @copyright 2009 Tim Hunt
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
42 */
1eb13556 43class core_form_duration_testcase extends basic_testcase {
a3d5830a
PS
44
45 /**
87554981
TH
46 * Get a form that can be used for testing.
47 *
48 * @return MoodleQuickForm
a3d5830a 49 */
221c293a 50 protected function get_test_form(): MoodleQuickForm {
64c45a29 51 $form = new temp_form_duration();
87554981 52 return $form->getform();
a3d5830a
PS
53 }
54
55 /**
87554981
TH
56 * Get a form with a duration element that can be used for testing.
57 *
58 * @return array with two elements, a MoodleQuickForm and a MoodleQuickForm_duration.
a3d5830a 59 */
221c293a 60 protected function get_test_form_and_element(): array {
87554981
TH
61 $mform = $this->get_test_form();
62 $element = $mform->addElement('duration', 'duration');
63 return [$mform, $element];
a3d5830a
PS
64 }
65
66 /**
221c293a 67 * Test the constructor error handling.
a3d5830a 68 */
221c293a 69 public function test_constructor_rejects_invalid_unit(): void {
a3d5830a 70 // Test trying to create with an invalid unit.
87554981 71 $mform = $this->get_test_form();
221c293a 72 $this->expectException('coding_exception');
87554981
TH
73 $mform->addElement('duration', 'testel', null, ['defaultunit' => 123, 'optional' => false]);
74 }
75
76 /**
221c293a 77 * Test constructor only some units.
87554981 78 */
221c293a 79 public function test_constructor_limited_units(): void {
87554981
TH
80 $mform = $this->get_test_form();
81 $mform->addElement('duration', 'testel', null, ['units' => [MINSECS, 1], 'optional' => false]);
82 $html = $mform->toHtml();
83 $html = preg_replace('~ +>~', '>', $html); // Clean HTML to avoid spurious errors.
84 $this->assertContains('<option value="60" selected>minutes</option>', $html);
85 $this->assertContains('<option value="1">seconds</option>', $html);
86 $this->assertNotContains('value="3600"', $html);
a3d5830a
PS
87 }
88
89 /**
90 * Testcase for testing units (seconds, minutes, hours and days)
91 */
221c293a 92 public function test_get_units(): void {
87554981
TH
93 [$mform, $element] = $this->get_test_form_and_element();
94 $units = $element->get_units();
95 $this->assertEquals($units, [1 => get_string('seconds'), 60 => get_string('minutes'),
96 3600 => get_string('hours'), 86400 => get_string('days'), 604800 => get_string('weeks')]);
a3d5830a
PS
97 }
98
99 /**
221c293a
TH
100 * Data provider for {@see test_seconds_to_unit()}.
101 *
102 * @return array test cases.
a3d5830a 103 */
221c293a
TH
104 public function seconds_to_unit_cases(): array {
105 return [
106 [[0, MINSECS], 0], // Zero minutes, for a nice default unit.
107 [[1, 1], 1],
108 [[3601, 1], 3601],
109 [[1, MINSECS], 60],
110 [[3, MINSECS], 180],
111 [[1, HOURSECS], 3600],
112 [[2, HOURSECS], 7200],
113 [[1, DAYSECS], 86400],
114 [[25, HOURSECS], 90000],
115 ];
116 }
87554981 117
221c293a
TH
118 /**
119 * Testcase for testing conversion of seconds to the best possible unit.
120 *
121 * @dataProvider seconds_to_unit_cases
122 * @param array $expected expected return value from seconds_to_unit
123 * @param int $seconds value to pass to seconds_to_unit
124 */
125 public function test_seconds_to_unit(array $expected, int $seconds): void {
126 [, $element] = $this->get_test_form_and_element();
127 $this->assertEquals($expected, $element->seconds_to_unit($seconds));
128 }
129
130 /**
131 * Testcase for testing conversion of seconds to the best possible unit with a non-default default unit.
132 */
133 public function test_seconds_to_unit_different_default_unit() {
134 $mform = $this->get_test_form();
87554981
TH
135 $element = $mform->addElement('duration', 'testel', null,
136 ['defaultunit' => DAYSECS, 'optional' => false]);
221c293a
TH
137 $this->assertEquals([0, DAYSECS], $element->seconds_to_unit(0));
138 }
139
140 /**
141 * Data provider for {@see test_export_value()}.
142 *
143 * @return array test cases.
144 */
145 public function export_value_cases(): array {
146 return [
147 [10, '10', 1],
148 [180, '3', MINSECS],
149 [90, '1.5', MINSECS],
150 [7200, '2', HOURSECS],
151 [86400, '1', DAYSECS],
152 [0, '0', HOURSECS],
153 [0, '10', 1, 0, true],
154 [20, '20', 1, 1, true],
155 [0, '10', 1, 0, true, ''],
156 [20, '20', 1, 1, true, ''],
157 ];
a3d5830a
PS
158 }
159
160 /**
161 * Testcase to check generated timestamp
221c293a
TH
162 *
163 * @dataProvider export_value_cases
164 * @param int $expected Expected value returned by the element.
165 * @param string $number Number entered into the element.
166 * @param int $unit Unit selected in the element.
167 * @param int $enabled Whether the enabled checkbox on the form was selected. (Only used if $optional is true.)
168 * @param bool $optional Whether the element has the optional option on.
169 * @param string|null $label The element's label.
a3d5830a 170 */
221c293a
TH
171 public function test_export_value(int $expected, string $number, int $unit, int $enabled = 0,
172 bool $optional = false, ?string $label = null): void {
173
174 // Create the test element.
87554981 175 $mform = $this->get_test_form();
221c293a
TH
176 $el = $mform->addElement('duration', 'testel', $label, $optional ? ['optional' => true] : []);
177
178 // Prepare the submitted values.
179 $values = ['testel' => ['number' => $number, 'timeunit' => $unit]];
180 if ($optional) {
181 $values['testel']['enabled'] = $enabled;
182 }
183
184 // Test.
185 $this->assertEquals(['testel' => $expected], $el->exportValue($values, true));
186 $this->assertEquals($expected, $el->exportValue($values));
a3d5830a
PS
187 }
188}
64c45a29
MG
189
190/**
191 * Form object to be used in test case.
192 */
193class temp_form_duration extends moodleform {
194 /**
195 * Form definition.
196 */
197 public function definition() {
198 // No definition required.
199 }
87554981 200
64c45a29
MG
201 /**
202 * Returns form reference
203 * @return MoodleQuickForm
204 */
205 public function getform() {
206 $mform = $this->_form;
207 // Set submitted flag, to simulate submission.
208 $mform->_flagSubmitted = true;
209 return $mform;
210 }
211}