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;
18
 
19
use core\tests\router\route_testcase;
20
use Slim\App;
21
 
22
/**
23
 * Tests for the router class.
24
 *
25
 * @package    core
26
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers     \core\router
29
 * @covers     \core\router\response_handler
30
 */
31
final class router_test extends route_testcase {
32
    public function test_get_app(): void {
33
        $router = $this->get_router('/example');
34
        $app = $router->get_app();
35
        $this->assertInstanceOf(App::class, $app);
36
 
37
        $this->assertEquals(di::get_container(), $app->getContainer());
38
    }
39
 
40
    public function test_request_normalisation(): void {
41
        $router = $this->get_router('');
42
 
43
        // Create handlers for the routes.
44
        // Note: These must all be created before any are accessed as the data is cached after first use.
45
        $app = $router->get_app();
46
        $app->get('/test/path', fn ($response) => $response->withStatus(299));
47
        $app->get('/', fn ($response) => $response->withStatus(275));
48
        $app->get('/test/otherpath', fn ($response) => $response->withStatus(250));
49
 
50
        // Duplicate slashes.
51
        $request = $this->create_request('GET', '/test//path', '');
52
        $response = $router->handle_request($request);
53
        $this->assertEquals(299, $response->getStatusCode());
54
 
55
        // An empty route.
56
        $request = $this->create_request('GET', '', '');
57
        $response = $router->handle_request($request);
58
        $this->assertEquals(275, $response->getStatusCode());
59
 
60
        // A route with a trailing slash.
61
        $request = $this->create_request('GET', '/test/otherpath/', '');
62
        $response = $router->handle_request($request);
63
        $this->assertEquals(250, $response->getStatusCode());
64
 
65
        // A route with a trailing double slash.
66
        $request = $this->create_request('GET', '/test/otherpath/////', '');
67
        $response = $router->handle_request($request);
68
        $this->assertEquals(250, $response->getStatusCode());
69
    }
70
 
71
    /**
72
     * Test an API route.
73
     */
74
    public function test_preferences_no_login(): void {
75
        $this->add_class_routes_to_route_loader(\core_user\route\api\preferences::class);
76
        $response = $this->process_api_request('GET', '/current/preferences');
77
 
78
        $this->assert_valid_response($response);
79
        $payload = $this->decode_response($response);
80
 
81
        $this->assertEmpty((array) $payload);
82
    }
83
 
84
    public function test_basepath_supplied(): void {
85
        $router = $this->get_router(
86
            basepath: '/example',
87
        );
88
        $this->assertEquals('/example', $router->basepath);
89
    }
90
 
91
    /**
92
     * @dataProvider basepath_provider
93
     */
94
    public function test_basepath(
95
        string $wwwroot,
96
        string $expected,
97
    ): void {
98
        global $CFG;
99
 
100
        $this->resetAfterTest();
101
        $CFG->wwwroot = $wwwroot;
102
 
103
        $router = di::get(router::class);
104
 
105
        $this->assertEquals($expected, $router->basepath);
106
    }
107
 
108
    public static function basepath_provider(): \Iterator {
109
        yield 'Domain' => ['http://example.com', '/r.php'];
110
        yield 'Subdirectory' => ['http://example.com/moodle', '/moodle/r.php'];
111
    }
112
 
113
    public function test_basepath_guessed_rphp(): void {
114
        $wwwroot = new \moodle_url('/r.php');
115
        $_SERVER['SCRIPT_FILENAME'] = 'r.php';
116
        $_SERVER['REQUEST_URI'] = $wwwroot->get_path();
117
 
118
        $router = di::get(router::class);
119
 
120
        $this->assertEquals($wwwroot->get_path(), $router->basepath);
121
    }
122
 
123
    /**
124
     * Test that the basepath is correctly guessed when the router is configured.
125
     *
126
     * @param string $wwwroot The wwwroot to use.
127
     * @param bool|null $configured The value of $CFG->routerconfigured.
128
     * @param string $requestedpath The path that was requested.
129
     * @param string $expected The expected basepath.
130
     */
131
    #[\PHPUnit\Framework\Attributes\DataProvider('router_configured_basepath_provider')]
132
    public function test_basepath_guessed_rphp_configuration_provided(
133
        string $wwwroot,
134
        ?bool $configured,
135
        string $requestedpath,
136
        string $expected,
137
    ): void {
138
        global $CFG;
139
 
140
        $this->resetAfterTest();
141
        $CFG->wwwroot = $wwwroot;
142
        $CFG->routerconfigured = $configured;
143
        $_SERVER['SCRIPT_FILENAME'] = "{$CFG->dirroot}/r.php";
144
        $_SERVER['REQUEST_URI'] = $requestedpath;
145
 
146
        $router = di::get(router::class);
147
 
148
        $this->assertEquals($expected, $router->basepath);
149
    }
150
 
151
    /**
152
     * Data provider for test_basepath_guessed_rphp_configuration_provided.
153
     *
154
     * @return \Generator<string, array<bool|string|null>, mixed, void>
155
     */
156
    public static function router_configured_basepath_provider(): \Iterator {
157
        global $CFG;
158
 
159
        yield 'Root domain, Not configured, accessed via r.php' => [
160
            'http://example.com',
161
            null,
162
            '/r.php/example',
163
            '/r.php',
164
        ];
165
        yield 'Root domain, Configured true, accessed via r.php' => [
166
            'http://example.com',
167
            true,
168
            "/r.php/example",
169
            '/r.php',
170
        ];
171
        yield 'Root domain, Configured false, accessed via r.php' => [
172
            'http://example.com',
173
            false,
174
            '/r.php/example',
175
            '/r.php',
176
        ];
177
        yield 'Sub directory, Not configured, accessed via r.php' => [
178
            'http://example.com/moodle',
179
            null,
180
            '/moodle/r.php/example',
181
            '/moodle/r.php',
182
        ];
183
        yield 'Sub directory, Configured true, accessed via r.php' => [
184
            'http://example.com/moodle',
185
            true,
186
            '/moodle/r.php/example',
187
            '/moodle/r.php',
188
        ];
189
        yield 'Sub directory, Configured false, accessed via r.php' => [
190
            'http://example.com/moodle',
191
            false,
192
            '/moodle/r.php/example',
193
            '/moodle/r.php',
194
        ];
195
        yield 'Root domain, Not configured, accessed without r.php' => [
196
            'http://example.com',
197
            null,
198
            '/example',
199
            '/r.php',
200
        ];
201
        yield 'Root domain, Configured true, accessed without r.php' => [
202
            'http://example.com',
203
            true,
204
            '/example',
205
            '',
206
        ];
207
        yield 'Root domain, Configured false, accessed without r.php' => [
208
            'http://example.com',
209
            false,
210
            '/example',
211
            '/r.php',
212
        ];
213
        yield 'Sub directory, Not configured, accessed without r.php' => [
214
            'http://example.com/moodle',
215
            null,
216
            '/example',
217
            '/moodle/r.php',
218
        ];
219
        yield 'Sub directory, Configured true, accessed without r.php' => [
220
            'http://example.com/moodle',
221
            true,
222
            '/example',
223
            '/moodle',
224
        ];
225
        yield 'Sub directory, Configured false, accessed without r.php' => [
226
            'http://example.com/moodle',
227
            false,
228
            '/example',
229
            '/moodle/r.php',
230
        ];
231
    }
232
}