ILIAS  release_8 Revision v8.24
TextareaTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
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 $textarea = $f->textarea("label", "byline");
57 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $textarea);
58 $this->assertInstanceOf(Field\Textarea::class, $textarea);
59 }
60
62 {
63 $f = $this->buildFactory();
64 $textarea = $f->textarea("label");
65 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $textarea);
66 $this->assertInstanceOf(Field\Textarea::class, $textarea);
67 }
68
69 public function test_with_min_limit(): void
70 {
71 $f = $this->buildFactory();
72 $limit = 5;
73 $textarea = $f->textarea('label')->withMinLimit($limit);
74 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $textarea);
75 $this->assertInstanceOf(Field\Textarea::class, $textarea);
76 $this->assertEquals($textarea->getMinLimit(), $limit);
77 }
78
79 public function test_with_max_limit(): void
80 {
81 $f = $this->buildFactory();
82 $limit = 15;
83 $textarea = $f->textarea('label')->withMaxLimit($limit);
84 $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $textarea);
85 $this->assertInstanceOf(Field\Textarea::class, $textarea);
86 $this->assertEquals($textarea->getMaxLimit(), $limit);
87 }
88
89 public function test_is_limited(): void
90 {
91 $f = $this->buildFactory();
92
93 // with min limit
94 $textarea = $f->textarea('label')->withMinLimit(5);
95 $this->assertTrue($textarea->isLimited());
96
97 // with max limit
98 $textarea = $f->textarea('label')->withMaxLimit(5);
99 $this->assertTrue($textarea->isLimited());
100
101 // with min-max limit
102 $textarea = $f->textarea('label')->withMinLimit(5)->withMaxLimit(20);
103 $this->assertTrue($textarea->isLimited());
104
105 // without limit
106 $textarea = $f->textarea('label');
107 $this->assertFalse($textarea->isLimited());
108 }
109
110 public function test_get_min_limit(): void
111 {
112 $f = $this->buildFactory();
113 $limit = 5;
114 $textarea = $f->textarea('label')->withMinLimit($limit);
115 $this->assertEquals($textarea->getMinLimit(), $limit);
116 }
117
118 public function test_get_max_limit(): void
119 {
120 $f = $this->buildFactory();
121 $limit = 15;
122 $textarea = $f->textarea('label')->withMaxLimit($limit);
123 $this->assertEquals($textarea->getMaxLimit(), $limit);
124 }
125
126 // RENDERER
127 public function test_renderer(): void
128 {
129 $f = $this->buildFactory();
130 $r = $this->getDefaultRenderer();
131 $label = "label";
132 $byline = "byline";
133 $name = "name_0";
134 $textarea = $f->textarea($label, $byline)->withNameFrom($this->name_source);
135
136 $expected = "<div class=\"form-group row\">"
137 . "<label for=\"id_1\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>"
138 . "<div class=\"col-sm-8 col-md-9 col-lg-10\">"
139 . "<textarea id=\"id_1\" name=\"$name\" class=\"form-control form-control-sm\"></textarea>"
140 . "<div class=\"help-block\">byline</div>"
141 . "</div>"
142 . "</div>";
143
144 $html = $this->normalizeHTML($r->render($textarea));
145 $this->assertHTMLEquals($expected, $html);
146 }
147
148 public function test_renderer_with_min_limit(): void
149 {
150 $f = $this->buildFactory();
151 $r = $this->getDefaultRenderer();
152 $name = "name_0";
153 $id = "id_1";
154 $label = "label";
155
156 $min = 5;
157 $byline = "This is just a byline Min: " . $min;
158 $textarea = $f->textarea($label, $byline)->withMinLimit($min)->withNameFrom($this->name_source);
159
160 $expected = "<div class=\"form-group row\">"
161 . "<label for=\"id_1\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>"
162 . "<div class=\"col-sm-8 col-md-9 col-lg-10\">"
163 . "<textarea id=\"$id\" name=\"$name\" class=\"form-control form-control-sm\"></textarea>"
164 . "<div id=\"textarea_feedback_$id\" data-maxchars=\"\"></div>"
165 . "<div class=\"help-block\">$byline</div>"
166 . "</div>"
167 . "</div>";
168
169 $html = $this->normalizeHTML($r->render($textarea));
170 $this->assertHTMLEquals($expected, $html);
171 }
172
173 public function test_renderer_with_max_limit(): void
174 {
175 $f = $this->buildFactory();
176 $r = $this->getDefaultRenderer();
177 $name = "name_0";
178 $id = "id_1";
179 $label = "label";
180 $max = 20;
181 $byline = "This is just a byline Max: " . $max;
182 $textarea = $f->textarea($label, $byline)->withMaxLimit($max)->withNameFrom($this->name_source);
183
184 $expected = "<div class=\"form-group row\">"
185 . "<label for=\"id_1\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>"
186 . "<div class=\"col-sm-8 col-md-9 col-lg-10\">"
187 . "<textarea id=\"$id\" name=\"$name\" class=\"form-control form-control-sm\"></textarea>"
188 . "<div id=\"textarea_feedback_$id\" data-maxchars=\"$max\"></div>"
189 . "<div class=\"help-block\">$byline</div>"
190 . "</div>"
191 . "</div>";
192
193 $html = $this->normalizeHTML($r->render($textarea));
194 $this->assertHTMLEquals($expected, $html);
195 }
196
198 {
199 $f = $this->buildFactory();
200 $r = $this->getDefaultRenderer();
201 $name = "name_0";
202 $id = "id_1";
203 $label = "label";
204 $min = 5;
205 $max = 20;
206 $byline = "This is just a byline Min: " . $min . " Max: " . $max;
207 $textarea = $f->textarea($label, $byline)->withMinLimit($min)->withMaxLimit($max)->withNameFrom($this->name_source);
208
209 $expected = "<div class=\"form-group row\">"
210 . "<label for=\"id_1\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>"
211 . "<div class=\"col-sm-8 col-md-9 col-lg-10\">"
212 . "<textarea id=\"$id\" name=\"$name\" class=\"form-control form-control-sm\"></textarea>"
213 . "<div id=\"textarea_feedback_$id\" data-maxchars=\"$max\"></div>"
214 . "<div class=\"help-block\">$byline</div>"
215 . "</div>"
216 . "</div>";
217
218 $html = $this->normalizeHTML($r->render($textarea));
219 $this->assertHTMLEquals($expected, $html);
220 }
221
222 public function test_renderer_counter_with_value(): void
223 {
224 $f = $this->buildFactory();
225 $r = $this->getDefaultRenderer();
226 $label = "label";
227 $byline = "byline";
228 $name = "name_0";
229 $value = "Lorem ipsum dolor sit";
230 $textarea = $f->textarea($label, $byline)->withValue($value)->withNameFrom($this->name_source);
231
232 $expected = "<div class=\"form-group row\">"
233 . "<label for=\"id_1\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>"
234 . "<div class=\"col-sm-8 col-md-9 col-lg-10\">"
235 . "<textarea id=\"id_1\" name=\"$name\" class=\"form-control form-control-sm\">$value</textarea>"
236 . "<div class=\"help-block\">byline</div>"
237 . "</div>"
238 . "</div>";
239
240 $html = $this->normalizeHTML($r->render($textarea));
241 $this->assertHTMLEquals($expected, $html);
242 }
243
244 public function test_renderer_with_error(): void
245 {
246 $f = $this->buildFactory();
247 $r = $this->getDefaultRenderer();
248 $label = "label";
249 $min = 5;
250 $byline = "This is just a byline Min: " . $min;
251 $error = "an_error";
252 $textarea = $f->textarea($label, $byline)->withNameFrom($this->name_source)->withError($error);
253
254 $expected = $this->brutallyTrimHTML('
255<div class="form-group row">
256 <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
257 <div class="col-sm-8 col-md-9 col-lg-10">
258 <div class="help-block alert alert-danger" aria-describedby="id_1" role="alert">an_error</div>
259 <textarea id="id_1" name="name_0" class="form-control form-control-sm"></textarea>
260 <div class="help-block">This is just a byline Min: 5</div>
261 </div>
262</div>
263');
264
265 $html = $this->brutallyTrimHTML($r->render($textarea));
266 $this->assertEquals($expected, $html);
267 }
268
269 public function test_renderer_with_disabled(): void
270 {
271 $f = $this->buildFactory();
272 $r = $this->getDefaultRenderer();
273 $label = "label";
274 $byline = "byline";
275 $name = "name_0";
276 $textarea = $f->textarea($label, $byline)->withNameFrom($this->name_source)->withDisabled(true);
277
278 $expected = "<div class=\"form-group row\">"
279 . "<label for=\"id_1\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>"
280 . "<div class=\"col-sm-8 col-md-9 col-lg-10\">"
281 . "<textarea id=\"id_1\" name=\"$name\" disabled=\"disabled\" class=\"form-control form-control-sm\"></textarea>"
282 . "<div class=\"help-block\">byline</div>"
283 . "</div>"
284 . "</div>";
285
286 $html = $this->normalizeHTML($r->render($textarea));
287 $this->assertHTMLEquals($expected, $html);
288 }
289
290 public function test_stripsTags(): void
291 {
292 $f = $this->buildFactory();
293 $name = "name_0";
294 $text = $f->textarea("")
295 ->withNameFrom($this->name_source)
296 ->withInput(new DefInputData([$name => "<script>alert()</script>"]));
297
298 $content = $text->getContent();
299 $this->assertEquals("alert()", $content->value());
300 }
301}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds data types.
Definition: Factory.php:21
Provides common functionality for UI tests.
Definition: Base.php:299
normalizeHTML(string $html)
Definition: Base.php:422
assertHTMLEquals(string $expected_html_as_string, string $html_as_string)
Definition: Base.php:427
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
test_renderer_counter_with_value()
test_implements_factory_interface_without_byline()
test_renderer_with_max_limit()
DefNamesource $name_source
test_renderer_with_error()
test_implements_factory_interface()
test_renderer_with_min_and_max_limit()
test_renderer_with_disabled()
test_renderer_with_min_limit()
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.