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