ILIAS  release_7 Revision v7.30-3-g800a261c036
GroupInputTest.php
Go to the documentation of this file.
1<?php
18declare(strict_types=1);
19
20require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
21require_once(__DIR__ . "/../../../Base.php");
22
25use \ILIAS\Data;
26
27abstract class Input1 extends Field\Input
28{
29};
30abstract class Input2 extends Field\Input
31{
32};
33
35{
39 private $refinery;
40
41 public function setUp() : void
42 {
43 $this->child1 = $this->createMock(Input1::class);
44 $this->child2 = $this->createMock(Input2::class);
45 $this->data_factory = new Data\Factory;
46 $this->language = $this->createMock(\ilLanguage::class);
47 $this->refinery = new \ILIAS\Refinery\Factory($this->data_factory, $this->language);
48
49 $this->group = new Field\Group(
50 $this->data_factory,
51 $this->refinery,
52 $this->language,
53 [$this->child1, $this->child2],
54 "LABEL",
55 "BYLINE"
56 );
57 }
58
60 {
61 $this->assertNotSame($this->child1, $this->child2);
62
63 $this->child1
64 ->expects($this->once())
65 ->method("withDisabled")
66 ->with(true)
67 ->willReturn($this->child2);
68 $this->child2
69 ->expects($this->once())
70 ->method("withDisabled")
71 ->with(true)
72 ->willReturn($this->child1);
73
74 $new_group = $this->group->withDisabled(true);
75
76 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
77 $this->assertInstanceOf(Field\Group::class, $new_group);
78 $this->assertNotSame($this->group, $new_group);
79 }
80
82 {
83 $this->assertNotSame($this->child1, $this->child2);
84
85 $this->child1
86 ->expects($this->once())
87 ->method("withRequired")
88 ->with(true)
89 ->willReturn($this->child2);
90 $this->child2
91 ->expects($this->once())
92 ->method("withRequired")
93 ->with(true)
94 ->willReturn($this->child1);
95
96 $new_group = $this->group->withRequired(true);
97
98 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
99 $this->assertInstanceOf(Field\Group::class, $new_group);
100 $this->assertNotSame($this->group, $new_group);
101 }
102
104 {
105 $this->expectException(\InvalidArgumentException::class);
106
107 $this->group = new Field\Group(
108 $this->data_factory,
109 $this->refinery,
110 $this->language,
111 ["foo", "bar"],
112 "LABEL",
113 "BYLINE"
114 );
115 }
116
118 {
119 $this->assertNotSame($this->child1, $this->child2);
120
121 $this->child1
122 ->expects($this->once())
123 ->method("withValue")
124 ->with(1)
125 ->willReturn($this->child2);
126 $this->child1
127 ->expects($this->once())
128 ->method("isClientSideValueOk")
129 ->with(1)
130 ->willReturn(true);
131 $this->child2
132 ->expects($this->once())
133 ->method("withValue")
134 ->with(2)
135 ->willReturn($this->child1);
136 $this->child2
137 ->expects($this->once())
138 ->method("isClientSideValueOk")
139 ->with(2)
140 ->willReturn(true);
141
142 $new_group = $this->group->withValue([1,2]);
143
144 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
145 $this->assertInstanceOf(Field\Group::class, $new_group);
146 $this->assertNotSame($this->group, $new_group);
147 }
148
150 {
151 $this->assertNotSame($this->child1, $this->child2);
152
153 $this->group = new Field\Group(
154 $this->data_factory,
155 $this->refinery,
156 $this->language,
157 ["child1" => $this->child1, "child2" => $this->child2],
158 "LABEL",
159 "BYLINE"
160 );
161
162 $this->child1
163 ->method("withValue")
164 ->willReturn($this->child2);
165 $this->child1
166 ->method("isClientSideValueOk")
167 ->willReturn(true);
168 $this->child2
169 ->method("withValue")
170 ->willReturn($this->child1);
171 $this->child2
172 ->method("isClientSideValueOk")
173 ->willReturn(true);
174
175 $new_group = $this->group->withValue(["child1" => 1,"child2" => 2]);
176
177 $this->assertEquals(["child1" => $this->child2, "child2" => $this->child1], $new_group->getInputs());
178 }
179
180
182 {
183 $this->expectException(\InvalidArgumentException::class);
184
185 $new_group = $this->group->withValue(1);
186 }
187
189 {
190 $this->expectException(\InvalidArgumentException::class);
191
192 $new_group = $this->group->withValue([1]);
193 }
194
196 {
197 $this->assertNotSame($this->child1, $this->child2);
198
199 $this->child1
200 ->expects($this->once())
201 ->method("getValue")
202 ->with()
203 ->willReturn("one");
204 $this->child2
205 ->expects($this->once())
206 ->method("getValue")
207 ->with()
208 ->willReturn("two");
209
210 $vals = $this->group->getValue();
211
212 $this->assertEquals(["one", "two"], $vals);
213 }
214
216 {
217 $this->assertNotSame($this->child1, $this->child2);
218
219 $input_data = $this->createMock(InputData::class);
220
221 $this->child1
222 ->expects($this->once())
223 ->method("withInput")
224 ->with($input_data)
225 ->willReturn($this->child2);
226 $this->child1
227 ->expects($this->once())
228 ->method("getContent")
229 ->willReturn($this->data_factory->ok("one"));
230 $this->child2
231 ->expects($this->once())
232 ->method("withInput")
233 ->with($input_data)
234 ->willReturn($this->child1);
235 $this->child2
236 ->expects($this->once())
237 ->method("getContent")
238 ->willReturn($this->data_factory->ok("two"));
239
240 $called = false;
241 $new_group = $this->group
242 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called) {
243 $called = true;
244 $this->assertEquals(["two", "one"], $v);
245 return "result";
246 }))
247 ->withInput($input_data);
248
249 $this->assertTrue($called);
250 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
251 $this->assertInstanceOf(Field\Group::class, $new_group);
252 $this->assertNotSame($this->group, $new_group);
253 $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
254 }
255
257 {
258 $this->assertNotSame($this->child1, $this->child2);
259
260 $input_data = $this->createMock(InputData::class);
261
262 $this->child1
263 ->expects($this->once())
264 ->method("withInput")
265 ->with($input_data)
266 ->willReturn($this->child2);
267 $this->child1
268 ->expects($this->once())
269 ->method("getContent")
270 ->willReturn($this->data_factory->error(""));
271 $this->child2
272 ->expects($this->once())
273 ->method("withInput")
274 ->with($input_data)
275 ->willReturn($this->child1);
276 $this->child2
277 ->expects($this->once())
278 ->method("getContent")
279 ->willReturn($this->data_factory->ok("two"));
280
281 $i18n = "THERE IS SOME ERROR IN THIS GROUP";
282 $this->language
283 ->expects($this->once())
284 ->method("txt")
285 ->with("ui_error_in_group")
286 ->willReturn($i18n);
287
288 $new_group = $this->group
289 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) {
290 $this->assertFalse(true, "This should not happen.");
291 }))
292 ->withInput($input_data);
293
294 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
295 $this->assertInstanceOf(Field\Group::class, $new_group);
296 $this->assertNotSame($this->group, $new_group);
297 $this->assertTrue($new_group->getContent()->isError());
298 }
299
300 public function testErrorIsI18NOnError()
301 {
302 $this->assertNotSame($this->child1, $this->child2);
303
304 $input_data = $this->createMock(InputData::class);
305
306 $this->child1
307 ->method("withInput")
308 ->willReturn($this->child2);
309 $this->child1
310 ->method("getContent")
311 ->willReturn($this->data_factory->error(""));
312 $this->child2
313 ->method("withInput")
314 ->willReturn($this->child1);
315 $this->child2
316 ->method("getContent")
317 ->willReturn($this->data_factory->ok("two"));
318
319 $i18n = "THERE IS SOME ERROR IN THIS GROUP";
320 $this->language
321 ->expects($this->once())
322 ->method("txt")
323 ->with("ui_error_in_group")
324 ->willReturn($i18n);
325
326 $new_group = $this->group
327 ->withInput($input_data);
328
329 $this->assertTrue($new_group->getContent()->isError());
330 $this->assertEquals($i18n, $new_group->getContent()->error());
331 }
332 public function testWithoutChildren()
333 {
334 $group = new Field\Group(
335 $this->data_factory,
336 $this->refinery,
337 $this->language,
338 [],
339 "LABEL",
340 "BYLINE"
341 );
342 $content = $group->getContent();
343 $this->assertInstanceOf(\ILIAS\Data\Result\Ok::class, $content);
344 $this->assertCount(0, $content->value());
345 }
346
347 public function getFieldFactory()
348 {
351 new Data\Factory(),
352 $this->getRefinery(),
353 $this->getLanguage()
354 );
355 return $factory;
356 }
357
358 public function testGroupRendering()
359 {
360 $f = $this->getFieldFactory();
361 $inputs = [
362 $f->text("input1", "in 1"),
363 $f->text("input2", "in 2")
364 ];
365 $label = 'group label';
366 $group = $f->group($inputs, $label);
367
368 $expected = <<<EOT
369 <div class="form-group row">
370 <label for="id_1" class="control-label col-sm-3">input1</label>
371 <div class="col-sm-9">
372 <input id="id_1" type="text" name="" class="form-control form-control-sm" />
373 <div class="help-block">in 1</div>
374 </div>
375 </div>
376 <div class="form-group row">
377 <label for="id_2" class="control-label col-sm-3">input2</label>
378 <div class="col-sm-9">
379 <input id="id_2" type="text" name="" class="form-control form-control-sm" />
380 <div class="help-block">in 2</div>
381 </div>
382 </div>
383EOT;
384 $actual = $this->brutallyTrimHTML($this->getDefaultRenderer()->render($group));
385 $expected = $this->brutallyTrimHTML($expected);
386 $this->assertEquals($expected, $actual);
387 }
388
389 public function testGroupWithoutRequiredField(): void
390 {
391 $f = $this->getFieldFactory();
392 $inputs = [
393 $f->text(""),
394 $f->text("")
395 ];
396 $group = $f->group($inputs, '');
397 $this->assertFalse($group->isRequired());
398 }
399
400 public function testGroupWithRequiredField(): void
401 {
402 $f = $this->getFieldFactory();
403 $inputs = [
404 $f->text(""),
405 $f->text("")->withRequired(true)
406 ];
407 $group = $f->group($inputs, '');
408 $this->assertTrue($group->isRequired());
409 }
410}
An exception for terminatinating execution or to throw for unit testing.
testWithInputCallsChildrenAndAppliesOperations()
testGroupOnlyDoesNoAcceptNonArrayValue()
testWithDisabledDisablesChildren()
testWithRequiredRequiresChildren()
testWithInputDoesNotApplyOperationsOnError()
testGroupOnlyDoesNoAcceptArrayValuesWithWrongLength()
testGroupForwardsValuesOnWithValue()
testGroupMayOnlyHaveInputChildren()
testGroupForwardsValuesOnGetValue()
Builds data types.
Definition: Factory.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
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:12
This describes a group of inputs.
Definition: Group.php:14
This describes commonalities between all inputs.
Definition: Input.php:32
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
language()
Definition: language.php:2
$factory
Definition: metadata.php:58
Class ChatMainBarProvider \MainMenu\Provider.