Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
declare(strict_types=1);
18
 
19
namespace customfield_number;
20
 
21
use advanced_testcase;
22
use core_customfield_generator;
23
use core_customfield\field_config_form;
24
 
25
/**
26
 * Tests for the field controller
27
 *
28
 * @package    customfield_number
29
 * @covers     \customfield_number\field_controller
30
 * @copyright  2024 Paul Holden <paulh@moodle.com>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
final class field_controller_test extends advanced_testcase {
34
 
35
    /**
36
     * Test that using base field controller returns our number type
37
     */
38
    public function test_create(): void {
39
        $this->resetAfterTest();
40
 
41
        /** @var core_customfield_generator $generator */
42
        $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
43
 
44
        $category = $generator->create_category();
45
        $field = $generator->create_field(['categoryid' => $category->get('id'), 'type' => 'number']);
46
 
47
        $this->assertInstanceOf(field_controller::class, \core_customfield\field_controller::create((int) $field->get('id')));
48
        $this->assertInstanceOf(field_controller::class, \core_customfield\field_controller::create(0, $field->to_record()));
49
    }
50
 
51
    /**
52
     * Data provider for {@see test_form_definition}
53
     *
54
     * @return array[]
55
     */
56
    public static function form_definition_provider(): array {
57
        return [
58
            'Defaults' => ['', '', '', '{value}', true],
59
            'Minimum greater than maximum' => ['', 12, 10, '{value}', false],
60
            'Default value less than minimum' => [1, 10, 12, '{value}', false],
61
            'Default value greater than maximum' => [13, 10, 12, '{value}', false],
62
            'Valid' => [11, 10, 12, '{value}', true],
63
            'Display valid single placeholder' => ['', '', '', '{value}', true],
64
            'Display invalid single placeholder' => ['', '', '', '111', false],
65
        ];
66
    }
67
 
68
    /**
69
     * Test submitting field definition form
70
     *
71
     * @param float|string $defaultvalue
72
     * @param float|string $minimumvalue
73
     * @param float|string $maximumvalue
74
     * @param string $display
75
     * @param bool $expected
76
     *
77
     * @dataProvider form_definition_provider
78
     */
79
    public function test_form_definition(
80
        float|string $defaultvalue,
81
        float|string $minimumvalue,
82
        float|string $maximumvalue,
83
        string $display,
84
        bool $expected,
85
    ): void {
86
        $this->resetAfterTest();
87
        $this->setAdminUser();
88
 
89
        /** @var core_customfield_generator $generator */
90
        $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
91
 
92
        $category = $generator->create_category();
93
        $field = $generator->create_field(['categoryid' => $category->get('id'), 'type' => 'number']);
94
 
95
        $submitdata = (array) $field->to_record();
96
        $submitdata['configdata'] = array_merge($field->get('configdata'), [
97
            'defaultvalue' => $defaultvalue,
98
            'minimumvalue' => $minimumvalue,
99
            'maximumvalue' => $maximumvalue,
100
            'display' => $display,
101
        ]);
102
 
103
        $formdata = field_config_form::mock_ajax_submit($submitdata);
104
        $form = new field_config_form(null, null, 'post', '', null, true, $formdata, true);
105
 
106
        $form->set_data_for_dynamic_submission();
107
        $this->assertEquals($expected, $form->is_validated());
108
        if ($expected) {
109
            $form->process_dynamic_submission();
110
        }
111
    }
112
}