ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
InputTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2017 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4
5require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6require_once(__DIR__ . "/../../../Base.php");
7
8use \ILIAS\UI\Implementation\Component\Input\Field\Input;
9use \ILIAS\UI\Implementation\Component\Input\NameSource;
10use \ILIAS\UI\Implementation\Component\Input\InputData;
11use \ILIAS\Data\Factory as DataFactory;
12use \ILIAS\Data\Result;
13
14class DefInput extends Input
15{
16 public $value_ok = true;
17
18
19 protected function isClientSideValueOk($value) : bool
20 {
21 return $this->value_ok;
22 }
23
24
26
27
28 protected function getConstraintForRequirement()
29 {
31 }
32
33
34 public function getUpdateOnLoadCode() : \Closure
35 {
36 return function () {
37 };
38 }
39}
40
41class DefNamesource implements NameSource
42{
43 public $count = 0;
44
45
46 public function getNewName()
47 {
48 $name = "name_{$this->count}";
49 $this->count++;
50
51 return $name;
52 }
53}
54
55class DefInputData implements InputData
56{
57 public $values = array();
58
59
60 public function __construct(array $values)
61 {
62 $this->values = $values;
63 }
64
65
66 public function get($name)
67 {
68 if (!is_string($name)) {
69 throw new \InvalidArgumentException('$name is no string.');
70 }
71 if (!isset($this->values[$name])) {
72 throw new \LogicException("'$name' does not exist.");
73 }
74
75 return $this->values[$name];
76 }
77
78
79 public function getOr($name, $value)
80 {
81 if (!is_string($name)) {
82 throw new \InvalidArgumentException('$name is no string.');
83 }
84 if (!isset($this->values[$name])) {
85 return $value;
86 }
87
88 return $this->values[$name];
89 }
90}
91
96{
97
101 private $refinery;
102
103 public function setUp() : void
104 {
105 $this->data_factory = new DataFactory();
106 $language = $this->createMock(\ilLanguage::class);
107 $this->refinery = new \ILIAS\Refinery\Factory($this->data_factory, $language);
108 $this->input = new DefInput(
109 $this->data_factory,
110 $this->refinery,
111 "label",
112 "byline"
113 );
114 $this->name_source = new DefNamesource();
115 }
116
117
118 public function test_constructor()
119 {
120 $this->assertEquals("label", $this->input->getLabel());
121 $this->assertEquals("byline", $this->input->getByline());
122 }
123
124
125 public function test_withLabel()
126 {
127 $label = "new label";
128 $input = $this->input->withLabel($label);
129 $this->assertEquals($label, $input->getLabel());
130 $this->assertNotSame($this->input, $input);
131 }
132
133
134 public function test_withByline()
135 {
136 $byline = "new byline";
137 $input = $this->input->withByline($byline);
138 $this->assertEquals($byline, $input->getByline());
139 $this->assertNotSame($this->input, $input);
140 }
141
142
143 public function test_withRequired()
144 {
145 $this->assertFalse($this->input->isRequired());
146 $input = $this->input->withRequired(true);
147 $this->assertTrue($input->isRequired());
148 $input = $input->withRequired(false);
149 $this->assertFalse($input->isRequired());
150 }
151
152
153 public function test_withDisabled()
154 {
155 $this->assertFalse($this->input->isDisabled());
156 $input = $this->input->withDisabled(true);
157 $this->assertTrue($input->isDisabled());
158 $input = $input->withDisabled(false);
159 $this->assertFalse($input->isDisabled());
160 }
161
162
163 public function test_withValue()
164 {
165 $value = "some value";
166 $input = $this->input->withValue($value);
167 $this->assertEquals(null, $this->input->getValue());
168 $this->assertEquals($value, $input->getValue());
169 $this->assertNotSame($this->input, $input);
170 }
171
172
173 public function test_withValue_throws()
174 {
175 $this->input->value_ok = false;
176 $raised = false;
177 try {
178 $this->input->withValue("foo");
179 $this->assertFalse("This should not happen.");
180 } catch (\InvalidArgumentException $e) {
181 $raised = true;
182 }
183 $this->assertTrue($raised);
184 $this->assertEquals(null, $this->input->getValue());
185 }
186
187
188 public function test_withName()
189 {
190 $name = "name_0";
191 $input = $this->input->withNameFrom($this->name_source);
192 $this->assertEquals(null, $this->input->getName());
193 $this->assertEquals($name, $input->getName());
194 $this->assertNotSame($this->input, $input);
195 $this->assertEquals(1, $this->name_source->count);
196 }
197
198
199 public function test_withError()
200 {
201 $error = "error";
202 $input = $this->input->withError($error);
203 $this->assertEquals(null, $this->input->getError());
204 $this->assertEquals($error, $input->getError());
205 $this->assertNotSame($this->input, $input);
206 }
207
208
209 public function test_getContent()
210 {
211 $this->expectException(\LogicException::class);
212
213 $this->input->getContent();
214 }
215
216
217 public function test_withInput()
218 {
219 $name = "name_0";
220 $value = "valu";
221 $input = $this->input->withNameFrom($this->name_source);
222 $values = new DefInputData([$name => $value]);
223
224 $input2 = $input->withInput($values);
225 $res = $input2->getContent();
226
227 $this->assertInstanceOf(Result::class, $res);
228 $this->assertTrue($res->isOk());
229 $this->assertEquals($value, $res->value());
230
231 $this->assertNotSame($input, $input2);
232 $this->assertEquals($value, $input2->getValue());
233 }
234
235
237 {
238 $raised = false;
239 try {
240 $this->input->withInput(new DefInputData([]));
241 $this->assertFalse("This should not happen.");
242 } catch (\LogicException $e) {
243 $raised = true;
244 }
245 $this->assertTrue($raised);
246 }
247
248
250 {
251 $name = "name_0";
252 $value = "value";
253 $transform_to = "other value";
254 $input = $this->input->withNameFrom($this->name_source);
255 $values = new DefInputData([$name => $value]);
256
257 $input2 = $input->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use ($value, $transform_to) {
258 $this->assertEquals($value, $v);
259
260 return $transform_to;
261 }))->withInput($values);
262 $res = $input2->getContent();
263
264 $this->assertInstanceOf(Result::class, $res);
265 $this->assertTrue($res->isOk());
266 $this->assertEquals($transform_to, $res->value());
267
268 $this->assertNotSame($input, $input2);
269 $this->assertEquals($value, $input2->getValue());
270 }
271
272
274 {
275 $name = "name_0";
276 $value = "value";
277 $transform_to = "other value";
278 $input = $this->input->withNameFrom($this->name_source);
279 $values = new DefInputData([$name => $value]);
280
281 $input2 = $input->withInput($values)->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (
282 $value,
283 $transform_to
284 ) {
285 $this->assertEquals($value, $v);
286
287 return $transform_to;
288 }));
289 $res = $input2->getContent();
290
291 $this->assertInstanceOf(Result::class, $res);
292 $this->assertTrue($res->isOk());
293 $this->assertEquals($transform_to, $res->value());
294
295 $this->assertNotSame($input, $input2);
296 $this->assertEquals($value, $input2->getValue());
297 }
298
299
301 {
302 $name = "name_0";
303 $value = "value";
304 $error = "an error";
305 $input = $this->input->withNameFrom($this->name_source);
306 $values = new DefInputData([$name => $value]);
307
308 $input2 = $input->withAdditionalTransformation($this->refinery->custom()->constraint(function ($_) {
309 return true;
310 }, $error))->withInput($values);
311 $res = $input2->getContent();
312
313 $this->assertInstanceOf(Result::class, $res);
314 $this->assertTrue($res->isOk());
315 $this->assertEquals($value, $res->value());
316
317 $this->assertNotSame($input, $input2);
318 $this->assertEquals($value, $input2->getValue());
319 $this->assertEquals(null, $input2->getError());
320 }
321
322
324 {
325 $name = "name_0";
326 $value = "value";
327 $error = "an error";
328 $input = $this->input->withNameFrom($this->name_source);
329 $values = new DefInputData([$name => $value]);
330
331 $input2 = $input->withAdditionalTransformation($this->refinery->custom()->constraint(function ($_) {
332 return false;
333 }, $error))->withInput($values);
334 $res = $input2->getContent();
335
336 $this->assertInstanceOf(Result::class, $res);
337 $this->assertTrue($res->isError());
338 $this->assertEquals($error, $res->error());
339
340 $this->assertNotSame($input, $input2);
341 $this->assertEquals($value, $input2->getValue());
342 $this->assertEquals($error, $input2->getError());
343 }
344
345
347 {
348 $rc = $this->refinery->custom();
349
350 $name = "name_0";
351 $value = "value";
352 $error = "an error";
353 $input = $this->input->withNameFrom($this->name_source);
354 $values = new DefInputData([$name => $value]);
355
356 $input2 = $input
357 ->withInput($values)
358 ->withAdditionalTransformation($rc->constraint(function ($_) {
359 return false;
360 }, $error));
361
362 $res = $input2->getContent();
363
364 $this->assertInstanceOf(Result::class, $res);
365 $this->assertTrue($res->isError());
366 $this->assertEquals($error, $res->error());
367
368 $this->assertNotSame($input, $input2);
369 $this->assertEquals($value, $input2->getValue());
370 $this->assertEquals($error, $input2->getError());
371 }
372
373
375 {
376 $name = "name_0";
377 $value = "value";
378 $transform_to = "other value";
379 $error = "an error";
380 $input = $this->input->withNameFrom($this->name_source);
381 $values = new DefInputData([$name => $value]);
382
383 $input2 = $input->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use ($value, $transform_to) {
384 $this->assertEquals($value, $v);
385
386 return $transform_to;
387 }))->withAdditionalTransformation($this->refinery->custom()->constraint(function ($v) use ($transform_to) {
388 $this->assertEquals($transform_to, $v);
389
390 return true;
391 }, $error))->withInput($values);
392 $res = $input2->getContent();
393
394 $this->assertInstanceOf(Result::class, $res);
395 $this->assertTrue($res->isOk());
396 $this->assertEquals($transform_to, $res->value());
397
398 $this->assertNotSame($input, $input2);
399 $this->assertEquals($value, $input2->getValue());
400 $this->assertEquals(null, $input2->getError());
401 }
402
403
405 {
406 $name = "name_0";
407 $value = "value";
408 $transform_to = "other value";
409 $error = "an error";
410 $input = $this->input->withNameFrom($this->name_source);
411 $values = new DefInputData([$name => $value]);
412
413 $input2 = $input->withInput($values)->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use (
414 $value,
415 $transform_to
416 ) {
417 $this->assertEquals($value, $v);
418
419 return $transform_to;
420 }))->withAdditionalTransformation($this->refinery->custom()->constraint(function ($v) use ($transform_to) {
421 $this->assertEquals($transform_to, $v);
422
423 return true;
424 }, $error));
425 $res = $input2->getContent();
426
427 $this->assertInstanceOf(Result::class, $res);
428 $this->assertTrue($res->isOk());
429 $this->assertEquals($transform_to, $res->value());
430
431 $this->assertNotSame($input, $input2);
432 $this->assertEquals($value, $input2->getValue());
433 $this->assertEquals(null, $input2->getError());
434 }
435
436
438 {
439 $name = "name_0";
440 $value = "value";
441 $transform_to = "other value";
442 $error = "an error";
443 $input = $this->input->withNameFrom($this->name_source);
444 $values = new DefInputData([$name => $value]);
445
446 $input2 = $input->withAdditionalTransformation($this->refinery->custom()->constraint(function ($v) use ($value) {
447 $this->assertEquals($value, $v);
448
449 return true;
450 }, $error))->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use ($value, $transform_to) {
451 $this->assertEquals($value, $v);
452
453 return $transform_to;
454 }))->withInput($values);
455 $res = $input2->getContent();
456
457 $this->assertInstanceOf(Result::class, $res);
458 $this->assertTrue($res->isOk());
459 $this->assertEquals($transform_to, $res->value());
460
461 $this->assertNotSame($input, $input2);
462 $this->assertEquals($value, $input2->getValue());
463 $this->assertEquals(null, $input2->getError());
464 }
465
466
468 {
469 $rc = $this->refinery->custom();
470
471 $name = "name_0";
472 $value = "value";
473 $transform_to = "other value";
474 $error = "an error";
475 $input = $this->input->withNameFrom($this->name_source);
476 $values = new DefInputData([$name => $value]);
477
478 $input2 = $input
479 ->withAdditionalTransformation($rc->constraint(function ($v) use ($value) {
480 $this->assertEquals($value, $v);
481
482 return false;
483 }, $error))
484 ->withAdditionalTransformation($rc->transformation(function ($v) use ($value, $transform_to) {
485 $this->assertFalse("This should not happen");
486
487 return $transform_to;
488 }))->withInput($values);
489 $res = $input2->getContent();
490
491 $this->assertInstanceOf(Result::class, $res);
492 $this->assertTrue($res->isError());
493 $this->assertEquals($error, $res->error());
494
495 $this->assertNotSame($input, $input2);
496 $this->assertEquals($value, $input2->getValue());
497 $this->assertEquals($error, $input2->getError());
498 }
499
500
502 {
503 $name = "name_0";
504 $value = "value";
505 $transform_to = "other value";
506 $error = "an error";
507 $input = $this->input->withNameFrom($this->name_source);
508 $values = new DefInputData([$name => $value]);
509
510 $input2 = $input->withInput($values)->withAdditionalTransformation($this->refinery->custom()->constraint(function ($v) use ($value) {
511 $this->assertEquals($value, $v);
512
513 return false;
514 }, $error))->withAdditionalTransformation($this->refinery->custom()->transformation(function ($v) use ($value, $transform_to) {
515 $this->assertFalse("This should not happen");
516
517 return $transform_to;
518 }));
519 $res = $input2->getContent();
520
521 $this->assertInstanceOf(Result::class, $res);
522 $this->assertTrue($res->isError());
523 $this->assertEquals($error, $res->error());
524
525 $this->assertNotSame($input, $input2);
526 $this->assertEquals($value, $input2->getValue());
527 $this->assertEquals($error, $input2->getError());
528 }
529
530
532 {
533 $name = "name_0";
534 $value = "value";
535 $error = "an error";
536 $input = $this->input->withNameFrom($this->name_source);
537 $values = new DefInputData([$name => $value]);
538
539 $input->requirement_constraint = $this->refinery->custom()->constraint(function ($_) {
540 return false;
541 }, $error);
542
543 $input2 = $input->withRequired(true)->withInput($values);
544 $res = $input2->getContent();
545
546 $this->assertInstanceOf(Result::class, $res);
547 $this->assertTrue($res->isError());
548 $this->assertEquals($error, $res->error());
549
550 $this->assertNotSame($input, $input2);
551 $this->assertEquals($value, $input2->getValue());
552 $this->assertEquals($error, $input2->getError());
553 }
554
555
557 {
558 $name = "name_0";
559 $value = "value";
560 $error = "an error";
561 $input = $this->input->withNameFrom($this->name_source);
562 $values = new DefInputData([$name => $value]);
563
564 $input->requirement_constraint = $this->refinery->custom()->constraint(function ($_) {
565 return false;
566 }, $error);
567
568 $input2 = $input->withRequired(true)->withRequired(false)->withInput($values);
569 $res = $input2->getContent();
570
571 $this->assertInstanceOf(Result::class, $res);
572 $this->assertFalse($res->isError());
573 $this->assertEquals($value, $res->value());
574 }
575}
An exception for terminatinating execution or to throw for unit testing.
__construct(array $values)
Definition: InputTest.php:60
getOr($name, $value)
Get a named value from the data and fallback to default if that name does not exist.
Definition: InputTest.php:79
$requirement_constraint
Definition: InputTest.php:25
isClientSideValueOk($value)
Check if the value is good to be displayed client side.
Definition: InputTest.php:19
getConstraintForRequirement()
This may return a constraint that will be checked first if the field is required.
Definition: InputTest.php:28
getUpdateOnLoadCode()
Get update code.
Definition: InputTest.php:34
getNewName()
Generates a unique name on every call.
Definition: InputTest.php:46
This implements commonalities between inputs.
Definition: Input.php:24
Provides common functionality for UI tests.
Definition: Base.php:225
Test on input implementation.
Definition: InputTest.php:96
test_getContent()
Definition: InputTest.php:209
test_withInput_and_constraint_fails()
Definition: InputTest.php:323
test_withValue_throws()
Definition: InputTest.php:173
test_withLabel()
Definition: InputTest.php:125
test_withByline()
Definition: InputTest.php:134
test_withInput_transformation_and_constraint_different_order()
Definition: InputTest.php:404
test_constructor()
Definition: InputTest.php:118
test_withInput_constraint_fails_and_transformation()
Definition: InputTest.php:467
test_withInput_constraint_and_transformation()
Definition: InputTest.php:437
test_withRequired()
Definition: InputTest.php:143
test_withInput_requirement_constraint()
Definition: InputTest.php:531
test_only_run_withInput_with_name()
Definition: InputTest.php:236
test_withInput_and_constraint_fails_different_order()
Definition: InputTest.php:346
test_withValue()
Definition: InputTest.php:163
test_withInput_and_transformation_different_order()
Definition: InputTest.php:273
test_withInput_toggle_requirement()
Definition: InputTest.php:556
test_withInput_transformation_and_constraint()
Definition: InputTest.php:374
test_withInput_constraint_fails_and_transformation_different_order()
Definition: InputTest.php:501
test_withDisabled()
Definition: InputTest.php:153
test_withName()
Definition: InputTest.php:188
test_withInput_and_constraint_successfull()
Definition: InputTest.php:300
test_withInput_and_transformation()
Definition: InputTest.php:249
test_withError()
Definition: InputTest.php:199
test_withInput()
Definition: InputTest.php:217
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:13
Describes a source for input names.
Definition: NameSource.php:11
if($format !==null) $name
Definition: metadata.php:230
foreach($_POST as $key=> $value) $res