Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
16
 
17
namespace core;
18
 
19
/**
20
 * Unit tests for parameter management.
21
 *
22
 * @package   core
23
 * @copyright Andrew Lyons <andrew@nicols.co.uk>
24
 * @license   https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @covers    \core\param
26
 */
27
class param_test extends \advanced_testcase {
28
    /**
29
     * Test that the Moodle `from_type` method provides canonicalised parameter values.
30
     *
31
     * @dataProvider valid_param_provider
32
     * @param string $type
33
     * @param param $expected
34
     */
35
    public function test_from_type(string $type, param $expected): void {
36
        $this->assertEquals($expected, param::from_type($type));
37
    }
38
 
39
    /**
40
     * Data provider containing valid paramter types and their name mapping.
41
     */
42
    public static function valid_param_provider(): array {
43
        return [
44
            // Standard values.
45
            [PARAM_RAW, param::RAW],
46
            [PARAM_RAW_TRIMMED, param::RAW_TRIMMED],
47
            [PARAM_FLOAT, param::FLOAT],
48
            [PARAM_INT, param::INT],
49
 
50
            // Using enum values (why would you, but...).
51
            [param::RAW->value, param::RAW],
52
            [param::RAW_TRIMMED->value, param::RAW_TRIMMED],
53
            [param::FLOAT->value, param::FLOAT],
54
            [param::INT->value, param::INT],
55
            [param::COMPONENT->value, param::COMPONENT],
56
 
57
            // Some aliases (canonicalised) parameters.
58
            [PARAM_INTEGER, param::INT],
59
            [PARAM_NUMBER, param::FLOAT],
60
        ];
61
    }
62
 
63
    /**
64
     * Ensure that we throw an exception if an invalid parameter type is used.
65
     */
66
    public function test_from_type_invalid(): void {
67
        $this->expectException(\coding_exception::class);
68
        param::from_type('not_a_param');
69
    }
70
 
71
    /**
72
     * Test that deprecated parameters are marked as such.
73
     *
74
     * @dataProvider is_deprecated_provider
75
     * @param param $param
76
     * @param bool $expected
77
     */
78
    public function test_is_deprecated(param $param, bool $expected): void {
79
        $this->assertEquals(
80
            $expected,
81
            $param->is_deprecated(),
82
        );
83
    }
84
 
85
    /**
86
     * Provider for deprecated parameter types.
87
     *
88
     * @return array
89
     */
90
    public static function is_deprecated_provider(): array {
91
        return [
92
            // Some undeprecated parameters.
93
            [param::RAW, false],
94
            [param::RAW_TRIMMED, false],
95
            [param::INT, false],
96
 
97
            // Some deprecated parameters.
98
            [param::INTEGER, true],
99
            [param::NUMBER, true],
100
            [param::CLEAN, true],
101
        ];
102
    }
103
 
104
 
105
    /**
106
     * Test that finally deprecated params throw an exception when cleaning.
107
     *
108
     * @dataProvider deprecated_param_provider
109
     * @param param $params
110
     */
111
    public function test_deprecated_params_except(param $param): void {
112
        $this->expectException(\coding_exception::class);
113
        $param->clean('foo');
114
    }
115
 
116
    /**
117
     * Provider for deprecated parameters.
118
     *
119
     * @return array
120
     */
121
    public static function deprecated_param_provider(): array {
122
        return array_map(
123
            fn (param $param): array => [$param],
124
            array_filter(
125
                param::cases(),
126
                function (param $param): bool {
127
                    if ($attribute = deprecation::from($param)) {
128
                        return $attribute->emit && $attribute->final;
129
                    }
130
                    return false;
131
                },
132
            ),
133
        );
134
    }
135
}