ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
OptionalGroupInputTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
22 require_once(__DIR__ . "/../../../Base.php");
23 require_once(__DIR__ . "/CommonFieldRendering.php");
24 
29 use ILIAS\Data;
32 
33 abstract class Input11 extends FormInput
34 {
35 };
36 abstract class Input12 extends FormInput
37 {
38 };
39 
41 {
43 
47  protected $child1;
48 
52  protected $child2;
53 
54  protected Data\Factory $data_factory;
55 
59  protected $language;
60 
61  protected Refinery $refinery;
63  protected OptionalGroup $group;
64 
65  public function setUp(): void
66  {
67  $this->child1 = $this->createMock(Input11::class);
68  $this->child2 = $this->createMock(Input12::class);
69  $this->data_factory = new Data\Factory();
70  $this->language = $this->createMock(ILIAS\Language\Language::class);
71  $this->refinery = new ILIAS\Refinery\Factory($this->data_factory, $this->language);
72 
73  $this->child1
74  ->method("withNameFrom")
75  ->willReturn($this->child1);
76  $this->child2
77  ->method("withNameFrom")
78  ->willReturn($this->child2);
79 
80  $this->optional_group = (new OptionalGroup(
81  $this->data_factory,
82  $this->refinery,
83  $this->language,
84  [$this->child1, $this->child2],
85  "LABEL",
86  "BYLINE"
87  ))->withNameFrom(new class () implements NameSource {
88  public function getNewName(): string
89  {
90  return "name0";
91  }
92  });
93  }
94 
95  public function testWithDisabledDisablesChildren(): void
96  {
97  $this->assertNotSame($this->child1, $this->child2);
98 
99  $this->child1
100  ->expects($this->once())
101  ->method("withDisabled")
102  ->with(true)
103  ->willReturn($this->child2);
104  $this->child2
105  ->expects($this->once())
106  ->method("withDisabled")
107  ->with(true)
108  ->willReturn($this->child1);
109 
110  $new_group = $this->optional_group->withDisabled(true);
111 
112  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
113  $this->assertInstanceOf(OptionalGroup::class, $new_group);
114  $this->assertNotSame($this->optional_group, $new_group);
115  }
116 
117  public function testWithRequiredDoesNotRequire(): void
118  {
119  $this->assertNotSame($this->child1, $this->child2);
120 
121  $this->child1
122  ->expects($this->never())
123  ->method("withRequired");
124  $this->child2
125  ->expects($this->never())
126  ->method("withRequired");
127 
128  $new_group = $this->optional_group->withRequired(true);
129 
130  $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
131  $this->assertInstanceOf(OptionalGroup::class, $new_group);
132  $this->assertNotSame($this->optional_group, $new_group);
133  }
134 
136  {
137  $this->assertNotSame($this->child1, $this->child2);
138  $this->child1->method('isRequired')->willReturn(true);
139  $this->child2->method('isRequired')->willReturn(true);
140 
141  $new_group = $this->optional_group;
142 
143  $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
144  $this->assertInstanceOf(OptionalGroup::class, $new_group);
145  $this->assertFalse($new_group->isRequired());
146  }
147 
149  {
150  $this->expectException(InvalidArgumentException::class);
151 
152  $this->group = new OptionalGroup(
153  $this->data_factory,
154  $this->refinery,
155  $this->language,
156  ["foo", "bar"],
157  "LABEL",
158  "BYLINE"
159  );
160  }
161 
163  {
164  $this->assertNotSame($this->child1, $this->child2);
165 
166  $this->child1
167  ->expects($this->once())
168  ->method("withValue")
169  ->with(1)
170  ->willReturn($this->child2);
171  $this->child1
172  ->expects($this->once())
173  ->method("isClientSideValueOk")
174  ->with(1)
175  ->willReturn(true);
176  $this->child2
177  ->expects($this->once())
178  ->method("withValue")
179  ->with(2)
180  ->willReturn($this->child1);
181  $this->child2
182  ->expects($this->once())
183  ->method("isClientSideValueOk")
184  ->with(2)
185  ->willReturn(true);
186 
187  $new_group = $this->optional_group->withValue([1,2]);
188 
189  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
190  $this->assertInstanceOf(OptionalGroup::class, $new_group);
191  $this->assertNotSame($this->optional_group, $new_group);
192  }
193 
195  {
196  $this->expectException(InvalidArgumentException::class);
197  $this->optional_group->withValue(1);
198  }
199 
201  {
202  $this->expectException(InvalidArgumentException::class);
203  $this->optional_group->withValue([1]);
204  }
205 
207  {
208  $this->assertNotSame($this->child1, $this->child2);
209 
210  $this->child1
211  ->expects($this->never())
212  ->method("withValue");
213  $this->child1
214  ->expects($this->never())
215  ->method("isClientSideValueOk");
216  $this->child2
217  ->expects($this->never())
218  ->method("withValue");
219  $this->child2
220  ->expects($this->never())
221  ->method("isClientSideValueOk");
222 
223  $new_group = $this->optional_group->withValue(null);
224 
225  $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
226  $this->assertInstanceOf(OptionalGroup::class, $new_group);
227  $this->assertNotSame($this->optional_group, $new_group);
228  $this->assertEquals(null, $new_group->getValue());
229  }
230 
231  public function testGroupForwardsValuesOnGetValue(): void
232  {
233  $this->assertNotSame($this->child1, $this->child2);
234 
235  $this->child1
236  ->expects($this->once())
237  ->method("getValue")
238  ->with()
239  ->willReturn("one");
240  $this->child2
241  ->expects($this->once())
242  ->method("getValue")
243  ->with()
244  ->willReturn("two");
245 
246  $vals = $this->optional_group->getValue();
247 
248  $this->assertEquals(["one", "two"], $vals);
249  }
250 
252  {
253  $this->assertNotSame($this->child1, $this->child2);
254 
255  $input_data = $this->createMock(InputData::class);
256 
257  $input_data
258  ->expects($this->once())
259  ->method("getOr")
260  ->with("name0", null)
261  ->willReturn("checked");
262 
263  $this->child1
264  ->expects($this->once())
265  ->method("withInput")
266  ->with($input_data)
267  ->willReturn($this->child2);
268  $this->child1
269  ->expects($this->once())
270  ->method("getContent")
271  ->with()
272  ->willReturn($this->data_factory->ok("one"));
273  $this->child2
274  ->expects($this->once())
275  ->method("withInput")
276  ->with($input_data)
277  ->willReturn($this->child1);
278  $this->child2
279  ->expects($this->once())
280  ->method("getContent")
281  ->with()
282  ->willReturn($this->data_factory->ok("two"));
283 
284  $called = false;
285  $new_group = $this->optional_group
286  ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called): string {
287  $called = true;
288  $this->assertEquals(["two", "one"], $v);
289  return "result";
290  }))
291  ->withInput($input_data);
292 
293  $this->assertTrue($called);
294  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
295  $this->assertInstanceOf(OptionalGroup::class, $new_group);
296  $this->assertNotSame($this->optional_group, $new_group);
297  $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
298  }
299 
301  {
302  $this->assertNotSame($this->child1, $this->child2);
303 
304  $input_data = $this->createMock(InputData::class);
305 
306  $input_data
307  ->expects($this->once())
308  ->method("getOr")
309  ->with("name0", null)
310  ->willReturn("checked");
311 
312  $this->child1
313  ->expects($this->once())
314  ->method("withInput")
315  ->with($input_data)
316  ->willReturn($this->child2);
317  $this->child1
318  ->expects($this->once())
319  ->method("getContent")
320  ->willReturn($this->data_factory->error(""));
321  $this->child2
322  ->expects($this->once())
323  ->method("withInput")
324  ->with($input_data)
325  ->willReturn($this->child1);
326  $this->child2
327  ->expects($this->once())
328  ->method("getContent")
329  ->willReturn($this->data_factory->ok("two"));
330 
331  $i18n = "THERE IS SOME ERROR IN THIS GROUP";
332  $this->language
333  ->expects($this->once())
334  ->method("txt")
335  ->with("ui_error_in_group")
336  ->willReturn($i18n);
337 
338  $new_group = $this->optional_group
339  ->withAdditionalTransformation($this->refinery->custom()->transformation(function (): void {
340  $this->fail("This should not happen.");
341  }))
342  ->withInput($input_data);
343 
344  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
345  $this->assertInstanceOf(OptionalGroup::class, $new_group);
346  $this->assertNotSame($this->optional_group, $new_group);
347  $this->assertTrue($new_group->getContent()->isError());
348  }
349 
351  {
352  $this->assertNotSame($this->child1, $this->child2);
353 
354  $input_data = $this->createMock(InputData::class);
355 
356  $input_data
357  ->expects($this->once())
358  ->method("getOr")
359  ->with("name0", null)
360  ->willReturn(null);
361 
362  $this->child1
363  ->expects($this->never())
364  ->method("withInput");
365  $this->child1
366  ->expects($this->never())
367  ->method("getContent");
368  $this->child2
369  ->expects($this->never())
370  ->method("withInput");
371  $this->child2
372  ->expects($this->never())
373  ->method("getContent");
374 
375  $called = false;
376  $new_group = $this->optional_group
377  ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called): string {
378  $called = true;
379  $this->assertEquals(null, $v);
380  return "result";
381  }))
382  ->withInput($input_data);
383 
384  $this->assertTrue($called);
385  $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
386  $this->assertInstanceOf(OptionalGroup::class, $new_group);
387  $this->assertNotSame($this->optional_group, $new_group);
388  $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
389  }
390 
391  public function testCommonRendering(): void
392  {
393  $f = $this->getFieldFactory();
394  $label = "label";
395  $og = $f->optionalGroup(
396  [
397  "field_1" => $f->text("f"),
398  "field_2" => $f->text("f2")
399  ],
400  $label
401  )->withNameFrom((new DefNamesource()));
402 
403  $this->testWithError($og);
404  $this->testWithNoByline($og);
405  $this->testWithRequired($og);
406  $this->testWithDisabled($og);
407  $this->testWithAdditionalOnloadCodeRendersId($og);
408  }
409 }
ILIAS UI Component Input Field Group $optional_group
Interface Observer Contains several chained tasks and infos about them.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
withNameFrom(NameSource $source, ?string $parent_name=null)
Definition: Input.php:200
language()
description: > Example for rendring a language glyph.
Definition: language.php:41
Describes a source for input names.
Definition: NameSource.php:26