ILIAS  release_7 Revision v7.30-3-g800a261c036
OptionalGroupInputTest.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
12use \ILIAS\Data;
13
14abstract class Input11 extends Input
15{
16};
17abstract class Input12 extends Input
18{
19};
20
22{
26 private $refinery;
27
28 public function setUp() : void
29 {
30 $this->child1 = $this->createMock(Input11::class);
31 $this->child2 = $this->createMock(Input12::class);
32 $this->data_factory = new Data\Factory();
33 $this->language = $this->createMock(\ilLanguage::class);
34 $this->refinery = new ILIAS\Refinery\Factory($this->data_factory, $this->language);
35
36 $this->child1
37 ->method("withNameFrom")
38 ->willReturn($this->child1);
39 $this->child2
40 ->method("withNameFrom")
41 ->willReturn($this->child2);
42
43 $this->optional_group = (new OptionalGroup(
44 $this->data_factory,
45 $this->refinery,
46 $this->language,
47 [$this->child1, $this->child2],
48 "LABEL",
49 "BYLINE"
50 ))->withNameFrom(new class implements NameSource {
51 public function getNewName()
52 {
53 return "name0";
54 }
55 });
56 }
57
59 {
60 $this->assertNotSame($this->child1, $this->child2);
61
62 $this->child1
63 ->expects($this->once())
64 ->method("withDisabled")
65 ->with(true)
66 ->willReturn($this->child2);
67 $this->child2
68 ->expects($this->once())
69 ->method("withDisabled")
70 ->with(true)
71 ->willReturn($this->child1);
72
73 $new_group = $this->optional_group->withDisabled(true);
74
75 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
76 $this->assertInstanceOf(OptionalGroup::class, $new_group);
77 $this->assertNotSame($this->optional_group, $new_group);
78 }
79
81 {
82 $this->assertNotSame($this->child1, $this->child2);
83
84 $this->child1
85 ->expects($this->never())
86 ->method("withRequired");
87 $this->child2
88 ->expects($this->never())
89 ->method("withRequired");
90
91 $new_group = $this->optional_group->withRequired(true);
92
93 $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
94 $this->assertInstanceOf(OptionalGroup::class, $new_group);
95 $this->assertNotSame($this->optional_group, $new_group);
96 }
97
99 {
100 $this->assertNotSame($this->child1, $this->child2);
101 $this->child1->method('isRequired')->willReturn(true);
102 $this->child2->method('isRequired')->willReturn(true);
103
104 $new_group = $this->optional_group;
105
106 $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
107 $this->assertInstanceOf(OptionalGroup::class, $new_group);
108 $this->assertFalse($new_group->isRequired());
109 }
110
112 {
113 $this->expectException(\InvalidArgumentException::class);
114
115 $this->group = new OptionalGroup(
116 $this->data_factory,
117 $this->refinery,
118 $this->language,
119 ["foo", "bar"],
120 "LABEL",
121 "BYLINE"
122 );
123 }
124
126 {
127 $this->assertNotSame($this->child1, $this->child2);
128
129 $this->child1
130 ->expects($this->once())
131 ->method("withValue")
132 ->with(1)
133 ->willReturn($this->child2);
134 $this->child1
135 ->expects($this->once())
136 ->method("isClientSideValueOk")
137 ->with(1)
138 ->willReturn(true);
139 $this->child2
140 ->expects($this->once())
141 ->method("withValue")
142 ->with(2)
143 ->willReturn($this->child1);
144 $this->child2
145 ->expects($this->once())
146 ->method("isClientSideValueOk")
147 ->with(2)
148 ->willReturn(true);
149
150 $new_group = $this->optional_group->withValue([1,2]);
151
152 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
153 $this->assertInstanceOf(OptionalGroup::class, $new_group);
154 $this->assertNotSame($this->optional_group, $new_group);
155 }
156
158 {
159 $this->expectException(\InvalidArgumentException::class);
160
161 $new_group = $this->optional_group->withValue(1);
162 }
163
165 {
166 $this->expectException(\InvalidArgumentException::class);
167
168 $new_group = $this->optional_group->withValue([1]);
169 }
170
172 {
173 $this->assertNotSame($this->child1, $this->child2);
174
175 $this->child1
176 ->expects($this->never())
177 ->method("withValue");
178 $this->child1
179 ->expects($this->never())
180 ->method("isClientSideValueOk");
181 $this->child2
182 ->expects($this->never())
183 ->method("withValue");
184 $this->child2
185 ->expects($this->never())
186 ->method("isClientSideValueOk");
187
188 $new_group = $this->optional_group->withValue(null);
189
190 $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
191 $this->assertInstanceOf(OptionalGroup::class, $new_group);
192 $this->assertNotSame($this->optional_group, $new_group);
193 $this->assertEquals(null, $new_group->getValue());
194 }
195
197 {
198 $this->assertNotSame($this->child1, $this->child2);
199
200 $this->child1
201 ->expects($this->once())
202 ->method("getValue")
203 ->with()
204 ->willReturn("one");
205 $this->child2
206 ->expects($this->once())
207 ->method("getValue")
208 ->with()
209 ->willReturn("two");
210
211 $vals = $this->optional_group->getValue();
212
213 $this->assertEquals(["one", "two"], $vals);
214 }
215
217 {
218 $this->assertNotSame($this->child1, $this->child2);
219
220 $input_data = $this->createMock(InputData::class);
221
222 $input_data
223 ->expects($this->once())
224 ->method("getOr")
225 ->with("name0", null)
226 ->willReturn("checked");
227
228 $this->child1
229 ->expects($this->once())
230 ->method("withInput")
231 ->with($input_data)
232 ->willReturn($this->child2);
233 $this->child1
234 ->expects($this->once())
235 ->method("getContent")
236 ->with()
237 ->willReturn($this->data_factory->ok("one"));
238 $this->child2
239 ->expects($this->once())
240 ->method("withInput")
241 ->with($input_data)
242 ->willReturn($this->child1);
243 $this->child2
244 ->expects($this->once())
245 ->method("getContent")
246 ->with()
247 ->willReturn($this->data_factory->ok("two"));
248
249 $called = false;
250 $new_group = $this->optional_group
251 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called) {
252 $called = true;
253 $this->assertEquals(["two", "one"], $v);
254 return "result";
255 }))
256 ->withInput($input_data);
257
258 $this->assertTrue($called);
259 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
260 $this->assertInstanceOf(OptionalGroup::class, $new_group);
261 $this->assertNotSame($this->optional_group, $new_group);
262 $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
263 }
264
266 {
267 $this->assertNotSame($this->child1, $this->child2);
268
269 $input_data = $this->createMock(InputData::class);
270
271 $input_data
272 ->expects($this->once())
273 ->method("getOr")
274 ->with("name0", null)
275 ->willReturn("checked");
276
277 $this->child1
278 ->expects($this->once())
279 ->method("withInput")
280 ->with($input_data)
281 ->willReturn($this->child2);
282 $this->child1
283 ->expects($this->once())
284 ->method("getContent")
285 ->willReturn($this->data_factory->error(""));
286 $this->child2
287 ->expects($this->once())
288 ->method("withInput")
289 ->with($input_data)
290 ->willReturn($this->child1);
291 $this->child2
292 ->expects($this->once())
293 ->method("getContent")
294 ->willReturn($this->data_factory->ok("two"));
295
296 $i18n = "THERE IS SOME ERROR IN THIS GROUP";
297 $this->language
298 ->expects($this->once())
299 ->method("txt")
300 ->with("ui_error_in_group")
301 ->willReturn($i18n);
302
303 $new_group = $this->optional_group
304 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) {
305 $this->assertFalse(true, "This should not happen.");
306 }))
307 ->withInput($input_data);
308
309 $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
310 $this->assertInstanceOf(OptionalGroup::class, $new_group);
311 $this->assertNotSame($this->optional_group, $new_group);
312 $this->assertTrue($new_group->getContent()->isError());
313 }
314
316 {
317 $this->assertNotSame($this->child1, $this->child2);
318
319 $input_data = $this->createMock(InputData::class);
320
321 $input_data
322 ->expects($this->once())
323 ->method("getOr")
324 ->with("name0", null)
325 ->willReturn(null);
326
327 $this->child1
328 ->expects($this->never())
329 ->method("withInput");
330 $this->child1
331 ->expects($this->never())
332 ->method("getContent");
333 $this->child2
334 ->expects($this->never())
335 ->method("withInput");
336 $this->child2
337 ->expects($this->never())
338 ->method("getContent");
339
340 $called = false;
341 $new_group = $this->optional_group
342 ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called) {
343 $called = true;
344 $this->assertEquals(null, $v);
345 return "result";
346 }))
347 ->withInput($input_data);
348
349 $this->assertTrue($called);
350 $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
351 $this->assertInstanceOf(OptionalGroup::class, $new_group);
352 $this->assertNotSame($this->optional_group, $new_group);
353 $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
354 }
355}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
This implements commonalities between inputs.
Definition: Input.php:24
Provides common functionality for UI tests.
Definition: Base.php:263
This describes commonalities between all inputs.
Definition: Input.php:32
This describes optional 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
language()
Definition: language.php:2