ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
InputTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../Base.php");
23
27use ILIAS\Data\Factory as DataFactory;
30use ILIAS\Refinery\Factory as Refinery;
31
32class DefInput extends FormInput
33{
34 public bool $value_ok = true;
36
37 public function isClientSideValueOk($value): bool
38 {
39 return $this->value_ok;
40 }
41
43 {
45 }
46
47 public function getUpdateOnLoadCode(): Closure
48 {
49 return function (): void {
50 };
51 }
52}
53
54class DefNamesource implements NameSource
55{
56 public int $count = 0;
57
58 public function getNewName(): string
59 {
60 $name = "name_{$this->count}";
61 $this->count++;
62
63 return $name;
64 }
65
66 public function getNewDedicatedName($dedicated_name = 'dedicated_name'): string
67 {
68 $name = $dedicated_name . "_{$this->count}";
69 $this->count++;
70
71 return $name;
72 }
73}
74
75class DefInputData implements InputData
76{
77 public array $values = array();
78
79 public function __construct(array $values)
80 {
81 $this->values = $values;
82 }
83
87 public function get(string $name)
88 {
89 if (!is_string($name)) {
90 throw new InvalidArgumentException('$name is no string.');
91 }
92 if (!isset($this->values[$name])) {
93 throw new LogicException("'$name' does not exist.");
94 }
95
96 return $this->values[$name];
97 }
98
102 public function getOr(string $name, $default)
103 {
104 if (!is_string($name)) {
105 throw new InvalidArgumentException('$name is no string.');
106 }
107 if (!isset($this->values[$name])) {
108 return $default;
109 }
110
111 return $this->values[$name];
112 }
113
114 public function has($name): bool
115 {
116 return array_key_exists($name, $this->values);
117 }
118}
119
124{
125 protected DataFactory $data_factory;
127 protected DefInput $input;
131
132 public function setUp(): void
133 {
134 $this->data_factory = new DataFactory();
135 $language = $this->createMock(ILIAS\Language\Language::class);
136 $this->refinery = new Refinery($this->data_factory, $language);
137 $this->input = new DefInput(
138 $this->data_factory,
139 $this->refinery,
140 "label",
141 "byline"
142 );
143 $this->named_input = $this->input->withDedicatedName('dedicated_name');
144 $this->name_source = new DefNamesource();
145 }
146
147 public function testConstructor(): void
148 {
149 $this->assertEquals("label", $this->input->getLabel());
150 $this->assertEquals("byline", $this->input->getByline());
151 }
152
153 public function testWithLabel(): void
154 {
155 $label = "new label";
156 $input = $this->input->withLabel($label);
157 $this->assertEquals($label, $input->getLabel());
158 $this->assertNotSame($this->input, $input);
159 }
160
161 public function testWithByline(): void
162 {
163 $byline = "new byline";
164 $input = $this->input->withByline($byline);
165 $this->assertEquals($byline, $input->getByline());
166 $this->assertNotSame($this->input, $input);
167 }
168
169 public function testWithRequired(): void
170 {
171 $this->assertFalse($this->input->isRequired());
172 $input = $this->input->withRequired(true);
173 $this->assertTrue($input->isRequired());
174 $input = $input->withRequired(false);
175 $this->assertFalse($input->isRequired());
176 }
177
179 {
180 $custom_constraint = $this->refinery->custom()->constraint(
181 function ($value) {
182 return (substr($value, 0, 1) === 'H') ? true : false;
183 },
184 "Your name does not start with an H"
185 );
186 $input = $this->input->withRequired(true, $custom_constraint);
187 $this->assertTrue($input->isRequired());
188 $this->assertEquals($input->requirement_constraint, $custom_constraint);
189 }
190
191 public function testWithDisabled(): void
192 {
193 $this->assertFalse($this->input->isDisabled());
194 $input = $this->input->withDisabled(true);
195 $this->assertTrue($input->isDisabled());
196 $input = $input->withDisabled(false);
197 $this->assertFalse($input->isDisabled());
198 }
199
200 public function testWithValue(): void
201 {
202 $value = "some value";
203 $input = $this->input->withValue($value);
204 $this->assertEquals(null, $this->input->getValue());
205 $this->assertEquals($value, $input->getValue());
206 $this->assertNotSame($this->input, $input);
207 }
208
209 public function testWithValueThrows(): void
210 {
211 $this->input->value_ok = false;
212 $raised = false;
213 try {
214 $this->input->withValue("foo");
215 $this->assertFalse("This should not happen.");
216 } catch (InvalidArgumentException $e) {
217 $raised = true;
218 }
219 $this->assertTrue($raised);
220 $this->assertEquals(null, $this->input->getValue());
221 }
222
223 public function testWithName(): void
224 {
225 $name = "name_0";
226 $input = $this->input->withNameFrom($this->name_source);
227 $this->assertEquals(null, $this->input->getName());
228 $this->assertEquals($name, $input->getName());
229 $this->assertNotSame($this->input, $input);
230 $this->assertEquals(1, $this->name_source->count);
231 }
232
233 public function testWithNameForNamedInput(): void
234 {
235 $name = "dedicated_name_0";
236 $input = $this->named_input->withNameFrom($this->name_source);
237 $this->assertEquals(null, $this->named_input->getName());
238 $this->assertEquals($name, $input->getName());
239 $this->assertNotSame($this->named_input, $input);
240 $this->assertEquals(1, $this->name_source->count);
241 }
242
243 public function testWithError(): void
244 {
245 $error = "error";
246 $input = $this->input->withError($error);
247 $this->assertEquals(null, $this->input->getError());
248 $this->assertEquals($error, $input->getError());
249 $this->assertNotSame($this->input, $input);
250 }
251
252 public function testGetContent(): void
253 {
254 $this->expectException(LogicException::class);
255
256 $this->input->getContent();
257 }
258
259 public function testWithInput(): void
260 {
261 $name = "name_0";
262 $value = "valu";
263 $input = $this->input->withNameFrom($this->name_source);
264 $values = new DefInputData([$name => $value]);
265
266 $input2 = $input->withInput($values);
267 $res = $input2->getContent();
268
269 $this->assertInstanceOf(Result::class, $res);
270 $this->assertTrue($res->isOk());
271 $this->assertEquals($value, $res->value());
272
273 $this->assertNotSame($input, $input2);
274 $this->assertEquals($value, $input2->getValue());
275 }
276
277 public function testOnlyRunWithInputWithName(): void
278 {
279 $raised = false;
280 try {
281 $this->input->withInput(new DefInputData([]));
282 $this->assertFalse("This should not happen.");
283 } catch (LogicException $e) {
284 $raised = true;
285 }
286 $this->assertTrue($raised);
287 }
288
289 public function testWithInputAndTransformation(): void
290 {
291 $name = "name_0";
292 $value = "value";
293 $transform_to = "other value";
294 $input = $this->input->withNameFrom($this->name_source);
295 $values = new DefInputData([$name => $value]);
296
298 $this->refinery->custom()->transformation(function ($v) use ($value, $transform_to): string {
299 $this->assertEquals($value, $v);
300 return $transform_to;
301 })
302 )->withInput($values);
303
304 $res = $input2->getContent();
305
306 $this->assertInstanceOf(Result::class, $res);
307 $this->assertTrue($res->isOk());
308 $this->assertEquals($transform_to, $res->value());
309
310 $this->assertNotSame($input, $input2);
311 $this->assertEquals($value, $input2->getValue());
312 }
313
315 {
316 $name = "name_0";
317 $value = "value";
318 $transform_to = "other value";
319 $input = $this->input->withNameFrom($this->name_source);
320 $values = new DefInputData([$name => $value]);
321
322 $input2 = $input->withInput($values)->withAdditionalTransformation(
323 $this->refinery->custom()->transformation(function ($v) use ($value, $transform_to): string {
324 $this->assertEquals($value, $v);
325 return $transform_to;
326 })
327 );
328
329 $res = $input2->getContent();
330
331 $this->assertInstanceOf(Result::class, $res);
332 $this->assertTrue($res->isOk());
333 $this->assertEquals($transform_to, $res->value());
334
335 $this->assertNotSame($input, $input2);
336 $this->assertEquals($value, $input2->getValue());
337 }
338
340 {
341 $name = "name_0";
342 $value = "value";
343 $error = "an error";
344 $input = $this->input->withNameFrom($this->name_source);
345 $values = new DefInputData([$name => $value]);
346
347 $input2 = $input->withAdditionalTransformation($this->refinery->custom()->constraint(function () {
348 return true;
349 }, $error))->withInput($values);
350 $res = $input2->getContent();
351
352 $this->assertInstanceOf(Result::class, $res);
353 $this->assertTrue($res->isOk());
354 $this->assertEquals($value, $res->value());
355
356 $this->assertNotSame($input, $input2);
357 $this->assertEquals($value, $input2->getValue());
358 $this->assertEquals(null, $input2->getError());
359 }
360
361 public function testWithInputAndConstraintFails(): void
362 {
363 $name = "name_0";
364 $value = "value";
365 $error = "an error";
366 $input = $this->input->withNameFrom($this->name_source);
367 $values = new DefInputData([$name => $value]);
368
369 $input2 = $input->withAdditionalTransformation($this->refinery->custom()->constraint(function () {
370 return false;
371 }, $error))->withInput($values);
372 $res = $input2->getContent();
373
374 $this->assertInstanceOf(Result::class, $res);
375 $this->assertTrue($res->isError());
376 $this->assertEquals($error, $res->error());
377
378 $this->assertNotSame($input, $input2);
379 $this->assertEquals($value, $input2->getValue());
380 $this->assertEquals($error, $input2->getError());
381 }
382
384 {
385 $rc = $this->refinery->custom();
386
387 $name = "name_0";
388 $value = "value";
389 $error = "an error";
390 $input = $this->input->withNameFrom($this->name_source);
391 $values = new DefInputData([$name => $value]);
392
393 $input2 = $input
394 ->withInput($values)
395 ->withAdditionalTransformation($rc->constraint(function () {
396 return false;
397 }, $error));
398
399 $res = $input2->getContent();
400
401 $this->assertInstanceOf(Result::class, $res);
402 $this->assertTrue($res->isError());
403 $this->assertEquals($error, $res->error());
404
405 $this->assertNotSame($input, $input2);
406 $this->assertEquals($value, $input2->getValue());
407 $this->assertEquals($error, $input2->getError());
408 }
409
411 {
412 $name = "name_0";
413 $value = "value";
414 $transform_to = "other value";
415 $error = "an error";
416 $input = $this->input->withNameFrom($this->name_source);
417 $values = new DefInputData([$name => $value]);
418
420 $this->refinery->custom()->transformation(function ($v) use ($value, $transform_to): string {
421 $this->assertEquals($value, $v);
422 return $transform_to;
423 })
424 )->withAdditionalTransformation(
425 $this->refinery->custom()->constraint(function ($v) use ($transform_to): bool {
426 $this->assertEquals($transform_to, $v);
427 return true;
428 }, $error)
429 )->withInput($values);
430
431 $res = $input2->getContent();
432
433 $this->assertInstanceOf(Result::class, $res);
434 $this->assertTrue($res->isOk());
435 $this->assertEquals($transform_to, $res->value());
436
437 $this->assertNotSame($input, $input2);
438 $this->assertEquals($value, $input2->getValue());
439 $this->assertEquals(null, $input2->getError());
440 }
441
443 {
444 $name = "name_0";
445 $value = "value";
446 $transform_to = "other value";
447 $error = "an error";
448 $input = $this->input->withNameFrom($this->name_source);
449 $values = new DefInputData([$name => $value]);
450
451 $input2 = $input->withInput($values)->withAdditionalTransformation(
452 $this->refinery->custom()->transformation(function ($v) use ($value, $transform_to): string {
453 $this->assertEquals($value, $v);
454 return $transform_to;
455 })
456 )->withAdditionalTransformation(
457 $this->refinery->custom()->constraint(function ($v) use ($transform_to): bool {
458 $this->assertEquals($transform_to, $v);
459 return true;
460 }, $error)
461 );
462
463 $res = $input2->getContent();
464
465 $this->assertInstanceOf(Result::class, $res);
466 $this->assertTrue($res->isOk());
467 $this->assertEquals($transform_to, $res->value());
468
469 $this->assertNotSame($input, $input2);
470 $this->assertEquals($value, $input2->getValue());
471 $this->assertEquals(null, $input2->getError());
472 }
473
475 {
476 $name = "name_0";
477 $value = "value";
478 $transform_to = "other value";
479 $error = "an error";
480 $input = $this->input->withNameFrom($this->name_source);
481 $values = new DefInputData([$name => $value]);
482
484 $this->refinery->custom()->constraint(function ($v) use ($value): bool {
485 $this->assertEquals($value, $v);
486 return true;
487 }, $error)
488 )->withAdditionalTransformation(
489 $this->refinery->custom()->transformation(function ($v) use ($value, $transform_to): string {
490 $this->assertEquals($value, $v);
491 return $transform_to;
492 })
493 )->withInput($values);
494
495 $res = $input2->getContent();
496
497 $this->assertInstanceOf(Result::class, $res);
498 $this->assertTrue($res->isOk());
499 $this->assertEquals($transform_to, $res->value());
500
501 $this->assertNotSame($input, $input2);
502 $this->assertEquals($value, $input2->getValue());
503 $this->assertEquals(null, $input2->getError());
504 }
505
507 {
508 $rc = $this->refinery->custom();
509
510 $name = "name_0";
511 $value = "value";
512 $transform_to = "other value";
513 $error = "an error";
514 $input = $this->input->withNameFrom($this->name_source);
515 $values = new DefInputData([$name => $value]);
516
517 $input2 = $input
518 ->withAdditionalTransformation($rc->constraint(function ($v) use ($value): bool {
519 $this->assertEquals($value, $v);
520
521 return false;
522 }, $error))
523 ->withAdditionalTransformation($rc->transformation(function () use ($value, $transform_to): string {
524 $this->assertFalse("This should not happen");
525
526 return $transform_to;
527 }))->withInput($values);
528 $res = $input2->getContent();
529
530 $this->assertInstanceOf(Result::class, $res);
531 $this->assertTrue($res->isError());
532 $this->assertEquals($error, $res->error());
533
534 $this->assertNotSame($input, $input2);
535 $this->assertEquals($value, $input2->getValue());
536 $this->assertEquals($error, $input2->getError());
537 }
538
540 {
541 $name = "name_0";
542 $value = "value";
543 $transform_to = "other value";
544 $error = "an error";
545 $input = $this->input->withNameFrom($this->name_source);
546 $values = new DefInputData([$name => $value]);
547
548 $input2 = $input->withInput($values)->withAdditionalTransformation(
549 $this->refinery->custom()->constraint(function ($v) use ($value): bool {
550 $this->assertEquals($value, $v);
551 return false;
552 }, $error)
553 )->withAdditionalTransformation(
554 $this->refinery->custom()->transformation(function () use ($value, $transform_to): string {
555 $this->assertFalse("This should not happen");
556 return $transform_to;
557 })
558 );
559
560 $res = $input2->getContent();
561
562 $this->assertInstanceOf(Result::class, $res);
563 $this->assertTrue($res->isError());
564 $this->assertEquals($error, $res->error());
565
566 $this->assertNotSame($input, $input2);
567 $this->assertEquals($value, $input2->getValue());
568 $this->assertEquals($error, $input2->getError());
569 }
570
572 {
573 $name = "name_0";
574 $value = "Adam";
575 $error = "Your name does not start with an H";
576 $input = $this->input->withNameFrom($this->name_source);
577 $values = new DefInputData([$name => $value]);
578 $custom_constraint = $this->refinery->custom()->constraint(
579 function ($value) {
580 return (substr($value, 0, 1) === 'H') ? true : false;
581 },
582 $error
583 );
584 $input2 = $input->withRequired(true, $custom_constraint)->withInput($values);
585 $res = $input2->getContent();
586 $this->assertInstanceOf(Result::class, $res);
587 $this->assertFalse($res->isOk());
588 $this->assertEquals($error, $input2->getError());
589 }
590
591 public function testWithInputToggleRequirement(): void
592 {
593 $name = "name_0";
594 $value = "value";
595 $error = "an error";
596 $input = $this->input->withNameFrom($this->name_source);
597 $values = new DefInputData([$name => $value]);
598
599 $custom_constraint = $this->refinery->custom()->constraint(function () {
600 return false;
601 }, $error);
602
603 $input2 = $input->withRequired(true, $custom_constraint)->withRequired(false)->withInput($values);
604 $res = $input2->getContent();
605
606 $this->assertInstanceOf(Result::class, $res);
607 $this->assertFalse($res->isError());
608 $this->assertEquals($value, $res->value());
609 }
610}
getOr(string $name, $default)
@ineritdoc
Definition: InputTest.php:102
__construct(array $values)
Definition: InputTest.php:79
array $values
Definition: InputTest.php:77
has($name)
Definition: InputTest.php:114
isClientSideValueOk($value)
Check if the value is good to be displayed client side.
Definition: InputTest.php:37
Constraint $requirement_constraint
Definition: InputTest.php:35
getConstraintForRequirement()
This may return a constraint that will be checked first if the field is required.
Definition: InputTest.php:42
bool $value_ok
Definition: InputTest.php:34
getUpdateOnLoadCode()
Get update code.
Definition: InputTest.php:47
getNewName()
Generates a unique name on every call.
Definition: InputTest.php:58
getNewDedicatedName($dedicated_name='dedicated_name')
Definition: InputTest.php:66
Builds data types.
Definition: Factory.php:36
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.static
Definition: FormInput.php:139
withByline(string $byline)
Get an input like this, but with an additional/replaced label.static
Definition: FormInput.php:102
withLabel(string $label)
Get an input like this, but with a replaced label.static
Definition: FormInput.php:84
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Get an input like this, but set the field to be required (or not).With the optional $required_constra...
Definition: FormInput.php:120
getName()
The name of the input as used in HTML.
Definition: Input.php:192
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Input.php:101
withAdditionalTransformation(Transformation $trafo)
Apply a transformation to the current or future content.
Definition: Input.php:145
getValue()
Get the value that is displayed in the input client side.
Definition: Input.php:89
withNameFrom(NameSource $source, ?string $parent_name=null)
Definition: Input.php:200
withError(string $error)
Get an input like this one, with a different error.
Definition: Input.php:127
getError()
The error of the input as used in HTML.
Definition: Input.php:119
Provides common functionality for UI tests.
Definition: Base.php:337
Test on field implementation.
Definition: InputTest.php:124
DefInput $dedicated_input
Definition: InputTest.php:128
testWithInput()
Definition: InputTest.php:259
testWithError()
Definition: InputTest.php:243
testGetContent()
Definition: InputTest.php:252
testOnlyRunWithInputWithName()
Definition: InputTest.php:277
testWithInputTransformationAndConstraint()
Definition: InputTest.php:410
testWithName()
Definition: InputTest.php:223
testWithInputRequirementConstraint()
Definition: InputTest.php:571
testWithNameForNamedInput()
Definition: InputTest.php:233
testWithInputToggleRequirement()
Definition: InputTest.php:591
testWithInputConstraintFailsAndTransformationDifferentOrder()
Definition: InputTest.php:539
testWithInputAndConstraintFailsDifferentOrder()
Definition: InputTest.php:383
testWithInputAndConstraintFails()
Definition: InputTest.php:361
testWithByline()
Definition: InputTest.php:161
testWithRequiredAndCustomConstraint()
Definition: InputTest.php:178
testWithInputAndTransformationDifferentOrder()
Definition: InputTest.php:314
testWithLabel()
Definition: InputTest.php:153
Refinery $refinery
Definition: InputTest.php:126
testWithInputAndTransformation()
Definition: InputTest.php:289
DataFactory $data_factory
Definition: InputTest.php:125
testWithValueThrows()
Definition: InputTest.php:209
testWithInputConstraintAndTransformation()
Definition: InputTest.php:474
testWithInputConstraintFailsAndTransformation()
Definition: InputTest.php:506
DefInput $input
Definition: InputTest.php:127
testWithDisabled()
Definition: InputTest.php:191
testWithRequired()
Definition: InputTest.php:169
testConstructor()
Definition: InputTest.php:147
testWithInputTransformationAndConstraintDifferentOrder()
Definition: InputTest.php:442
testWithInputAndConstraintSuccessfull()
Definition: InputTest.php:339
DefNamesource $name_source
Definition: InputTest.php:129
FormInput $named_input
Definition: InputTest.php:130
testWithValue()
Definition: InputTest.php:200
A constraint encodes some resrtictions on values.
Definition: Constraint.php:32
This describes inputs that can be used in forms.
Definition: FormInput.php:33
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:30
Describes a source for input names.
Definition: NameSource.php:27
$res
Definition: ltiservices.php:69
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.