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
 
19
use core\exception\not_found_exception;
20
use core\router\schema\referenced_object;
21
use core\tests\router\route_testcase;
22
use GuzzleHttp\Psr7\ServerRequest;
23
use stdClass;
24
 
25
/**
26
 * Tests for the User Path parameter.
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\parameters\path_user
32
 */
33
final class path_user_test extends route_testcase {
34
    public function test_current_user(): void {
35
        $this->resetAfterTest();
36
 
37
        $user = $this->getDataGenerator()->create_user();
38
        $this->setUser($user);
39
        $context = \core\context\user::instance($user->id);
40
 
41
        $param = new path_user();
42
        $newrequest = $param->add_attributes_for_parameter_value(
43
            new ServerRequest('GET', '/user'),
44
            'current',
45
        );
46
 
47
        $this->assertInstanceOf(stdClass::class, $newrequest->getAttribute('user'));
48
        $this->assertInstanceOf(\core\context\user::class, $newrequest->getAttribute('usercontext'));
49
 
50
        $this->assertEquals($user->id, $newrequest->getAttribute('user')->id);
51
        $this->assertEquals($context->id, $newrequest->getAttribute('usercontext')->id);
52
    }
53
 
54
    public function test_user_id(): void {
55
        $this->resetAfterTest();
56
 
57
        $user = $this->getDataGenerator()->create_user();
58
        $context = \core\context\user::instance($user->id);
59
 
60
        $param = new path_user();
61
        $newrequest = $param->add_attributes_for_parameter_value(
62
            new ServerRequest('GET', '/user'),
63
            $user->id,
64
        );
65
 
66
        $this->assertInstanceOf(stdClass::class, $newrequest->getAttribute('user'));
67
        $this->assertInstanceOf(\core\context\user::class, $newrequest->getAttribute('usercontext'));
68
 
69
        $this->assertEquals($user->id, $newrequest->getAttribute('user')->id);
70
        $this->assertEquals($context->id, $newrequest->getAttribute('usercontext')->id);
71
    }
72
 
73
    public function test_user_idnumber(): void {
74
        $this->resetAfterTest();
75
 
76
        $user = $this->getDataGenerator()->create_user((object) [
77
            'idnumber' => '000117-user',
78
        ]);
79
        $context = \core\context\user::instance($user->id);
80
 
81
        $param = new path_user();
82
        $newrequest = $param->add_attributes_for_parameter_value(
83
            new ServerRequest('GET', '/user'),
84
            "idnumber:{$user->idnumber}",
85
        );
86
 
87
        $this->assertInstanceOf(stdClass::class, $newrequest->getAttribute('user'));
88
        $this->assertInstanceOf(\core\context\user::class, $newrequest->getAttribute('usercontext'));
89
 
90
        $this->assertEquals($user->id, $newrequest->getAttribute('user')->id);
91
        $this->assertEquals($context->id, $newrequest->getAttribute('usercontext')->id);
92
    }
93
 
94
    public function test_username(): void {
95
        $this->resetAfterTest();
96
 
97
        $user = $this->getDataGenerator()->create_user();
98
        $context = \core\context\user::instance($user->id);
99
 
100
        $param = new path_user();
101
        $newrequest = $param->add_attributes_for_parameter_value(
102
            new ServerRequest('GET', '/user'),
103
            "username:{$user->username}",
104
        );
105
 
106
        $this->assertInstanceOf(stdClass::class, $newrequest->getAttribute('user'));
107
        $this->assertInstanceOf(\core\context\user::class, $newrequest->getAttribute('usercontext'));
108
 
109
        $this->assertEquals($user->id, $newrequest->getAttribute('user')->id);
110
        $this->assertEquals($context->id, $newrequest->getAttribute('usercontext')->id);
111
    }
112
 
113
    public function test_validation(): void {
114
        $this->resetAfterTest();
115
 
116
        $user = $this->getDataGenerator()->create_user();
117
        $context = \core\context\user::instance($user->id);
118
 
119
        $request = $this->create_route(
120
            '/user/{user}',
121
            "/user/username:{$user->username}",
122
        );
123
        $route = $this->get_slim_route_from_request($request);
124
 
125
        $param = new path_user();
126
        $newrequest = $param->validate($request, $route);
127
 
128
        $this->assertInstanceOf(stdClass::class, $newrequest->getAttribute('user'));
129
        $this->assertInstanceOf(\core\context\user::class, $newrequest->getAttribute('usercontext'));
130
 
131
        $this->assertEquals($user->id, $newrequest->getAttribute('user')->id);
132
        $this->assertEquals($context->id, $newrequest->getAttribute('usercontext')->id);
133
    }
134
 
135
    /**
136
     * Tests for when a course was not found.
137
     *
138
     * @dataProvider invalid_course_provider
139
     * @param string $searchkey
140
     */
141
    public function test_course_not_found(string $searchkey): void {
142
        $param = new path_user();
143
        $request = new ServerRequest('GET', '/user');
144
 
145
        $this->expectException(not_found_exception::class);
146
        $param->add_attributes_for_parameter_value($request, $searchkey);
147
    }
148
 
149
    /**
150
     * Data provider for test_course_not_found.
151
     */
152
    public static function invalid_course_provider(): array {
153
        return [
154
            'id' => ['999999'],
155
            'idnumber' => ['idnumber:999999'],
156
            'name' => ['username:999999'],
157
            'random string' => ['ksdjflajsdfkjaf:jkladjg9pomadbs902po3'],
158
        ];
159
    }
160
 
161
    public function test_schema(): void {
162
        $param = new path_user();
163
        $api = new \core\router\schema\specification();
164
        $api->add_component($param);
165
        $result = $param->get_openapi_schema($api);
166
 
167
        // Should be a reference.
168
        $this->assertInstanceOf(referenced_object::class, $param);
169
 
170
        $schema = $this->get_api_component_schema($api, $param);
171
        $this->assertIsObject($schema);
172
        $this->assertIsObject($schema->schema);
173
        $this->assertObjectHasProperty('pattern', $schema->schema);
174
 
175
        // We do provide some examples here. Make sure they're valid per the regexp.
176
        $this->assertIsArray($schema->examples);
177
        foreach ($schema->examples as $example) {
178
            $this->assertMatchesRegularExpression("/{$schema->schema->pattern}/", $example->value);
179
        }
180
 
181
        // Some deliberately invalid ones.
182
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'id');
183
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'id:1');
184
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'id;1');
185
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'idnumber');
186
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'idnumber:');
187
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'idnumber;12344');
188
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'name');
189
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'name:');
190
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'name;12345');
191
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'shortname');
192
        $this->assertDoesNotMatchRegularExpression("/{$schema->schema->pattern}/", 'shortname:12345');
193
    }
194
}