-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathConfigControllerTest.php
More file actions
135 lines (113 loc) Β· 3.58 KB
/
ConfigControllerTest.php
File metadata and controls
135 lines (113 loc) Β· 3.58 KB
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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Forms\Tests\Unit\Controller;
use OCA\Forms\Controller\ConfigController;
use OCA\Forms\Service\ConfigService;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\Services\IAppConfig;
use OCP\IRequest;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class ConfigControllerTest extends TestCase {
private ConfigController $configController;
private ConfigService|MockObject $configService;
private IAppConfig|MockObject $config;
private LoggerInterface|MockObject $logger;
private IRequest|MockObject $request;
public function setUp(): void {
parent::setUp();
$this->configService = $this->createMock(ConfigService::class);
$this->config = $this->createMock(IAppConfig::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->request = $this->createMock(IRequest::class);
$this->configController = new ConfigController(
'forms',
$this->configService,
$this->config,
$this->logger,
$this->request
);
}
public function testGetAppConfig() {
$this->configService->expects($this->once())
->method('getAppConfig')
->willReturn([
'allow' => 'someConfig',
'permit' => 'all'
]);
$this->assertEquals(new DataResponse([
'allow' => 'someConfig',
'permit' => 'all'
]), $this->configController->getAppConfig());
}
public static function dataUpdateAppConfig() {
return [
'booleanAllowPermitAll' => [
'configKey' => 'allowPermitAll',
'configValue' => true,
'strConfig' => 'true'
],
'booleanAllowShowToAll' => [
'configKey' => 'allowShowToAll',
'configValue' => true,
'strConfig' => 'true'
],
'arrayCreationAllowedGroups' => [
'configKey' => 'creationAllowedGroups',
'configValue' => [
'admin',
'group1'
],
'strConfig' => '["admin","group1"]'
]
];
}
/**
* @dataProvider dataUpdateAppConfig
*
* @param string $configKey
* @param mixed $configValue
* @param string $strConfig The configValue as json-string
*/
public function testUpdateAppConfig(string $configKey, $configValue, string $strConfig) {
$this->logger->expects($this->once())
->method('debug');
if (is_array($configValue)) {
$this->config->expects($this->once())
->method('setAppValueArray')
->with($configKey, $configValue);
} else {
$this->config->expects($this->once())
->method('setAppValueBool')
->with($configKey, $configValue);
}
$this->assertEquals(new DataResponse(), $this->configController->updateAppConfig($configKey, $configValue));
}
public function testUpdateAppConfig_unknownKey() {
$this->logger->expects($this->once())
->method('debug');
$this->config->expects($this->never())
->method('setAppValueBool');
$this->config->expects($this->never())
->method('setAppValueArray');
$this->assertEquals(new DataResponse('Unknown appConfig key: someUnknownKey', 400), $this->configController->updateAppConfig('someUnknownKey', 'storeThisValue!'));
}
public function testUpdateAppConfigRejectsConfirmationEmailWhenMailIsNotConfigured() {
$this->logger->expects($this->once())
->method('debug');
$this->configService->expects($this->once())
->method('isMailConfigured')
->willReturn(false);
$this->config->expects($this->never())
->method('setAppValue');
$this->assertEquals(
new DataResponse('Mail server is not configured', 400),
$this->configController->updateAppConfig('allowConfirmationEmail', true)
);
}
}