ILIAS  release_8 Revision v8.24
GroupInputTest.php
Go to the documentation of this file.
1<?php
18declare(strict_types=1);
19
20
21require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../Base.php");
23
28use ILIAS\Data;
29use ILIAS\Refinery\Factory as Refinery;
30use PHPUnit\Framework\MockObject\MockObject;
32
33abstract class Input1 extends FormInput
34{
35}
36
37abstract class Input2 extends FormInput
38{
39}
40
42{
46 protected $child1;
47
51 protected $child2;
52
54
58 protected $language;
59 protected Refinery $refinery;
60 protected Group $group;
61
62 public function setUp(): void
63 {
64 $this->child1 = $this->createMock(Input1::class);
65 $this->child2 = $this->createMock(Input2::class);
66 $this->data_factory = new Data\Factory();
67 $this->language = $this->createMock(ilLanguage::class);
68 $this->refinery = new Refinery($this->data_factory, $this->language);
69
70 $this->group = new Group(
71 $this->data_factory,
72 $this->refinery,
73 $this->language,
74 [$this->child1, $this->child2],
75 "LABEL",
76 "BYLINE"
77 );
78 }
79
80 public function testWithDisabledDisablesChildren(): void
81 {
82 $this->assertNotSame($this->child1, $this->child2);
83
84 $this->child1
85 ->expects($this->once())
86 ->method("withDisabled")
87 ->with(true)
88 ->willReturn($this->child2);
89 $this->child2
90 ->expects($this->once())
91 ->method("withDisabled")
92 ->with(true)
93 ->willReturn($this->child1);
94
95 $new_group = $this->group->withDisabled(true);
96
97 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
98 $this->assertInstanceOf(Group::class, $new_group);
99 $this->assertNotSame($this->group, $new_group);
100 }
101
102 public function testWithRequiredRequiresChildren(): void
103 {
104 $this->assertNotSame($this->child1, $this->child2);
105
106 $this->child1
107 ->expects($this->once())
108 ->method("withRequired")
109 ->with(true)
110 ->willReturn($this->child2);
111 $this->child2
112 ->expects($this->once())
113 ->method("withRequired")
114 ->with(true)
115 ->willReturn($this->child1);
116
117 $new_group = $this->group->withRequired(true);
118
119 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
120 $this->assertInstanceOf(Group::class, $new_group);
121 $this->assertNotSame($this->group, $new_group);
122 }
123
124 public function testGroupMayOnlyHaveInputChildren(): void
125 {
126 $this->expectException(InvalidArgumentException::class);
127
128 $this->group = new Group(
129 $this->data_factory,
130 $this->refinery,
131 $this->language,
132 ["foo", "bar"],
133 "LABEL",
134 "BYLINE"
135 );
136 }
137
139 {
140 $this->assertNotSame($this->child1, $this->child2);
141
142 $this->child1
143 ->expects($this->once())
144 ->method("withValue")
145 ->with(1)
146 ->willReturn($this->child2);
147 $this->child1
148 ->expects($this->once())
149 ->method("isClientSideValueOk")
150 ->with(1)
151 ->willReturn(true);
152 $this->child2
153 ->expects($this->once())
154 ->method("withValue")
155 ->with(2)
156 ->willReturn($this->child1);
157 $this->child2
158 ->expects($this->once())
159 ->method("isClientSideValueOk")
160 ->with(2)
161 ->willReturn(true);
162
163 $new_group = $this->group->withValue([1,2]);
164
165 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
166 $this->assertInstanceOf(Group::class, $new_group);
167 $this->assertNotSame($this->group, $new_group);
168 }
169
170 public function testWithValuePreservesKeys(): void
171 {
172 $this->assertNotSame($this->child1, $this->child2);
173
174 $this->group = new Group(
175 $this->data_factory,
176 $this->refinery,
177 $this->language,
178 ["child1" => $this->child1, "child2" => $this->child2],
179 "LABEL",
180 "BYLINE"
181 );
182
183 $this->child1
184 ->method("withValue")
185 ->willReturn($this->child2);
186 $this->child1
187 ->method("isClientSideValueOk")
188 ->willReturn(true);
189 $this->child2
190 ->method("withValue")
191 ->willReturn($this->child1);
192 $this->child2
193 ->method("isClientSideValueOk")
194 ->willReturn(true);
195
196 $new_group = $this->group->withValue(["child1" => 1,"child2" => 2]);
197
198 $this->assertEquals(["child1" => $this->child2, "child2" => $this->child1], $new_group->getInputs());
199 }
200
202 {
203 $this->expectException(InvalidArgumentException::class);
204
205 $this->group->withValue(1);
206 }
207
209 {
210 $this->expectException(InvalidArgumentException::class);
211
212 $this->group->withValue([1]);
213 }
214
215 public function testGroupForwardsValuesOnGetValue(): void
216 {
217 $this->assertNotSame($this->child1, $this->child2);
218
219 $this->child1
220 ->expects($this->once())
221 ->method("getValue")
222 ->with()
223 ->willReturn("one");
224 $this->child2
225 ->expects($this->once())
226 ->method("getValue")
227 ->with()
228 ->willReturn("two");
229
230 $vals = $this->group->getValue();
231
232 $this->assertEquals(["one", "two"], $vals);
233 }
234
236 {
237 $this->assertNotSame($this->child1, $this->child2);
238
239 $input_data = $this->createMock(InputData::class);
240
241 $this->child1
242 ->expects($this->once())
243 ->method("withInput")
244 ->with($input_data)
245 ->willReturn($this->child2);
246 $this->child1
247 ->expects($this->once())
248 ->method("getContent")
249 ->willReturn($this->data_factory->ok("one"));
250 $this->child2
251 ->expects($this->once())
252 ->method("withInput")
253 ->with($input_data)
254 ->willReturn($this->child1);
255 $this->child2
256 ->expects($this->once())
257 ->method("getContent")
258 ->willReturn($this->data_factory->ok("two"));
259
260 $called = false;
261 $new_group = $this->group
262 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called): string {
263 $called = true;
264 $this->assertEquals(["two", "one"], $v);
265 return "result";
266 }))
267 ->withInput($input_data);
268
269 $this->assertTrue($called);
270 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
271 $this->assertInstanceOf(Group::class, $new_group);
272 $this->assertNotSame($this->group, $new_group);
273 $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
274 }
275
277 {
278 $this->assertNotSame($this->child1, $this->child2);
279
280 $input_data = $this->createMock(InputData::class);
281
282 $this->child1
283 ->expects($this->once())
284 ->method("withInput")
285 ->with($input_data)
286 ->willReturn($this->child2);
287 $this->child1
288 ->expects($this->once())
289 ->method("getContent")
290 ->willReturn($this->data_factory->error(""));
291 $this->child2
292 ->expects($this->once())
293 ->method("withInput")
294 ->with($input_data)
295 ->willReturn($this->child1);
296 $this->child2
297 ->expects($this->once())
298 ->method("getContent")
299 ->willReturn($this->data_factory->ok("two"));
300
301 $i18n = "THERE IS SOME ERROR IN THIS GROUP";
302 $this->language
303 ->expects($this->once())
304 ->method("txt")
305 ->with("ui_error_in_group")
306 ->willReturn($i18n);
307
308 $new_group = $this->group
309 ->withAdditionalTransformation($this->refinery->custom()->transformation(function (): void {
310 $this->fail("This should not happen.");
311 }))
312 ->withInput($input_data);
313
314 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
315 $this->assertInstanceOf(Group::class, $new_group);
316 $this->assertNotSame($this->group, $new_group);
317 $this->assertTrue($new_group->getContent()->isError());
318 }
319
320 public function testErrorIsI18NOnError(): void
321 {
322 $this->assertNotSame($this->child1, $this->child2);
323
324 $input_data = $this->createMock(InputData::class);
325
326 $this->child1
327 ->method("withInput")
328 ->willReturn($this->child2);
329 $this->child1
330 ->method("getContent")
331 ->willReturn($this->data_factory->error(""));
332 $this->child2
333 ->method("withInput")
334 ->willReturn($this->child1);
335 $this->child2
336 ->method("getContent")
337 ->willReturn($this->data_factory->ok("two"));
338
339 $i18n = "THERE IS SOME ERROR IN THIS GROUP";
340 $this->language
341 ->expects($this->once())
342 ->method("txt")
343 ->with("ui_error_in_group")
344 ->willReturn($i18n);
345
346 $new_group = $this->group
347 ->withInput($input_data);
348
349 $this->assertTrue($new_group->getContent()->isError());
350 $this->assertEquals($i18n, $new_group->getContent()->error());
351 }
352
353 public function testWithoutChildren(): void
354 {
355 $group = new Group(
356 $this->data_factory,
357 $this->refinery,
358 $this->language,
359 [],
360 "LABEL",
361 "BYLINE"
362 );
363 $content = $group->getContent();
364 $this->assertInstanceOf(Ok::class, $content);
365 $this->assertCount(0, $content->value());
366 }
367
368 public function getFieldFactory(): FieldFactory
369 {
370 return new FieldFactory(
371 $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
373 new Data\Factory(),
374 $this->getRefinery(),
375 $this->getLanguage()
376 );
377 }
378
379 public function testGroupRendering(): void
380 {
381 $f = $this->getFieldFactory();
382 $inputs = [
383 $f->text("input1", "in 1"),
384 $f->text("input2", "in 2")
385 ];
386 $label = 'group label';
387 $group = $f->group($inputs, $label);
388
389 $expected = <<<EOT
390 <div class="form-group row">
391 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">input1</label>
392 <div class="col-sm-8 col-md-9 col-lg-10">
393 <input id="id_1" type="text" name="" class="form-control form-control-sm" />
394 <div class="help-block">in 1</div>
395 </div>
396 </div>
397 <div class="form-group row">
398 <label for="id_2" class="control-label col-sm-4 col-md-3 col-lg-2">input2</label>
399 <div class="col-sm-8 col-md-9 col-lg-10">
400 <input id="id_2" type="text" name="" class="form-control form-control-sm" />
401 <div class="help-block">in 2</div>
402 </div>
403 </div>
404EOT;
405 $actual = $this->brutallyTrimHTML($this->getDefaultRenderer()->render($group));
406 $expected = $this->brutallyTrimHTML($expected);
407 $this->assertEquals($expected, $actual);
408 }
409
410
411 public function testBylineProperty(): void
412 {
413 $bl = 'some byline';
414 $f = $this->getFieldFactory();
415 $group = $f->group([], "LABEL", $bl);
416 $this->assertEquals($bl, $group->getByline());
417 }
418
419 public function testGroupWithoutRequiredField(): void
420 {
421 $f = $this->getFieldFactory();
422 $inputs = [
423 $f->text(""),
424 $f->text("")
425 ];
426 $group = $f->group($inputs, '');
427 $this->assertFalse($group->isRequired());
428 }
429
430 public function testGroupWithRequiredField(): void
431 {
432 $f = $this->getFieldFactory();
433 $inputs = [
434 $f->text(""),
435 $f->text("")->withRequired(true)
436 ];
437 $group = $f->group($inputs, '');
438 $this->assertTrue($group->isRequired());
439 }
440}
static return function(ContainerConfigurator $containerConfigurator)
Definition: basic_rector.php:9
testWithInputCallsChildrenAndAppliesOperations()
Data Factory $data_factory
testGroupOnlyDoesNoAcceptNonArrayValue()
testWithDisabledDisablesChildren()
testWithRequiredRequiresChildren()
Refinery $refinery
testWithInputDoesNotApplyOperationsOnError()
testGroupOnlyDoesNoAcceptArrayValuesWithWrongLength()
testGroupForwardsValuesOnWithValue()
testGroupMayOnlyHaveInputChildren()
testGroupForwardsValuesOnGetValue()
Factory for Date Formats.
Definition: Factory.php:27
Builds data types.
Definition: Factory.php:21
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Ok.php:17
This implements the group input.
Definition: Group.php:43
getContent()
Get the current content of the input.
Definition: Group.php:133
Provides common functionality for UI tests.
Definition: Base.php:299
brutallyTrimHTML(string $html)
A more radical version of normalizeHTML.
Definition: Base.php:444
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:355
This describes inputs that can be used in forms.
Definition: FormInput.php:32
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider \MainMenu\Provider.
Class Factory.