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