ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
TextInputTest.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");
23require_once(__DIR__ . "/InputTest.php");
24require_once(__DIR__ . "/CommonFieldRendering.php");
25
29use ILIAS\Data;
30use ILIAS\Refinery\Factory as Refinery;
31
33{
34 use CommonFieldRendering;
35
37
38 public function setUp(): void
39 {
40 $this->name_source = new DefNamesource();
41 }
42
43 public function testImplementsFactoryInterface(): void
44 {
45 $f = $this->getFieldFactory();
46
47 $text = $f->text("label", "byline");
48
49 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $text);
50 $this->assertInstanceOf(Field\Text::class, $text);
51 }
52
53 public function testRender(): void
54 {
55 $f = $this->getFieldFactory();
56 $label = "label";
57 $byline = "byline";
58 $text = $f->text($label, $byline)->withNameFrom($this->name_source);
59 $expected = $this->getFormWrappedHtml(
60 'text-field-input',
61 $label,
62 '<input id="id_1" type="text" name="name_0" class="c-field-text" />',
63 $byline,
64 'id_1'
65 );
66 $this->assertEquals($expected, $this->render($text));
67 }
68
69 public function testCommonRendering(): void
70 {
71 $f = $this->getFieldFactory();
72 $label = "label";
73 $text = $f->text($label)->withNameFrom($this->name_source);
74
75 $this->testWithError($text);
76 $this->testWithNoByline($text);
77 $this->testWithRequired($text);
78 $this->testWithDisabled($text);
79 $this->testWithAdditionalOnloadCodeRendersId($text);
80 }
81
82 public function testRenderValue(): void
83 {
84 $f = $this->getFieldFactory();
85 $label = "label";
86 $value = "value";
87 $text = $f->text($label)->withValue($value)->withNameFrom($this->name_source);
88 $expected = $this->getFormWrappedHtml(
89 'text-field-input',
90 $label,
91 '<input id="id_1" type="text" value="value" name="name_0" class="c-field-text" />',
92 null,
93 'id_1'
94 );
95 $this->assertEquals($expected, $this->render($text));
96 }
97
98 public function testMaxLength(): void
99 {
100 $f = $this->getFieldFactory();
101
102 $text = $f->text("")
103 ->withMaxLength(4);
104
105 $this->assertEquals(4, $text->getMaxLength());
106
107 $text1 = $text->withValue("1234");
108 $this->assertEquals("1234", $text1->getValue());
109
110 $this->expectException(InvalidArgumentException::class);
111 $this->expectExceptionMessage("Argument 'value': Display value does not match input type.");
112 $text->withValue("12345");
113 }
114
115 public function testRenderMaxValue(): void
116 {
117 $f = $this->getFieldFactory();
118 $label = "label";
119 $text = $f->text($label)->withNameFrom($this->name_source)->withMaxLength(8);
120 $expected = $this->getFormWrappedHtml(
121 'text-field-input',
122 $label,
123 '<input id="id_1" type="text" name="name_0" maxlength="8" class="c-field-text" />',
124 null,
125 'id_1'
126 );
127 $this->assertEquals($expected, $this->render($text));
128 }
129
130 public function testValueRequired(): void
131 {
132 $f = $this->getFieldFactory();
133 $label = "label";
134 $name = "name_0";
135 $text = $f->text($label)->withNameFrom($this->name_source)->withRequired(true);
136
137 $text1 = $text->withInput(new DefInputData([$name => "0"]));
138 $value1 = $text1->getContent();
139 $this->assertTrue($value1->isOk());
140 $this->assertEquals("0", $value1->value());
141
142 $text2 = $text->withInput(new DefInputData([$name => ""]));
143 $value2 = $text2->getContent();
144 $this->assertTrue($value2->isError());
145 }
146
147 public function testStripsTags(): void
148 {
149 $f = $this->getFieldFactory();
150 $name = "name_0";
151 $text = $f->text("")
152 ->withNameFrom($this->name_source)
153 ->withInput(new DefInputData([$name => "<script>alert()</script>"]));
154
155 $content = $text->getContent();
156 $this->assertEquals("alert()", $content->value());
157 }
158
159 public function testWithoutStripsTags(): void
160 {
161 $f = $this->getFieldFactory();
162 $name = "name_0";
163 $text = $f->text("")
164 ->withNameFrom($this->name_source)
165 ->withoutStripTags()
166 ->withInput(new DefInputData([$name => "<script>alert()</script>"]));
167
168 $content = $text->getContent();
169 $this->assertEquals("<script>alert()</script>", $content->value());
170 }
171}
Builds data types.
Definition: Factory.php:36
Definition: UI.php:24
Provides common functionality for UI tests.
Definition: Base.php:337
DefNamesource $name_source
testImplementsFactoryInterface()
This describes commonalities between all inputs.
Definition: Input.php:47
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.