ILIAS  release_8 Revision v8.24
TextInputTest.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
21require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../Base.php");
23require_once(__DIR__ . "/InputTest.php");
24
28use ILIAS\Data;
29use ILIAS\Refinery\Factory as Refinery;
30
32{
34
35 public function setUp(): void
36 {
37 $this->name_source = new DefNamesource();
38 }
39
40 protected function buildFactory(): I\Input\Field\Factory
41 {
42 $df = new Data\Factory();
43 $language = $this->createMock(ilLanguage::class);
44 return new I\Input\Field\Factory(
45 $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
46 new SignalGenerator(),
47 $df,
48 new Refinery($df, $language),
49 $language
50 );
51 }
52
53 public function test_implements_factory_interface(): void
54 {
55 $f = $this->buildFactory();
56
57 $text = $f->text("label", "byline");
58
59 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $text);
60 $this->assertInstanceOf(Field\Text::class, $text);
61 }
62
63 public function test_render(): void
64 {
65 $f = $this->buildFactory();
66 $label = "label";
67 $byline = "byline";
68 $text = $f->text($label, $byline)->withNameFrom($this->name_source);
69
70 $r = $this->getDefaultRenderer();
71 $html = $this->brutallyTrimHTML($r->render($text));
72
73 $expected = $this->brutallyTrimHTML('
74<div class="form-group row">
75 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
76 <div class="col-sm-8 col-md-9 col-lg-10">
77 <input id="id_1" type="text" name="name_0" class="form-control form-control-sm" />
78 <div class="help-block">byline</div>
79 </div>
80</div>
81');
82 $this->assertEquals($expected, $html);
83 }
84
85 public function test_render_error(): void
86 {
87 $f = $this->buildFactory();
88 $label = "label";
89 $byline = "byline";
90 $error = "an_error";
91 $text = $f->text($label, $byline)->withNameFrom($this->name_source)->withError($error);
92
93 $r = $this->getDefaultRenderer();
94 $html = $this->brutallyTrimHTML($r->render($text));
95
96 $expected = $this->brutallyTrimHTML('
97<div class="form-group row">
98 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
99 <div class="col-sm-8 col-md-9 col-lg-10">
100 <div class="help-block alert alert-danger" aria-describedby="id_1" role="alert">an_error</div>
101 <input id="id_1" type="text" name="name_0" class="form-control form-control-sm" />
102 <div class="help-block">byline</div>
103 </div>
104</div>
105');
106 $this->assertEquals($expected, $html);
107 }
108
109 public function test_render_no_byline(): void
110 {
111 $f = $this->buildFactory();
112 $label = "label";
113 $text = $f->text($label)->withNameFrom($this->name_source);
114
115 $r = $this->getDefaultRenderer();
116 $html = $this->brutallyTrimHTML($r->render($text));
117
118 $expected = $this->brutallyTrimHTML('
119<div class="form-group row">
120 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
121 <div class="col-sm-8 col-md-9 col-lg-10"><input id="id_1" type="text" name="name_0" class="form-control form-control-sm" /></div>
122</div>
123');
124 $this->assertEquals($expected, $html);
125 }
126
127 public function test_render_value(): void
128 {
129 $f = $this->buildFactory();
130 $label = "label";
131 $value = "value";
132 $text = $f->text($label)->withValue($value)->withNameFrom($this->name_source);
133
134 $r = $this->getDefaultRenderer();
135 $html = $this->brutallyTrimHTML($r->render($text));
136
137 $expected = $this->brutallyTrimHTML('
138<div class="form-group row">
139 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
140 <div class="col-sm-8 col-md-9 col-lg-10"><input id="id_1" type="text" value="value" name="name_0" class="form-control form-control-sm" /></div>
141</div>
142');
143 $this->assertEquals($expected, $html);
144 }
145
146 public function test_render_value_0(): void
147 {
148 $f = $this->buildFactory();
149 $label = "label";
150 $value = "0";
151 $text = $f->text($label)->withValue($value)->withNameFrom($this->name_source);
152
153 $r = $this->getDefaultRenderer();
154 $html = $this->brutallyTrimHTML($r->render($text));
155
156 $expected = $this->brutallyTrimHTML('
157<div class="form-group row">
158 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
159 <div class="col-sm-8 col-md-9 col-lg-10"><input id="id_1" type="text" value="0" name="name_0" class="form-control form-control-sm" /></div>
160</div>
161');
162 $this->assertEquals($expected, $html);
163 }
164
165 public function test_render_required(): void
166 {
167 $f = $this->buildFactory();
168 $label = "label";
169 $text = $f->text($label)->withNameFrom($this->name_source)->withRequired(true);
170
171 $r = $this->getDefaultRenderer();
172 $html = $this->brutallyTrimHTML($r->render($text));
173
174 $expected = $this->brutallyTrimHTML('
175<div class="form-group row">
176 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label<span class="asterisk">*</span></label>
177 <div class="col-sm-8 col-md-9 col-lg-10"><input id="id_1" type="text" name="name_0" class="form-control form-control-sm" /></div>
178</div>
179');
180 $this->assertEquals($expected, $html);
181 }
182
183 public function test_render_disabled(): void
184 {
185 $f = $this->buildFactory();
186 $label = "label";
187 $text = $f->text($label)->withNameFrom($this->name_source)->withDisabled(true);
188
189 $r = $this->getDefaultRenderer();
190 $html = $this->brutallyTrimHTML($r->render($text));
191
192 $expected = $this->brutallyTrimHTML('
193<div class="form-group row">
194 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
195 <div class="col-sm-8 col-md-9 col-lg-10"><input id="id_1" type="text" name="name_0" disabled="disabled" class="form-control form-control-sm" /></div>
196</div>
197');
198 $this->assertEquals($expected, $html);
199 }
200
201 public function test_max_length(): void
202 {
203 $f = $this->buildFactory();
204
205 $text = $f->text("")
206 ->withMaxLength(4);
207
208 $this->assertEquals(4, $text->getMaxLength());
209
210 $text1 = $text->withValue("1234");
211 $this->assertEquals("1234", $text1->getValue());
212
213 $this->expectException(InvalidArgumentException::class);
214 $this->expectExceptionMessage("Argument 'value': Display value does not match input type.");
215 $text->withValue("12345");
216 }
217
218 public function test_render_max_value(): void
219 {
220 $f = $this->buildFactory();
221 $label = "label";
222 $text = $f->text($label)->withNameFrom($this->name_source)->withMaxLength(8);
223
224 $r = $this->getDefaultRenderer();
225 $html = $this->brutallyTrimHTML($r->render($text));
226
227 $expected = $this->brutallyTrimHTML('
228<div class="form-group row">
229 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
230 <div class="col-sm-8 col-md-9 col-lg-10"> <input id="id_1" type="text" name="name_0" maxlength="8" class="form-control form-control-sm" /> </div>
231</div>
232');
233 $this->assertEquals($expected, $html);
234 }
235
236 public function test_value_required(): void
237 {
238 $f = $this->buildFactory();
239 $label = "label";
240 $name = "name_0";
241 $text = $f->text($label)->withNameFrom($this->name_source)->withRequired(true);
242
243 $text1 = $text->withInput(new DefInputData([$name => "0"]));
244 $value1 = $text1->getContent();
245 $this->assertTrue($value1->isOk());
246 $this->assertEquals("0", $value1->value());
247
248 $text2 = $text->withInput(new DefInputData([$name => ""]));
249 $value2 = $text2->getContent();
250 $this->assertTrue($value2->isError());
251 }
252
253 public function test_stripsTags(): void
254 {
255 $f = $this->buildFactory();
256 $name = "name_0";
257 $text = $f->text("")
258 ->withNameFrom($this->name_source)
259 ->withInput(new DefInputData([$name => "<script>alert()</script>"]));
260
261 $content = $text->getContent();
262 $this->assertEquals("alert()", $content->value());
263 }
264}
Builds data types.
Definition: Factory.php:21
Provides common functionality for UI tests.
Definition: Base.php:299
brutallyTrimHTML(string $html)
A more radical version of normalizeHTML.
Definition: Base.php:444
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:355
DefNamesource $name_source
test_implements_factory_interface()
This is a legacy support of Component\Input\Field\Input that has been moved to Component\Input\Contai...
Definition: Input.php:32
if($format !==null) $name
Definition: metadata.php:247
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider \MainMenu\Provider.
Class Factory.