ILIAS  trunk Revision v13.0_alpha-62-g3b72d83f2e5
SetLanguageDetectionEnabledTest.php
Go to the documentation of this file.
1<?php
2
14declare(strict_types=1);
15
17
24use ILIAS\UI\Component\Input\Factory as InputFactory;
25use PHPUnit\Framework\Attributes\DataProvider;
26
33{
35 {
36 return $this->createActivity();
37 }
38
40 {
41 $rbac = $this->createMock(\ilRbacSystem::class);
42 $rbac->expects($this->once())
43 ->method('checkAccessOfUser')
44 ->with(6, 'write', 42)
45 ->willReturn(true);
46
47 $activity = $this->createActivity(rbac_system: $rbac, language_folder_ref_id: 42);
48
49 $this->assertTrue($activity->isAllowedToPerform(6, ['enabled' => true]));
50 }
51
53 {
54 $rbac = $this->createStub(\ilRbacSystem::class);
55 $rbac->method('checkAccessOfUser')->willReturn(false);
56
57 $activity = $this->createActivity(rbac_system: $rbac);
58
59 $this->assertFalse($activity->isAllowedToPerform(6, ['enabled' => true]));
60 }
61
64 {
65 $repository = $this->createMock(RepositoryProvider::class);
66 $repository->expects($this->never())->method('setLanguageDetectionEnabled');
67
68 $rbac = $this->createStub(\ilRbacSystem::class);
69 $rbac->method('checkAccessOfUser')->willReturn(true);
70
71 $activity = $this->createActivity(rbac_system: $rbac, repository: $repository);
72
73 $this->assertTrue($activity->isAllowedToPerform(6, ['enabled' => true]));
74 }
75
77 {
78 $repository = $this->createMock(RepositoryProvider::class);
79 $repository->expects($this->once())
80 ->method('setLanguageDetectionEnabled')->with(true);
81
82 $result = $this->createActivity(repository: $repository)->perform(['enabled' => true]);
83
84 $this->assertSame(['enabled' => true], $result);
85 }
86
88 {
89 $repository = $this->createMock(RepositoryProvider::class);
90 $repository->expects($this->once())
91 ->method('setLanguageDetectionEnabled')->with(false);
92
93 $result = $this->createActivity(repository: $repository)->perform(['enabled' => false]);
94
95 $this->assertSame(['enabled' => false], $result);
96 }
97
99 {
100 $repository = $this->createMock(RepositoryProvider::class);
101 $repository->expects($this->never())->method('setLanguageDetectionEnabled');
102
103 $this->expectException(InvalidInputException::class);
104
105 $this->createActivity(repository: $repository)->perform([]);
106 }
107
109 {
110 $repository = $this->createMock(RepositoryProvider::class);
111 $repository->expects($this->never())->method('setLanguageDetectionEnabled');
112
113 $this->expectException(InvalidInputException::class);
114
115 $this->createActivity(repository: $repository)->perform('not-an-array');
116 }
117
119 #[DataProvider('nonBooleanEnabledValuesProvider')]
120 public function testPerformRejectsNonStrictBooleanEnabledValues(mixed $value): void
121 {
122 $repository = $this->createMock(RepositoryProvider::class);
123 $repository->expects($this->never())->method('setLanguageDetectionEnabled');
124
125 $this->expectException(InvalidInputException::class);
126
127 $this->createActivity(repository: $repository)->perform(['enabled' => $value]);
128 }
129
130 public static function nonBooleanEnabledValuesProvider(): array
131 {
132 return [
133 'string "1"' => ['1'],
134 'string "true"' => ['true'],
135 'string "0"' => ['0'],
136 'string "false"' => ['false'],
137 'string ""' => [''],
138 'int 1' => [1],
139 'int 0' => [0],
140 'float 1.0' => [1.0],
141 'null' => [null],
142 'array' => [[true]],
143 ];
144 }
145
147 {
148 $rbac = $this->createStub(\ilRbacSystem::class);
149 $rbac->method('checkAccessOfUser')->willReturn(true);
150
151 $repository = $this->createMock(RepositoryProvider::class);
152 $repository->expects($this->once())->method('setLanguageDetectionEnabled')->with(true);
153
154 $result = $this->createActivity(rbac_system: $rbac, repository: $repository)
155 ->maybePerformAs($this->createRealFieldsUiFactory()->input(), 6, ['enabled' => true]);
156
157 $this->assertFalse($result->isError());
158 $this->assertSame(['enabled' => true], $result->value());
159 }
160
163 {
164 $rbac = $this->createMock(\ilRbacSystem::class);
165 $rbac->expects($this->once())
166 ->method('checkAccessOfUser')
167 ->with(6, 'write', $this->anything())
168 ->willReturn(false);
169
170 $repository = $this->createMock(RepositoryProvider::class);
171 $repository->expects($this->never())->method('setLanguageDetectionEnabled');
172
173 $result = $this->createActivity(rbac_system: $rbac, repository: $repository)
174 ->maybePerformAs($this->createRealFieldsUiFactory()->input(), 6, ['enabled' => true]);
175
176 $this->assertTrue($result->isError());
177 }
178
180 {
181 $rbac = $this->createStub(\ilRbacSystem::class);
182 $rbac->method('checkAccessOfUser')->willReturn(true);
183
184 $repository = $this->createMock(RepositoryProvider::class);
185 $repository->expects($this->never())->method('setLanguageDetectionEnabled');
186
187 $result = $this->createActivity(rbac_system: $rbac, repository: $repository)
188 ->maybePerformAs($this->createRealFieldsUiFactory()->input(), 6, ['enabled' => 'not-a-bool']);
189
190 $this->assertTrue($result->isError());
191 // outside the whitelist of normalizeCheckboxRawValue(): grind() turns it into an InvalidInputException
192 $this->assertInstanceOf(InvalidInputException::class, $result->error());
193 $this->assertSame(400, $result->error()->getCode());
194 }
195
198 {
199 $rbac = $this->createMock(\ilRbacSystem::class);
200 $rbac->expects($this->once())
201 ->method('checkAccessOfUser')
202 ->willReturn(true);
203
204 $repository = $this->createMock(RepositoryProvider::class);
205 $repository->expects($this->once())->method('setLanguageDetectionEnabled')->with(false);
206
207 $result = $this->createActivity(rbac_system: $rbac, repository: $repository)->maybePerformAs($this->createRealFieldsUiFactory()->input(), 6, []);
208
209 $this->assertFalse($result->isError());
210 $this->assertSame(['enabled' => false], $result->value());
211 }
212
213 public static function tolerantlyAcceptedTruthyEnabledValuesProvider(): array
214 {
215 return [
216 'string "1"' => ['1'],
217 'string "true"' => ['true'],
218 'int 1' => [1],
219 ];
220 }
221
222 public static function tolerantlyAcceptedFalsyEnabledValuesProvider(): array
223 {
224 return [
225 'string "0"' => ['0'],
226 'string "false"' => ['false'],
227 'string ""' => [''],
228 'int 0' => [0],
229 'null' => [null],
230 ];
231 }
232
234 #[DataProvider('tolerantlyAcceptedTruthyEnabledValuesProvider')]
236 {
237 $rbac = $this->createStub(\ilRbacSystem::class);
238 $rbac->method('checkAccessOfUser')->willReturn(true);
239
240 $repository = $this->createMock(RepositoryProvider::class);
241 $repository->expects($this->once())->method('setLanguageDetectionEnabled')->with(true);
242
243 $result = $this->createActivity(rbac_system: $rbac, repository: $repository)
244 ->maybePerformAs($this->createRealFieldsUiFactory()->input(), 6, ['enabled' => $value]);
245
246 $this->assertFalse($result->isError());
247 $this->assertSame(['enabled' => true], $result->value());
248 }
249
251 #[DataProvider('tolerantlyAcceptedFalsyEnabledValuesProvider')]
253 {
254 $rbac = $this->createStub(\ilRbacSystem::class);
255 $rbac->method('checkAccessOfUser')->willReturn(true);
256
257 $repository = $this->createMock(RepositoryProvider::class);
258 $repository->expects($this->once())->method('setLanguageDetectionEnabled')->with(false);
259
260 $result = $this->createActivity(rbac_system: $rbac, repository: $repository)
261 ->maybePerformAs($this->createRealFieldsUiFactory()->input(), 6, ['enabled' => $value]);
262
263 $this->assertFalse($result->isError());
264 $this->assertSame(['enabled' => false], $result->value());
265 }
266
267 public static function rejectedEnabledValuesProvider(): array
268 {
269 return [
270 'float 1.0' => [1.0],
271 'array' => [[true]],
272 ];
273 }
274
278 #[DataProvider('rejectedEnabledValuesProvider')]
280 {
281 $rbac = $this->createStub(\ilRbacSystem::class);
282 $rbac->method('checkAccessOfUser')->willReturn(true);
283
284 $repository = $this->createMock(RepositoryProvider::class);
285 $repository->expects($this->never())->method('setLanguageDetectionEnabled');
286
287 $result = $this->createActivity(rbac_system: $rbac, repository: $repository)
288 ->maybePerformAs($this->createRealFieldsUiFactory()->input(), 6, ['enabled' => $value]);
289
290 $this->assertTrue($result->isError());
291 $this->assertInstanceOf(InvalidInputException::class, $result->error());
292 $this->assertSame(400, $result->error()->getCode());
293 }
294
296 {
297 $checkbox = $this->createMock(Checkbox::class);
298 $checkbox->expects($this->once())
299 ->method('withDedicatedName')
300 ->with('enabled')
301 ->willReturnSelf();
302
303 $field = $this->createMock(\ILIAS\UI\Component\Input\Field\Factory::class);
304 $field->expects($this->once())
305 ->method('checkbox')
306 ->with(
307 'Enabled',
308 'Whether automatic language detection from the browser should be enabled.'
309 )
310 ->willReturn($checkbox);
311
312 $group = $this->createMock(\ILIAS\UI\Component\Input\Field\Section::class);
313 $field->expects($this->once())
314 ->method('section')
315 ->with(['enabled' => $checkbox], $this->anything())
316 ->willReturn($group);
317
318 $activity = $this->createActivity();
319
320 $this->assertSame($group, $activity->getInputDescription($field));
321 }
322
324 {
325 $bool_description = $this->createStub(Description::class);
326 $object_description = $this->createStub(Description::class);
327
328 $f = $this->createMock(\ILIAS\Data\Description\Factory::class);
329 $f->expects($this->once())->method('bool')->willReturn($bool_description);
330 $f->expects($this->once())
331 ->method('object')
332 ->with($this->anything(), ['enabled' => $bool_description])
333 ->willReturn($object_description);
334
335 $this->assertSame($object_description, $this->createActivity()->getOutputDescription($f));
336 }
337
338 private function createActivity(
339 ?\ilRbacSystem $rbac_system = null,
340 ?RepositoryProvider $repository = null,
341 int $language_folder_ref_id = 0
344 $this->rbacOf($rbac_system ?? $this->createMock(\ilRbacSystem::class)),
345 $this->languageFolder($language_folder_ref_id),
346 $repository ?? $this->createMock(RepositoryProvider::class)
347 );
348 }
349}
This describes some datastructure in terms of standard data structures such as primitives,...
Definition: Description.php:33
Every legacy and repository access of the runtime language activities.
Contract tests shared by every Activity test case; subclasses only supply createDefaultActivity().
SetLanguageDetectionEnabled writes the "lang_detection" setting through the RepositoryProvider,...
testMaybePerformAsToleratesCommonPrimitiveTruthyRepresentationsAndWritesEnabledTrue(mixed $value)
Unlike perform(), maybePerformAs() accepts common primitive representations of "true".
testMaybePerformAsWithDeniedPermissionReturnsErrorAndNeverWritesTheSetting()
The permission check is a real gate: a denied "write" yields a Result\Error and leaves the Setting un...
testMaybePerformAsRejectsValuesGrindCannotInterpretAsACheckboxWithoutTouchingSettings(mixed $value)
Values outside the whitelist of GrindsFormInput::normalizeCheckboxRawValue() are still rejected.
testPerformRejectsNonStrictBooleanEnabledValues(mixed $value)
perform() requires a strict bool for 'enabled'; truthy/falsy look-alikes ("1", 1, ....
createActivity(?\ilRbacSystem $rbac_system=null, ?RepositoryProvider $repository=null, int $language_folder_ref_id=0)
testMaybePerformAsWithMissingEnabledKeyDefaultsToFalseAndStillPerformsTheWrite()
A missing 'enabled' defaults to false like an unchecked HTML checkbox, and still reaches perform().
testMaybePerformAsToleratesCommonPrimitiveFalsyRepresentationsAndWritesEnabledFalse(mixed $value)
Same for "false".
testIsAllowedToPerformNeverTouchesSettings()
isAllowedToPerform() is a pure permission check: no Setting access.
Definition: UI.php:27
rbacOf(\ilRbacSystem $system)
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
return['3gp', '7z', 'ai', 'aif', 'aifc', 'aiff', 'au', 'arw', 'avi', 'backup', 'bak', 'bas', 'bpmn', 'bpmn2', 'bmp', 'bib', 'bibtex', 'bz', 'bz2', 'c', 'c++', 'cc', 'cct', 'cdf', 'cer', 'class', 'cls', 'conf', 'cpp', 'crt', 'crs', 'crw', 'cr2', 'css', 'cst', 'csv', 'cur', 'db', 'dcr', 'des', 'dng', 'doc', 'docx', 'dot', 'dotx', 'dtd', 'dvi', 'el', 'eps', 'epub', 'f', 'f77', 'f90', 'flv', 'for', 'g3', 'gif', 'gl', 'gan', 'ggb', 'gsd', 'gsm', 'gtar', 'gz', 'gzip', 'h', 'hpp', 'htm', 'html', 'htmls', 'ibooks', 'ico', 'ics', 'ini', 'ipynb', 'java', 'jbf', 'jpeg', 'jpg', 'js', 'jsf', 'jso', 'json', 'latex', 'lang', 'less', 'log', 'lsp', 'ltx', 'm1v', 'm2a', 'm2v', 'm3u', 'm4a', 'm4v', 'markdown', 'm', 'mat', 'md', 'mdl', 'mdown', 'mid', 'min', 'midi', 'mobi', 'mod', 'mov', 'movie', 'mp2', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'mph', 'mpga', 'mpp', 'mpt', 'mpv', 'mpx', 'mv', 'mw', 'mv4', 'nb', 'nbp', 'nef', 'nif', 'niff', 'obj', 'obm', 'odt', 'ods', 'odp', 'odg', 'odf', 'oga', 'ogg', 'ogv', 'old', 'p', 'pas', 'pbm', 'pcl', 'pct', 'pcx', 'pdf', 'pgm', 'pic', 'pict', 'png', 'por', 'pov', 'project', 'properties', 'ppa', 'ppm', 'pps', 'ppsx', 'ppt', 'pptx', 'ppz', 'ps', 'psd', 'pwz', 'qt', 'qtc', 'qti', 'qtif', 'r', 'ra', 'ram', 'rar', 'rast', 'rda', 'rev', 'rexx', 'ris', 'rf', 'rgb', 'rm', 'rmd', 'rmi', 'rmm', 'rmp', 'rt', 'rtf', 'rtx', 'rv', 's', 's3m', 'sav', 'sbs', 'sec', 'sdml', 'sgm', 'sgml', 'smi', 'smil', 'srt', 'sps', 'spv', 'stl', 'svg', 'swa', 'swf', 'swz', 'tar', 'tex', 'texi', 'texinfo', 'text', 'tgz', 'tif', 'tiff', 'ttf', 'txt', 'tmp', 'uvproj', 'vdf', 'vimeo', 'viv', 'vivo', 'vrml', 'vsdx', 'vtt', 'wav', 'webm', 'wmv', 'wmx', 'wmz', 'woff', 'wwd', 'xhtml', 'xif', 'xls', 'xlsx', 'xmind', 'xml', 'xsl', 'xsd', 'zip']
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This is how a factory for inputs looks like.
Definition: Factory.php:27
This describes checkbox inputs.
Definition: Checkbox.php:29
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.