ILIAS  release_7 Revision v7.30-3-g800a261c036
TextareaTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2018 Jesús López <lopez@leifos.com> Extended GPL, see docs/LICENSE */
4
5require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6require_once(__DIR__ . "/../../../Base.php");
7require_once(__DIR__ . "/InputTest.php");
8
10use \ILIAS\UI\Component\Input\Field;
11use \ILIAS\Data;
13
15{
16
20 private $name_source;
21
22 public function setUp() : void
23 {
24 $this->name_source = new DefNamesource();
25 }
26
27
28 protected function buildFactory()
29 {
30 $df = new Data\Factory();
31 $language = $this->createMock(\ilLanguage::class);
33 new SignalGenerator(),
34 $df,
35 new ILIAS\Refinery\Factory($df, $language),
36 $language
37 );
38 }
39
40
42 {
43 $f = $this->buildFactory();
44 $textarea = $f->textarea("label", "byline");
45 $this->assertInstanceOf(Field\Input::class, $textarea);
46 $this->assertInstanceOf(Field\Textarea::class, $textarea);
47 }
48
49
51 {
52 $f = $this->buildFactory();
53 $textarea = $f->textarea("label");
54 $this->assertInstanceOf(Field\Input::class, $textarea);
55 $this->assertInstanceOf(Field\Textarea::class, $textarea);
56 }
57
58
59 public function test_with_min_limit()
60 {
61 $f = $this->buildFactory();
62 $limit = 5;
63 $textarea = $f->textarea('label')->withMinLimit($limit);
64 $this->assertInstanceOf(Field\Input::class, $textarea);
65 $this->assertInstanceOf(Field\Textarea::class, $textarea);
66 $this->assertEquals($textarea->getMinLimit(), $limit);
67 }
68
69
70 public function test_with_max_limit()
71 {
72 $f = $this->buildFactory();
73 $limit = 15;
74 $textarea = $f->textarea('label')->withMaxLimit($limit);
75 $this->assertInstanceOf(Field\Input::class, $textarea);
76 $this->assertInstanceOf(Field\Textarea::class, $textarea);
77 $this->assertEquals($textarea->getMaxLimit(), $limit);
78 }
79
80
81 public function test_is_limited()
82 {
83 $f = $this->buildFactory();
84
85 // with min limit
86 $textarea = $f->textarea('label')->withMinLimit(5);
87 $this->assertTrue($textarea->isLimited());
88
89 // with max limit
90 $textarea = $f->textarea('label')->withMaxLimit(5);
91 $this->assertTrue($textarea->isLimited());
92
93 // with min-max limit
94 $textarea = $f->textarea('label')->withMinLimit(5)->withMaxLimit(20);
95 $this->assertTrue($textarea->isLimited());
96
97 // without limit
98 $textarea = $f->textarea('label');
99 $this->assertFalse($textarea->isLimited());
100 }
101
102
103 public function test_get_min_limit()
104 {
105 $f = $this->buildFactory();
106 $limit = 5;
107 $textarea = $f->textarea('label')->withMinLimit($limit);
108 $this->assertEquals($textarea->getMinLimit(), $limit);
109 }
110
111
112 public function test_get_max_limit()
113 {
114 $f = $this->buildFactory();
115 $limit = 15;
116 $textarea = $f->textarea('label')->withMaxLimit($limit);
117 $this->assertEquals($textarea->getMaxLimit(), $limit);
118 }
119
120
121 // RENDERER
122 public function test_renderer()
123 {
124 $f = $this->buildFactory();
125 $r = $this->getDefaultRenderer();
126 $label = "label";
127 $byline = "byline";
128 $name = "name_0";
129 $textarea = $f->textarea($label, $byline)->withNameFrom($this->name_source);
130
131 $expected = "<div class=\"form-group row\">"
132 . "<label for=\"id_1\" class=\"control-label col-sm-3\">$label</label>"
133 . "<div class=\"col-sm-9\">"
134 . "<textarea id=\"id_1\" name=\"$name\" class=\"form-control form-control-sm\"></textarea>"
135 . "<div class=\"help-block\">byline</div>"
136 . "</div>"
137 . "</div>";
138
139 $html = $this->normalizeHTML($r->render($textarea));
140 $this->assertHTMLEquals($expected, $html);
141 }
142
144 {
145 $f = $this->buildFactory();
146 $r = $this->getDefaultRenderer();
147 $name = "name_0";
148 $id = "id_1";
149 $label = "label";
150
151 $min = 5;
152 $byline = "This is just a byline Min: " . $min;
153 $textarea = $f->textarea($label, $byline)->withMinLimit($min)->withNameFrom($this->name_source);
154
155 $expected = "<div class=\"form-group row\">"
156 . "<label for=\"id_1\" class=\"control-label col-sm-3\">$label</label>"
157 . "<div class=\"col-sm-9\">"
158 . "<textarea id=\"$id\" name=\"$name\" class=\"form-control form-control-sm\"></textarea>"
159 . "<div id=\"textarea_feedback_$id\" data-maxchars=\"\"></div>"
160 . "<div class=\"help-block\">$byline</div>"
161 . "</div>"
162 . "</div>";
163
164 $html = $this->normalizeHTML($r->render($textarea));
165 $this->assertHTMLEquals($expected, $html);
166 }
167
169 {
170 $f = $this->buildFactory();
171 $r = $this->getDefaultRenderer();
172 $name = "name_0";
173 $id = "id_1";
174 $label = "label";
175 $max = 20;
176 $byline = "This is just a byline Max: " . $max;
177 $textarea = $f->textarea($label, $byline)->withMaxLimit($max)->withNameFrom($this->name_source);
178
179 $expected = "<div class=\"form-group row\">"
180 . "<label for=\"id_1\" class=\"control-label col-sm-3\">$label</label>"
181 . "<div class=\"col-sm-9\">"
182 . "<textarea id=\"$id\" name=\"$name\" class=\"form-control form-control-sm\"></textarea>"
183 . "<div id=\"textarea_feedback_$id\" data-maxchars=\"$max\"></div>"
184 . "<div class=\"help-block\">$byline</div>"
185 . "</div>"
186 . "</div>";
187
188 $html = $this->normalizeHTML($r->render($textarea));
189 $this->assertHTMLEquals($expected, $html);
190 }
191
193 {
194 $f = $this->buildFactory();
195 $r = $this->getDefaultRenderer();
196 $name = "name_0";
197 $id = "id_1";
198 $label = "label";
199 $min = 5;
200 $max = 20;
201 $byline = "This is just a byline Min: " . $min . " Max: " . $max;
202 $textarea = $f->textarea($label, $byline)->withMinLimit($min)->withMaxLimit($max)->withNameFrom($this->name_source);
203
204 $expected = "<div class=\"form-group row\">"
205 . "<label for=\"id_1\" class=\"control-label col-sm-3\">$label</label>"
206 . "<div class=\"col-sm-9\">"
207 . "<textarea id=\"$id\" name=\"$name\" class=\"form-control form-control-sm\"></textarea>"
208 . "<div id=\"textarea_feedback_$id\" data-maxchars=\"$max\"></div>"
209 . "<div class=\"help-block\">$byline</div>"
210 . "</div>"
211 . "</div>";
212
213 $html = $this->normalizeHTML($r->render($textarea));
214 $this->assertHTMLEquals($expected, $html);
215 }
216
218 {
219 $f = $this->buildFactory();
220 $r = $this->getDefaultRenderer();
221 $label = "label";
222 $byline = "byline";
223 $name = "name_0";
224 $value = "Lorem ipsum dolor sit";
225 $textarea = $f->textarea($label, $byline)->withValue($value)->withNameFrom($this->name_source);
226
227 $expected = "<div class=\"form-group row\">"
228 . "<label for=\"id_1\" class=\"control-label col-sm-3\">$label</label>"
229 . "<div class=\"col-sm-9\">"
230 . "<textarea id=\"id_1\" name=\"$name\" class=\"form-control form-control-sm\">$value</textarea>"
231 . "<div class=\"help-block\">byline</div>"
232 . "</div>"
233 . "</div>";
234
235 $html = $this->normalizeHTML($r->render($textarea));
236 $this->assertHTMLEquals($expected, $html);
237 }
238
239 public function test_renderer_with_error()
240 {
241 $f = $this->buildFactory();
242 $r = $this->getDefaultRenderer();
243 $name = "name_0";
244 $label = "label";
245 $min = 5;
246 $byline = "This is just a byline Min: " . $min;
247 $error = "an_error";
248 $textarea = $f->textarea($label, $byline)->withNameFrom($this->name_source)->withError($error);
249
250 $expected = $this->brutallyTrimHTML('
251<div class="form-group row">
252 <label for="id_1" class="control-label col-sm-3">label</label>
253 <div class="col-sm-9">
254 <div class="help-block alert alert-danger" role="alert">an_error</div>
255 <textarea id="id_1" name="name_0" class="form-control form-control-sm"></textarea>
256 <div class="help-block">This is just a byline Min: 5</div>
257 </div>
258</div>
259');
260
261 $html = $this->brutallyTrimHTML($r->render($textarea));
262 $this->assertEquals($expected, $html);
263 }
264
266 {
267 $f = $this->buildFactory();
268 $r = $this->getDefaultRenderer();
269 $label = "label";
270 $byline = "byline";
271 $name = "name_0";
272 $textarea = $f->textarea($label, $byline)->withNameFrom($this->name_source)->withDisabled(true);
273
274 $expected = "<div class=\"form-group row\">"
275 . "<label for=\"id_1\" class=\"control-label col-sm-3\">$label</label>"
276 . "<div class=\"col-sm-9\">"
277 . "<textarea id=\"id_1\" name=\"$name\" disabled=\"disabled\" class=\"form-control form-control-sm\"></textarea>"
278 . "<div class=\"help-block\">byline</div>"
279 . "</div>"
280 . "</div>";
281
282 $html = $this->normalizeHTML($r->render($textarea));
283 $this->assertHTMLEquals($expected, $html);
284 }
285
286 public function test_stripsTags()
287 {
288 $f = $this->buildFactory();
289 $name = "name_0";
290 $text = $f->textarea("")
291 ->withNameFrom($this->name_source)
292 ->withInput(new DefInputData([$name => "<script>alert()</script>"]));
293
294 $content = $text->getContent();
295 $this->assertEquals("alert()", $content->value());
296 }
297}
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:20
Provides common functionality for UI tests.
Definition: Base.php:263
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:372
getDefaultRenderer(JavaScriptBinding $js_binding=null, $with_stub_renderings=[])
Definition: Base.php:311
normalizeHTML($html)
Definition: Base.php:363
brutallyTrimHTML($html)
A more radical version of normalizeHTML.
Definition: Base.php:392
test_renderer_counter_with_value()
test_implements_factory_interface_without_byline()
test_renderer_with_max_limit()
test_renderer_with_error()
test_implements_factory_interface()
test_renderer_with_min_and_max_limit()
test_renderer_with_disabled()
test_renderer_with_min_limit()
if($format !==null) $name
Definition: metadata.php:230
Class ChatMainBarProvider \MainMenu\Provider.