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