ILIAS  release_7 Revision v7.30-3-g800a261c036
SwitchableGroupInputTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2018 Nils Haagen <nils.haagen@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6require_once(__DIR__ . "/../../../Base.php");
7
13use \ILIAS\Data;
15
16class Group1 extends Group
17{
18};
19class Group2 extends Group
20{
21};
22
24{
28 private $refinery;
29
30 public function setUp() : void
31 {
32 $this->child1 = $this->createMock(Group1::class);
33 $this->child2 = $this->createMock(Group2::class);
34 $this->data_factory = new Data\Factory();
35 $this->refinery = new ILIAS\Refinery\Factory($this->data_factory, $this->createMock(\ilLanguage::class));
36 $this->lng = $this->createMock(\ilLanguage::class);
37
38 $this->child1
39 ->method("withNameFrom")
40 ->willReturn($this->child1);
41 $this->child2
42 ->method("withNameFrom")
43 ->willReturn($this->child2);
44
45 $this->switchable_group = (new SwitchableGroup(
46 $this->data_factory,
47 $this->refinery,
48 $this->lng,
49 ["child1" => $this->child1, "child2" => $this->child2],
50 "LABEL",
51 "BYLINE"
52 ))->withNameFrom(new class implements NameSource {
53 public function getNewName()
54 {
55 return "name0";
56 }
57 });
58 }
59
60 protected function buildFactory()
61 {
63 new SignalGenerator(),
64 $this->data_factory,
65 $this->refinery,
66 $this->lng
67 );
68 }
69
71 {
72 $this->assertNotSame($this->child1, $this->child2);
73
74 $this->child1
75 ->expects($this->once())
76 ->method("withDisabled")
77 ->with(true)
78 ->willReturn($this->child2);
79 $this->child2
80 ->expects($this->once())
81 ->method("withDisabled")
82 ->with(true)
83 ->willReturn($this->child1);
84
85 $new_group = $this->switchable_group->withDisabled(true);
86
87 $this->assertEquals(["child1" => $this->child2, "child2" => $this->child1], $new_group->getInputs());
88 $this->assertInstanceOf(SwitchableGroup::class, $new_group);
89 $this->assertNotSame($this->switchable_group, $new_group);
90 }
91
93 {
94 $this->assertNotSame($this->child1, $this->child2);
95
96 $this->child1
97 ->expects($this->never())
98 ->method("withRequired");
99 $this->child2
100 ->expects($this->never())
101 ->method("withRequired");
102
103 $new_group = $this->switchable_group->withRequired(true);
104
105 $this->assertEquals(["child1" => $this->child1, "child2" => $this->child2], $new_group->getInputs());
106 $this->assertInstanceOf(SwitchableGroup::class, $new_group);
107 $this->assertNotSame($this->switchable_group, $new_group);
108 }
109
111 {
112 $this->expectException(\InvalidArgumentException::class);
113
114 $this->group = new SwitchableGroup(
115 $this->data_factory,
116 $this->refinery,
117 $this->lng,
118 [$this->createMock(Input::class)],
119 "LABEL",
120 "BYLINE"
121 );
122 }
123
125 {
126 $this->assertNotSame($this->child1, $this->child2);
127
128 $this->child1
129 ->expects($this->never())
130 ->method("withValue");
131 $this->child2
132 ->expects($this->once())
133 ->method("withValue")
134 ->with(2)
135 ->willReturn($this->child2);
136
137 $new_group = $this->switchable_group->withValue(["child2", 2]);
138
139 $this->assertEquals(["child1" => $this->child1, "child2" => $this->child2], $new_group->getInputs());
140 $this->assertInstanceOf(SwitchableGroup::class, $new_group);
141 $this->assertNotSame($this->switchable_group, $new_group);
142 }
143
145 {
146 $this->expectException(\InvalidArgumentException::class);
147
148 $new_group = $this->switchable_group->withValue(null);
149 }
150
152 {
153 $this->expectException(\InvalidArgumentException::class);
154
155 $new_group = $this->switchable_group->withValue([1, 2, 3]);
156 }
157
159 {
160 $new_group = $this->switchable_group->withValue("child1");
161 $this->assertEquals(["child1" => $this->child1, "child2" => $this->child2], $new_group->getInputs());
162 $this->assertInstanceOf(SwitchableGroup::class, $new_group);
163 $this->assertNotSame($this->switchable_group, $new_group);
164 }
165
167 {
168 $this->expectException(\InvalidArgumentException::class);
169
170 $new_group = $this->switchable_group->withValue("child3");
171 }
172
174 {
175 $this->assertNotSame($this->child1, $this->child2);
176
177 $this->child1
178 ->expects($this->once())
179 ->method("getValue")
180 ->with()
181 ->willReturn("one");
182 $this->child2
183 ->expects($this->never())
184 ->method("getValue");
185
186 $vals = $this->switchable_group->withValue("child1")->getValue();
187
188 $this->assertEquals(["child1", "one"], $vals);
189 }
190
192 {
193 $this->assertNotSame($this->child1, $this->child2);
194
195 $input_data = $this->createMock(InputData::class);
196
197 $input_data
198 ->expects($this->once())
199 ->method("get")
200 ->with("name0")
201 ->willReturn("child1");
202
203 $this->child1
204 ->expects($this->once())
205 ->method("withInput")
206 ->with($input_data)
207 ->willReturn($this->child1);
208 $this->child1
209 ->expects($this->once())
210 ->method("getValue")
211 ->with()
212 ->willReturn("one");
213 $this->child1
214 ->expects($this->once())
215 ->method("getContent")
216 ->with()
217 ->willReturn($this->data_factory->ok("one"));
218 $this->child2
219 ->expects($this->never())
220 ->method("withInput");
221 $this->child2
222 ->expects($this->never())
223 ->method("getContent");
224
225 $called = false;
226 $new_group = $this->switchable_group
227 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called) {
228 $called = true;
229 $this->assertEquals(["child1", "one"], $v);
230 return "result";
231 }))
232 ->withInput($input_data);
233
234 $this->assertTrue($called);
235 $this->assertEquals(["child1" => $this->child1, "child2" => $this->child2], $new_group->getInputs());
236 $this->assertInstanceOf(SwitchableGroup::class, $new_group);
237 $this->assertNotSame($this->switchable_group, $new_group);
238 $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
239 }
240
242 {
243 $this->assertNotSame($this->child1, $this->child2);
244
245 $input_data = $this->createMock(InputData::class);
246
247 $input_data
248 ->expects($this->once())
249 ->method("get")
250 ->with("name0")
251 ->willReturn("child2");
252
253 $this->child1
254 ->expects($this->never())
255 ->method("withInput");
256 $this->child1
257 ->expects($this->never())
258 ->method("getContent");
259 $this->child2
260 ->expects($this->once())
261 ->method("withInput")
262 ->with($input_data)
263 ->willReturn($this->child2);
264 $this->child2
265 ->expects($this->once())
266 ->method("getContent")
267 ->willReturn($this->data_factory->error(""));
268
269 $i18n = "THERE IS SOME ERROR IN THIS GROUP";
270 $this->lng
271 ->expects($this->once())
272 ->method("txt")
273 ->with("ui_error_in_group")
274 ->willReturn($i18n);
275
276 $new_group = $this->switchable_group
277 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) {
278 $this->assertFalse(true, "This should not happen.");
279 }))
280 ->withInput($input_data);
281
282 $this->assertEquals(["child1" => $this->child1, "child2" => $this->child2], $new_group->getInputs());
283 $this->assertInstanceOf(SwitchableGroup::class, $new_group);
284 $this->assertNotSame($this->switchable_group, $new_group);
285 $this->assertTrue($new_group->getContent()->isError());
286 }
287
288 public function testErrorIsI18NOnError()
289 {
290 $this->assertNotSame($this->child1, $this->child2);
291
292 $input_data = $this->createMock(InputData::class);
293
294 $input_data
295 ->expects($this->once())
296 ->method("get")
297 ->with("name0")
298 ->willReturn("child2");
299
300 $this->child2
301 ->method("withInput")
302 ->willReturn($this->child2);
303 $this->child2
304 ->method("getContent")
305 ->willReturn($this->data_factory->error(""));
306
307 $i18n = "THERE IS SOME ERROR IN THIS GROUP";
308 $this->lng
309 ->expects($this->once())
310 ->method("txt")
311 ->with("ui_error_in_group")
312 ->willReturn($i18n);
313
314 $switchable_group = $this->switchable_group
315 ->withInput($input_data);
316
317 $this->assertTrue($switchable_group->getContent()->isError());
318 $this->assertEquals($i18n, $switchable_group->getContent()->error());
319 }
320
321
323 {
324 $this->expectException(\InvalidArgumentException::class);
325
326 $input_data = $this->createMock(InputData::class);
327
328 $input_data
329 ->expects($this->once())
330 ->method("get")
331 ->with("name0")
332 ->willReturn(123);
333
334 $this->child1
335 ->expects($this->never())
336 ->method("withInput");
337 $this->child1
338 ->expects($this->never())
339 ->method("getContent");
340 $this->child2
341 ->expects($this->never())
342 ->method("withInput");
343 $this->child2
344 ->expects($this->never())
345 ->method("getContent");
346
347 $new_group = $this->switchable_group
348 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called) {
349 $this->assertFalse(true, "This should not happen.");
350 }))
351 ->withInput($input_data);
352 }
353
354 public function testRender()
355 {
356 $f = $this->buildFactory();
357 $label = "label";
358 $byline = "byline";
359
360 $group1 = $f->group([
361 "field_1" => $f->text("f", "some field")
362 ]);
363 $group2 = $f->group([
364 "field_2" => $f->text("f2", "some other field")
365 ]);
366
367 $sg = $f->switchableGroup(
368 [
369 "g1" => $group1,
370 "g2" => $group2
371 ],
372 $label,
373 $byline
374 );
375
376 $r = $this->getDefaultRenderer();
377 $html = $r->render($sg);
378 $expected = <<<EOT
379<div class="form-group row">
380 <label class="control-label col-sm-3">label</label>
381 <div class="col-sm-9">
382 <div id="id_1" class="il-input-radio">
383 <div class="form-control form-control-sm il-input-radiooption">
384 <input type="radio" id="id_1_g1_opt" name="" value="g1" /><label for="id_1_g1_opt"></label>
385 <div class="form-group row">
386 <label for="id_2" class="control-label col-sm-3">f</label>
387 <div class="col-sm-9">
388 <input id="id_2" type="text" name="" class="form-control form-control-sm" />
389 <div class="help-block">some field</div>
390 </div>
391 </div>
392 </div>
393 <div class="form-control form-control-sm il-input-radiooption">
394 <input type="radio" id="id_1_g2_opt" name="" value="g2" /><label for="id_1_g2_opt"></label>
395 <div class="form-group row">
396 <label for="id_3" class="control-label col-sm-3">f2</label>
397 <div class="col-sm-9">
398 <input id="id_3" type="text" name="" class="form-control form-control-sm" />
399 <div class="help-block">some other field</div>
400 </div>
401 </div>
402 </div>
403 </div>
404 <div class="help-block">byline</div>
405 </div>
406</div>
407
408EOT;
409 $this->assertEquals(
410 $this->brutallyTrimHTML($expected),
411 $this->brutallyTrimHTML($html)
412 );
413 return $sg;
414 }
415
419 public function testRenderWithValue($sg)
420 {
421 $r = $this->getDefaultRenderer();
422 $html = $r->render($sg->withValue('g2'));
423 $expected = <<<EOT
424<div class="form-group row">
425 <label class="control-label col-sm-3">label</label>
426 <div class="col-sm-9">
427 <div id="id_1" class="il-input-radio">
428 <div class="form-control form-control-sm il-input-radiooption">
429 <input type="radio" id="id_1_g1_opt" name="" value="g1" /><label for="id_1_g1_opt"></label>
430 <div class="form-group row">
431 <label for="id_2" class="control-label col-sm-3">f</label>
432 <div class="col-sm-9">
433 <input id="id_2" type="text" name="" class="form-control form-control-sm" />
434 <div class="help-block">some field</div>
435 </div>
436 </div>
437 </div>
438 <div class="form-control form-control-sm il-input-radiooption">
439 <input type="radio" id="id_1_g2_opt" name="" value="g2" checked="checked" /><label for="id_1_g2_opt"></label>
440 <div class="form-group row">
441 <label for="id_3" class="control-label col-sm-3">f2</label>
442 <div class="col-sm-9">
443 <input id="id_3" type="text" name="" class="form-control form-control-sm" />
444 <div class="help-block">some other field</div>
445 </div>
446 </div>
447 </div>
448 </div>
449 <div class="help-block">byline</div>
450 </div>
451</div>
452
453EOT;
454 $this->assertEquals(
455 $this->brutallyTrimHTML($expected),
456 $this->brutallyTrimHTML($html)
457 );
458 }
459
461 {
462 $f = $this->buildFactory();
463 $label = "label";
464 $byline = "byline";
465
466 $group1 = $f->group([
467 "field_1" => $f->text("f", "some field")
468 ]);
469 $group2 = $f->group([
470 "field_2" => $f->text("f2", "some other field")
471 ]);
472
473 //construct without string-key:
474 $sg = $f->switchableGroup([$group1,$group2], $label, $byline);
475
476 $r = $this->getDefaultRenderer();
477 $html = $r->render($sg->withValue('1'));
478
479 $expected = <<<EOT
480<div class="form-group row">
481 <label class="control-label col-sm-3">label</label>
482 <div class="col-sm-9">
483 <div id="id_1" class="il-input-radio">
484 <div class="form-control form-control-sm il-input-radiooption">
485 <input type="radio" id="id_1_0_opt" name="" value="0" /><label for="id_1_0_opt"></label>
486 <div class="form-group row">
487 <label for="id_2" class="control-label col-sm-3">f</label>
488 <div class="col-sm-9">
489 <input id="id_2" type="text" name="" class="form-control form-control-sm" />
490 <div class="help-block">some field</div>
491 </div>
492 </div>
493 </div>
494 <div class="form-control form-control-sm il-input-radiooption">
495 <input type="radio" id="id_1_1_opt" name="" value="1" checked="checked" /><label for="id_1_1_opt"></label>
496 <div class="form-group row">
497 <label for="id_3" class="control-label col-sm-3">f2</label>
498 <div class="col-sm-9">
499 <input id="id_3" type="text" name="" class="form-control form-control-sm" />
500 <div class="help-block">some other field</div>
501 </div>
502 </div>
503 </div>
504 </div>
505 <div class="help-block">byline</div>
506 </div>
507</div>
508EOT;
509 $this->assertEquals(
510 $this->brutallyTrimHTML($expected),
511 $this->brutallyTrimHTML($html)
512 );
513 }
514}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
This implements the group input.
Definition: Group.php:20
Provides common functionality for UI tests.
Definition: Base.php:263
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311
brutallyTrimHTML($html)
A more radical version of normalizeHTML.
Definition: Base.php:392
testRenderWithValue($sg)
@depends testRender
This describes a group of inputs.
Definition: Group.php:14
This describes commonalities between all inputs.
Definition: Input.php:32
This describes switchable group inputs.
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
Describes a source for input names.
Definition: NameSource.php:11