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