ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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 
5 require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
6 require_once(__DIR__ . "/../../../Base.php");
7 require_once(__DIR__ . "/InputTest.php");
8 
10 use \ILIAS\UI\Component\Input\Field;
11 use \ILIAS\Data;
12 use \ILIAS\Validation;
13 use \ILIAS\Transformation;
14 
16 {
17 
21  private $name_source;
22 
23  public function setUp()
24  {
25  $this->name_source = new DefNamesource();
26  }
27 
28 
29  protected function buildFactory()
30  {
31  $df = new Data\Factory();
33  new SignalGenerator(),
34  $df,
35  new Validation\Factory($df, $this->createMock(\ilLanguage::class)),
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=\"$name\" class=\"control-label col-sm-3\">$label</label>"
133  . "<div class=\"col-sm-9\">"
134  . "<textarea name=\"$name\" class=\"form-control form-control-sm\" id=\"\"></textarea>"
135  . "<div id=\"textarea_feedback_\" data-maxchars=\"\"></div>"
136  . "<div class=\"help-block\">byline</div>"
137  . "</div>"
138  . "</div>";
139 
140  $html = $this->normalizeHTML($r->render($textarea));
141  $this->assertHTMLEquals($expected, $html);
142  }
143 
145  {
146  $f = $this->buildFactory();
147  $r = $this->getDefaultRenderer();
148  $name = "name_0";
149  $id = "id_1";
150  $label = "label";
151 
152  $min = 5;
153  $byline = "This is just a byline Min: " . $min;
154  $textarea = $f->textarea($label, $byline)->withMinLimit($min)->withNameFrom($this->name_source);
155 
156  $expected = "<div class=\"form-group row\">"
157  . "<label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
158  . "<div class=\"col-sm-9\">"
159  . "<textarea name=\"$name\" class=\"form-control form-control-sm\" id=\"$id\"></textarea>"
160  . "<div id=\"textarea_feedback_$id\" data-maxchars=\"\"></div>"
161  . "<div class=\"help-block\">$byline</div>"
162  . "</div>"
163  . "</div>";
164 
165  $html = $this->normalizeHTML($r->render($textarea));
166  $this->assertHTMLEquals($expected, $html);
167  }
168 
170  {
171  $f = $this->buildFactory();
172  $r = $this->getDefaultRenderer();
173  $name = "name_0";
174  $id = "id_1";
175  $label = "label";
176  $max = 20;
177  $byline = "This is just a byline Max: " . $max;
178  $textarea = $f->textarea($label, $byline)->withMaxLimit($max)->withNameFrom($this->name_source);
179 
180  $expected = "<div class=\"form-group row\">"
181  . "<label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
182  . "<div class=\"col-sm-9\">"
183  . "<textarea name=\"$name\" class=\"form-control form-control-sm\" id=\"$id\"></textarea>"
184  . "<div id=\"textarea_feedback_$id\" data-maxchars=\"$max\"></div>"
185  . "<div class=\"help-block\">$byline</div>"
186  . "</div>"
187  . "</div>";
188 
189  $html = $this->normalizeHTML($r->render($textarea));
190  $this->assertHTMLEquals($expected, $html);
191  }
192 
194  {
195  $f = $this->buildFactory();
196  $r = $this->getDefaultRenderer();
197  $name = "name_0";
198  $id = "id_1";
199  $label = "label";
200  $min = 5;
201  $max = 20;
202  $byline = "This is just a byline Min: " . $min . " Max: " . $max;
203  $textarea = $f->textarea($label, $byline)->withMinLimit($min)->withMaxLimit($max)->withNameFrom($this->name_source);
204 
205  $expected = "<div class=\"form-group row\">"
206  . "<label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
207  . "<div class=\"col-sm-9\">"
208  . "<textarea name=\"$name\" class=\"form-control form-control-sm\" id=\"$id\"></textarea>"
209  . "<div id=\"textarea_feedback_$id\" data-maxchars=\"$max\"></div>"
210  . "<div class=\"help-block\">$byline</div>"
211  . "</div>"
212  . "</div>";
213 
214  $html = $this->normalizeHTML($r->render($textarea));
215  $this->assertHTMLEquals($expected, $html);
216  }
217 
219  {
220  $f = $this->buildFactory();
221  $r = $this->getDefaultRenderer();
222  $label = "label";
223  $byline = "byline";
224  $name = "name_0";
225  $value = "Lorem ipsum dolor sit";
226  $textarea = $f->textarea($label, $byline)->withValue($value)->withNameFrom($this->name_source);
227 
228  $expected = "<div class=\"form-group row\">"
229  . "<label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
230  . "<div class=\"col-sm-9\">"
231  . "<textarea name=\"$name\" class=\"form-control form-control-sm\" id=\"\">$value</textarea>"
232  . "<div id=\"textarea_feedback_\" data-maxchars=\"\"></div>"
233  . "<div class=\"help-block\">byline</div>"
234  . "</div>"
235  . "</div>";
236 
237  $html = $this->normalizeHTML($r->render($textarea));
238  $this->assertHTMLEquals($expected, $html);
239  }
240 
241  public function test_renderer_with_error()
242  {
243  $f = $this->buildFactory();
244  $r = $this->getDefaultRenderer();
245  $name = "name_0";
246  $label = "label";
247  $min = 5;
248  $byline = "This is just a byline Min: " . $min;
249  $error = "an_error";
250  $textarea = $f->textarea($label, $byline)->withNameFrom($this->name_source)->withError($error);
251 
252  $expected = "<div class=\"form-group row\">"
253  . "<label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
254  . "<div class=\"col-sm-9\">"
255  . "<textarea name=\"$name\" class=\"form-control form-control-sm\" id=\"\"></textarea>"
256  . "<div id=\"textarea_feedback_\" data-maxchars=\"\"></div>"
257  . "<div class=\"help-block\">$byline</div>"
258  . "<div class=\"help-block alert alert-danger\" role=\"alert\">"
259  . "<img border=\"0\" src=\"./templates/default/images/icon_alert.svg\" alt=\"alert\" />"
260  . "$error</div></div></div>";
261 
262  $html = $this->normalizeHTML($r->render($textarea));
263  $html = trim(preg_replace('/\t+/', '', $html));
264  $expected = trim(preg_replace('/\t+/', '', $expected));
265  $this->assertEquals($expected, $html);
266  }
267 
268  public function test_stripsTags()
269  {
270  $f = $this->buildFactory();
271  $name = "name_0";
272  $text = $f->textarea("")
273  ->withNameFrom($this->name_source)
274  ->withInput(new DefPostData([$name => "<script>alert()</script>"]));
275 
276  $content = $text->getContent();
277  $this->assertEquals("alert()", $content->value());
278  }
279 }
test_renderer_with_min_limit()
A transformation is a function from one datatype to another.
if(!array_key_exists('StateId', $_REQUEST)) $id
test_renderer_counter_with_value()
test_renderer_with_max_limit()
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:228
normalizeHTML($html)
Definition: Base.php:261
test_implements_factory_interface_without_byline()
$r
Definition: example_031.php:79
Provides common functionality for UI tests.
Definition: Base.php:191
$text
Definition: errorreport.php:18
test_implements_factory_interface()
Builds data types.
Definition: Factory.php:14
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:270
test_renderer_with_error()
test_renderer_with_min_and_max_limit()
$html
Definition: example_001.php:87