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\parameters;
18
use core\tests\router\route_testcase;
19
use GuzzleHttp\Psr7\ServerRequest;
20
use Psr\Http\Message\ServerRequestInterface;
21
 
22
/**
23
 * Tests for the language header.
24
 *
25
 * @package    core
26
 * @category   test
27
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 * @covers    \core\router\parameters\header_language
30
 */
31
final class header_language_test extends route_testcase {
32
    /**
33
     * Test that the parameter is valid when the component is not specified.
34
     */
35
    public function test_component_not_specified(): void {
36
        $param = new header_language();
37
 
38
        $request = new ServerRequest('GET', '/example');
39
 
40
        $this->assertInstanceOf(
41
            ServerRequestInterface::class,
42
            $param->validate($request),
43
        );
44
    }
45
 
46
    /**
47
     * Test that the parameter name is respected
48
     */
49
    public function test_name(): void {
50
        $param = new header_language(name: 'not_the_default_name');
51
        $this->assertEquals('not_the_default_name', $param->get_name());
52
    }
53
 
54
    /**
55
     * Test valid components.
56
     *
57
     * @param string $component
58
     * @dataProvider valid_values
59
     */
60
    public function test_valid_value(string $component): void {
61
        $param = new header_language();
62
 
63
        /** @var ServerRequestInterface $request */ // phpcs:ignore moodle.Commenting.InlineComment.DocBlock
64
        $request = (new ServerRequest('GET', '/example'))
65
            ->withAddedHeader('Language', $component);
66
 
67
        $this->assertInstanceOf(
68
            ServerRequestInterface::class,
69
            $param->validate($request),
70
        );
71
    }
72
 
73
    /**
74
     * Test invalid components.
75
     *
76
     * @param string $component
77
     * @dataProvider invalid_values
78
     */
79
    public function test_invalid_value(string $component): void {
80
        $param = new header_language();
81
 
82
        /** @var ServerRequestInterface */ // phpcs:ignore moodle.Commenting.InlineComment.DocBlock
83
        $request = (new ServerRequest('GET', '/example'))
84
            ->withAddedHeader('Language', $component);
85
 
86
        $this->expectException(\core\exception\invalid_parameter_exception::class);
87
        $param->validate($request);
88
    }
89
 
90
    /**
91
     * Data provider containing seemingly-valid components.
92
     *
93
     * @return array
94
     */
95
    public static function valid_values(): array {
96
        return [
97
            [''],
98
            ['en'],
99
        ];
100
    }
101
 
102
    /**
103
     * Data provider containing invalid components.
104
     *
105
     * @return array
106
     */
107
    public static function invalid_values(): array {
108
        return [
109
            ['de'],
110
            ['Something wrong!!!'],
111
        ];
112
    }
113
}