ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
StandardFormTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/../../../../../../../../vendor/composer/vendor/autoload.php");
22 require_once(__DIR__ . "/../../../../Base.php");
23 require_once(__DIR__ . "/FormTest.php");
24 require_once(__DIR__ . "/../../Field/CommonFieldRendering.php");
25 
27 use ILIAS\Data;
33 
35 {
37 
38  public function __construct(Factory $button_factory)
39  {
40  $this->button_factory = $button_factory;
41  }
42 
43  public function button(): Factory
44  {
45  return $this->button_factory;
46  }
47 }
48 
49 class InputNameSource implements NameSource
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 {
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\UploadLimitResolver::class),
236  new SignalGenerator(),
237  $df,
238  $refinery,
239  $language
240  );
241 
242  $fail = $refinery->custom()->constraint(function ($v) {
243  return false;
244  }, "This is invalid...");
245  $input = $if->text("label", "byline");
246 
247  $input = $input->withAdditionalTransformation($fail);
248 
249  $form = new Form\Standard(new SignalGenerator(), $if, new InputNameSource(), '', [$input]);
250 
251  $request = $this->createMock(ServerRequestInterface::class);
252  $request
253  ->expects($this->once())
254  ->method("getParsedBody")
255  ->willReturn([
256  'form_0/input_1' => ''
257  ]);
258 
259  $form = $form->withRequest($request);
260  $this->assertNull($form->getData());
261 
262  $html = $this->brutallyTrimHTML($r->render($form));
263  $expected = $this->brutallyTrimHTML('
264 <form class="c-form c-form--horizontal" enctype="multipart/form-data" method="post">
265  <div class="c-form__header">
266  <div class="c-form__actions">
267  <button class="btn btn-default" data-action="">save</button>
268  </div>
269  </div>
270  <div class="c-form__error-msg alert alert-danger"><span class="sr-only">ui_error:</span>testing error
271  message
272  </div>
273  <fieldset class="c-input" data-il-ui-component="text-field-input" data-il-ui-input-name="form_0/input_1"
274  aria-describedby="id_2"><label for="id_1">label</label>
275  <div class="c-input__field"><input id="id_1" type="text" name="form_0/input_1" class="c-field-text" /></div>
276  <div class="c-input__error-msg alert alert-danger" id="id_2"><span class="sr-only">ui_error:</span>This is
277  invalid...
278  </div>
279  <div class="c-input__help-byline">byline</div>
280  </fieldset>
281  <div class="c-form__footer">
282  <div class="c-form__actions">
283  <button class="btn btn-default" data-action="">save</button>
284  </div>
285  </div>
286 </form>
287 ');
288  $this->assertEquals($expected, $html);
289  $this->assertHTMLEquals($expected, $html);
290  }
291 
292 
293  public function testRenderWithErrorOnForm(): void
294  {
295  $r = $this->getDefaultRenderer();
296  $df = new Data\Factory();
297  $language = $this->createMock(\ILIAS\Language\Language::class);
298  $refinery = new \ILIAS\Refinery\Factory($df, $language);
299 
301  $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
302  new SignalGenerator(),
303  $df,
304  $refinery,
305  $language
306  );
307 
308  $fail = $refinery->custom()->constraint(function ($v) {
309  return false;
310  }, "This is a fail on form.");
311  $input = $if->text("label", "byline");
312 
313  $form = new Form\Standard(new SignalGenerator(), $if, new InputNameSource(), '', [$input]);
314  $form = $form->withAdditionalTransformation($fail);
315 
316  $request = $this->createMock(ServerRequestInterface::class);
317  $request
318  ->expects($this->once())
319  ->method("getParsedBody")
320  ->willReturn([
321  'form_0/input_1' => ''
322  ]);
323 
324  $form = $form->withRequest($request);
325  $this->assertNull($form->getData());
326 
327 
328  $field_html = $this->getFormWrappedHtml(
329  'text-field-input',
330  'label',
331  '<input id="id_1" type="text" name="form_0/input_1" class="c-field-text"/>',
332  'byline',
333  'id_1',
334  null,
335  'form_0/input_1'
336  );
337 
338  $html = $this->brutallyTrimHTML($r->render($form));
339  $expected = $this->brutallyTrimHTML('
340  <form class="c-form c-form--horizontal" enctype="multipart/form-data" method="post">
341  <div class="c-form__header">
342  <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
343  </div>
344  <div class="c-form__error-msg alert alert-danger"><span class="sr-only">ui_error:</span>This is a fail on form.</div>
345  ' . $field_html . '
346  <div class="c-form__footer">
347  <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
348  </div>
349  </form>
350  ');
351  $this->assertHTMLEquals($expected, $html);
352  }
353 
354  public function testStandardFormRenderWithRequired(): void
355  {
356  $f = $this->buildFactory();
357  $if = $this->getFieldFactory();
358 
359  $url = "MY_URL";
360  $form = $f->standard($url, [$if->text("label", "byline")->withRequired(true)]);
361 
362  $r = $this->getDefaultRenderer();
363  $html = $this->brutallyTrimHTML($r->render($form));
364 
365  $field_html = $this->getFormWrappedHtml(
366  'text-field-input',
367  'label<span class="asterisk" aria-label="required_field">*</span>',
368  '<input id="id_1" type="text" name="form/input_0" class="c-field-text" />',
369  'byline',
370  'id_1',
371  null,
372  'form/input_0'
373  );
374 
375  $expected = $this->brutallyTrimHTML('
376 <form class="c-form c-form--horizontal" enctype="multipart/form-data" action="MY_URL" method="post">
377  <div class="c-form__header">
378  <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
379  <div class="c-form__required">
380  <span class="asterisk">*</span><span class="small"> required_field</span>
381  </div>
382  </div>
383  ' . $field_html . '
384  <div class="c-form__footer">
385  <div class="c-form__required">
386  <span class="asterisk">*</span><span class="small"> required_field</span>
387  </div>
388  <div class="c-form__actions"><button class="btn btn-default" data-action="">save</button></div>
389  </div>
390 </form>
391  ');
392  $this->assertHTMLEquals($expected, $html);
393  }
394 }
getNewDedicatedName(string $dedicated_name)
Interface Observer Contains several chained tasks and infos about them.
This implements commonalities between all forms.
Definition: Container.php:34
__construct(Factory $button_factory)
$url
Definition: shib_logout.php:66
button()
description: purpose: > Buttons trigger interactions that change the system’s or view&#39;s status...
Builds a Color from either hex- or rgb values.
Definition: Factory.php:30
This implements commonalities between all forms.
Definition: Form.php:33
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
Test on standard form implementation.
getNewName()
Generates a unique name on every call.
This describes commonalities between all inputs.
Definition: Input.php:46
Describes a source for input names.
Definition: NameSource.php:26
$r