ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PasswordInputTest.php
Go to the documentation of this file.
1<?php
2
3/* Copyright (c) 2018 Nils Haagen <nils.haagen@concepts-and-training.de> 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
11use ILIAS\Data\Password as PWD;
12use \ILIAS\UI\Component\Input\Field;
13use \ILIAS\Data;
14use \ILIAS\Validation;
15use \ILIAS\Transformation;
16
17class _PWDPostData implements PostData
18{
19 public function get($name)
20 {
21 return 'some value';
22 }
23 public function getOr($name, $default)
24 {
25 return 'some alternative value';
26 }
27}
28
30{
31 public function setUp()
32 {
33 $this->name_source = new DefNamesource();
34 }
35
36
37 protected function buildFactory()
38 {
39 $df = new Data\Factory();
41 new SignalGenerator(),
42 $df,
43 new Validation\Factory($df, $this->createMock(\ilLanguage::class)),
45 );
46 }
47
48
50 {
51 $f = $this->buildFactory();
52 $pwd = $f->password("label", "byline");
53 $this->assertInstanceOf(Field\Input::class, $pwd);
54 $this->assertInstanceOf(Field\Password::class, $pwd);
55 }
56
57
58 public function test_render()
59 {
60 $f = $this->buildFactory();
61 $label = "label";
62 $byline = "byline";
63 $name = "name_0";
64 $pwd = $f->password($label, $byline)->withNameFrom($this->name_source);
65
66 $r = $this->getDefaultRenderer();
67 $expected = ""
68 . "<div class=\"form-group row\">"
69 . "<label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
70 . "<div class=\"col-sm-9\">"
71 . "<div class=\"il-input-password\">"
72 . "<input type=\"password\" name=\"$name\" class=\"form-control form-control-sm\" />"
73 . "</div>"
74 . "<div class=\"help-block\">$byline</div>"
75 . "</div>"
76 . "</div>";
77 $this->assertHTMLEquals($expected, $r->render($pwd));
78 }
79
80
81 public function test_render_error()
82 {
83 $f = $this->buildFactory();
84 $label = "label";
85 $byline = "byline";
86 $name = "name_0";
87 $error = "an_error";
88 $pwd = $f->password($label, $byline)->withNameFrom($this->name_source)->withError($error);
89
90 $r = $this->getDefaultRenderer();
91 $html = $this->normalizeHTML($r->render($pwd));
92 $expected = ""
93 . "<div class=\"form-group row\">"
94 . " <label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
95 . " <div class=\"col-sm-9\">"
96 . " <div class=\"il-input-password\">"
97 . " <input type=\"password\" name=\"$name\" class=\"form-control form-control-sm\" />"
98 . " </div>"
99 . " <div class=\"help-block\">$byline</div>"
100 . " <div class=\"help-block alert alert-danger\" role=\"alert\">"
101 . " <img border=\"0\" src=\"./templates/default/images/icon_alert.svg\" alt=\"alert\" />"
102 . " $error"
103 . " </div>"
104 . " </div>"
105 . "</div>";
106
107 $html = preg_replace('!\s+!', ' ', $html);
108 $expected = preg_replace('!\s+!', ' ', $expected);
109 $html = explode(' ', $html); //so you can actually _see_ the difference...
110 $expected = explode(' ', $expected);
111 $this->assertEquals($expected, $html);
112 }
113
114
115 public function test_render_no_byline()
116 {
117 $f = $this->buildFactory();
118 $label = "label";
119 $name = "name_0";
120 $pwd = $f->password($label)->withNameFrom($this->name_source);
121
122 $r = $this->getDefaultRenderer();
123 $expected = ""
124 . "<div class=\"form-group row\">"
125 . "<label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
126 . "<div class=\"col-sm-9\">"
127 . "<div class=\"il-input-password\">"
128 . "<input type=\"password\" name=\"$name\" class=\"form-control form-control-sm\" />"
129 . "</div>"
130 . "</div>"
131 . "</div>";
132 $this->assertHTMLEquals($expected, $r->render($pwd));
133 }
134
135
136 public function test_render_value()
137 {
138 $f = $this->buildFactory();
139 $label = "label";
140 $value = "value_0";
141 $name = "name_0";
142 $pwd = $f->password($label)->withValue($value)->withNameFrom($this->name_source);
143
144 $r = $this->getDefaultRenderer();
145 $expected = ""
146 . "<div class=\"form-group row\">"
147 . "<label for=\"$name\" class=\"control-label col-sm-3\">$label</label>"
148 . "<div class=\"col-sm-9\">"
149 . "<div class=\"il-input-password\">"
150 . "<input type=\"password\" name=\"$name\" value=\"$value\" class=\"form-control form-control-sm\" />"
151 . "</div>"
152 . "</div>"
153 . "</div>";
154 $this->assertHTMLEquals($expected, $r->render($pwd));
155 }
156
157
158 public function test_render_required()
159 {
160 $f = $this->buildFactory();
161 $label = "label";
162 $name = "name_0";
163 $pwd = $f->password($label)->withNameFrom($this->name_source)->withRequired(true);
164
165 $r = $this->getDefaultRenderer();
166 $html = $r->render($pwd);
167
168 $expected = ""
169 . "<div class=\"form-group row\">"
170 . "<label for=\"$name\" class=\"control-label col-sm-3\">" . "$label"
171 . "<span class=\"asterisk\">*</span>"
172 . "</label>"
173 . "<div class=\"col-sm-9\">"
174 . "<div class=\"il-input-password\">"
175 . "<input type=\"password\" name=\"$name\" class=\"form-control form-control-sm\" />"
176 . "</div>"
177 . "</div>"
178 . "</div>";
179 $this->assertHTMLEquals($expected, $html);
180 }
181
182
183 public function test_value_required()
184 {
185 $f = $this->buildFactory();
186 $label = "label";
187 $name = "name_0";
188 $pwd = $f->password($label)->withNameFrom($this->name_source)->withRequired(true);
189
190 $pwd1 = $pwd->withInput(new DefPostData([$name => "0"]));
191 $value1 = $pwd1->getContent();
192 $this->assertTrue($value1->isOk());
193
194 $pwd2 = $pwd->withInput(new DefPostData([$name => ""]));
195 $value2 = $pwd2->getContent();
196 $this->assertTrue($value2->isError());
197 }
198
199 public function test_value_type()
200 {
201 $f = $this->buildFactory();
202 $label = "label";
203 $pwd = $f->password($label);
204 $this->assertNull($pwd->getValue());
205
206 $post = new _PWDPostData();
207 $pwd = $pwd->withInput($post);
208 $this->assertEquals($post->getOr('', ''), $pwd->getValue());
209 $this->assertInstanceOf(PWD::class, $pwd->getContent()->value());
210 }
211}
$default
Definition: build.php:20
An exception for terminatinating execution or to throw for unit testing.
Builds data types.
Definition: Factory.php:15
A password is used as part of credentials for authentication.
Definition: Password.php:14
Provides common functionality for UI tests.
Definition: Base.php:192
assertHTMLEquals($expected_html_as_string, $html_as_string)
Definition: Base.php:270
getDefaultRenderer(JavaScriptBinding $js_binding=null)
Definition: Base.php:228
normalizeHTML($html)
Definition: Base.php:261
getOr($name, $default)
Get a named value from the data and fallback to default if that name does not exist.
$html
Definition: example_001.php:87
$r
Definition: example_031.php:79
A transformation is a function from one datatype to another.
Describes how Input-Elements want to interact with posted data.
Definition: PostData.php:13
$post
Definition: post.php:34