5 require_once(__DIR__ .
"/../../../../../libs/composer/vendor/autoload.php");
6 require_once(__DIR__ .
"/../../../Base.php");
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;
12 use \ILIAS\Transformation\Factory as TransformationFactory;
13 use \ILIAS\Validation\Factory as ValidationFactory;
14 use \ILIAS\Data\Result;
43 $name =
"name_{$this->count}";
63 if (!is_string(
$name)) {
64 throw new \InvalidArgumentException(
'$name is no string.');
66 if (!isset($this->values[
$name])) {
67 throw new \LogicException(
"'$name' does not exist.");
70 return $this->values[
$name];
76 if (!is_string(
$name)) {
77 throw new \InvalidArgumentException(
'$name is no string.');
79 if (!isset($this->values[
$name])) {
83 return $this->values[
$name];
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");
104 $this->assertEquals(
"label", $this->
input->getLabel());
105 $this->assertEquals(
"byline", $this->
input->getByline());
129 $this->assertFalse($this->
input->isRequired());
131 $this->assertTrue($input->isRequired());
132 $input = $input->withRequired(
false);
133 $this->assertFalse($input->isRequired());
141 $this->assertEquals(null, $this->
input->getValue());
149 $this->
input->value_ok =
false;
152 $this->
input->withValue(
"foo");
153 $this->assertFalse(
"This should not happen.");
157 $this->assertTrue($raised);
158 $this->assertEquals(null, $this->
input->getValue());
165 $input = $this->
input->withNameFrom($this->name_source);
166 $this->assertEquals(null, $this->
input->getName());
169 $this->assertEquals(1, $this->name_source->count);
177 $this->assertEquals(null, $this->
input->getError());
185 $this->expectException(\LogicException::class);
187 $this->
input->getContent();
195 $input = $this->
input->withNameFrom($this->name_source);
199 $res = $input2->getContent();
201 $this->assertInstanceOf(Result::class,
$res);
202 $this->assertTrue(
$res->isOk());
205 $this->assertNotSame(
$input, $input2);
206 $this->assertEquals(
$value, $input2->getValue());
215 $this->assertFalse(
"This should not happen.");
219 $this->assertTrue($raised);
227 $transform_to =
"other value";
228 $input = $this->
input->withNameFrom($this->name_source);
231 $input2 =
$input->withAdditionalTransformation($this->transformation_factory->custom(
function ($v) use (
$value, $transform_to) {
232 $this->assertEquals(
$value, $v);
234 return $transform_to;
236 $res = $input2->getContent();
238 $this->assertInstanceOf(Result::class,
$res);
239 $this->assertTrue(
$res->isOk());
240 $this->assertEquals($transform_to,
$res->value());
242 $this->assertNotSame(
$input, $input2);
243 $this->assertEquals(
$value, $input2->getValue());
251 $transform_to =
"other value";
252 $input = $this->
input->withNameFrom($this->name_source);
255 $input2 =
$input->withInput(
$values)->withAdditionalTransformation($this->transformation_factory->custom(
function ($v) use (
259 $this->assertEquals(
$value, $v);
261 return $transform_to;
263 $res = $input2->getContent();
265 $this->assertInstanceOf(Result::class,
$res);
266 $this->assertTrue(
$res->isOk());
267 $this->assertEquals($transform_to,
$res->value());
269 $this->assertNotSame(
$input, $input2);
270 $this->assertEquals(
$value, $input2->getValue());
279 $input = $this->
input->withNameFrom($this->name_source);
282 $input2 =
$input->withAdditionalConstraint($this->validation_factory->custom(
function ($_) {
285 $res = $input2->getContent();
287 $this->assertInstanceOf(Result::class,
$res);
288 $this->assertTrue(
$res->isOk());
291 $this->assertNotSame(
$input, $input2);
292 $this->assertEquals(
$value, $input2->getValue());
293 $this->assertEquals(null, $input2->getError());
302 $input = $this->
input->withNameFrom($this->name_source);
305 $input2 =
$input->withAdditionalConstraint($this->validation_factory->custom(
function ($_) {
308 $res = $input2->getContent();
310 $this->assertInstanceOf(Result::class,
$res);
311 $this->assertTrue(
$res->isError());
314 $this->assertNotSame(
$input, $input2);
315 $this->assertEquals(
$value, $input2->getValue());
316 $this->assertEquals(
$error, $input2->getError());
325 $input = $this->
input->withNameFrom($this->name_source);
328 $input2 =
$input->withInput(
$values)->withAdditionalConstraint($this->validation_factory->custom(
function ($_) {
331 $res = $input2->getContent();
333 $this->assertInstanceOf(Result::class,
$res);
334 $this->assertTrue(
$res->isError());
337 $this->assertNotSame(
$input, $input2);
338 $this->assertEquals(
$value, $input2->getValue());
339 $this->assertEquals(
$error, $input2->getError());
347 $transform_to =
"other value";
349 $input = $this->
input->withNameFrom($this->name_source);
352 $input2 =
$input->withAdditionalTransformation($this->transformation_factory->custom(
function ($v) use (
$value, $transform_to) {
353 $this->assertEquals(
$value, $v);
355 return $transform_to;
356 }))->withAdditionalConstraint($this->validation_factory->custom(
function ($v) use ($transform_to) {
357 $this->assertEquals($transform_to, $v);
361 $res = $input2->getContent();
363 $this->assertInstanceOf(Result::class,
$res);
364 $this->assertTrue(
$res->isOk());
365 $this->assertEquals($transform_to,
$res->value());
367 $this->assertNotSame(
$input, $input2);
368 $this->assertEquals(
$value, $input2->getValue());
369 $this->assertEquals(null, $input2->getError());
377 $transform_to =
"other value";
379 $input = $this->
input->withNameFrom($this->name_source);
382 $input2 =
$input->withInput(
$values)->withAdditionalTransformation($this->transformation_factory->custom(
function ($v) use (
386 $this->assertEquals(
$value, $v);
388 return $transform_to;
389 }))->withAdditionalConstraint($this->validation_factory->custom(
function ($v) use ($transform_to) {
390 $this->assertEquals($transform_to, $v);
394 $res = $input2->getContent();
396 $this->assertInstanceOf(Result::class,
$res);
397 $this->assertTrue(
$res->isOk());
398 $this->assertEquals($transform_to,
$res->value());
400 $this->assertNotSame(
$input, $input2);
401 $this->assertEquals(
$value, $input2->getValue());
402 $this->assertEquals(null, $input2->getError());
410 $transform_to =
"other value";
412 $input = $this->
input->withNameFrom($this->name_source);
415 $input2 =
$input->withAdditionalConstraint($this->validation_factory->custom(
function ($v) use (
$value) {
416 $this->assertEquals(
$value, $v);
419 },
$error))->withAdditionalTransformation($this->transformation_factory->custom(
function ($v) use (
$value, $transform_to) {
420 $this->assertEquals(
$value, $v);
422 return $transform_to;
424 $res = $input2->getContent();
426 $this->assertInstanceOf(Result::class,
$res);
427 $this->assertTrue(
$res->isOk());
428 $this->assertEquals($transform_to,
$res->value());
430 $this->assertNotSame(
$input, $input2);
431 $this->assertEquals(
$value, $input2->getValue());
432 $this->assertEquals(null, $input2->getError());
440 $transform_to =
"other value";
442 $input = $this->
input->withNameFrom($this->name_source);
445 $input2 =
$input->withAdditionalConstraint($this->validation_factory->custom(
function ($v) use (
$value) {
446 $this->assertEquals(
$value, $v);
449 },
$error))->withAdditionalTransformation($this->transformation_factory->custom(
function ($v) use (
$value, $transform_to) {
450 $this->assertFalse(
"This should not happen");
452 return $transform_to;
454 $res = $input2->getContent();
456 $this->assertInstanceOf(Result::class,
$res);
457 $this->assertTrue(
$res->isError());
460 $this->assertNotSame(
$input, $input2);
461 $this->assertEquals(
$value, $input2->getValue());
462 $this->assertEquals(
$error, $input2->getError());
470 $transform_to =
"other value";
472 $input = $this->
input->withNameFrom($this->name_source);
476 $this->assertEquals(
$value, $v);
479 },
$error))->withAdditionalTransformation($this->transformation_factory->custom(
function ($v) use (
$value, $transform_to) {
480 $this->assertFalse(
"This should not happen");
482 return $transform_to;
484 $res = $input2->getContent();
486 $this->assertInstanceOf(Result::class,
$res);
487 $this->assertTrue(
$res->isError());
490 $this->assertNotSame(
$input, $input2);
491 $this->assertEquals(
$value, $input2->getValue());
492 $this->assertEquals(
$error, $input2->getError());
501 $input = $this->
input->withNameFrom($this->name_source);
504 $input->requirement_constraint = $this->validation_factory->custom(
function ($_) {
509 $res = $input2->getContent();
511 $this->assertInstanceOf(Result::class,
$res);
512 $this->assertTrue(
$res->isError());
515 $this->assertNotSame(
$input, $input2);
516 $this->assertEquals(
$value, $input2->getValue());
517 $this->assertEquals(
$error, $input2->getError());
526 $input = $this->
input->withNameFrom($this->name_source);
529 $input->requirement_constraint = $this->validation_factory->custom(
function ($_) {
533 $input2 =
$input->withRequired(
true)->withRequired(
false)->withInput(
$values);
534 $res = $input2->getContent();
536 $this->assertInstanceOf(Result::class,
$res);
537 $this->assertFalse(
$res->isError());
foreach($_POST as $key=> $value) $res
Provides common functionality for UI tests.
Describes how Input-Elements want to interact with posted data.
__construct(array $values)
getOr($name, $value)
Get a named value from the data and fallback to default if that name does not exist.
getNewName()
Generates a unique name on every call.