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\response;
18
 
19
use core\exception\access_denied_exception;
20
use core\router\schema\response\payload_response;
21
use core\router\schema\specification;
22
use core\tests\router\route_testcase;
23
use GuzzleHttp\Psr7\ServerRequest;
24
 
25
/**
26
 * Tests for the access denied response.
27
 *
28
 * @package    core
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\response\exception_response
32
 * @covers     \core\router\response\access_denied_response
33
 */
34
final class access_denied_response_test extends route_testcase {
35
    public function test_basics(): void {
36
        $this->assertIsInt(access_denied_response::get_exception_status_code());
37
        $this->assertEquals(403, access_denied_response::get_exception_status_code());
38
    }
39
 
40
    public function test_get_response(): void {
41
        $exception = new access_denied_exception('the thing', 'theidentifier');
42
        $request = new ServerRequest('GET', '/example');
43
 
44
        $payload = access_denied_response::get_response($request, $exception);
45
        $this->assertInstanceOf(payload_response::class, $payload);
46
 
47
        $response = $payload->get_response($this->get_router()->get_response_factory());
48
        $this->assertInstanceOf(\Psr\Http\Message\ResponseInterface::class, $response);
49
        $this->assertEquals(403, $response->getStatusCode());
50
        $content = (string) $response->getBody();
51
 
52
        $this->assertStringContainsString(
53
            'the thing',
54
            $content,
55
        );
56
        $this->assertStringContainsString(
57
            'theidentifier',
58
            $content,
59
        );
60
    }
61
 
62
    public function test_openapi_description(): void {
63
        $response = new access_denied_response();
64
        $openapi = $response->get_openapi_description(new specification());
65
 
66
        // The OpenAPI description should be present.
67
        // Note: We do not need to test the value of it. Doing so just reduces maintainability.
68
        $this->assertIsString($openapi->description);
69
    }
70
}