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
namespace core\router\schema\parameters;
18
 
19
use core\param;
20
use core\router\route;
21
use core\router\schema\referenced_object;
22
use core\router\schema\specification;
23
use core\tests\router\route_testcase;
24
use GuzzleHttp\Psr7\ServerRequest;
25
 
26
/**
27
 * Tests for the query parameters.
28
 *
29
 * @package    core
30
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
31
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 * @covers     \core\router\schema\parameter
33
 * @covers     \core\router\schema\parameters\query_parameter
34
 * @covers     \core\router\schema\openapi_base
35
 */
36
final class query_parameter_test extends route_testcase {
37
    public function test_in_path(): void {
38
        $param = new query_parameter(name: 'example');
39
        $this->assertEquals('query', $param->get_in());
40
        $this->assertEquals('example', $param->get_name());
41
    }
42
 
43
    /**
44
     * Test the is_required method.
45
     *
46
     * @dataProvider is_required_provider
47
     * @param bool|null $required
48
     * @param bool $expected
49
     */
50
    public function test_is_required(?bool $required, bool $expected): void {
51
        $param = new query_parameter(
52
            name: 'value',
53
            required: $required,
54
        );
55
        $this->assertEquals($expected, $param->is_required(new route()));
56
    }
57
 
58
    /**
59
     * Data provider for the is_required method.
60
     *
61
     * @return array
62
     */
63
    public static function is_required_provider(): array {
64
        return [
65
            [true, true],
66
            [false, false],
67
            [null, false],
68
        ];
69
    }
70
 
71
    public function test_referenced_object(): void {
72
        $object = new class (
73
            name: 'example',
74
            type: param::INT,
75
        ) extends query_parameter implements referenced_object {
76
        };
77
 
78
        $schema = $object->get_openapi_description(new specification());
79
        $this->assertObjectNotHasProperty('$ref', $schema);
80
        $this->assertObjectHasProperty('schema', $schema);
81
 
82
        $reference = $object->get_openapi_schema(new specification());
83
        $this->assertObjectNotHasProperty('schema', $reference);
84
        $this->assertObjectHasProperty('$ref', $reference);
85
    }
86
 
87
    /**
88
     * Test for the allowReserved property.
89
     *
90
     * @dataProvider allow_reserved_provider
91
     * @param bool|null $allowreserved
92
     * @param bool $expected
93
     */
94
    public function test_allow_reserved(
95
        ?bool $allowreserved,
96
        bool $expected,
97
    ): void {
98
        $param = new query_parameter(
99
            name: 'example',
100
            type: param::INT,
101
            allowreserved: $allowreserved,
102
        );
103
 
104
        $schema = $param->get_openapi_description(new specification());
105
        if ($expected) {
106
            $this->assertObjectHasProperty('allowReserved', $schema);
107
            $this->assertTrue($schema->allowReserved);
108
        } else {
109
            $this->assertObjectNotHasProperty('allowReserved', $schema);
110
        }
111
    }
112
 
113
    /**
114
     * Data provider ofr testing whether reserved characters are allowed.
115
     *
116
     * @return array
117
     */
118
    public static function allow_reserved_provider(): array {
119
        return [
120
            [true, true],
121
            [false, false],
122
            [null, false],
123
        ];
124
    }
125
 
126
    /**
127
     * Tests of the param validation.
128
     *
129
     * @dataProvider validation_provider
130
     * @param array $properties
131
     * @param array $params
132
     * @param array $expected
133
     */
134
    public function test_validation(
135
        array $properties,
136
        array $params,
137
        array $expected,
138
    ): void {
139
        $param = new query_parameter(...$properties);
140
 
141
        $request = new ServerRequest('GET', '/example');
142
        $request = $request->withQueryParams(array_merge(
143
            $request->getQueryParams(),
144
            $params,
145
        ));
146
 
147
        $newrequest = $param->validate($request, $request->getQueryParams());
148
        $this->assertEquals($expected, $newrequest->getQueryParams());
149
    }
150
 
151
    /**
152
     * Validation data provider.
153
     *
154
     * @return array
155
     */
156
    public static function validation_provider(): array {
157
        return [
158
            'Basic required param' => [
159
                [
160
                    'name' => 'example',
161
                    'type'  => param::INT,
162
                    'required' => true,
163
                ],
164
                [
165
                    'example' => 12345,
166
                    'otherfield' => 'abcde',
167
                ],
168
                [
169
                    'example' => 12345,
170
                    'otherfield' => 'abcde',
171
                ],
172
            ],
173
            'Basic optional param' => [
174
                [
175
                    'name' => 'example',
176
                    'type'  => param::INT,
177
                ],
178
                [
179
                    'example' => 12345,
180
                    'otherfield' => 'abcde',
181
                ],
182
                [
183
                    'example' => 12345,
184
                    'otherfield' => 'abcde',
185
                ],
186
            ],
187
            'Basic optional param not provided' => [
188
                [
189
                    'name' => 'example',
190
                    'type'  => param::INT,
191
                ],
192
                [
193
                    'otherfield' => 'abcde',
194
                ],
195
                [
196
                    'otherfield' => 'abcde',
197
                    'example' => null,
198
                ],
199
            ],
200
            'Basic optional param not provided with defaults' => [
201
                [
202
                    'name' => 'example',
203
                    'type'  => param::INT,
204
                    'default' => 999,
205
                ],
206
                [
207
                    'otherfield' => 'abcde',
208
                ],
209
                [
210
                    'otherfield' => 'abcde',
211
                    'example' => 999,
212
                ],
213
            ],
214
            'Special handling for a bool (true)' => [
215
                [
216
                    'name' => 'example',
217
                    'type'  => param::BOOL,
218
                ],
219
                [
220
                    'otherfield' => 'abcde',
221
                    'example' => 'true',
222
                ],
223
                [
224
                    'otherfield' => 'abcde',
225
                    'example' => true,
226
                ],
227
            ],
228
            'Special handling for a bool (false)' => [
229
                [
230
                    'name' => 'example',
231
                    'type'  => param::BOOL,
232
                ],
233
                [
234
                    'otherfield' => 'abcde',
235
                    'example' => 'false',
236
                ],
237
                [
238
                    'otherfield' => 'abcde',
239
                    'example' => false,
240
                ],
241
            ],
242
            'Special handling for a bool - not specified' => [
243
                [
244
                    'name' => 'example',
245
                    'type'  => param::BOOL,
246
                ],
247
                [
248
                    'otherfield' => 'abcde',
249
                ],
250
                [
251
                    'otherfield' => 'abcde',
252
                    'example' => null,
253
                ],
254
            ],
255
            'Special handling for a bool - default true' => [
256
                [
257
                    'name' => 'example',
258
                    'type'  => param::BOOL,
259
                    'default' => true,
260
                ],
261
                [
262
                    'otherfield' => 'abcde',
263
                ],
264
                [
265
                    'otherfield' => 'abcde',
266
                    'example' => true,
267
                ],
268
            ],
269
            'Special handling for a bool - default false' => [
270
                [
271
                    'name' => 'example',
272
                    'type'  => param::BOOL,
273
                    'default' => false,
274
                ],
275
                [
276
                    'otherfield' => 'abcde',
277
                ],
278
                [
279
                    'otherfield' => 'abcde',
280
                    'example' => false,
281
                ],
282
            ],
283
        ];
284
    }
285
 
286
    public function test_validation_boolean_failure(): void {
287
        $param = new query_parameter(
288
            name: 'example',
289
            type: param::BOOL,
290
        );
291
 
292
        $request = new ServerRequest('GET', '/example');
293
        $request = $request->withQueryParams(array_merge(
294
            $request->getQueryParams(),
295
            [
296
                'example' => 'notaboolean',
297
            ],
298
        ));
299
 
300
        $this->expectException(\ValueError::class);
301
        $param->validate($request, $request->getQueryParams());
302
    }
303
 
304
    public function test_validation_required_not_set(): void {
305
        $param = new query_parameter(
306
            name: 'example',
307
            type: param::BOOL,
308
            required: true,
309
        );
310
 
311
        $request = new ServerRequest('GET', '/example');
312
 
313
        $this->expectException(\coding_exception::class);
314
        $param->validate($request, $request->getQueryParams());
315
    }
316
}