ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
FormTest.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 2017 Richard Klees <richard.klees@concepts-and-training.de> Extended GPL, see docs/LICENSE */
4 
5 require_once(__DIR__ . "/../../../../Base.php");
6 
8 use \ILIAS\UI\Implementation\Component\Input\Field\InputInternal;
9 use \ILIAS\UI\Implementation\Component\Input\NameSource;
10 use \ILIAS\UI\Implementation\Component\Input\InputData;
11 use \ILIAS\UI\Implementation\Component\Input\Container\Form\Form;
13 
14 use \ILIAS\Data;
15 use ILIAS\Refinery;
16 
18 
19 class FixedNameSource implements NameSource
20 {
21  public $name = "name";
22 
23  public function getNewName()
24  {
25  return $this->name;
26  }
27 }
28 
29 class ConcreteForm extends Form
30 {
31  public $input_data = null;
32 
33  public function __construct(Input\Field\Factory $field_factory, array $inputs)
34  {
35  $this->input_factory = $field_factory;
36  parent::__construct($field_factory, $inputs);
37  }
38 
39  public function _extractPostData(ServerRequestInterface $request)
40  {
41  return $this->extractPostData($request);
42  }
43 
44 
45  public function extractPostData(ServerRequestInterface $request)
46  {
47  if ($this->input_data !== null) {
48  return $this->input_data;
49  }
50 
51  return parent::extractPostData($request);
52  }
53 
54 
55  public function setInputs(array $inputs)
56  {
57  $this->input_group = $this->input_factory->group($inputs);
58  $this->inputs = $inputs;
59  }
60 
61 
62  public function _getPostInput(ServerRequestInterface $request)
63  {
64  return $this->getPostInput($request);
65  }
66 }
67 
72 {
73  protected function buildFactory()
74  {
75  return new ILIAS\UI\Implementation\Component\Input\Container\Form\Factory($this->buildInputFactory());
76  }
77 
78 
79  protected function buildInputFactory()
80  {
81  $df = new Data\Factory();
82  $this->language = $this->createMock(\ilLanguage::class);
84  new SignalGenerator(),
85  $df,
86  new \ILIAS\Refinery\Factory($df, $this->language),
87  $this->language
88  );
89  }
90 
91 
92  protected function buildButtonFactory()
93  {
95  }
96 
97 
98  protected function buildTransformation(\Closure $trafo)
99  {
100  $dataFactory = new Data\Factory();
101  $language = $this->createMock(\ilLanguage::class);
102  $refinery = new \ILIAS\Refinery\Factory($dataFactory, $language);
103 
104  return $refinery->custom()->transformation($trafo);
105  }
106 
107 
108  public function getUIFactory()
109  {
110  return new WithButtonNoUIFactory($this->buildButtonFactory());
111  }
112 
113 
114  public function buildDataFactory()
115  {
116  return new \ILIAS\Data\Factory;
117  }
118 
119 
120  public function test_getInputs()
121  {
122  $f = $this->buildFactory();
123  $if = $this->buildInputFactory();
124  $name_source = new FixedNameSource();
125 
126  $inputs = [$if->text(""), $if->text("")];
127  $form = new ConcreteForm($this->buildInputFactory(), $inputs);
128 
129  $seen_names = [];
130  $inputs = $form->getInputs();
131  $this->assertEquals(count($inputs), count($inputs));
132 
133  foreach ($inputs as $input) {
134  $name = $input->getName();
135  $name_source->name = $name;
136 
137  // name is a string
138  $this->assertIsString($name);
139 
140  // only name is attached
141  $input = array_shift($inputs);
142  $this->assertEquals($input->withNameFrom($name_source), $input);
143 
144  // every name can only be contained once.
145  $this->assertNotContains($name, $seen_names);
146  $seen_names[] = $name;
147  }
148  }
149 
150 
151  public function test_extractPostData()
152  {
153  $form = new ConcreteForm($this->buildInputFactory(), []);
154  $request = $this->createMock(ServerRequestInterface::class);
155  $request
156  ->expects($this->once())
157  ->method("getParsedBody")
158  ->willReturn([]);
159  $input_data = $form->_extractPostData($request);
160  $this->assertInstanceOf(InputData::class, $input_data);
161  }
162 
163 
164  public function test_withRequest()
165  {
166  $df = $this->buildDataFactory();
167  $request = $this->createMock(ServerRequestInterface::class);
168  $input_data = $this->createMock(InputData::class);
169 
170  $input_1 = $this->inputMock();
171  $input_1
172  ->expects($this->once())
173  ->method("withInput")
174  ->with($input_data)
175  ->willReturn($input_1);
176  $input_1
177  ->expects($this->once())
178  ->method("getContent")
179  ->willReturn($df->ok(0));
180 
181  $input_2 = $this->inputMock();
182  $input_2
183  ->expects($this->once())
184  ->method("withInput")
185  ->with($input_data)
186  ->willReturn($input_2);
187  $input_2
188  ->expects($this->once())
189  ->method("getContent")
190  ->willReturn($df->ok(0));
191 
192  $form = new ConcreteForm($this->buildInputFactory(), []);
193  $form->setInputs([$input_1, $input_2]);
194  $form->input_data = $input_data;
195 
196  $form2 = $form->withRequest($request);
197 
198  $this->assertNotSame($form2, $form);
199  $this->assertInstanceOf(Form::class, $form2);
200  $this->assertEquals([$input_1, $input_2], $form2->getInputs());
201  }
202 
203 
205  {
206  $df = $this->buildDataFactory();
207  $request = $this->createMock(ServerRequestInterface::class);
208  $input_data = $this->createMock(InputData::class);
209 
210  $input_1 = $this->inputMock();
211  $input_1
212  ->expects($this->once())
213  ->method("withInput")
214  ->with($input_data)
215  ->willReturn($input_1);
216  $input_1
217  ->expects($this->once())
218  ->method("getContent")
219  ->willReturn($df->ok(0));
220 
221  $input_2 = $this->inputMock();
222  $input_2
223  ->expects($this->once())
224  ->method("withInput")
225  ->with($input_data)
226  ->willReturn($input_2);
227  $input_2
228  ->expects($this->once())
229  ->method("getContent")
230  ->willReturn($df->ok(0));
231 
232  $form = new ConcreteForm($this->buildInputFactory(), []);
233  $form->setInputs(["foo" => $input_1, "bar" => $input_2]);
234  $form->input_data = $input_data;
235 
236  $form2 = $form->withRequest($request);
237 
238  $this->assertNotSame($form2, $form);
239  $this->assertInstanceOf(Form::class, $form2);
240  $this->assertEquals(["foo" => $input_1, "bar" => $input_2], $form2->getInputs());
241  }
242 
243 
244  public function test_getData()
245  {
246  $df = $this->buildDataFactory();
247  $request = $this->createMock(ServerRequestInterface::class);
248  $request
249  ->expects($this->once())
250  ->method("getParsedBody")
251  ->willReturn([]);
252 
253  $input_1 = $this->inputMock();
254  $input_1
255  ->expects($this->once())
256  ->method("getContent")
257  ->willReturn($df->ok(1));
258  $input_1
259  ->expects($this->once())
260  ->method("withInput")
261  ->willReturn($input_1);
262 
263  $input_2 = $this->inputMock();
264  $input_2
265  ->expects($this->once())
266  ->method("getContent")
267  ->willReturn($df->ok(2));
268  $input_2
269  ->expects($this->once())
270  ->method("withInput")
271  ->willReturn($input_2);
272 
273  $form = new ConcreteForm($this->buildInputFactory(), []);
274  $form->setInputs([$input_1, $input_2]);
275  $form = $form->withRequest($request);
276  $this->assertEquals([1, 2], $form->getData());
277  }
278 
279 
280  public function test_getData_respects_keys()
281  {
282  $df = $this->buildDataFactory();
283  $request = $this->createMock(ServerRequestInterface::class);
284  $request
285  ->expects($this->once())
286  ->method("getParsedBody")
287  ->willReturn([]);
288 
289  $input_1 = $this->inputMock();
290  $input_1
291  ->expects($this->once())
292  ->method("getContent")
293  ->willReturn($df->ok(1));
294  $input_1
295  ->expects($this->once())
296  ->method("withInput")
297  ->willReturn($input_1);
298 
299  $input_2 = $this->inputMock();
300  $input_2
301  ->expects($this->once())
302  ->method("getContent")
303  ->willReturn($df->ok(2));
304  $input_2
305  ->expects($this->once())
306  ->method("withInput")
307  ->willReturn($input_2);
308 
309  $form = new ConcreteForm($this->buildInputFactory(), []);
310  $form->setInputs(["foo" => $input_1, "bar" => $input_2]);
311  $form = $form->withRequest($request);
312  $this->assertEquals(["foo" => 1, "bar" => 2], $form->getData());
313  }
314 
315 
316  public function test_getData_faulty()
317  {
318  $df = $this->buildDataFactory();
319  $request = $this->createMock(ServerRequestInterface::class);
320  $request
321  ->expects($this->once())
322  ->method("getParsedBody")
323  ->willReturn([]);
324 
325  $input_1 = $this->inputMock();
326  $input_1
327  ->expects($this->once())
328  ->method("getContent")
329  ->willReturn($df->error("error"));
330  $input_1
331  ->expects($this->once())
332  ->method("withInput")
333  ->willReturn($input_1);
334 
335  $input_2 = $this->inputMock();
336  $input_2
337  ->expects($this->once())
338  ->method("getContent")
339  ->willReturn($df->ok(2));
340  $input_2
341  ->expects($this->once())
342  ->method("withInput")
343  ->willReturn($input_2);
344 
345  $form = new ConcreteForm($this->buildInputFactory(), []);
346  $form->setInputs(["foo" => $input_1, "bar" => $input_2]);
347 
348  $i18n = "THERE IS SOME ERROR IN THIS GROUP";
349  $this->language
350  ->expects($this->once())
351  ->method("txt")
352  ->with("ui_error_in_group")
353  ->willReturn($i18n);
354 
355  //Todo: This is not good, this should throw an error or similar.
356  $form = $form->withRequest($request);
357  $this->assertEquals(null, null);
358  }
359 
360 
362  {
363  $df = $this->buildDataFactory();
364  $request = $this->createMock(ServerRequestInterface::class);
365  $request
366  ->expects($this->once())
367  ->method("getParsedBody")
368  ->willReturn([]);
369 
370  $input_1 = $this->inputMock();
371  $input_1
372  ->expects($this->once())
373  ->method("getContent")
374  ->willReturn($df->ok(1));
375  $input_1
376  ->expects($this->once())
377  ->method("withInput")
378  ->willReturn($input_1);
379 
380  $input_2 = $this->inputMock();
381  $input_2
382  ->expects($this->once())
383  ->method("getContent")
384  ->willReturn($df->ok(2));
385  $input_2
386  ->expects($this->once())
387  ->method("withInput")
388  ->willReturn($input_2);
389 
390  $form = new ConcreteForm($this->buildInputFactory(), []);
391  $form->setInputs([$input_1, $input_2]);
392 
393  $form2 = $form->withAdditionalTransformation($this->buildTransformation(function ($v) {
394  return "transformed";
395  }));
396 
397  $this->assertNotSame($form2, $form);
398  $form2 = $form2->withRequest($request);
399 
400  $this->assertEquals("transformed", $form2->getData());
401  }
402 
403 
405  {
406  $if = $this->buildInputFactory();
407  $inputs = [
408  2 => $if->text(""),
409  "foo" => $if->text(""),
410  1 => $if->text(""),
411  $if->text(""),
412  ];
413  $form = new ConcreteForm($this->buildInputFactory(), []);
414  $form->setInputs($inputs);
415  $named_inputs = $form->getInputs();
416  $this->assertEquals(array_keys($inputs), array_keys($named_inputs));
417  }
418 
419  protected function inputMock()
420  {
421  static $no = 1000;
422  $config = $this
423  ->getMockBuilder(InputInternal::class)
424  ->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"])
425  ->setMockClassName("Mock_InputNo" . ($no++))
426  ->getMock();
427  return $config;
428  }
429 }
buildInputFactory()
Definition: FormTest.php:79
inputMock()
Definition: FormTest.php:419
__construct(Input\Field\Factory $field_factory, array $inputs)
Definition: FormTest.php:33
Class ChatMainBarProvider .
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:68
getNewName()
Generates a unique name on every call.
Definition: FormTest.php:23
buildTransformation(\Closure $trafo)
Definition: FormTest.php:98
Test on form implementation.
Definition: FormTest.php:71
buildDataFactory()
Definition: FormTest.php:114
test_withRequest_respects_keys()
Definition: FormTest.php:204
test_getData()
Definition: FormTest.php:244
This implements commonalities between all forms.
Definition: Form.php:21
test_getData_respects_keys()
Definition: FormTest.php:280
test_withAdditionalTransformation()
Definition: FormTest.php:361
Provides common functionality for UI tests.
Definition: Base.php:224
getUIFactory()
Definition: FormTest.php:108
extractPostData(ServerRequestInterface $request)
Definition: FormTest.php:45
Builds data types.
Definition: Factory.php:19
test_nameInputs_respects_keys()
Definition: FormTest.php:404
test_getInputs()
Definition: FormTest.php:120
test_withRequest()
Definition: FormTest.php:164
setInputs(array $inputs)
Definition: FormTest.php:55
__construct(Container $dic, ilPlugin $plugin)
_getPostInput(ServerRequestInterface $request)
Definition: FormTest.php:62
test_getData_faulty()
Definition: FormTest.php:316
buildButtonFactory()
Definition: FormTest.php:92
language()
Definition: language.php:2
Describes a source for input names.
Definition: NameSource.php:10
buildFactory()
Definition: FormTest.php:73
test_extractPostData()
Definition: FormTest.php:151
_extractPostData(ServerRequestInterface $request)
Definition: FormTest.php:39