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\router\route_loader_interface;
|
|
|
21 |
use core\tests\router\route_testcase;
|
|
|
22 |
use GuzzleHttp\Psr7\ServerRequest;
|
|
|
23 |
|
|
|
24 |
/**
|
|
|
25 |
* Tests for the Moodle route attribute middleware.
|
|
|
26 |
*
|
|
|
27 |
* @package core
|
|
|
28 |
* @category test
|
|
|
29 |
* @copyright Andrew Lyons <andrew@nicols.co.uk>
|
|
|
30 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
31 |
* @covers \core\router\middleware\moodle_route_attribute_middleware
|
|
|
32 |
*/
|
|
|
33 |
final class moodle_route_attribute_middleware_test extends route_testcase {
|
|
|
34 |
/**
|
|
|
35 |
* Test the Moodle route will be set on a request which has a Moodle route attached.
|
|
|
36 |
*/
|
|
|
37 |
public function test_has_route(): void {
|
|
|
38 |
self::load_fixture('core', '/router/route_on_method_only.php');
|
|
|
39 |
self::load_fixture('core', '/router/route_on_class.php');
|
|
|
40 |
|
|
|
41 |
$this->add_route_to_route_loader(
|
|
|
42 |
\core\fixtures\route_on_method_only::class,
|
|
|
43 |
'method_with_route',
|
|
|
44 |
grouppath: '',
|
|
|
45 |
);
|
|
|
46 |
|
|
|
47 |
$testcase = $this;
|
|
|
48 |
|
|
|
49 |
$app = $this->get_simple_app();
|
|
|
50 |
|
|
|
51 |
$app->add(function ($request, $handler) use ($testcase) {
|
|
|
52 |
$route = $request->getAttribute(\core\router\route::class);
|
|
|
53 |
$testcase->assertInstanceOf(\core\router\route::class, $route);
|
|
|
54 |
|
|
|
55 |
return $handler->handle($request);
|
|
|
56 |
});
|
|
|
57 |
|
|
|
58 |
$app->add(di::get(moodle_route_attribute_middleware::class));
|
|
|
59 |
$app->addRoutingMiddleware();
|
|
|
60 |
$app->add(function ($request, $handler) use ($testcase) {
|
|
|
61 |
$testcase->assertNull($request->getAttribute(\core\router\route::class));
|
|
|
62 |
|
|
|
63 |
return $handler->handle($request);
|
|
|
64 |
});
|
|
|
65 |
|
|
|
66 |
di::get(route_loader_interface::class)->configure_routes($app);
|
|
|
67 |
|
|
|
68 |
$request = new ServerRequest('GET', '/method/path');
|
|
|
69 |
$app->handle($request);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Test that no error occurs when no Moodle route is found.
|
|
|
74 |
*/
|
|
|
75 |
public function test_has_no_route(): void {
|
|
|
76 |
self::load_fixture('core', '/router/route_on_method_only.php');
|
|
|
77 |
self::load_fixture('core', '/router/route_on_class.php');
|
|
|
78 |
|
|
|
79 |
$testcase = $this;
|
|
|
80 |
|
|
|
81 |
$app = $this->get_simple_app();
|
|
|
82 |
$app->map(['GET'], '/method/path', fn ($request, $response) => $response);
|
|
|
83 |
|
|
|
84 |
$app->add(function ($request, $handler) use ($testcase) {
|
|
|
85 |
$testcase->assertNull($request->getAttribute(\core\router\route::class));
|
|
|
86 |
|
|
|
87 |
return $handler->handle($request);
|
|
|
88 |
});
|
|
|
89 |
|
|
|
90 |
$app->add(di::get(moodle_route_attribute_middleware::class));
|
|
|
91 |
$app->addRoutingMiddleware();
|
|
|
92 |
$app->add(function ($request, $handler) use ($testcase) {
|
|
|
93 |
$testcase->assertNull($request->getAttribute(\core\router\route::class));
|
|
|
94 |
|
|
|
95 |
return $handler->handle($request);
|
|
|
96 |
});
|
|
|
97 |
|
|
|
98 |
di::get(route_loader_interface::class)->configure_routes($app);
|
|
|
99 |
|
|
|
100 |
$request = new ServerRequest('GET', '/method/path');
|
|
|
101 |
$app->handle($request);
|
|
|
102 |
}
|
|
|
103 |
}
|