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