ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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->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 class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
135 </div>'
136 . $this->getTextFieldHtml() .
137 '<div class="c-form__footer">
138 <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
139 </div>
140 </form>
141 ');
142 $this->assertHTMLEquals($expected, $html);
143 }
144
145 public function testSubmitCaption(): void
146 {
147 $f = $this->buildFactory();
148 $if = $this->getFieldFactory();
149
150 $url = "MY_URL";
151 $form = $f->standard($url, [
152 $if->text("label", "byline"),
153 ]);
154
155 $this->assertNull($form->getSubmitLabel());
156
157 $caption = 'Caption';
158 $form = $form->withSubmitLabel($caption);
159
160 $this->assertEquals($caption, $form->getSubmitLabel());
161 }
162
163 public function testSubmitCaptionRender(): void
164 {
165 $f = $this->buildFactory();
166 $if = $this->getFieldFactory();
167
168 $url = "MY_URL";
169 $form = $f->standard($url, [
170 $if->text("label", "byline"),
171 ])->withSubmitLabel('create');
172
173 $r = $this->getDefaultRenderer();
174 $html = $this->brutallyTrimHTML($r->render($form));
175
176 $expected = $this->brutallyTrimHTML('
177 <form class="c-form c-form--horizontal" enctype="multipart/form-data" action="MY_URL" method="post">
178 <div class="c-form__header">
179 <div class="c-form__actions"><button class="btn btn-default" data-action="">create</button></div>
180 </div>'
181 . $this->getTextFieldHtml() .
182 '<div class="c-form__footer">
183 <div class="c-form__actions"><button class="btn btn-default" data-action="">create</button></div>
184 </div>
185 </form>
186 ');
187 $this->assertHTMLEquals($expected, $html);
188 }
189
190 public function testRenderNoUrl(): void
191 {
192 $f = $this->buildFactory();
193 $if = $this->getFieldFactory();
194
195 $url = "";
196 $form = $f->standard($url, [
197 $if->text("label", "byline"),
198 ]);
199
200 $r = $this->getDefaultRenderer();
201 $html = $this->brutallyTrimHTML($r->render($form));
202
203 $expected = $this->brutallyTrimHTML('
204 <form class="c-form c-form--horizontal" enctype="multipart/form-data" method="post">
205 <div class="c-form__header">
206 <div class="c-form__actions">
207 <button class="btn btn-default" data-action="">save</button>
208 </div>
209 </div>'
210 . $this->getTextFieldHtml() .
211 '<div class="c-form__footer">
212 <div class="c-form__actions">
213 <button class="btn btn-default" data-action="">save</button>
214 </div>
215 </div>
216 </form>
217 ');
218 $this->assertHTMLEquals($expected, $html);
219 }
220
221
222 public function testRenderWithErrorOnField(): void
223 {
224 $r = $this->getDefaultRenderer();
225 $df = new Data\Factory();
226 $language = $this->createMock(\ILIAS\Language\Language::class);
227 $language
228 ->expects($this->once())
229 ->method("txt")
230 ->willReturn('testing error message');
231
232 $refinery = new \ILIAS\Refinery\Factory($df, $language);
233
235 $this->createMock(\ILIAS\UI\Implementation\Component\Input\Field\Node\Factory::class),
236 $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
237 new SignalGenerator(),
238 $df,
239 $refinery,
240 $language
241 );
242
243 $fail = $refinery->custom()->constraint(function ($v) {
244 return false;
245 }, "This is invalid...");
246 $input = $if->text("label", "byline");
247
248 $input = $input->withAdditionalTransformation($fail);
249
250 $form = new Form\Standard(new SignalGenerator(), $if, new InputNameSource(), '', [$input]);
251
252 $request = $this->createMock(ServerRequestInterface::class);
253 $request
254 ->expects($this->once())
255 ->method("getParsedBody")
256 ->willReturn([
257 'form_0/input_1' => ''
258 ]);
259
260 $form = $form->withRequest($request);
261 $this->assertNull($form->getData());
262
263 $html = $this->brutallyTrimHTML($r->render($form));
264 $expected = $this->brutallyTrimHTML('
265<form class="c-form c-form--horizontal" enctype="multipart/form-data" method="post">
266 <div class="c-form__header">
267 <div class="c-form__actions">
268 <button class="btn btn-default" data-action="">save</button>
269 </div>
270 </div>
271 <div class="c-form__error-msg alert alert-danger"><span class="sr-only">ui_error:</span>testing error
272 message
273 </div>
274 <fieldset class="c-input" data-il-ui-component="text-field-input" data-il-ui-input-name="form_0/input_1"
275 aria-describedby="id_2"><label for="id_1">label</label>
276 <div class="c-input__field"><input id="id_1" type="text" name="form_0/input_1" class="c-field-text" /></div>
277 <div class="c-input__error-msg alert alert-danger" id="id_2"><span class="sr-only">ui_error:</span>This is
278 invalid...
279 </div>
280 <div class="c-input__help-byline">byline</div>
281 </fieldset>
282 <div class="c-form__footer">
283 <div class="c-form__actions">
284 <button class="btn btn-default" data-action="">save</button>
285 </div>
286 </div>
287</form>
288');
289 $this->assertEquals($expected, $html);
290 $this->assertHTMLEquals($expected, $html);
291 }
292
293
294 public function testRenderWithErrorOnForm(): void
295 {
296 $r = $this->getDefaultRenderer();
297 $df = new Data\Factory();
298 $language = $this->createMock(\ILIAS\Language\Language::class);
299 $refinery = new \ILIAS\Refinery\Factory($df, $language);
300
302 $this->createMock(\ILIAS\UI\Implementation\Component\Input\Field\Node\Factory::class),
303 $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
304 new SignalGenerator(),
305 $df,
306 $refinery,
307 $language
308 );
309
310 $fail = $refinery->custom()->constraint(function ($v) {
311 return false;
312 }, "This is a fail on form.");
313 $input = $if->text("label", "byline");
314
315 $form = new Form\Standard(new SignalGenerator(), $if, new InputNameSource(), '', [$input]);
316 $form = $form->withAdditionalTransformation($fail);
317
318 $request = $this->createMock(ServerRequestInterface::class);
319 $request
320 ->expects($this->once())
321 ->method("getParsedBody")
322 ->willReturn([
323 'form_0/input_1' => ''
324 ]);
325
326 $form = $form->withRequest($request);
327 $this->assertNull($form->getData());
328
329
330 $field_html = $this->getFormWrappedHtml(
331 'text-field-input',
332 'label',
333 '<input id="id_1" type="text" name="form_0/input_1" class="c-field-text"/>',
334 'byline',
335 'id_1',
336 null,
337 'form_0/input_1'
338 );
339
340 $html = $this->brutallyTrimHTML($r->render($form));
341 $expected = $this->brutallyTrimHTML('
342 <form class="c-form c-form--horizontal" enctype="multipart/form-data" method="post">
343 <div class="c-form__header">
344 <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
345 </div>
346 <div class="c-form__error-msg alert alert-danger"><span class="sr-only">ui_error:</span>This is a fail on form.</div>
347 ' . $field_html . '
348 <div class="c-form__footer">
349 <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
350 </div>
351 </form>
352 ');
353 $this->assertHTMLEquals($expected, $html);
354 }
355
357 {
358 $f = $this->buildFactory();
359 $if = $this->getFieldFactory();
360
361 $url = "MY_URL";
362 $form = $f->standard($url, [$if->text("label", "byline")->withRequired(true)]);
363
364 $r = $this->getDefaultRenderer();
365 $html = $this->brutallyTrimHTML($r->render($form));
366
367 $field_html = $this->getFormWrappedHtml(
368 'text-field-input',
369 'label<span class="asterisk" aria-label="required_field">*</span>',
370 '<input id="id_1" type="text" name="form/input_0" class="c-field-text" />',
371 'byline',
372 'id_1',
373 null,
374 'form/input_0'
375 );
376
377 $expected = $this->brutallyTrimHTML('
378<form class="c-form c-form--horizontal" enctype="multipart/form-data" action="MY_URL" method="post">
379 <div class="c-form__header">
380 <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
381 <div class="c-form__required">
382 <span class="asterisk">*</span><span class="small"> required_field</span>
383 </div>
384 </div>
385 ' . $field_html . '
386 <div class="c-form__footer">
387 <div class="c-form__required">
388 <span class="asterisk">*</span><span class="small"> required_field</span>
389 </div>
390 <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
391 </div>
392</form>
393 ');
394 $this->assertHTMLEquals($expected, $html);
395 }
396}
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:34
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:68