ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
TextareaTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
22 require_once(__DIR__ . "/../../../Base.php");
23 require_once(__DIR__ . "/InputTest.php");
24 
28 use ILIAS\Data;
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 testImplementsFactoryInterface(): 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 testWithMinLimit(): 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 testWithMaxLimit(): 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 testIsLimited(): 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 testGetMinLimit(): 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 testGetMaxLimit(): 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 testRenderer(): void
128  {
129  $f = $this->buildFactory();
130  $r = $this->getDefaultRenderer();
131  $id = "id_1";
132  $label = "label";
133  $byline = "byline";
134  $name = "name_0";
135  $textarea = $f->textarea($label, $byline)->withNameFrom($this->name_source);
136 
137  $expected = "
138  <div class=\"form-group row\">
139  <label for=\"$id\" 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\">
141  <div class=\"ui-input-textarea\">
142  <textarea id=\"$id\" class=\"form-control form-control-sm\" name=\"$name\"></textarea>
143  </div>
144  <div class=\"help-block\">$byline</div>
145  </div>
146  </div>
147  ";
148 
149  $html = $this->normalizeHTML($r->render($textarea));
150  $this->assertHTMLEquals($expected, $html);
151  }
152 
153  public function testRendererWithMinLimit(): void
154  {
155  $f = $this->buildFactory();
156  $r = $this->getDefaultRenderer();
157  $name = "name_0";
158  $id = "id_1";
159  $label = "label";
160 
161  $min = 5;
162  $byline = "This is just a byline Min: " . $min;
163  $textarea = $f->textarea($label, $byline)->withMinLimit($min)->withNameFrom($this->name_source);
164 
165  $expected = "
166  <div class=\"form-group row\">
167  <label for=\"$id\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>
168  <div class=\"col-sm-8 col-md-9 col-lg-10\">
169  <div class=\"ui-input-textarea\">
170  <textarea id=\"$id\" class=\"form-control form-control-sm\" name=\"$name\" minlength=\"$min\"></textarea>
171  </div>
172  <div class=\"help-block\">$byline</div>
173  </div>
174  </div>
175  ";
176 
177  $html = $this->normalizeHTML($r->render($textarea));
178  $this->assertHTMLEquals($expected, $html);
179  }
180 
181  public function testRendererWithMaxLimit(): void
182  {
183  $f = $this->buildFactory();
184  $r = $this->getDefaultRenderer();
185  $name = "name_0";
186  $id = "id_1";
187  $label = "label";
188  $max = 20;
189  $byline = "This is just a byline Max: " . $max;
190  $textarea = $f->textarea($label, $byline)->withMaxLimit($max)->withNameFrom($this->name_source);
191 
192  $expected = "
193  <div class=\"form-group row\">
194  <label for=\"$id\" 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\">
196  <div class=\"ui-input-textarea\">
197  <textarea id=\"$id\" class=\"form-control form-control-sm\" name=\"$name\" maxlength=\"$max\"></textarea>
198  <div class=\"ui-input-textarea-remainder\"> ui_chars_remaining <span data-action=\"remainder\">$max</span> </div>
199  </div>
200  <div class=\"help-block\">$byline</div>
201  </div>
202  </div>
203  ";
204 
205  $html = $this->brutallyTrimHTML($r->render($textarea));
206  $this->assertHTMLEquals($this->brutallyTrimHTML($expected), $html);
207  }
208 
209  public function testRendererWithMinAndMaxLimit(): void
210  {
211  $f = $this->buildFactory();
212  $r = $this->getDefaultRenderer();
213  $name = "name_0";
214  $id = "id_1";
215  $label = "label";
216  $min = 5;
217  $max = 20;
218  $byline = "This is just a byline Min: " . $min . " Max: " . $max;
219  $textarea = $f->textarea($label, $byline)->withMinLimit($min)->withMaxLimit($max)->withNameFrom(
220  $this->name_source
221  );
222 
223  $expected = "
224  <div class=\"form-group row\">
225  <label for=\"$id\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>
226  <div class=\"col-sm-8 col-md-9 col-lg-10\">
227  <div class=\"ui-input-textarea\">
228  <textarea id=\"$id\" class=\"form-control form-control-sm\" name=\"$name\" minlength=\"5\" maxlength=\"20\"></textarea>
229  <div class=\"ui-input-textarea-remainder\"> ui_chars_remaining <span data-action=\"remainder\">$max</span> </div>
230  </div>
231  <div class=\"help-block\">$byline</div>
232  </div>
233  </div>
234  ";
235 
236  $html = $this->brutallyTrimHTML($r->render($textarea));
237  $this->assertHTMLEquals($this->brutallyTrimHTML($expected), $html);
238  }
239 
240  public function testRendererCounterWithValue(): void
241  {
242  $f = $this->buildFactory();
243  $r = $this->getDefaultRenderer();
244  $id = 'id_1';
245  $label = "label";
246  $byline = "byline";
247  $name = "name_0";
248  $value = "Lorem ipsum dolor sit";
249  $textarea = $f->textarea($label, $byline)->withValue($value)->withNameFrom($this->name_source);
250 
251  $expected = "
252  <div class=\"form-group row\">
253  <label for=\"$id\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>
254  <div class=\"col-sm-8 col-md-9 col-lg-10\">
255  <div class=\"ui-input-textarea\">
256  <textarea id=\"$id\" class=\"form-control form-control-sm\" name=\"$name\">$value</textarea>
257  </div>
258  <div class=\"help-block\">$byline</div>
259  </div>
260  </div>
261  ";
262 
263  $html = $this->normalizeHTML($r->render($textarea));
264  $this->assertHTMLEquals($expected, $html);
265  }
266 
267  public function testRendererWithError(): void
268  {
269  $f = $this->buildFactory();
270  $r = $this->getDefaultRenderer();
271  $id = "id_1";
272  $label = "label";
273  $name = "name_0";
274  $min = 5;
275  $byline = "This is just a byline Min: " . $min;
276  $error = "an_error";
277  $textarea = $f->textarea($label, $byline)->withNameFrom($this->name_source)->withError($error);
278 
279  $expected = "
280  <div class=\"form-group row\">
281  <label for=\"$id\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>
282  <div class=\"col-sm-8 col-md-9 col-lg-10\">
283  <div class=\"help-block alert alert-danger\" aria-describedby=\"$id\" role=\"alert\">an_error</div>
284  <div class=\"ui-input-textarea\">
285  <textarea id=\"$id\" class=\"form-control form-control-sm\" name=\"$name\"></textarea>
286  </div>
287  <div class=\"help-block\">$byline</div>
288  </div>
289  </div>
290  ";
291 
292  $html = $this->brutallyTrimHTML($r->render($textarea));
293  $this->assertEquals($this->brutallyTrimHTML($expected), $html);
294  }
295 
296  public function testRendererWithDisabled(): void
297  {
298  $f = $this->buildFactory();
299  $r = $this->getDefaultRenderer();
300  $id = "id_1";
301  $label = "label";
302  $byline = "byline";
303  $name = "name_0";
304  $textarea = $f->textarea($label, $byline)->withNameFrom($this->name_source)->withDisabled(true);
305 
306  $expected = "
307  <div class=\"form-group row\">
308  <label for=\"$id\" class=\"control-label col-sm-4 col-md-3 col-lg-2\">$label</label>
309  <div class=\"col-sm-8 col-md-9 col-lg-10\">
310  <div class=\"ui-input-textarea\">
311  <textarea id=\"$id\" class=\"form-control form-control-sm\" name=\"$name\" disabled=\"disabled\"></textarea>
312  </div>
313  <div class=\"help-block\">$byline</div>
314  </div>
315  </div>
316  ";
317 
318  $html = $this->normalizeHTML($r->render($textarea));
319  $this->assertHTMLEquals($expected, $html);
320  }
321 
322  public function testStripsTags(): void
323  {
324  $f = $this->buildFactory();
325  $name = "name_0";
326  $text = $f->textarea("")
327  ->withNameFrom($this->name_source)
328  ->withInput(new DefInputData([$name => "<script>alert()</script>"]));
329 
330  $content = $text->getContent();
331  $this->assertEquals("alert()", $content->value());
332  }
333 }
testRendererCounterWithValue()
getDefaultRenderer(JavaScriptBinding $js_binding=null, array $with_stub_renderings=[])
Definition: Base.php:377
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testImplementsFactoryInterface_without_byline()
testRendererWithDisabled()
testRendererWithMaxLimit()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
brutallyTrimHTML(string $html)
A more radical version of normalizeHTML.
Definition: Base.php:475
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Checkbox.php:21
assertHTMLEquals(string $expected_html_as_string, string $html_as_string)
Definition: Base.php:458
DefNamesource $name_source
Provides common functionality for UI tests.
Definition: Base.php:310
testRendererWithMinAndMaxLimit()
testRendererWithMinLimit()
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
normalizeHTML(string $html)
Definition: Base.php:453
testImplementsFactoryInterface()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$r