ILIAS  release_8 Revision v8.24
OptionalGroupInputTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
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;
31
32abstract class Input11 extends FormInput
33{
34};
35abstract class Input12 extends FormInput
36{
37};
38
40{
44 protected $child1;
45
49 protected $child2;
50
52
56 protected $language;
57
58 protected Refinery $refinery;
61
62 public function setUp(): void
63 {
64 $this->child1 = $this->createMock(Input11::class);
65 $this->child2 = $this->createMock(Input12::class);
66 $this->data_factory = new Data\Factory();
67 $this->language = $this->createMock(ilLanguage::class);
68 $this->refinery = new ILIAS\Refinery\Factory($this->data_factory, $this->language);
69
70 $this->child1
71 ->method("withNameFrom")
72 ->willReturn($this->child1);
73 $this->child2
74 ->method("withNameFrom")
75 ->willReturn($this->child2);
76
77 $this->optional_group = (new OptionalGroup(
78 $this->data_factory,
79 $this->refinery,
80 $this->language,
81 [$this->child1, $this->child2],
82 "LABEL",
83 "BYLINE"
84 ))->withNameFrom(new class () implements NameSource {
85 public function getNewName(): string
86 {
87 return "name0";
88 }
89 });
90 }
91
92 public function testWithDisabledDisablesChildren(): void
93 {
94 $this->assertNotSame($this->child1, $this->child2);
95
96 $this->child1
97 ->expects($this->once())
98 ->method("withDisabled")
99 ->with(true)
100 ->willReturn($this->child2);
101 $this->child2
102 ->expects($this->once())
103 ->method("withDisabled")
104 ->with(true)
105 ->willReturn($this->child1);
106
107 $new_group = $this->optional_group->withDisabled(true);
108
109 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
110 $this->assertInstanceOf(OptionalGroup::class, $new_group);
111 $this->assertNotSame($this->optional_group, $new_group);
112 }
113
114 public function testWithRequiredDoesNotRequire(): void
115 {
116 $this->assertNotSame($this->child1, $this->child2);
117
118 $this->child1
119 ->expects($this->never())
120 ->method("withRequired");
121 $this->child2
122 ->expects($this->never())
123 ->method("withRequired");
124
125 $new_group = $this->optional_group->withRequired(true);
126
127 $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
128 $this->assertInstanceOf(OptionalGroup::class, $new_group);
129 $this->assertNotSame($this->optional_group, $new_group);
130 }
131
133 {
134 $this->assertNotSame($this->child1, $this->child2);
135 $this->child1->method('isRequired')->willReturn(true);
136 $this->child2->method('isRequired')->willReturn(true);
137
138 $new_group = $this->optional_group;
139
140 $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
141 $this->assertInstanceOf(OptionalGroup::class, $new_group);
142 $this->assertFalse($new_group->isRequired());
143 }
144
146 {
147 $this->expectException(InvalidArgumentException::class);
148
149 $this->group = new OptionalGroup(
150 $this->data_factory,
151 $this->refinery,
152 $this->language,
153 ["foo", "bar"],
154 "LABEL",
155 "BYLINE"
156 );
157 }
158
160 {
161 $this->assertNotSame($this->child1, $this->child2);
162
163 $this->child1
164 ->expects($this->once())
165 ->method("withValue")
166 ->with(1)
167 ->willReturn($this->child2);
168 $this->child1
169 ->expects($this->once())
170 ->method("isClientSideValueOk")
171 ->with(1)
172 ->willReturn(true);
173 $this->child2
174 ->expects($this->once())
175 ->method("withValue")
176 ->with(2)
177 ->willReturn($this->child1);
178 $this->child2
179 ->expects($this->once())
180 ->method("isClientSideValueOk")
181 ->with(2)
182 ->willReturn(true);
183
184 $new_group = $this->optional_group->withValue([1,2]);
185
186 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
187 $this->assertInstanceOf(OptionalGroup::class, $new_group);
188 $this->assertNotSame($this->optional_group, $new_group);
189 }
190
192 {
193 $this->expectException(InvalidArgumentException::class);
194 $this->optional_group->withValue(1);
195 }
196
198 {
199 $this->expectException(InvalidArgumentException::class);
200 $this->optional_group->withValue([1]);
201 }
202
204 {
205 $this->assertNotSame($this->child1, $this->child2);
206
207 $this->child1
208 ->expects($this->never())
209 ->method("withValue");
210 $this->child1
211 ->expects($this->never())
212 ->method("isClientSideValueOk");
213 $this->child2
214 ->expects($this->never())
215 ->method("withValue");
216 $this->child2
217 ->expects($this->never())
218 ->method("isClientSideValueOk");
219
220 $new_group = $this->optional_group->withValue(null);
221
222 $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
223 $this->assertInstanceOf(OptionalGroup::class, $new_group);
224 $this->assertNotSame($this->optional_group, $new_group);
225 $this->assertEquals(null, $new_group->getValue());
226 }
227
228 public function testGroupForwardsValuesOnGetValue(): void
229 {
230 $this->assertNotSame($this->child1, $this->child2);
231
232 $this->child1
233 ->expects($this->once())
234 ->method("getValue")
235 ->with()
236 ->willReturn("one");
237 $this->child2
238 ->expects($this->once())
239 ->method("getValue")
240 ->with()
241 ->willReturn("two");
242
243 $vals = $this->optional_group->getValue();
244
245 $this->assertEquals(["one", "two"], $vals);
246 }
247
249 {
250 $this->assertNotSame($this->child1, $this->child2);
251
252 $input_data = $this->createMock(InputData::class);
253
254 $input_data
255 ->expects($this->once())
256 ->method("getOr")
257 ->with("name0", null)
258 ->willReturn("checked");
259
260 $this->child1
261 ->expects($this->once())
262 ->method("withInput")
263 ->with($input_data)
264 ->willReturn($this->child2);
265 $this->child1
266 ->expects($this->once())
267 ->method("getContent")
268 ->with()
269 ->willReturn($this->data_factory->ok("one"));
270 $this->child2
271 ->expects($this->once())
272 ->method("withInput")
273 ->with($input_data)
274 ->willReturn($this->child1);
275 $this->child2
276 ->expects($this->once())
277 ->method("getContent")
278 ->with()
279 ->willReturn($this->data_factory->ok("two"));
280
281 $called = false;
282 $new_group = $this->optional_group
283 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called): string {
284 $called = true;
285 $this->assertEquals(["two", "one"], $v);
286 return "result";
287 }))
288 ->withInput($input_data);
289
290 $this->assertTrue($called);
291 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
292 $this->assertInstanceOf(OptionalGroup::class, $new_group);
293 $this->assertNotSame($this->optional_group, $new_group);
294 $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
295 }
296
298 {
299 $this->assertNotSame($this->child1, $this->child2);
300
301 $input_data = $this->createMock(InputData::class);
302
303 $input_data
304 ->expects($this->once())
305 ->method("getOr")
306 ->with("name0", null)
307 ->willReturn("checked");
308
309 $this->child1
310 ->expects($this->once())
311 ->method("withInput")
312 ->with($input_data)
313 ->willReturn($this->child2);
314 $this->child1
315 ->expects($this->once())
316 ->method("getContent")
317 ->willReturn($this->data_factory->error(""));
318 $this->child2
319 ->expects($this->once())
320 ->method("withInput")
321 ->with($input_data)
322 ->willReturn($this->child1);
323 $this->child2
324 ->expects($this->once())
325 ->method("getContent")
326 ->willReturn($this->data_factory->ok("two"));
327
328 $i18n = "THERE IS SOME ERROR IN THIS GROUP";
329 $this->language
330 ->expects($this->once())
331 ->method("txt")
332 ->with("ui_error_in_group")
333 ->willReturn($i18n);
334
335 $new_group = $this->optional_group
336 ->withAdditionalTransformation($this->refinery->custom()->transformation(function (): void {
337 $this->fail("This should not happen.");
338 }))
339 ->withInput($input_data);
340
341 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
342 $this->assertInstanceOf(OptionalGroup::class, $new_group);
343 $this->assertNotSame($this->optional_group, $new_group);
344 $this->assertTrue($new_group->getContent()->isError());
345 }
346
348 {
349 $this->assertNotSame($this->child1, $this->child2);
350
351 $input_data = $this->createMock(InputData::class);
352
353 $input_data
354 ->expects($this->once())
355 ->method("getOr")
356 ->with("name0", null)
357 ->willReturn(null);
358
359 $this->child1
360 ->expects($this->never())
361 ->method("withInput");
362 $this->child1
363 ->expects($this->never())
364 ->method("getContent");
365 $this->child2
366 ->expects($this->never())
367 ->method("withInput");
368 $this->child2
369 ->expects($this->never())
370 ->method("getContent");
371
372 $called = false;
373 $new_group = $this->optional_group
374 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called): string {
375 $called = true;
376 $this->assertEquals(null, $v);
377 return "result";
378 }))
379 ->withInput($input_data);
380
381 $this->assertTrue($called);
382 $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
383 $this->assertInstanceOf(OptionalGroup::class, $new_group);
384 $this->assertNotSame($this->optional_group, $new_group);
385 $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
386 }
387}
static return function(ContainerConfigurator $containerConfigurator)
Definition: basic_rector.php:9
Builds data types.
Definition: Factory.php:21
Provides common functionality for UI tests.
Definition: Base.php:299
ILIAS UI Component Input Field Group $optional_group
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
Describes a source for input names.
Definition: NameSource.php:27
withNameFrom(NameSource $source, ?string $parent_name=null)
@inheritDoc