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;
20
use core\tests\router\route_testcase;
21
use GuzzleHttp\Psr7\Response;
22
use GuzzleHttp\Psr7\ServerRequest;
23
use GuzzleHttp\Psr7\Uri;
24
use Psr\Http\Message\ResponseInterface;
25
use Slim\Exception\HttpNotFoundException;
26
 
27
/**
28
 * Tests for the route_controller trait.
29
 *
30
 * @package    core
31
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @covers     \core\router\route_controller
34
 */
35
final class route_controller_test extends route_testcase {
36
    /**
37
     * Test that the redirect method works as expected.
38
     *
39
     * @covers ::redirect
40
     */
41
    public function test_redirect(): void {
42
        $helper = new class (\core\di::get_container()) {
43
            use route_controller;
44
 
45
            // phpcs:ignore moodle.Commenting.MissingDocblock.MissingTestcaseMethodDescription
46
            public function test(
47
                ResponseInterface $response,
48
                $url,
49
            ) {
50
                return $this->redirect($response, $url);
51
            }
52
        };
53
 
54
        $response = $helper->test(new Response(), '/test');
55
        $this->assertEquals(302, $response->getStatusCode());
56
        $this->assertEquals('/test', $response->getHeaderLine('Location'));
57
    }
58
 
59
    public function test_page_not_found(): void {
60
        $request = new ServerRequest('GET', '/test');
61
        $response = new Response();
62
 
63
        $helper = new class (\core\di::get_container()) {
64
            use route_controller;
65
        };
66
 
67
        $rc = new \ReflectionClass($helper);
68
        $rcm = $rc->getMethod('page_not_found');
69
 
70
        $this->expectException(HttpNotFoundException::class);
71
        $rcm->invokeArgs($helper, [$request, $response]);
72
    }
73
 
74
    /**
75
     * Test that get_param works as expected.
76
     *
77
     * @covers \core\router\route_controller::get_param
78
     */
79
    public function test_get_param(): void {
80
        $request = (new \GuzzleHttp\Psr7\ServerRequest('GET', '/test'))
81
            ->withQueryParams(['test' => 'value']);
82
 
83
        $helper = new class (\core\di::get_container()) {
84
            use route_controller;
85
        };
86
 
87
        $rc = new \ReflectionClass($helper);
88
        $rcm = $rc->getMethod('get_param');
89
 
90
        // Test a value that exists.
91
        $result = $rcm->invokeArgs($helper, [$request, 'test', null]);
92
        $this->assertEquals('value', $result);
93
 
94
        $result = $rcm->invokeArgs($helper, [$request, 'test', 'Unused default']);
95
        $this->assertEquals('value', $result);
96
 
97
        // Test a value that does not existexists.
98
        $result = $rcm->invokeArgs($helper, [$request, 'fake', null]);
99
        $this->assertEquals(null, $result);
100
        $this->assertdebuggingcalledcount(1);
101
 
102
        $result = $rcm->invokeArgs($helper, [$request, 'fake', 'Used default']);
103
        $this->assertEquals('Used default', $result);
104
        $this->assertdebuggingcalledcount(1);
105
    }
106
 
107
    /**
108
     * Test that it is possible to redirect to a callable.
109
     *
110
     * @covers \core\router\route_controller::redirect_to_callable
111
     */
112
    public function test_redirect_to_callable(): void {
113
        self::load_fixture('core', '/router/route_on_class.php');
114
 
115
        $rc = new \ReflectionClass(\core\fixtures\route_on_class::class);
116
        $rcm = $rc->getMethod('method_with_route');
117
        $route = $rcm->getAttributes(route::class)[0]->newInstance();
118
        $router = $this->get_router();
119
        $app = $router->get_app();
120
        \core\di::get_container()->set(router::class, $router);
121
 
122
        $app
123
            ->get(
124
                $route->get_path(),
125
                ['core\fixtures\route_on_class', 'method_with_route'],
126
            )
127
            ->setName('core\fixtures\route_on_class::method_with_route');
128
 
129
        $helper = new class (\core\di::get_container()) {
130
            use route_controller;
131
        };
132
        $rc = new \ReflectionClass($helper);
133
        $rcm = $rc->getMethod('redirect_to_callable');
134
 
135
        $response = $rcm->invokeArgs(
136
            $helper,
137
            [
138
                new ServerRequest('GET', '/test'),
139
                new Response(),
140
                'core\fixtures\route_on_class::method_with_route',
141
            ],
142
        );
143
        $this->assertInstanceOf(ResponseInterface::class, $response);
144
        $this->assertEquals(302, $response->getStatusCode());
145
        $this->assertTrue($response->hasHeader('Location'));
146
 
147
        $uri = new Uri($response->getHeader('Location')[0]);
148
 
149
        $this->assertEmpty($uri->getQuery());
150
    }
151
}