1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Settings;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\Settings\DeclarativeManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IAppConfig;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\Settings\DeclarativeSettingsTypes;
use OCP\Settings\Events\DeclarativeSettingsSetValueEvent;
use OCP\Settings\IDeclarativeManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class DeclarativeManagerTest extends TestCase {
/** @var IDeclarativeManager|MockObject */
private $declarativeManager;
/** @var IEventDispatcher|MockObject */
private $eventDispatcher;
/** @var IGroupManager|MockObject */
private $groupManager;
/** @var Coordinator|MockObject */
private $coordinator;
/** @var IConfig|MockObject */
private $config;
/** @var IAppConfig|MockObject */
private $appConfig;
/** @var LoggerInterface|MockObject */
private $logger;
/** @var IUser|MockObject */
private $user;
/** @var IUser|MockObject */
private $adminUser;
public const validSchemaAllFields = [
'id' => 'test_form_1',
'priority' => 10,
'section_type' => DeclarativeSettingsTypes::SECTION_TYPE_ADMIN, // admin, personal
'section_id' => 'additional',
'storage_type' => DeclarativeSettingsTypes::STORAGE_TYPE_INTERNAL, // external, internal (handled by core to store in appconfig and preferences)
'title' => 'Test declarative settings', // NcSettingsSection name
'description' => 'These fields are rendered dynamically from declarative schema', // NcSettingsSection description
'doc_url' => '', // NcSettingsSection doc_url for documentation or help page, empty string if not needed
'fields' => [
[
'id' => 'test_field_7', // configkey
'title' => 'Multi-selection', // name or label
'description' => 'Select some option setting', // hint
'type' => DeclarativeSettingsTypes::MULTI_SELECT,
'options' => ['foo', 'bar', 'baz'], // simple options for select, radio, multi-select
'placeholder' => 'Select some multiple options', // input placeholder
'default' => ['foo', 'bar'],
],
[
'id' => 'some_real_setting',
'title' => 'Select single option',
'description' => 'Single option radio buttons',
'type' => DeclarativeSettingsTypes::RADIO, // radio (NcCheckboxRadioSwitch type radio)
'placeholder' => 'Select single option, test interval',
'default' => '40m',
'options' => [
[
'name' => 'Each 40 minutes', // NcCheckboxRadioSwitch display name
'value' => '40m' // NcCheckboxRadioSwitch value
],
[
'name' => 'Each 60 minutes',
'value' => '60m'
],
[
'name' => 'Each 120 minutes',
'value' => '120m'
],
[
'name' => 'Each day',
'value' => 60 * 24 . 'm'
],
],
],
[
'id' => 'test_field_1', // configkey
'title' => 'Default text field', // label
'description' => 'Set some simple text setting', // hint
'type' => DeclarativeSettingsTypes::TEXT,
'placeholder' => 'Enter text setting', // placeholder
'default' => 'foo',
],
[
'id' => 'test_field_1_1',
'title' => 'Email field',
'description' => 'Set email config',
'type' => DeclarativeSettingsTypes::EMAIL,
'placeholder' => 'Enter email',
'default' => '',
],
[
'id' => 'test_field_1_2',
'title' => 'Tel field',
'description' => 'Set tel config',
'type' => DeclarativeSettingsTypes::TEL,
'placeholder' => 'Enter your tel',
'default' => '',
],
[
'id' => 'test_field_1_3',
'title' => 'Url (website) field',
'description' => 'Set url config',
'type' => 'url',
'placeholder' => 'Enter url',
'default' => '',
],
[
'id' => 'test_field_1_4',
'title' => 'Number field',
'description' => 'Set number config',
'type' => DeclarativeSettingsTypes::NUMBER,
'placeholder' => 'Enter number value',
'default' => 0,
],
[
'id' => 'test_field_2',
'title' => 'Password',
'description' => 'Set some secure value setting',
'type' => 'password',
'placeholder' => 'Set secure value',
'default' => '',
],
[
'id' => 'test_field_3',
'title' => 'Selection',
'description' => 'Select some option setting',
'type' => DeclarativeSettingsTypes::SELECT,
'options' => ['foo', 'bar', 'baz'],
'placeholder' => 'Select some option setting',
'default' => 'foo',
],
[
'id' => 'test_field_4',
'title' => 'Toggle something',
'description' => 'Select checkbox option setting',
'type' => DeclarativeSettingsTypes::CHECKBOX,
'label' => 'Verify something if enabled',
'default' => false,
],
[
'id' => 'test_field_5',
'title' => 'Multiple checkbox toggles, describing one setting, checked options are saved as an JSON object {foo: true, bar: false}',
'description' => 'Select checkbox option setting',
'type' => DeclarativeSettingsTypes::MULTI_CHECKBOX,
'default' => ['foo' => true, 'bar' => true],
'options' => [
[
'name' => 'Foo',
'value' => 'foo', // multiple-checkbox configkey
],
[
'name' => 'Bar',
'value' => 'bar',
],
[
'name' => 'Baz',
'value' => 'baz',
],
[
'name' => 'Qux',
'value' => 'qux',
],
],
],
[
'id' => 'test_field_6',
'title' => 'Radio toggles, describing one setting like single select',
'description' => 'Select radio option setting',
'type' => DeclarativeSettingsTypes::RADIO, // radio (NcCheckboxRadioSwitch type radio)
'label' => 'Select single toggle',
'default' => 'foo',
'options' => [
[
'name' => 'First radio', // NcCheckboxRadioSwitch display name
'value' => 'foo' // NcCheckboxRadioSwitch value
],
[
'name' => 'Second radio',
'value' => 'bar'
],
[
'name' => 'Second radio',
'value' => 'baz'
],
],
],
],
];
public static bool $testSetInternalValueAfterChange = false;
protected function setUp(): void {
parent::setUp();
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->groupManager = $this->createMock(IGroupManager::class);
$this->coordinator = $this->createMock(Coordinator::class);
$this->config = $this->createMock(IConfig::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->declarativeManager = new DeclarativeManager(
$this->eventDispatcher,
$this->groupManager,
$this->coordinator,
$this->config,
$this->appConfig,
$this->logger
);
$this->user = $this->createMock(IUser::class);
$this->user->expects($this->any())
->method('getUID')
->willReturn('test_user');
$this->adminUser = $this->createMock(IUser::class);
$this->adminUser->expects($this->any())
->method('getUID')
->willReturn('admin_test_user');
$this->groupManager->expects($this->any())
->method('isAdmin')
->willReturnCallback(function ($userId) {
return $userId === 'admin_test_user';
});
}
public function testRegisterSchema(): void {
$app = 'testing';
$schema = self::validSchemaAllFields;
$this->declarativeManager->registerSchema($app, $schema);
$formIds = $this->declarativeManager->getFormIDs($this->adminUser, $schema['section_type'], $schema['section_id']);
$this->assertTrue(isset($formIds[$app]) && in_array($schema['id'], $formIds[$app]));
}
/**
* Simple test to verify that exception is thrown when trying to register schema with duplicate id
*/
public function testRegisterDuplicateSchema(): void {
$this->declarativeManager->registerSchema('testing', self::validSchemaAllFields);
$this->expectException(\Exception::class);
$this->declarativeManager->registerSchema('testing', self::validSchemaAllFields);
}
/**
* It's not allowed to register schema with duplicate fields ids for the same app
*/
public function testRegisterSchemaWithDuplicateFields(): void {
// Register first valid schema
$this->declarativeManager->registerSchema('testing', self::validSchemaAllFields);
// Register second schema with duplicate fields, but different schema id
$this->expectException(\Exception::class);
$schema = self::validSchemaAllFields;
$schema['id'] = 'test_form_2';
$this->declarativeManager->registerSchema('testing', $schema);
}
public function testRegisterMultipleSchemasAndDuplicate(): void {
$app = 'testing';
$schema = self::validSchemaAllFields;
$this->declarativeManager->registerSchema($app, $schema);
$formIds = $this->declarativeManager->getFormIDs($this->adminUser, $schema['section_type'], $schema['section_id']);
// 1. Check that form is registered for the app
$this->assertTrue(isset($formIds[$app]) && in_array($schema['id'], $formIds[$app]));
$app = 'testing2';
$this->declarativeManager->registerSchema($app, $schema);
$formIds = $this->declarativeManager->getFormIDs($this->adminUser, $schema['section_type'], $schema['section_id']);
// 2. Check that form is registered for the second app
$this->assertTrue(isset($formIds[$app]) && in_array($schema['id'], $formIds[$app]));
$app = 'testing';
$this->expectException(\Exception::class); // expecting duplicate form id and duplicate fields ids exception
$this->declarativeManager->registerSchema($app, $schema);
$schemaDuplicateFields = self::validSchemaAllFields;
$schemaDuplicateFields['id'] = 'test_form_2'; // change form id to test duplicate fields
$this->declarativeManager->registerSchema($app, $schemaDuplicateFields);
// 3. Check that not valid form with duplicate fields is not registered
$formIds = $this->declarativeManager->getFormIDs($this->adminUser, $schemaDuplicateFields['section_type'], $schemaDuplicateFields['section_id']);
$this->assertFalse(isset($formIds[$app]) && in_array($schemaDuplicateFields['id'], $formIds[$app]));
}
/**
* @dataProvider dataValidateSchema
*/
public function testValidateSchema(bool $expected, bool $expectException, string $app, array $schema): void {
if ($expectException) {
$this->expectException(\Exception::class);
}
$this->declarativeManager->registerSchema($app, $schema);
$formIds = $this->declarativeManager->getFormIDs($this->adminUser, $schema['section_type'], $schema['section_id']);
$this->assertEquals($expected, isset($formIds[$app]) && in_array($schema['id'], $formIds[$app]));
}
public static function dataValidateSchema(): array {
return [
'valid schema with all supported fields' => [
true,
false,
'testing',
self::validSchemaAllFields,
],
'invalid schema with missing id' => [
false,
true,
'testing',
[
'priority' => 10,
'section_type' => DeclarativeSettingsTypes::SECTION_TYPE_ADMIN,
'section_id' => 'additional',
'storage_type' => DeclarativeSettingsTypes::STORAGE_TYPE_INTERNAL,
'title' => 'Test declarative settings',
'description' => 'These fields are rendered dynamically from declarative schema',
'doc_url' => '',
'fields' => [
[
'id' => 'test_field_7',
'title' => 'Multi-selection',
'description' => 'Select some option setting',
'type' => DeclarativeSettingsTypes::MULTI_SELECT,
'options' => ['foo', 'bar', 'baz'],
'placeholder' => 'Select some multiple options',
'default' => ['foo', 'bar'],
],
],
],
],
'invalid schema with invalid field' => [
false,
true,
'testing',
[
'id' => 'test_form_1',
'priority' => 10,
'section_type' => DeclarativeSettingsTypes::SECTION_TYPE_ADMIN,
'section_id' => 'additional',
'storage_type' => DeclarativeSettingsTypes::STORAGE_TYPE_INTERNAL,
'title' => 'Test declarative settings',
'description' => 'These fields are rendered dynamically from declarative schema',
'doc_url' => '',
'fields' => [
[
'id' => 'test_invalid_field',
'title' => 'Invalid field',
'description' => 'Some invalid setting description',
'type' => 'some_invalid_type',
'placeholder' => 'Some invalid field placeholder',
'default' => null,
],
],
],
],
];
}
public function testGetFormIDs(): void {
$app = 'testing';
$schema = self::validSchemaAllFields;
$this->declarativeManager->registerSchema($app, $schema);
$formIds = $this->declarativeManager->getFormIDs($this->adminUser, $schema['section_type'], $schema['section_id']);
$this->assertTrue(isset($formIds[$app]) && in_array($schema['id'], $formIds[$app]));
$app = 'testing2';
$this->declarativeManager->registerSchema($app, $schema);
$formIds = $this->declarativeManager->getFormIDs($this->adminUser, $schema['section_type'], $schema['section_id']);
$this->assertTrue(isset($formIds[$app]) && in_array($schema['id'], $formIds[$app]));
}
/**
* Check that form with default values is returned with internal storage_type
*/
public function testGetFormsWithDefaultValues(): void {
$app = 'testing';
$schema = self::validSchemaAllFields;
$this->declarativeManager->registerSchema($app, $schema);
$this->config->expects($this->any())
->method('getAppValue')
->willReturnCallback(fn ($app, $configkey, $default) => $default);
$forms = $this->declarativeManager->getFormsWithValues($this->adminUser, $schema['section_type'], $schema['section_id']);
$this->assertNotEmpty($forms);
$this->assertTrue(array_search($schema['id'], array_column($forms, 'id')) !== false);
// Check some_real_setting field default value
$someRealSettingField = array_values(array_filter(array_filter($forms, fn ($form) => $form['id'] === $schema['id'])[0]['fields'], fn ($field) => $field['id'] === 'some_real_setting'))[0];
$schemaSomeRealSettingField = array_values(array_filter($schema['fields'], fn ($field) => $field['id'] === 'some_real_setting'))[0];
$this->assertEquals($schemaSomeRealSettingField['default'], $someRealSettingField['default']);
}
/**
* Check values in json format to ensure that they are properly encoded
*/
public function testGetFormsWithDefaultValuesJson(): void {
$app = 'testing';
$schema = [
'id' => 'test_form_1',
'priority' => 10,
'section_type' => DeclarativeSettingsTypes::SECTION_TYPE_PERSONAL,
'section_id' => 'additional',
'storage_type' => DeclarativeSettingsTypes::STORAGE_TYPE_INTERNAL,
'title' => 'Test declarative settings',
'description' => 'These fields are rendered dynamically from declarative schema',
'doc_url' => '',
'fields' => [
[
'id' => 'test_field_json',
'title' => 'Multi-selection',
'description' => 'Select some option setting',
'type' => DeclarativeSettingsTypes::MULTI_SELECT,
'options' => ['foo', 'bar', 'baz'],
'placeholder' => 'Select some multiple options',
'default' => ['foo', 'bar'],
],
],
];
$this->declarativeManager->registerSchema($app, $schema);
// config->getUserValue() should be called with json encoded default value
$this->config->expects($this->once())
->method('getUserValue')
->with($this->adminUser->getUID(), $app, 'test_field_json', json_encode($schema['fields'][0]['default']))
->willReturn(json_encode($schema['fields'][0]['default']));
$forms = $this->declarativeManager->getFormsWithValues($this->adminUser, $schema['section_type'], $schema['section_id']);
$this->assertNotEmpty($forms);
$this->assertTrue(array_search($schema['id'], array_column($forms, 'id')) !== false);
$testFieldJson = array_values(array_filter(array_filter($forms, fn ($form) => $form['id'] === $schema['id'])[0]['fields'], fn ($field) => $field['id'] === 'test_field_json'))[0];
$this->assertEquals(json_encode($schema['fields'][0]['default']), $testFieldJson['value']);
}
/**
* Check that saving value for field with internal storage_type is handled by core
*/
public function testSetInternalValue(): void {
$app = 'testing';
$schema = self::validSchemaAllFields;
$this->declarativeManager->registerSchema($app, $schema);
self::$testSetInternalValueAfterChange = false;
$this->config->expects($this->any())
->method('getAppValue')
->willReturnCallback(function ($app, $configkey, $default) {
if ($configkey === 'some_real_setting' && self::$testSetInternalValueAfterChange) {
return '120m';
}
return $default;
});
$this->appConfig->expects($this->once())
->method('setValueString')
->with($app, 'some_real_setting', '120m');
$forms = $this->declarativeManager->getFormsWithValues($this->adminUser, $schema['section_type'], $schema['section_id']);
$someRealSettingField = array_values(array_filter(array_filter($forms, fn ($form) => $form['id'] === $schema['id'])[0]['fields'], fn ($field) => $field['id'] === 'some_real_setting'))[0];
$this->assertEquals('40m', $someRealSettingField['value']); // first check that default value (40m) is returned
// Set new value for some_real_setting field
$this->declarativeManager->setValue($this->adminUser, $app, $schema['id'], 'some_real_setting', '120m');
self::$testSetInternalValueAfterChange = true;
$forms = $this->declarativeManager->getFormsWithValues($this->adminUser, $schema['section_type'], $schema['section_id']);
$this->assertNotEmpty($forms);
$this->assertTrue(array_search($schema['id'], array_column($forms, 'id')) !== false);
// Check some_real_setting field default value
$someRealSettingField = array_values(array_filter(array_filter($forms, fn ($form) => $form['id'] === $schema['id'])[0]['fields'], fn ($field) => $field['id'] === 'some_real_setting'))[0];
$this->assertEquals('120m', $someRealSettingField['value']);
}
public function testSetExternalValue(): void {
$app = 'testing';
$schema = self::validSchemaAllFields;
// Change storage_type to external and section_type to personal
$schema['storage_type'] = DeclarativeSettingsTypes::STORAGE_TYPE_EXTERNAL;
$schema['section_type'] = DeclarativeSettingsTypes::SECTION_TYPE_PERSONAL;
$this->declarativeManager->registerSchema($app, $schema);
$setDeclarativeSettingsValueEvent = new DeclarativeSettingsSetValueEvent(
$this->adminUser,
$app,
$schema['id'],
'some_real_setting',
'120m'
);
$this->eventDispatcher->expects($this->once())
->method('dispatchTyped')
->with($setDeclarativeSettingsValueEvent);
$this->declarativeManager->setValue($this->adminUser, $app, $schema['id'], 'some_real_setting', '120m');
}
public function testAdminFormUserUnauthorized(): void {
$app = 'testing';
$schema = self::validSchemaAllFields;
$this->declarativeManager->registerSchema($app, $schema);
$this->expectException(\Exception::class);
$this->declarativeManager->getFormsWithValues($this->user, $schema['section_type'], $schema['section_id']);
}
}
|