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\middleware;
18
 
19
use core\di;
20
use core\url;
21
use core\tests\router\route_testcase;
22
use GuzzleHttp\Psr7\ServerRequest;
23
use Slim\Exception\HttpNotFoundException;
24
 
25
/**
26
 * Tests for the Moodle Bootstrap middleware.
27
 *
28
 * @package    core
29
 * @category   test
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\middleware\moodle_bootstrap_middleware
33
 */
34
final class moodle_bootstrap_middleware_test extends route_testcase {
35
    /**
36
     * Test setting of the page URI based on the request URI.
37
     *
38
     * @dataProvider page_url_provider
39
     *
40
     * @param string $pattern The pattern to register with the app.
41
     * @param string $basepath The basepath of the wwwroot.
42
     * @param string $uri The URI to test.
43
     * @param array $cfg The configuration to use.
44
     * @param string|false $expected The expected page URI, or false if no page URI is expected.
45
     */
46
    public function test_set_page_to_uri(
47
        string $pattern,
48
        string $basepath,
49
        string $uri,
50
        array $cfg,
51
        string|false $expected
52
    ): void {
53
        global $CFG, $PAGE;
54
 
55
        $this->resetAfterTest();
56
 
57
        foreach ($cfg as $key => $value) {
58
            $CFG->{$key} = $value;
59
        }
60
 
61
        $app = $this->get_simple_app();
62
        $app->setBasePath($basepath);
63
        $app->add(di::get(moodle_bootstrap_middleware::class));
64
        $app->addRoutingMiddleware();
65
 
66
        $app->map(
67
            methods: ['GET'],
68
            pattern: $pattern,
69
            callable: fn ($request, $response) => $response,
70
        );
71
 
72
        // Handle the request.
73
        $request = new ServerRequest('GET', $uri);
74
 
75
        if ($expected) {
76
            $app->handle($request);
77
            $expect = new url($expected);
78
            $this->assertEquals($expect->out(), $PAGE->url->out());
79
        } else {
80
            $this->expectException(HttpNotFoundException::class);
81
            $app->handle($request);
82
        }
83
    }
84
 
85
    /**
86
     * Data provider for test_set_page_to_uri.
87
     *
88
     * @return array
89
     */
90
    public static function page_url_provider(): array {
91
        return [
92
            'A basic URI' => [
93
                'pattern' => '/example',
94
                'basepath' => '',
95
                'uri' => '/example',
96
                'cfg' => [],
97
                'expected' => '/example',
98
            ],
99
            'A basic URI including wwwroot' => [
100
                'pattern' => '/example',
101
                'basepath' => '/example/path',
102
                'uri' => "https://example.com/example/path/example",
103
                'cfg' => [
104
                    'wwwroot' => 'https://example.com/example/path',
105
                ],
106
                'expected' => '/example',
107
            ],
108
            'A basic URI with query parameters' => [
109
                'pattern' => '/example',
110
                'basepath' => '',
111
                'uri' => "https://example.com/example?foo=bar&baz=qux",
112
                'cfg' => [
113
                    'wwwroot' => 'https://example.com',
114
                ],
115
                'expected' => "https://example.com/example?foo=bar&baz=qux",
116
            ],
117
            'A request behind a terminating SSL proxy' => [
118
                'pattern' => '/example',
119
                'basepath' => '/moodle',
120
                'uri' => "http://example.com:443/moodle/example",
121
                'cfg' => [
122
                    'wwwroot' => 'https://example.com/moodle',
123
                    'sslproxy' => true,
124
                ],
125
                'expected' => 'https://example.com/moodle/example',
126
            ],
127
            'A request behind a terminating SSL proxy with query parameters' => [
128
                'pattern' => '/example',
129
                'basepath' => '/moodle',
130
                'uri' => "http://example.com:443/moodle/example?foo=bar&baz=qux",
131
                'cfg' => [
132
                    'wwwroot' => 'https://example.com/moodle',
133
                    'sslproxy' => true,
134
                ],
135
                'expected' => 'https://example.com/moodle/example?foo=bar&baz=qux',
136
            ],
137
            'A request behind a terminating SSL proxy and random port' => [
138
                'pattern' => '/example',
139
                'basepath' => '/moodle',
140
                'uri' => "http://example.com:1024/moodle/example",
141
                'cfg' => [
142
                    'wwwroot' => 'https://example.com/moodle',
143
                    'sslproxy' => true,
144
                ],
145
                'expected' => 'https://example.com/moodle/example',
146
            ],
147
            'A request behind a terminating SSL proxy and random port where the wwwroot has an explicit port' => [
148
                'pattern' => '/example',
149
                'basepath' => '/moodle',
150
                'uri' => "http://example.com:1024/moodle/example",
151
                'cfg' => [
152
                    'wwwroot' => 'https://example.com:8443/moodle',
153
                    'sslproxy' => true,
154
                ],
155
                'expected' => 'https://example.com:8443/moodle/example',
156
            ],
157
            'A request behind a terminating SSL proxy where the wwwroot has an explicity port and query parameters' => [
158
                'pattern' => '/example',
159
                'basepath' => '/moodle',
160
                'uri' => "http://example.com:1024/moodle/example?foo=bar&baz=qux",
161
                'cfg' => [
162
                    'wwwroot' => 'https://example.com:8443/moodle',
163
                    'sslproxy' => true,
164
                ],
165
                'expected' => 'https://example.com:8443/moodle/example?foo=bar&baz=qux',
166
            ],
167
            'A request behind a SSL proxy proxy with different path is invalid' => [
168
                'pattern' => '/example',
169
                'basepath' => '/moodle',
170
                'uri' => "http://example.com:443/example",
171
                'cfg' => [
172
                    'wwwroot' => 'https://example.com/moodle',
173
                    'sslproxy' => true,
174
                ],
175
                'expected' => false,
176
            ],
177
            'A request behind a reverse proxy' => [
178
                'pattern' => '/example',
179
                'basepath' => '/moodle',
180
                'uri' => "http://172.30.10.101:443/moodle/example",
181
                'cfg' => [
182
                    'wwwroot' => 'https://example.com/moodle',
183
                    'reverseproxy' => true,
184
                ],
185
                'expected' => 'https://example.com/moodle/example',
186
            ],
187
            'A request behind a reverse proxy with query params' => [
188
                'pattern' => '/example',
189
                'basepath' => '/moodle',
190
                'uri' => "http://172.30.10.101:443/moodle/example?foo=bar&baz=qux",
191
                'cfg' => [
192
                    'wwwroot' => 'https://example.com/moodle',
193
                    'reverseproxy' => true,
194
                ],
195
                'expected' => 'https://example.com/moodle/example?foo=bar&baz=qux',
196
            ],
197
            'A request behind a reverse proxy with different path is invalid' => [
198
                'pattern' => '/example',
199
                'basepath' => '/moodle',
200
                'uri' => "http://172.30.10.101:443/example",
201
                'cfg' => [
202
                    'wwwroot' => 'https://example.com/moodle',
203
                    'reverseproxy' => true,
204
                ],
205
                'expected' => false,
206
            ],
207
            'A request behind a reverse proxy where the wwwroot has an explicit port' => [
208
                'pattern' => '/example',
209
                'basepath' => '/moodle',
210
                'uri' => "http://172.30.10.101:443/moodle/example",
211
                'cfg' => [
212
                    'wwwroot' => 'https://example.com:8443/moodle',
213
                    'reverseproxy' => true,
214
                ],
215
                'expected' => 'https://example.com:8443/moodle/example',
216
            ],
217
            'A request behind a reverse proxy where the wwwroot has an explicit port and parameters' => [
218
                'pattern' => '/example',
219
                'basepath' => '/moodle',
220
                'uri' => "http://172.30.10.101:443/moodle/example?foo=bar&baz=qux",
221
                'cfg' => [
222
                    'wwwroot' => 'https://example.com:8443/moodle',
223
                    'reverseproxy' => true,
224
                ],
225
                'expected' => 'https://example.com:8443/moodle/example?foo=bar&baz=qux',
226
            ],
227
        ];
228
    }
229
}