ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
5 require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6 require_once(__DIR__ . "/../../../Base.php");
7 
12 use \ILIAS\Data;
13 
14 abstract class Input11 extends Input
15 {
16 };
17 abstract 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->expectException(\InvalidArgumentException::class);
101 
102  $this->group = new OptionalGroup(
103  $this->data_factory,
104  $this->refinery,
105  $this->language,
106  ["foo", "bar"],
107  "LABEL",
108  "BYLINE"
109  );
110  }
111 
113  {
114  $this->assertNotSame($this->child1, $this->child2);
115 
116  $this->child1
117  ->expects($this->once())
118  ->method("withValue")
119  ->with(1)
120  ->willReturn($this->child2);
121  $this->child1
122  ->expects($this->once())
123  ->method("isClientSideValueOk")
124  ->with(1)
125  ->willReturn(true);
126  $this->child2
127  ->expects($this->once())
128  ->method("withValue")
129  ->with(2)
130  ->willReturn($this->child1);
131  $this->child2
132  ->expects($this->once())
133  ->method("isClientSideValueOk")
134  ->with(2)
135  ->willReturn(true);
136 
137  $new_group = $this->optional_group->withValue([1,2]);
138 
139  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
140  $this->assertInstanceOf(OptionalGroup::class, $new_group);
141  $this->assertNotSame($this->optional_group, $new_group);
142  }
143 
145  {
146  $this->expectException(\InvalidArgumentException::class);
147 
148  $new_group = $this->optional_group->withValue(1);
149  }
150 
152  {
153  $this->expectException(\InvalidArgumentException::class);
154 
155  $new_group = $this->optional_group->withValue([1]);
156  }
157 
159  {
160  $this->assertNotSame($this->child1, $this->child2);
161 
162  $this->child1
163  ->expects($this->never())
164  ->method("withValue");
165  $this->child1
166  ->expects($this->never())
167  ->method("isClientSideValueOk");
168  $this->child2
169  ->expects($this->never())
170  ->method("withValue");
171  $this->child2
172  ->expects($this->never())
173  ->method("isClientSideValueOk");
174 
175  $new_group = $this->optional_group->withValue(null);
176 
177  $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
178  $this->assertInstanceOf(OptionalGroup::class, $new_group);
179  $this->assertNotSame($this->optional_group, $new_group);
180  $this->assertEquals(null, $new_group->getValue());
181  }
182 
184  {
185  $this->assertNotSame($this->child1, $this->child2);
186 
187  $this->child1
188  ->expects($this->once())
189  ->method("getValue")
190  ->with()
191  ->willReturn("one");
192  $this->child2
193  ->expects($this->once())
194  ->method("getValue")
195  ->with()
196  ->willReturn("two");
197 
198  $vals = $this->optional_group->getValue();
199 
200  $this->assertEquals(["one", "two"], $vals);
201  }
202 
204  {
205  $this->assertNotSame($this->child1, $this->child2);
206 
207  $input_data = $this->createMock(InputData::class);
208 
209  $input_data
210  ->expects($this->once())
211  ->method("getOr")
212  ->with("name0", null)
213  ->willReturn("checked");
214 
215  $this->child1
216  ->expects($this->once())
217  ->method("withInput")
218  ->with($input_data)
219  ->willReturn($this->child2);
220  $this->child1
221  ->expects($this->once())
222  ->method("getContent")
223  ->with()
224  ->willReturn($this->data_factory->ok("one"));
225  $this->child2
226  ->expects($this->once())
227  ->method("withInput")
228  ->with($input_data)
229  ->willReturn($this->child1);
230  $this->child2
231  ->expects($this->once())
232  ->method("getContent")
233  ->with()
234  ->willReturn($this->data_factory->ok("two"));
235 
236  $called = false;
237  $new_group = $this->optional_group
238  ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called) {
239  $called = true;
240  $this->assertEquals(["two", "one"], $v);
241  return "result";
242  }))
243  ->withInput($input_data);
244 
245  $this->assertTrue($called);
246  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
247  $this->assertInstanceOf(OptionalGroup::class, $new_group);
248  $this->assertNotSame($this->optional_group, $new_group);
249  $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
250  }
251 
253  {
254  $this->assertNotSame($this->child1, $this->child2);
255 
256  $input_data = $this->createMock(InputData::class);
257 
258  $input_data
259  ->expects($this->once())
260  ->method("getOr")
261  ->with("name0", null)
262  ->willReturn("checked");
263 
264  $this->child1
265  ->expects($this->once())
266  ->method("withInput")
267  ->with($input_data)
268  ->willReturn($this->child2);
269  $this->child1
270  ->expects($this->once())
271  ->method("getContent")
272  ->willReturn($this->data_factory->error(""));
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  ->willReturn($this->data_factory->ok("two"));
282 
283  $i18n = "THERE IS SOME ERROR IN THIS GROUP";
284  $this->language
285  ->expects($this->once())
286  ->method("txt")
287  ->with("ui_error_in_group")
288  ->willReturn($i18n);
289 
290  $new_group = $this->optional_group
291  ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) {
292  $this->assertFalse(true, "This should not happen.");
293  }))
294  ->withInput($input_data);
295 
296  $this->assertEquals([$this->child2, $this->child1], $new_group->getInputs());
297  $this->assertInstanceOf(OptionalGroup::class, $new_group);
298  $this->assertNotSame($this->optional_group, $new_group);
299  $this->assertTrue($new_group->getContent()->isError());
300  }
301 
303  {
304  $this->assertNotSame($this->child1, $this->child2);
305 
306  $input_data = $this->createMock(InputData::class);
307 
308  $input_data
309  ->expects($this->once())
310  ->method("getOr")
311  ->with("name0", null)
312  ->willReturn(null);
313 
314  $this->child1
315  ->expects($this->never())
316  ->method("withInput");
317  $this->child1
318  ->expects($this->never())
319  ->method("getContent");
320  $this->child2
321  ->expects($this->never())
322  ->method("withInput");
323  $this->child2
324  ->expects($this->never())
325  ->method("getContent");
326 
327  $called = false;
328  $new_group = $this->optional_group
329  ->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (&$called) {
330  $called = true;
331  $this->assertEquals(null, $v);
332  return "result";
333  }))
334  ->withInput($input_data);
335 
336  $this->assertTrue($called);
337  $this->assertEquals([$this->child1, $this->child2], $new_group->getInputs());
338  $this->assertInstanceOf(OptionalGroup::class, $new_group);
339  $this->assertNotSame($this->optional_group, $new_group);
340  $this->assertEquals($this->data_factory->ok("result"), $new_group->getContent());
341  }
342 }
Provides common functionality for UI tests.
Definition: Base.php:224
language()
Definition: language.php:2
Describes a source for input names.
Definition: NameSource.php:10
withNameFrom(NameSource $source)
Get an input like this one, with a different name.Input
Definition: Input.php:345