ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
StandardFormTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(__DIR__ . "/../../../../../../../../vendor/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../../Base.php");
23require_once(__DIR__ . "/FormTest.php");
24require_once(__DIR__ . "/../../Field/CommonFieldRendering.php");
25
27use ILIAS\Data;
30use Psr\Http\Message\ServerRequestInterface;
33
35{
37
39 {
40 $this->button_factory = $button_factory;
41 }
42
43 public function button(): Factory
44 {
46 }
47}
48
50{
51 public int $count = 0;
52
53 public function getNewName(): string
54 {
55 $name = "input_{$this->count}";
56 $this->count++;
57
58 return $name;
59 }
60
61 public function getNewDedicatedName(string $dedicated_name): string
62 {
63 $name = $dedicated_name . "_{$this->count}";
64 $this->count++;
65
66 return $name;
67 }
68}
69
74{
75 use CommonFieldRendering;
76
77 protected function buildFactory(): I\Input\Container\Form\Factory
78 {
79
80 return new I\Input\Container\Form\Factory(
81 $this->getFieldFactory(),
82 new SignalGenerator()
83 );
84 }
85
86 protected function buildButtonFactory(): I\Button\Factory
87 {
88 return new I\Button\Factory();
89 }
90
92 {
93 return new WithButtonNoUIFactory($this->buildButtonFactory());
94 }
95
96 public function testGetPostURL(): void
97 {
98 $f = $this->buildFactory();
99 $if = $this->getFieldFactory();
100 $url = "MY_URL";
101 $form = $f->standard($url, [$if->text("label")]);
102 $this->assertEquals($url, $form->getPostURL());
103 }
104
105 protected function getTextFieldHtml(): string
106 {
107 return $this->getFormWrappedHtml(
108 'text-field-input',
109 'label',
110 '<input id="id_1" type="text" name="form/input_0" class="c-field-text" />',
111 'byline',
112 'id_1',
113 null,
114 'form/input_0'
115 );
116 }
117
118 public function testRender(): void
119 {
120 $f = $this->buildFactory();
121 $if = $this->getFieldFactory();
122
123 $url = "MY_URL";
124 $form = $f->standard($url, [
125 $if->text("label", "byline"),
126 ]);
127
128 $r = $this->getDefaultRenderer();
129 $html = $this->brutallyTrimHTML($this->getDefaultRenderer()->render($form));
130
131 $expected = $this->brutallyTrimHTML('
132 <form class="c-form c-form--horizontal" enctype="multipart/form-data" action="MY_URL" method="post">
133 <div class="c-form__header">
134 </div>'
135 . $this->getTextFieldHtml() .
136 '<div class="c-form__footer">
137 <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
138 </div>
139 </form>
140 ');
141 $this->assertHTMLEquals($expected, $html);
142 }
143
144 public function testSubmitCaption(): void
145 {
146 $f = $this->buildFactory();
147 $if = $this->getFieldFactory();
148
149 $url = "MY_URL";
150 $form = $f->standard($url, [
151 $if->text("label", "byline"),
152 ]);
153
154 $this->assertNull($form->getSubmitLabel());
155
156 $caption = 'Caption';
157 $form = $form->withSubmitLabel($caption);
158
159 $this->assertEquals($caption, $form->getSubmitLabel());
160 }
161
162 public function testSubmitCaptionRender(): void
163 {
164 $f = $this->buildFactory();
165 $if = $this->getFieldFactory();
166
167 $url = "MY_URL";
168 $form = $f->standard($url, [
169 $if->text("label", "byline"),
170 ])->withSubmitLabel('create');
171
172 $r = $this->getDefaultRenderer();
173 $html = $this->brutallyTrimHTML($r->render($form));
174
175 $expected = $this->brutallyTrimHTML('
176 <form class="c-form c-form--horizontal" enctype="multipart/form-data" action="MY_URL" method="post">
177 <div class="c-form__header">
178 </div>'
179 . $this->getTextFieldHtml() .
180 '<div class="c-form__footer">
181 <div class="c-form__actions"><button class="btn btn-default" data-action="">create</button></div>
182 </div>
183 </form>
184 ');
185 $this->assertHTMLEquals($expected, $html);
186 }
187
188 public function testRenderNoUrl(): void
189 {
190 $f = $this->buildFactory();
191 $if = $this->getFieldFactory();
192
193 $url = "";
194 $form = $f->standard($url, [
195 $if->text("label", "byline"),
196 ]);
197
198 $r = $this->getDefaultRenderer();
199 $html = $this->brutallyTrimHTML($r->render($form));
200
201 $expected = $this->brutallyTrimHTML('
202 <form class="c-form c-form--horizontal" enctype="multipart/form-data" method="post">
203 <div class="c-form__header">
204 </div>'
205 . $this->getTextFieldHtml() .
206 '<div class="c-form__footer">
207 <div class="c-form__actions">
208 <button class="btn btn-default" data-action="">save</button>
209 </div>
210 </div>
211 </form>
212 ');
213 $this->assertHTMLEquals($expected, $html);
214 }
215
216
217 public function testRenderWithErrorOnField(): void
218 {
219 $r = $this->getDefaultRenderer();
220 $df = new Data\Factory();
221 $language = $this->createMock(\ILIAS\Language\Language::class);
222 $language
223 ->expects($this->once())
224 ->method("txt")
225 ->willReturn('testing error message');
226
227 $refinery = new \ILIAS\Refinery\Factory($df, $language);
228
230 $this->createMock(\ILIAS\UI\Implementation\Component\Input\Field\Node\Factory::class),
231 $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
232 new SignalGenerator(),
233 $df,
234 $refinery,
235 $language
236 );
237
238 $fail = $refinery->custom()->constraint(function ($v) {
239 return false;
240 }, "This is invalid...");
241 $input = $if->text("label", "byline");
242
243 $input = $input->withAdditionalTransformation($fail);
244
245 $form = new Form\Standard(new SignalGenerator(), $if, new InputNameSource(), '', [$input]);
246
247 $request = $this->createMock(ServerRequestInterface::class);
248 $request
249 ->expects($this->once())
250 ->method("getParsedBody")
251 ->willReturn([
252 'form_0/input_1' => ''
253 ]);
254
255 $form = $form->withRequest($request);
256 $this->assertNull($form->getData());
257
258 $html = $this->brutallyTrimHTML($r->render($form));
259 $expected = $this->brutallyTrimHTML('
260<form class="c-form c-form--horizontal" enctype="multipart/form-data" method="post">
261 <div class="c-form__header">
262 </div>
263 <div class="c-form__error-msg alert alert-danger"><span class="sr-only">ui_error:</span>testing error
264 message
265 </div>
266 <fieldset class="c-input" data-il-ui-component="text-field-input" data-il-ui-input-name="form_0/input_1"
267 aria-describedby="id_2"><label for="id_1">label</label>
268 <div class="c-input__field"><input id="id_1" type="text" name="form_0/input_1" class="c-field-text" /></div>
269 <div class="c-input__error-msg alert alert-danger" id="id_2"><span class="sr-only">ui_error:</span>This is
270 invalid...
271 </div>
272 <div class="c-input__help-byline">byline</div>
273 </fieldset>
274 <div class="c-form__footer">
275 <div class="c-form__actions">
276 <button class="btn btn-default" data-action="">save</button>
277 </div>
278 </div>
279</form>
280');
281 $this->assertEquals($expected, $html);
282 $this->assertHTMLEquals($expected, $html);
283 }
284
285
286 public function testRenderWithErrorOnForm(): void
287 {
288 $r = $this->getDefaultRenderer();
289 $df = new Data\Factory();
290 $language = $this->createMock(\ILIAS\Language\Language::class);
291 $refinery = new \ILIAS\Refinery\Factory($df, $language);
292
294 $this->createMock(\ILIAS\UI\Implementation\Component\Input\Field\Node\Factory::class),
295 $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
296 new SignalGenerator(),
297 $df,
298 $refinery,
299 $language
300 );
301
302 $fail = $refinery->custom()->constraint(function ($v) {
303 return false;
304 }, "This is a fail on form.");
305 $input = $if->text("label", "byline");
306
307 $form = new Form\Standard(new SignalGenerator(), $if, new InputNameSource(), '', [$input]);
308 $form = $form->withAdditionalTransformation($fail);
309
310 $request = $this->createMock(ServerRequestInterface::class);
311 $request
312 ->expects($this->once())
313 ->method("getParsedBody")
314 ->willReturn([
315 'form_0/input_1' => ''
316 ]);
317
318 $form = $form->withRequest($request);
319 $this->assertNull($form->getData());
320
321
322 $field_html = $this->getFormWrappedHtml(
323 'text-field-input',
324 'label',
325 '<input id="id_1" type="text" name="form_0/input_1" class="c-field-text"/>',
326 'byline',
327 'id_1',
328 null,
329 'form_0/input_1'
330 );
331
332 $html = $this->brutallyTrimHTML($r->render($form));
333 $expected = $this->brutallyTrimHTML('
334 <form class="c-form c-form--horizontal" enctype="multipart/form-data" method="post">
335 <div class="c-form__header">
336 </div>
337 <div class="c-form__error-msg alert alert-danger"><span class="sr-only">ui_error:</span>This is a fail on form.</div>
338 ' . $field_html . '
339 <div class="c-form__footer">
340 <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
341 </div>
342 </form>
343 ');
344 $this->assertHTMLEquals($expected, $html);
345 }
346
348 {
349 $f = $this->buildFactory();
350 $if = $this->getFieldFactory();
351
352 $url = "MY_URL";
353 $form = $f->standard($url, [$if->text("label", "byline")->withRequired(true)]);
354
355 $r = $this->getDefaultRenderer();
356 $html = $this->brutallyTrimHTML($r->render($form));
357
358 $field_html = $this->getFormWrappedHtml(
359 'text-field-input',
360 'label<span class="sr-only">required_field</span><span class="asterisk" aria-hidden="true">*</span>',
361 '<input id="id_1" type="text" name="form/input_0" class="c-field-text" />',
362 'byline',
363 'id_1',
364 null,
365 'form/input_0'
366 );
367
368 $expected = $this->brutallyTrimHTML('
369<form class="c-form c-form--horizontal" enctype="multipart/form-data" action="MY_URL" method="post">
370 <div class="c-form__header">
371 <div class="c-form__required">
372 <span class="asterisk">*</span><span class="small"> required_field</span>
373 </div>
374 </div>
375 ' . $field_html . '
376 <div class="c-form__footer">
377 <div class="c-form__required">
378 <span class="asterisk">*</span><span class="small"> required_field</span>
379 </div>
380 <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
381 </div>
382</form>
383 ');
384 $this->assertHTMLEquals($expected, $html);
385 }
386}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Factory for Date Formats.
Definition: Factory.php:27
This implements commonalities between all forms.
Definition: Container.php:35
This implements commonalities between all forms.
Definition: Form.php:35
Definition: UI.php:24
Provides common functionality for UI tests.
Definition: Base.php:337
getNewName()
Generates a unique name on every call.
getNewDedicatedName(string $dedicated_name)
Test on standard form implementation.
__construct(Factory $button_factory)
This describes commonalities between all inputs.
Definition: Input.php:47
Describes a source for input names.
Definition: NameSource.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: Bulky.php:21
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
$url
Definition: shib_logout.php:70