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