-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathRulesServiceTest.php
More file actions
226 lines (198 loc) · 5.97 KB
/
RulesServiceTest.php
File metadata and controls
226 lines (198 loc) · 5.97 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
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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Tests\Unit\Service\Certificate;
use OCA\Libresign\AppInfo\Application;
use OCA\Libresign\Service\Certificate\RulesService;
use OCP\IL10N;
use OCP\L10N\IFactory as IL10NFactory;
use PHPUnit\Framework\Attributes\DataProvider;
final class RulesServiceTest extends \OCA\Libresign\Tests\Unit\TestCase {
private IL10N $l10n;
public function setUp(): void {
$this->l10n = \OCP\Server::get(IL10NFactory::class)->get(Application::APP_ID);
}
public function getService(): RulesService {
return new RulesService(
$this->l10n,
);
}
#[DataProvider('providerGetHelperTextWithValidFieldName')]
public function testGetHelperTextWithValidFieldName(string $fieldName, string $expected): void {
$service = $this->getService();
$helperText = $service->getHelperText($fieldName);
$this->assertNotEmpty($helperText);
$this->assertStringContainsString($expected, $helperText);
}
public static function providerGetHelperTextWithValidFieldName(): array {
return [
['CN', 'Common Name'],
['C', 'country code'],
['ST', 'states'],
['L', 'locality'],
['O', 'organization'],
['OU', 'organizational unit'],
];
}
#[DataProvider('providerGetHelperTextWithInvalidFieldName')]
public function testGetHelperTextWithInvalidFieldName(string $fieldName): void {
$service = $this->getService();
$helperText = $service->getHelperText($fieldName);
$this->assertNull($helperText);
}
public static function providerGetHelperTextWithInvalidFieldName(): array {
return [
['INVALID'],
[''],
['123'],
['!@#'],
['CN1'],
['C2'],
['ST3'],
['L4'],
['O5'],
['OU6'],
];
}
#[DataProvider('providerGetRuleToValidField')]
public function testGetRuleToValidField(string $fieldName, array $expected): void {
$service = $this->getService();
$actual = $service->getRule($fieldName);
$this->assertArrayHasKey('helperText', $actual);
unset($actual['helperText']);
$this->assertCount(count($expected), $actual, "Expected count does not match actual count for field: $fieldName");
$this->assertSame($expected, $actual, "Mismatch for field: $fieldName");
}
public static function providerGetRuleToValidField(): array {
return [
[
'CN', [
'type' => 'string',
'required' => true,
'min' => 1,
'max' => 64,
],
],
[
'C', [
'type' => 'string',
'min' => 2,
'max' => 2,
],
],
[
'ST', [
'type' => 'string',
'min' => 1,
'max' => 128,
],
],
[
'L', [
'type' => 'string',
'min' => 1,
'max' => 128,
],
],
[
'O', [
'type' => 'string',
'min' => 1,
'max' => 64,
],
],
[
'OU', [
'type' => 'array',
'required' => false,
'min' => 1,
'max' => 64,
'minItems' => 0,
'maxItems' => 10,
],
],
];
}
#[DataProvider('providerGetRuleToInvalidField')]
public function testGetRuleToInvalidField(string $fieldName): void {
$service = $this->getService();
$actual = $service->getRule($fieldName);
$this->assertEmpty($actual);
}
public static function providerGetRuleToInvalidField(): array {
return [
['INVALID'],
[''],
['123'],
['!@#'],
['CN1'],
['C2'],
['ST3'],
['L4'],
['O5'],
['OU6'],
];
}
#[DataProvider('providerGetRuleTypeValidation')]
public function testGetRuleTypeValidation(string $fieldName, string $expectedType): void {
$service = $this->getService();
$rule = $service->getRule($fieldName);
$this->assertArrayHasKey('type', $rule, "Field $fieldName should have a type defined");
$this->assertSame($expectedType, $rule['type'], "Field $fieldName should be of type $expectedType");
}
public static function providerGetRuleTypeValidation(): array {
return [
['CN', 'string'],
['C', 'string'],
['ST', 'string'],
['L', 'string'],
['O', 'string'],
['OU', 'array'],
];
}
#[DataProvider('providerGetRuleArraySpecificValidation')]
public function testGetRuleArraySpecificValidation(string $fieldName, array $expectedProperties): void {
$service = $this->getService();
$rule = $service->getRule($fieldName);
foreach ($expectedProperties as $property => $expectedValue) {
$this->assertArrayHasKey($property, $rule, "Field $fieldName should have property $property");
$this->assertSame($expectedValue, $rule[$property], "Field $fieldName property $property should be $expectedValue");
}
}
public static function providerGetRuleArraySpecificValidation(): array {
return [
[
'OU', [
'type' => 'array',
'minItems' => 0,
'maxItems' => 10,
'required' => false,
]
],
];
}
public function testStringFieldsDoNotHaveArrayProperties(): void {
$service = $this->getService();
$stringFields = ['CN', 'C', 'ST', 'L', 'O'];
foreach ($stringFields as $fieldName) {
$rule = $service->getRule($fieldName);
$this->assertArrayNotHasKey('minItems', $rule, "String field $fieldName should not have minItems property");
$this->assertArrayNotHasKey('maxItems', $rule, "String field $fieldName should not have maxItems property");
$this->assertSame('string', $rule['type'], "Field $fieldName should be of type string");
}
}
public function testArrayFieldsHaveArrayProperties(): void {
$service = $this->getService();
$rule = $service->getRule('OU');
$this->assertSame('array', $rule['type'], 'OU field should be of type array');
$this->assertArrayHasKey('minItems', $rule, 'Array field OU should have minItems property');
$this->assertArrayHasKey('maxItems', $rule, 'Array field OU should have maxItems property');
$this->assertIsInt($rule['minItems'], 'minItems should be an integer');
$this->assertIsInt($rule['maxItems'], 'maxItems should be an integer');
$this->assertGreaterThanOrEqual(0, $rule['minItems'], 'minItems should be >= 0');
$this->assertGreaterThan($rule['minItems'], $rule['maxItems'], 'maxItems should be > minItems');
}
}