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\parameters;
18
 
19
use core\tests\router\route_testcase;
20
use invalid_parameter_exception;
21
use GuzzleHttp\Psr7\Response;
22
use GuzzleHttp\Psr7\ServerRequest;
23
use Psr\Http\Message\ServerRequestInterface;
24
use Slim\Routing\RouteContext;
25
 
26
/**
27
 * Tests for the Component path parameter.
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\parameters\path_component
33
 * @covers     \core\router\schema\parameters\path_parameter
34
 */
35
final class path_component_test extends route_testcase {
36
    /**
37
     * Test that the parameter is valid when the component is not specified.
38
     */
39
    public function test_component_not_specified(): void {
40
        $param = new path_component();
41
 
42
        $app = $this->get_simple_app();
43
        $app->get('/example', fn () => new Response());
44
 
45
        $request = $this->route_request($app, new ServerRequest('GET', '/example'));
46
 
47
        $context = RouteContext::fromRequest($request);
48
        $route = $context->getRoute();
49
 
50
        $this->assertInstanceOf(
51
            ServerRequestInterface::class,
52
            $param->validate($request, $route),
53
        );
54
    }
55
 
56
    /**
57
     * Test that the parameter name is respected
58
     */
59
    public function test_name(): void {
60
        $param = new path_component(name: 'not_the_default_name');
61
        $this->assertEquals('not_the_default_name', $param->get_name());
62
    }
63
 
64
    /**
65
     * Test valid components.
66
     *
67
     * @param string $component
68
     * @dataProvider valid_components
69
     */
70
    public function test_valid_value(string $component): void {
71
        $param = new path_component();
72
 
73
        $app = $this->get_simple_app();
74
        $app->get('/example/{component}', fn () => new Response());
75
 
76
        $request = $this->route_request($app, new ServerRequest('GET', "/example/{$component}"));
77
 
78
        $context = RouteContext::fromRequest($request);
79
        $route = $context->getRoute();
80
 
81
        $this->assertInstanceOf(
82
            ServerRequestInterface::class,
83
            $param->validate($request, $route),
84
        );
85
    }
86
 
87
    /**
88
     * Test invalid components.
89
     *
90
     * @param string $component
91
     * @dataProvider invalid_components
92
     */
93
    public function test_invalid_value(string $component): void {
94
        $this->resetAfterTest();
95
        $param = new path_component();
96
 
97
        $app = $this->get_simple_app();
98
        $app->get('/example/{component}', fn () => new Response());
99
 
100
        $request = $this->route_request($app, new ServerRequest('GET', "/example/{$component}"));
101
 
102
        $context = RouteContext::fromRequest($request);
103
        $route = $context->getRoute();
104
 
105
        $this->expectException(invalid_parameter_exception::class);
106
        $param->validate($request, $route);
107
    }
108
 
109
    /**
110
     * Data provider containing seemingly-valid components.
111
     *
112
     * @return array
113
     */
114
    public static function valid_components(): array {
115
        return [
116
            ['core'],
117
            ['core_message'],
118
            ['mod_forum'],
119
            ['assignsubmission_file'],
120
            // Note: This is handled with a regex, not an actual lookup.
121
            ['blueberry_jam'],
122
        ];
123
    }
124
 
125
    /**
126
     * Data provider containing invalid components.
127
     *
128
     * @return array
129
     */
130
    public static function invalid_components(): array {
131
        return [
132
            ['4things_todo'],
133
            ['EASY_AS'],
134
        ];
135
    }
136
}