2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for forms lib.
20 * This file contains all unit test related to forms library.
24 * @copyright 2009 Tim Hunt
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
31 require_once($CFG->libdir . '/form/duration.php');
34 * Unit tests for MoodleQuickForm_duration
36 * Contains test cases for testing MoodleQuickForm_duration
40 * @copyright 2009 Tim Hunt
41 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43 class core_form_duration_testcase extends basic_testcase {
46 * Get a form that can be used for testing.
48 * @return MoodleQuickForm
50 protected function get_test_form() {
51 $form = new temp_form_duration();
52 return $form->getform();
56 * Get a form with a duration element that can be used for testing.
58 * @return array with two elements, a MoodleQuickForm and a MoodleQuickForm_duration.
60 protected function get_test_form_and_element() {
61 $mform = $this->get_test_form();
62 $element = $mform->addElement('duration', 'duration');
63 return [$mform, $element];
67 * Testcase for testing contructor.
69 * @expectedException coding_exception
71 public function test_constructor() {
72 // Test trying to create with an invalid unit.
73 $mform = $this->get_test_form();
74 $mform->addElement('duration', 'testel', null, ['defaultunit' => 123, 'optional' => false]);
78 * Test contructor only some units.
80 public function test_constructor_limited_units() {
81 $mform = $this->get_test_form();
82 $mform->addElement('duration', 'testel', null, ['units' => [MINSECS, 1], 'optional' => false]);
83 $html = $mform->toHtml();
84 $html = preg_replace('~ +>~', '>', $html); // Clean HTML to avoid spurious errors.
85 $this->assertContains('<option value="60" selected>minutes</option>', $html);
86 $this->assertContains('<option value="1">seconds</option>', $html);
87 $this->assertNotContains('value="3600"', $html);
91 * Testcase for testing units (seconds, minutes, hours and days)
93 public function test_get_units() {
94 [$mform, $element] = $this->get_test_form_and_element();
95 $units = $element->get_units();
96 $this->assertEquals($units, [1 => get_string('seconds'), 60 => get_string('minutes'),
97 3600 => get_string('hours'), 86400 => get_string('days'), 604800 => get_string('weeks')]);
101 * Testcase for testing conversion of seconds to the best possible unit
103 public function test_seconds_to_unit() {
104 [$mform, $element] = $this->get_test_form_and_element();
105 $this->assertEquals([0, MINSECS], $element->seconds_to_unit(0)); // Zero minutes, for a nice default unit.
106 $this->assertEquals([1, 1], $element->seconds_to_unit(1));
107 $this->assertEquals([3601, 1], $element->seconds_to_unit(3601));
108 $this->assertEquals([1, MINSECS], $element->seconds_to_unit(60));
109 $this->assertEquals([3, MINSECS], $element->seconds_to_unit(180));
110 $this->assertEquals([1, HOURSECS], $element->seconds_to_unit(3600));
111 $this->assertEquals([2, HOURSECS], $element->seconds_to_unit(7200));
112 $this->assertEquals([1, DAYSECS], $element->seconds_to_unit(86400));
113 $this->assertEquals([25, HOURSECS], $element->seconds_to_unit(90000));
115 $element = $mform->addElement('duration', 'testel', null,
116 ['defaultunit' => DAYSECS, 'optional' => false]);
117 $this->assertEquals([0, DAYSECS], $element->seconds_to_unit(0)); // Zero minutes, for a nice default unit.
121 * Testcase to check generated timestamp
123 public function test_exportValue() {
124 $mform = $this->get_test_form();
125 $el = $mform->addElement('duration', 'testel');
126 $values = ['testel' => ['number' => 10, 'timeunit' => 1]];
127 $this->assertEquals(['testel' => 10], $el->exportValue($values, true));
128 $this->assertEquals(10, $el->exportValue($values));
129 $values = ['testel' => ['number' => 3, 'timeunit' => MINSECS]];
130 $this->assertEquals(['testel' => 180], $el->exportValue($values, true));
131 $this->assertEquals(180, $el->exportValue($values));
132 $values = ['testel' => ['number' => 1.5, 'timeunit' => MINSECS]];
133 $this->assertEquals(['testel' => 90], $el->exportValue($values, true));
134 $this->assertEquals(90, $el->exportValue($values));
135 $values = ['testel' => ['number' => 2, 'timeunit' => HOURSECS]];
136 $this->assertEquals(['testel' => 7200], $el->exportValue($values, true));
137 $this->assertEquals(7200, $el->exportValue($values));
138 $values = ['testel' => ['number' => 1, 'timeunit' => DAYSECS]];
139 $this->assertEquals(['testel' => 86400], $el->exportValue($values, true));
140 $this->assertEquals(86400, $el->exportValue($values));
141 $values = ['testel' => ['number' => 0, 'timeunit' => HOURSECS]];
142 $this->assertEquals(['testel' => 0], $el->exportValue($values, true));
143 $this->assertEquals(0, $el->exportValue($values));
145 $el = $mform->addElement('duration', 'testel', null, ['optional' => true]);
146 $values = ['testel' => ['number' => 10, 'timeunit' => 1]];
147 $this->assertEquals(['testel' => 0], $el->exportValue($values, true));
148 $this->assertEquals(0, $el->exportValue($values));
149 $values = ['testel' => ['number' => 20, 'timeunit' => 1, 'enabled' => 1]];
150 $this->assertEquals(['testel' => 20], $el->exportValue($values, true));
151 $this->assertEquals(20, $el->exportValue($values));
154 $el2 = $mform->addElement('duration', 'testel', '', ['optional' => true]);
155 $values = ['testel' => ['number' => 10, 'timeunit' => 1, 'enabled' => 1]];
156 $this->assertEquals(['testel' => 10], $el2->exportValue($values, true));
157 $this->assertEquals(10, $el2->exportValue($values));
158 $values = ['testel' => ['number' => 10, 'timeunit' => 1, 'enabled' => 0]];
159 $this->assertEquals(['testel' => 0], $el2->exportValue($values, true));
160 $this->assertEquals(null, $el2->exportValue($values));
165 * Form object to be used in test case.
167 class temp_form_duration extends moodleform {
171 public function definition() {
172 // No definition required.
176 * Returns form reference
177 * @return MoodleQuickForm
179 public function getform() {
180 $mform = $this->_form;
181 // Set submitted flag, to simulate submission.
182 $mform->_flagSubmitted = true;