Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
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/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
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
 
16
 
Línea 28... Línea 17...
28
namespace core_form;
17
namespace core_form;
29
 
18
 
Línea 42... Línea 31...
42
 *
31
 *
43
 * @package    core_form
32
 * @package    core_form
44
 * @category   test
33
 * @category   test
45
 * @copyright  2009 Tim Hunt
34
 * @copyright  2009 Tim Hunt
46
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
36
 * @covers     \MoodleQuickForm_duration
47
 */
37
 */
48
class duration_test extends \basic_testcase {
38
final class duration_test extends \basic_testcase {
Línea 49... Línea 39...
49
 
39
 
50
    /**
40
    /**
51
     * Get a form that can be used for testing.
41
     * Get a form that can be used for testing.
52
     *
42
     *
53
     * @return MoodleQuickForm
43
     * @return MoodleQuickForm
54
     */
44
     */
55
    protected function get_test_form(): MoodleQuickForm {
45
    protected function get_test_form(): MoodleQuickForm {
56
        $form = new temp_form_duration();
46
        $form = new temp_form_duration();
57
        return $form->getform();
47
        return $form->get_form();
Línea 58... Línea 48...
58
    }
48
    }
59
 
49
 
60
    /**
50
    /**
Línea 104... Línea 94...
104
    /**
94
    /**
105
     * Data provider for {@see test_seconds_to_unit()}.
95
     * Data provider for {@see test_seconds_to_unit()}.
106
     *
96
     *
107
     * @return array test cases.
97
     * @return array test cases.
108
     */
98
     */
109
    public function seconds_to_unit_cases(): array {
99
    public static function seconds_to_unit_cases(): array {
110
        return [
100
        return [
111
            [[0, MINSECS], 0], // Zero minutes, for a nice default unit.
101
            [[0, MINSECS], 0], // Zero minutes, for a nice default unit.
112
            [[1, 1], 1],
102
            [[1, 1], 1],
113
            [[3601, 1], 3601],
103
            [[3601, 1], 3601],
114
            [[1, MINSECS], 60],
104
            [[1, MINSECS], 60],
Línea 145... Línea 135...
145
    /**
135
    /**
146
     * Data provider for {@see test_export_value()}.
136
     * Data provider for {@see test_export_value()}.
147
     *
137
     *
148
     * @return array test cases.
138
     * @return array test cases.
149
     */
139
     */
150
    public function export_value_cases(): array {
140
    public static function export_value_cases(): array {
151
        return [
141
        return [
152
            [10, '10', 1],
142
            [10, '10', 1],
153
            [9, '9.3', 1],
143
            [9, '9.3', 1],
154
            [10, '9.5', 1],
144
            [10, '9.5', 1],
155
            [180, '3', MINSECS],
145
            [180, '3', MINSECS],
Línea 190... Línea 180...
190
 
180
 
191
        // Test.
181
        // Test.
192
        $this->assertEquals(['testel' => $expected], $el->exportValue($values, true));
182
        $this->assertEquals(['testel' => $expected], $el->exportValue($values, true));
193
        $this->assertEquals($expected, $el->exportValue($values));
183
        $this->assertEquals($expected, $el->exportValue($values));
-
 
184
    }
-
 
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
        }
194
    }
225
    }
Línea 195... Línea 226...
195
}
226
}
196
 
227
 
197
/**
228
/**
Línea 207... Línea 238...
207
 
238
 
208
    /**
239
    /**
209
     * Returns form reference
240
     * Returns form reference
210
     * @return MoodleQuickForm
241
     * @return MoodleQuickForm
211
     */
242
     */
212
    public function getform() {
243
    public function get_form() {
213
        $mform = $this->_form;
244
        $mform = $this->_form;
214
        // Set submitted flag, to simulate submission.
245
        // Set submitted flag, to simulate submission.
215
        $mform->_flagSubmitted = true;
246
        $mform->_flagSubmitted = true;
216
        return $mform;
247
        return $mform;