Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

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