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_user\route\api;
18
 
19
use core\tests\router\route_testcase;
20
use GuzzleHttp\Psr7\Utils;
21
 
22
/**
23
 * Tests for user preference API handler.
24
 *
25
 * @package    core_user
26
 * @copyright  Andrew Lyons <andrew@nicols.co.uk>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 * @covers \core_user\route\api\preferences
29
 * @covers \core_user\route\responses\user_preferences_response
30
 */
31
final class preferences_test extends route_testcase {
32
    /**
33
     * Ensure that preferences returned for a user without login are empty.
34
     */
35
    public function test_preferences_no_login(): void {
36
        $this->add_class_routes_to_route_loader(preferences::class);
37
        $response = $this->process_api_request('GET', '/current/preferences');
38
 
39
        $this->assert_valid_response($response);
40
        $payload = $this->decode_response($response);
41
 
42
        $this->assertEmpty((array) $payload);
43
    }
44
 
45
    /**
46
     * Test that the preferences are returned when logged in.
47
     */
48
    public function test_preferences_returned(): void {
49
        $this->resetAfterTest();
50
 
51
        $this->add_class_routes_to_route_loader(preferences::class);
52
 
53
        $this->setAdminUser();
54
        set_user_preference('filemanager_recentviewmode', 1);
55
 
56
        $response = $this->process_api_request('GET', '/current/preferences');
57
 
58
        $this->assert_valid_response($response);
59
 
60
        $payload = $this->decode_response($response);
61
 
62
        $this->assertObjectHasProperty('filemanager_recentviewmode', $payload);
63
        $this->assertEquals(1, $payload->filemanager_recentviewmode);
64
    }
65
 
66
    public function test_preference_returned(): void {
67
        $this->resetAfterTest();
68
 
69
        $this->add_class_routes_to_route_loader(preferences::class);
70
 
71
        $this->setAdminUser();
72
        set_user_preference('filemanager_recentviewmode', 1);
73
 
74
        $response = $this->process_api_request('GET', '/current/preferences/filemanager_recentviewmode');
75
 
76
        $this->assert_valid_response($response);
77
 
78
        $payload = $this->decode_response($response);
79
 
80
        $this->assertObjectHasProperty('filemanager_recentviewmode', $payload);
81
        $this->assertEquals(1, $payload->filemanager_recentviewmode);
82
    }
83
 
84
    public function test_preferences_set(): void {
85
        $this->resetAfterTest();
86
 
87
        $this->add_class_routes_to_route_loader(preferences::class);
88
 
89
        $this->setAdminUser();
90
 
91
        $response = $this->process_api_request(
92
            'POST',
93
            '/current/preferences',
94
            body: Utils::streamFor(json_encode([
95
                'preferences' => [
96
                    'filemanager_recentviewmode' => 2,
97
                    'drawer-open-index' => 1,
98
                ],
99
            ])),
100
        );
101
 
102
        $this->assert_valid_response($response);
103
 
104
        // Check that the response contained the updtaed parameter.
105
        $payload = (object) $this->decode_response($response);
106
        $this->assertObjectHasProperty('filemanager_recentviewmode', $payload);
107
        $this->assertObjectHasProperty('drawer-open-index', $payload);
108
        $this->assertEquals(2, $payload->filemanager_recentviewmode);
109
 
110
        // Check that the preference was updated.
111
        $this->assertEquals(2, get_user_preferences('filemanager_recentviewmode'));
112
        $this->assertEquals(1, get_user_preferences('drawer-open-index'));
113
    }
114
 
115
    /**
116
     * Test that an invalid preference is rejected.
117
     */
118
    public function test_preferences_set_invalid_value(): void {
119
        $this->resetAfterTest();
120
 
121
        $this->add_class_routes_to_route_loader(preferences::class);
122
 
123
        $this->setAdminUser();
124
 
125
        $response = $this->process_api_request(
126
            'POST',
127
            '/current/preferences',
128
            body: Utils::streamFor(json_encode([
129
                'preferences' => [
130
                    'filemanager_recentviewmode' => 4,
131
                ],
132
            ])),
133
        );
134
 
135
        $this->assert_invalid_parameter_response($response);
136
        $payload = $this->decode_response($response);
137
        $this->assertStringContainsString('filemanager_recentviewmode', $payload->message);
138
    }
139
 
140
    /**
141
     * Test that a preference the user does not have permission to is rejected.
142
     */
143
    public function test_preferences_set_not_permitted_valid_login(): void {
144
        $this->resetAfterTest();
145
 
146
        $this->add_class_routes_to_route_loader(preferences::class);
147
 
148
        $this->setAdminUser();
149
 
150
        $response = $this->process_api_request(
151
            'POST',
152
            '/current/preferences',
153
            body: Utils::streamFor(json_encode([
154
                'preferences' => [
155
                    'auth_forcepasswordchange' => 4,
156
                ],
157
            ])),
158
        );
159
 
160
        $this->assert_access_denied_response($response);
161
    }
162
 
163
    public function test_preference_set(): void {
164
        $this->resetAfterTest();
165
 
166
        $this->add_class_routes_to_route_loader(preferences::class);
167
 
168
        $this->setAdminUser();
169
 
170
        $response = $this->process_api_request(
171
            'POST',
172
            '/current/preferences/filemanager_recentviewmode',
173
            body: Utils::streamFor(json_encode([
174
                'value' => 2,
175
            ])),
176
        );
177
 
178
        $this->assert_valid_response($response);
179
 
180
        // Check that the response contained the updtaed parameter.
181
        $payload = $this->decode_response($response);
182
        $this->assertObjectHasProperty('filemanager_recentviewmode', $payload);
183
        $this->assertEquals(2, $payload->filemanager_recentviewmode);
184
 
185
        // Check that the preference was updated.
186
        $this->assertEquals(2, get_user_preferences('filemanager_recentviewmode'));
187
    }
188
 
189
    /**
190
     * Test that an invalid preference is rejected.
191
     */
192
    public function test_preference_set_invalid_value(): void {
193
        $this->resetAfterTest();
194
 
195
        $this->add_class_routes_to_route_loader(preferences::class);
196
 
197
        $this->setAdminUser();
198
 
199
        $response = $this->process_api_request(
200
            'POST',
201
            '/current/preferences/filemanager_recentviewmode',
202
            body: Utils::streamFor(json_encode([
203
                'value' => 4,
204
            ])),
205
        );
206
 
207
        $this->assert_invalid_parameter_response($response);
208
        $payload = $this->decode_response($response);
209
        $this->assertStringContainsString('filemanager_recentviewmode', $payload->message);
210
    }
211
 
212
    /**
213
     * Test that an invalid preference inentifier is rejected.
214
     */
215
    public function test_preference_set_invalid_preference(): void {
216
        $this->resetAfterTest();
217
 
218
        $this->add_class_routes_to_route_loader(preferences::class);
219
 
220
        $this->setAdminUser();
221
 
222
        $response = $this->process_api_request(
223
            'POST',
224
            '/current/preferences/what_a_fake',
225
            body: Utils::streamFor(json_encode([
226
                'value' => 4,
227
            ])),
228
        );
229
 
230
        $this->assert_invalid_parameter_response($response);
231
        $payload = $this->decode_response($response);
232
        $this->assertStringContainsString('what_a_fake', $payload->message);
233
    }
234
 
235
    /**
236
     * Test that a preference the user does not have permission to is rejected.
237
     */
238
    public function test_preference_set_not_permitted_valid_login(): void {
239
        $this->resetAfterTest();
240
 
241
        $this->add_class_routes_to_route_loader(preferences::class);
242
 
243
        $this->setAdminUser();
244
 
245
        $response = $this->process_api_request(
246
            'POST',
247
            '/current/preferences/auth_forcepasswordchange',
248
            body: Utils::streamFor(json_encode([
249
                'value' => 4,
250
            ])),
251
        );
252
 
253
        $this->assert_access_denied_response($response);
254
    }
255
 
256
    /**
257
     * A user cannot get or set preferences for anothe ruser.
258
     */
259
    public function test_preference_get_other_user(): void {
260
        $this->resetAfterTest();
261
        $this->setAdminUser();
262
        $user = $this->getDataGenerator()->create_user();
263
 
264
        $this->add_class_routes_to_route_loader(preferences::class);
265
 
266
        // Get all preferences.
267
        $response = $this->process_api_request('GET', "/{$user->id}/preferences");
268
        $this->assert_access_denied_response($response);
269
 
270
        // Get one preference.
271
        $response = $this->process_api_request('GET', "/{$user->id}/preferences/example");
272
        $this->assert_access_denied_response($response);
273
 
274
        // Set all preferences.
275
        $response = $this->process_api_request(
276
            'POST',
277
            "/{$user->id}/preferences/filemanager_recentviewmode",
278
            body: Utils::streamFor(json_encode([
279
                'value' => 4,
280
            ])),
281
        );
282
        $this->assert_access_denied_response($response);
283
 
284
        // Get all preferences.
285
        $response = $this->process_api_request(
286
            'POST',
287
            "/{$user->id}/preferences",
288
            body: Utils::streamFor(json_encode([
289
                'preferences' => [
290
                    'filemanager_recentviewmode' => 2,
291
                ],
292
            ])),
293
        );
294
        $this->assert_access_denied_response($response);
295
    }
296
}