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