ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
TextInputTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2017 Richard Klees <richard.klees@concepts-and-training.de> 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  public function setUp()
18  {
19  $this->name_source = new DefNamesource();
20  }
21 
22 
23  protected function buildFactory()
24  {
25  $df = new Data\Factory();
27  new SignalGenerator(),
28  $df,
29  new Validation\Factory($df, $this->createMock(\ilLanguage::class)),
31  );
32  }
33 
34 
36  {
37  $f = $this->buildFactory();
38 
39  $text = $f->text("label", "byline");
40 
41  $this->assertInstanceOf(Field\Input::class, $text);
42  $this->assertInstanceOf(Field\Text::class, $text);
43  }
44 
45 
46  public function test_render()
47  {
48  $f = $this->buildFactory();
49  $label = "label";
50  $byline = "byline";
51  $name = "name_0";
52  $text = $f->text($label, $byline)->withNameFrom($this->name_source);
53 
54  $r = $this->getDefaultRenderer();
55  $html = $this->normalizeHTML($r->render($text));
56 
57  $expected = "<div class=\"form-group row\">" . " <label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
58  . " <div class=\"col-sm-9\">" . " <input type=\"text\" name=\"$name\" class=\"form-control form-control-sm\" />"
59  . " <div class=\"help-block\">$byline</div>" . " " . " </div>" . "</div>";
60  $this->assertEquals($expected, $html);
61  }
62 
63 
64  public function test_render_error()
65  {
66  $f = $this->buildFactory();
67  $label = "label";
68  $byline = "byline";
69  $name = "name_0";
70  $error = "an_error";
71  $text = $f->text($label, $byline)->withNameFrom($this->name_source)->withError($error);
72 
73  $r = $this->getDefaultRenderer();
74  $html = $this->normalizeHTML($r->render($text));
75 
76  $expected = "<div class=\"form-group row\">" . " <label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
77  . " <div class=\"col-sm-9\">" . " <input type=\"text\" name=\"$name\" class=\"form-control form-control-sm\" />"
78  . " <div class=\"help-block\">$byline</div>" . " <div class=\"help-block alert alert-danger\" role=\"alert\">"
79  . " <img border=\"0\" src=\"./templates/default/images/icon_alert.svg\" alt=\"alert\" />" . " $error"
80  . " </div>" . " </div>" . "</div>";
81  $this->assertEquals($expected, $html);
82  }
83 
84 
85  public function test_render_no_byline()
86  {
87  $f = $this->buildFactory();
88  $label = "label";
89  $name = "name_0";
90  $text = $f->text($label)->withNameFrom($this->name_source);
91 
92  $r = $this->getDefaultRenderer();
93  $html = $this->normalizeHTML($r->render($text));
94 
95  $expected = "<div class=\"form-group row\">" . " <label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
96  . " <div class=\"col-sm-9\">" . " <input type=\"text\" name=\"$name\" class=\"form-control form-control-sm\" />" . " "
97  . " " . " </div>" . "</div>";
98  $this->assertEquals($expected, $html);
99  }
100 
101 
102  public function test_render_value()
103  {
104  $f = $this->buildFactory();
105  $label = "label";
106  $value = "value";
107  $name = "name_0";
108  $text = $f->text($label)->withValue($value)->withNameFrom($this->name_source);
109 
110  $r = $this->getDefaultRenderer();
111  $html = $this->normalizeHTML($r->render($text));
112 
113  $expected = "<div class=\"form-group row\">" . " <label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
114  . " <div class=\"col-sm-9\">"
115  . " <input type=\"text\" value=\"$value\" name=\"$name\" class=\"form-control form-control-sm\" />" . " " . " "
116  . " </div>" . "</div>";
117  $this->assertEquals($expected, $html);
118  }
119 
120 
121  public function test_render_required()
122  {
123  $f = $this->buildFactory();
124  $label = "label";
125  $name = "name_0";
126  $text = $f->text($label)->withNameFrom($this->name_source)->withRequired(true);
127 
128  $r = $this->getDefaultRenderer();
129  $html = $this->normalizeHTML($r->render($text));
130 
131  $expected = "<div class=\"form-group row\">" . " <label for=\"$name\" class=\"control-label col-sm-3\">" . "$label"
132  . "<span class=\"asterisk\">*</span>" . "</label>" . " <div class=\"col-sm-9\">"
133  . " <input type=\"text\" name=\"$name\" class=\"form-control form-control-sm\" />" . " " . " " . " </div>"
134  . "</div>";
135  $this->assertEquals($expected, $html);
136  }
137 
138 
139  public function test_value_required()
140  {
141  $f = $this->buildFactory();
142  $label = "label";
143  $name = "name_0";
144  $text = $f->text($label)->withNameFrom($this->name_source)->withRequired(true);
145 
146  $text1 = $text->withInput(new DefPostData([$name => "0"]));
147  $value1 = $text1->getContent();
148  $this->assertTrue($value1->isOk());
149  $this->assertEquals("0", $value1->value());
150 
151  $text2 = $text->withInput(new DefPostData([$name => ""]));
152  $value2 = $text2->getContent();
153  $this->assertTrue($value2->isError());
154  }
155 
156  public function test_stripsTags()
157  {
158  $f = $this->buildFactory();
159  $name = "name_0";
160  $text = $f->text("")
161  ->withNameFrom($this->name_source)
162  ->withInput(new DefPostData([$name => "<script>alert()</script>"]));
163 
164  $content = $text->getContent();
165  $this->assertEquals("alert()", $content->value());
166  }
167 }
test_implements_factory_interface()
A transformation is a function from one datatype to another.
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:228
normalizeHTML($html)
Definition: Base.php:261
$r
Definition: example_031.php:79
Provides common functionality for UI tests.
Definition: Base.php:191
$text
Definition: errorreport.php:18
Builds data types.
Definition: Factory.php:14
$html
Definition: example_001.php:87