ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
FormTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/../../../../Base.php");
22 
30 use ILIAS\Data;
36 
37 class FixedNameSource implements NameSource
38 {
39  public string $name = "name";
40 
41  public function getNewName(): string
42  {
43  return $this->name;
44  }
45 }
46 
47 class ConcreteForm extends Form
48 {
49  public ?Input\InputData $input_data = null;
50  protected Input\Field\Factory $input_factory;
51  protected Group $input_group;
52  protected array $inputs;
53 
54  public function __construct(Input\Field\Factory $input_factory, NameSource $name_source, array $inputs)
55  {
56  $this->input_factory = $input_factory;
57  parent::__construct($input_factory, $name_source, $inputs);
58  }
59 
60  public function _extractRequestData(ServerRequestInterface $request): Input\InputData
61  {
62  return $this->extractRequestData($request);
63  }
64 
65  public function extractRequestData(ServerRequestInterface $request): Input\InputData
66  {
67  if ($this->input_data !== null) {
68  return $this->input_data;
69  }
70 
71  return parent::extractRequestData($request);
72  }
73 
74 
75  public function setInputs(array $inputs): void
76  {
77  $this->input_group = $this->input_factory->group($inputs);
78  $this->inputs = $inputs;
79  }
80 }
81 
86 {
90  protected $language;
91  protected array $inputs;
92 
93  protected function buildFactory(): Input\Container\Form\Factory
94  {
95  return new Input\Container\Form\Factory($this->buildInputFactory());
96  }
97 
98  protected function buildInputFactory(): Input\Field\Factory
99  {
100  $df = new Data\Factory();
101  $this->language = $this->createMock(ilLanguage::class);
102  return new Input\Field\Factory(
103  $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
104  new SignalGenerator(),
105  $df,
106  new Refinery($df, $this->language),
107  $this->language
108  );
109  }
110 
111  protected function buildButtonFactory(): I\Button\Factory
112  {
113  return new I\Button\Factory();
114  }
115 
116  protected function buildTransformation(Closure $trafo): Transformation
117  {
118  $dataFactory = new Data\Factory();
119  $language = $this->createMock(ilLanguage::class);
120  $refinery = new Refinery($dataFactory, $language);
121 
122  return $refinery->custom()->transformation($trafo);
123  }
124 
125  public function getUIFactory(): NoUIFactory
126  {
127  return new WithButtonNoUIFactory($this->buildButtonFactory());
128  }
129 
130  public function buildDataFactory(): Data\Factory
131  {
132  return new Data\Factory();
133  }
134 
135  public function testGetInputs(): void
136  {
137  $this->buildFactory();
138  $if = $this->buildInputFactory();
139  $name_source = new FixedNameSource();
140 
141  $inputs = [$if->text(""), $if->text("")];
142  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), $inputs);
143 
144  $seen_names = [];
145  $form_inputs = $form->getInputs();
146  $this->assertSameSize($inputs, $form_inputs);
147 
148  foreach ($form_inputs as $input) {
149  $name = $input->getName();
150  $name_source->name = $name;
151 
152  // name is a string
153  $this->assertIsString($name);
154 
155  // only name is attached
156  $input = array_shift($form_inputs);
157  $this->assertEquals($input->withNameFrom($name_source), $input);
158 
159  // every name can only be contained once.
160  $this->assertNotContains($name, $seen_names);
161  $seen_names[] = $name;
162  }
163  }
164 
165  public function testExtractPostData(): void
166  {
167  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), []);
168  $request = $this->createMock(ServerRequestInterface::class);
169  $request
170  ->expects($this->once())
171  ->method("getParsedBody")
172  ->willReturn([]);
173  $input_data = $form->_extractRequestData($request);
174  $this->assertInstanceOf(InputData::class, $input_data);
175  }
176 
177  public function testWithRequest(): void
178  {
179  $df = $this->buildDataFactory();
180  $request = $this->createMock(ServerRequestInterface::class);
181  $input_data = $this->createMock(InputData::class);
182 
183  $input_1 = $this->inputMock();
184  $input_1
185  ->expects($this->once())
186  ->method("withInput")
187  ->with($input_data)
188  ->willReturn($input_1);
189  $input_1
190  ->expects($this->once())
191  ->method("getContent")
192  ->willReturn($df->ok(0));
193 
194  $input_2 = $this->inputMock();
195  $input_2
196  ->expects($this->once())
197  ->method("withInput")
198  ->with($input_data)
199  ->willReturn($input_2);
200  $input_2
201  ->expects($this->once())
202  ->method("getContent")
203  ->willReturn($df->ok(0));
204 
205  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), []);
206  $form->setInputs([$input_1, $input_2]);
207  $form->input_data = $input_data;
208 
209  $form2 = $form->withRequest($request);
210 
211  $this->assertNotSame($form2, $form);
212  $this->assertInstanceOf(Form::class, $form2);
213  $this->assertEquals([$input_1, $input_2], $form2->getInputs());
214  }
215 
216  public function testWithRequestRespectsKeys(): void
217  {
218  $df = $this->buildDataFactory();
219  $request = $this->createMock(ServerRequestInterface::class);
220  $input_data = $this->createMock(InputData::class);
221 
222  $input_1 = $this->inputMock();
223  $input_1
224  ->expects($this->once())
225  ->method("withInput")
226  ->with($input_data)
227  ->willReturn($input_1);
228  $input_1
229  ->expects($this->once())
230  ->method("getContent")
231  ->willReturn($df->ok(0));
232 
233  $input_2 = $this->inputMock();
234  $input_2
235  ->expects($this->once())
236  ->method("withInput")
237  ->with($input_data)
238  ->willReturn($input_2);
239  $input_2
240  ->expects($this->once())
241  ->method("getContent")
242  ->willReturn($df->ok(0));
243 
244  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), []);
245  $form->setInputs(["foo" => $input_1, "bar" => $input_2]);
246  $form->input_data = $input_data;
247 
248  $form2 = $form->withRequest($request);
249 
250  $this->assertNotSame($form2, $form);
251  $this->assertInstanceOf(Form::class, $form2);
252  $this->assertEquals(["foo" => $input_1, "bar" => $input_2], $form2->getInputs());
253  }
254 
255  public function testGetData(): void
256  {
257  $df = $this->buildDataFactory();
258  $request = $this->createMock(ServerRequestInterface::class);
259  $request
260  ->expects($this->once())
261  ->method("getParsedBody")
262  ->willReturn([]);
263 
264  $input_1 = $this->inputMock();
265  $input_1
266  ->expects($this->once())
267  ->method("getContent")
268  ->willReturn($df->ok(1));
269  $input_1
270  ->expects($this->once())
271  ->method("withInput")
272  ->willReturn($input_1);
273 
274  $input_2 = $this->inputMock();
275  $input_2
276  ->expects($this->once())
277  ->method("getContent")
278  ->willReturn($df->ok(2));
279  $input_2
280  ->expects($this->once())
281  ->method("withInput")
282  ->willReturn($input_2);
283 
284  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), []);
285  $form->setInputs([$input_1, $input_2]);
286  $form = $form->withRequest($request);
287  $this->assertEquals([1, 2], $form->getData());
288  }
289 
290  public function testGetDataRespectsKeys(): void
291  {
292  $df = $this->buildDataFactory();
293  $request = $this->createMock(ServerRequestInterface::class);
294  $request
295  ->expects($this->once())
296  ->method("getParsedBody")
297  ->willReturn([]);
298 
299  $input_1 = $this->inputMock();
300  $input_1
301  ->expects($this->once())
302  ->method("getContent")
303  ->willReturn($df->ok(1));
304  $input_1
305  ->expects($this->once())
306  ->method("withInput")
307  ->willReturn($input_1);
308 
309  $input_2 = $this->inputMock();
310  $input_2
311  ->expects($this->once())
312  ->method("getContent")
313  ->willReturn($df->ok(2));
314  $input_2
315  ->expects($this->once())
316  ->method("withInput")
317  ->willReturn($input_2);
318 
319  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), []);
320  $form->setInputs(["foo" => $input_1, "bar" => $input_2]);
321  $form = $form->withRequest($request);
322  $this->assertEquals(["foo" => 1, "bar" => 2], $form->getData());
323  }
324 
325  public function testGetDataFaulty(): void
326  {
327  $df = $this->buildDataFactory();
328  $request = $this->createMock(ServerRequestInterface::class);
329  $request
330  ->expects($this->once())
331  ->method("getParsedBody")
332  ->willReturn([]);
333 
334  $input_1 = $this->inputMock();
335  $input_1
336  ->expects($this->once())
337  ->method("getContent")
338  ->willReturn($df->error("error"));
339  $input_1
340  ->expects($this->once())
341  ->method("withInput")
342  ->willReturn($input_1);
343 
344  $input_2 = $this->inputMock();
345  $input_2
346  ->expects($this->once())
347  ->method("getContent")
348  ->willReturn($df->ok(2));
349  $input_2
350  ->expects($this->once())
351  ->method("withInput")
352  ->willReturn($input_2);
353 
354  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), []);
355  $form->setInputs(["foo" => $input_1, "bar" => $input_2]);
356 
357  $i18n = "THERE IS SOME ERROR IN THIS GROUP";
358  $this->language
359  ->expects($this->once())
360  ->method("txt")
361  ->with("ui_error_in_group")
362  ->willReturn($i18n);
363 
364  //Todo: This is not good, this should throw an error or similar.
365  $form = $form->withRequest($request);
366  $this->assertEquals(null, null);
367  }
368 
369  public function testWithAdditionalTransformation(): void
370  {
371  $df = $this->buildDataFactory();
372  $request = $this->createMock(ServerRequestInterface::class);
373  $request
374  ->expects($this->once())
375  ->method("getParsedBody")
376  ->willReturn([]);
377 
378  $input_1 = $this->inputMock();
379  $input_1
380  ->expects($this->once())
381  ->method("getContent")
382  ->willReturn($df->ok(1));
383  $input_1
384  ->expects($this->once())
385  ->method("withInput")
386  ->willReturn($input_1);
387 
388  $input_2 = $this->inputMock();
389  $input_2
390  ->expects($this->once())
391  ->method("getContent")
392  ->willReturn($df->ok(2));
393  $input_2
394  ->expects($this->once())
395  ->method("withInput")
396  ->willReturn($input_2);
397 
398  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), []);
399  $form->setInputs([$input_1, $input_2]);
400 
401  $form2 = $form->withAdditionalTransformation($this->buildTransformation(function () {
402  return "transformed";
403  }));
404 
405  $this->assertNotSame($form2, $form);
406  $form2 = $form2->withRequest($request);
407 
408  $this->assertEquals("transformed", $form2->getData());
409  }
410 
411  public function testNameInputsRespectsKeys(): void
412  {
413  $if = $this->buildInputFactory();
414  $inputs = [
415  2 => $if->text(""),
416  "foo" => $if->text(""),
417  1 => $if->text(""),
418  $if->text(""),
419  ];
420  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), []);
421  $form->setInputs($inputs);
422  $named_inputs = $form->getInputs();
423  $this->assertEquals(array_keys($inputs), array_keys($named_inputs));
424  }
425 
429  protected function inputMock()
430  {
431  static $no = 1000;
432  return $this
433  ->getMockBuilder(Input\Field\FormInputInternal::class)
434  ->onlyMethods([
435  "getName",
436  "withDedicatedName",
437  "withNameFrom",
438  "withInput",
439  "getContent",
440  "getLabel",
441  "withLabel",
442  "getByline",
443  "withByline",
444  "isRequired",
445  "withRequired",
446  "isDisabled",
447  "withDisabled",
448  "getValue",
449  "withValue",
450  "getError",
451  "withError",
452  "withAdditionalTransformation",
453  "getUpdateOnLoadCode",
454  "getCanonicalName",
455  "withOnLoadCode",
456  "withAdditionalOnLoadCode",
457  "getOnLoadCode",
458  "withOnUpdate",
459  "appendOnUpdate",
460  "withResetTriggeredSignals",
461  "getTriggeredSignals"
462  ])
463  ->setMockClassName("Mock_InputNo" . ($no++))
464  ->getMock();
465  }
466 
467  public function testFormWithoutRequiredField(): void
468  {
469  $f = $this->buildFactory();
470  $if = $this->buildInputFactory();
471  $inputs = [$if->text(""), $if->text("")];
472  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), $inputs);
473 
474  $this->assertFalse($form->hasRequiredInputs());
475  }
476 
477  public function testFormWithRequiredField(): void
478  {
479  $f = $this->buildFactory();
480  $if = $this->buildInputFactory();
481  $inputs = [
482  $if->text("")->withRequired(true),
483  $if->text("")
484  ];
485  $form = new ConcreteForm($this->buildInputFactory(), new DefNamesource(), $inputs);
486  $this->assertTrue($form->hasRequiredInputs());
487  }
488 }
buildInputFactory()
Definition: FormTest.php:98
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testGetDataFaulty()
Definition: FormTest.php:325
Transform values according to custom configuration.
testWithAdditionalTransformation()
Definition: FormTest.php:369
inputMock()
Definition: FormTest.php:429
testWithRequestRespectsKeys()
Definition: FormTest.php:216
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
testGetData()
Definition: FormTest.php:255
testGetDataRespectsKeys()
Definition: FormTest.php:290
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getNewName()
Generates a unique name on every call.
Definition: FormTest.php:41
testGetInputs()
Definition: FormTest.php:135
testWithRequest()
Definition: FormTest.php:177
Test on form implementation.
Definition: FormTest.php:85
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
testExtractPostData()
Definition: FormTest.php:165
testFormWithRequiredField()
Definition: FormTest.php:477
__construct(Input\Field\Factory $input_factory, NameSource $name_source, array $inputs)
Definition: FormTest.php:54
array $inputs
Definition: FormTest.php:91
string $name
Definition: FormTest.php:39
array $inputs
Definition: FormTest.php:52
buildDataFactory()
Definition: FormTest.php:130
This implements commonalities between all forms.
Definition: Form.php:33
_extractRequestData(ServerRequestInterface $request)
Definition: FormTest.php:60
__construct(VocabulariesInterface $vocabularies)
Provides common functionality for UI tests.
Definition: Base.php:310
getUIFactory()
Definition: FormTest.php:125
extractRequestData(ServerRequestInterface $request)
Definition: FormTest.php:65
testFormWithoutRequiredField()
Definition: FormTest.php:467
testNameInputsRespectsKeys()
Definition: FormTest.php:411
setInputs(array $inputs)
Definition: FormTest.php:75
Group $input_group
Definition: FormTest.php:51
This describes commonalities between all inputs.
Definition: Input.php:46
buildButtonFactory()
Definition: FormTest.php:111
Describes a source for input names.
Definition: NameSource.php:26
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
buildFactory()
Definition: FormTest.php:93
buildTransformation(Closure $trafo)
Definition: FormTest.php:116
Input Field Factory $input_factory
Definition: FormTest.php:50
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Refinery Factory $refinery