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;
18
 
19
use core\router\middleware\moodle_route_attribute_middleware;
20
use core\tests\router\route_testcase;
21
use GuzzleHttp\Psr7\ServerRequest;
22
use Slim\Middleware\RoutingMiddleware;
23
 
24
/**
25
 * Tests for the route utility class.
26
 *
27
 * @package    core
28
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
29
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30
 * @covers     \core\router\util
31
 */
32
final class util_test extends route_testcase {
33
    /**
34
     * Ensure that no error is thrown when getting a route instance for a callable.
35
     */
36
    public function test_get_route_instance_for_method_not_array_callable(): void {
37
        $this->assertNull(util::get_route_instance_for_method(fn () => null));
38
    }
39
 
40
    /**
41
     * Test getting the path for a callable.
42
     */
43
    public function test_get_path_for_callable(): void {
44
        global $CFG;
45
 
46
        $this->resetAfterTest();
47
 
48
        $CFG->routerconfigured = true;
49
        self::load_fixture('core', 'router/route_on_class.php');
50
 
51
        $this->add_route_to_route_loader(
52
            \core\fixtures\route_on_class::class,
53
            'method_with_route',
54
            grouppath: '/example',
55
        );
56
 
57
        $url = util::get_path_for_callable(
58
            [\core\fixtures\route_on_class::class, 'method_with_route'],
59
            [],
60
            [],
61
        );
62
 
63
        $parsedurl = parse_url($url);
64
        $this->assertEquals(
65
            (new \moodle_url('/example/class/path/method/path'))->get_path(),
66
            $parsedurl['path'],
67
        );
68
    }
69
 
70
    public function test_get_route_instance_for_method(): void {
71
        self::load_fixture('core', 'router/route_on_method_only.php');
72
        self::load_fixture('core', 'router/route_on_class.php');
73
 
74
        // The class has no route attribute.
75
 
76
        // Test a method that has no route attribute.
77
        $this->assertNull(util::get_route_instance_for_method('core\fixtures\route_on_method_only::method_without_route'));
78
        $this->assertNull(util::get_route_instance_for_method(['core\fixtures\route_on_method_only', 'method_without_route']));
79
 
80
        // Test a method that has a route attribute.
81
        $this->assert_route_callable_data(
82
            'core\fixtures\route_on_method_only::method_with_route',
83
            '/method/path',
84
            'core\fixtures\route_on_method_only::method_with_route',
85
        );
86
        $this->assert_route_callable_data(
87
            ['core\fixtures\route_on_method_only', 'method_with_route'],
88
            '/method/path',
89
            'core\fixtures\route_on_method_only::method_with_route',
90
        );
91
 
92
        // The class has a route attribute.
93
 
94
        // Test a method that has no route attribute.
95
        $this->assertNull(util::get_route_instance_for_method('core\fixtures\route_on_class::method_without_route'));
96
        $this->assertNull(util::get_route_instance_for_method(['core\fixtures\route_on_class', 'method_without_route']));
97
 
98
        // Test a method that has a route attribute - it is merged with parent.
99
        $this->assert_route_callable_data(
100
            'core\fixtures\route_on_class::method_with_route',
101
            '/class/path/method/path',
102
            'core\fixtures\route_on_class::method_with_route',
103
        );
104
        $this->assert_route_callable_data(
105
            ['core\fixtures\route_on_class', 'method_with_route'],
106
            '/class/path/method/path',
107
            'core\fixtures\route_on_class::method_with_route',
108
        );
109
    }
110
 
111
    /**
112
     * Assertion helper to asser that a callable is a route and has the expected path and name.
113
     *
114
     * @param callable $callable The callable to check.
115
     * @param string $path The expected path.
116
     * @param string $routename The expected route name.
117
     */
118
    protected function assert_route_callable_data(
119
        $callable,
120
        string $path,
121
        string $routename,
122
    ): void {
123
        $route = util::get_route_instance_for_method($callable);
124
        $this->assertInstanceOf(route::class, $route);
125
        $this->assertEquals($path, $route->get_path());
126
        $this->assertIsString(util::get_route_name_for_callable($callable));
127
        $this->assertEquals($routename, util::get_route_name_for_callable($callable));
128
    }
129
 
130
    /**
131
     * Test getting the route name for an anonymous callable.
132
     */
133
    public function test_get_route_for_callable_not_array_callable(): void {
134
        $this->expectException(\coding_exception::class);
135
        $this->assertNull(util::get_route_name_for_callable(fn () => null));
136
    }
137
 
138
    public function test_get_route_instance_for_request(): void {
139
        self::load_fixture('core', 'router/route_on_method_only.php');
140
 
141
        $app = $this->get_simple_app();
142
        $app->add(moodle_route_attribute_middleware::class);
143
        $app->addRoutingMiddleware();
144
        $app->get('/method/path', [\core\fixtures\route_on_method_only::class, 'method_with_route']);
145
        $app->handle(new ServerRequest('GET', '/method/path'));
146
 
147
        $request = $this->route_request($app, new ServerRequest('GET', '/method/path'));
148
 
149
        $route = util::get_route_instance_for_request($request);
150
 
151
        $this->assertInstanceOf(route::class, $route);
152
        $this->assertEquals('/method/path', $route->get_path());
153
 
154
        $secondroute = util::get_route_instance_for_request($request);
155
        $this->assertInstanceOf(route::class, $secondroute);
156
        $this->assertEquals('/method/path', $secondroute->get_path());
157
    }
158
}