ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
5require_once(__DIR__ . "/../../../../../../libs/composer/vendor/autoload.php");
6require_once(__DIR__ . "/../../../../Base.php");
7
9use \ILIAS\UI\Implementation\Component\Input\Field\InputInternal;
10use \ILIAS\UI\Implementation\Component\Input\NameSource;
11use \ILIAS\UI\Implementation\Component\Input\PostData;
12use \ILIAS\UI\Implementation\Component\Input\Container\Form\Form;
14
15use \ILIAS\Transformation\Factory as TransformationFactory;
16use \ILIAS\Data;
17use \ILIAS\Validation;
18use \ILIAS\Transformation;
19
21
23{
24 public $name = "name";
25
26 public function getNewName()
27 {
28 return $this->name;
29 }
30}
31
32class ConcreteForm extends Form
33{
34 public $post_data = null;
35
36 public function __construct(Input\Field\Factory $field_factory, array $inputs)
37 {
38 $this->input_factory = $field_factory;
39 parent::__construct($field_factory, $inputs);
40 }
41
43 {
44 return $this->extractPostData($request);
45 }
46
47
49 {
50 if ($this->post_data !== null) {
51 return $this->post_data;
52 }
53
54 return parent::extractPostData($request);
55 }
56
57
58 public function setInputs(array $inputs)
59 {
60 $this->input_group = $this->input_factory->group($inputs);
61 $this->inputs = $inputs;
62 }
63
64
66 {
67 return $this->getPostInput($request);
68 }
69}
70
75{
76 protected function buildFactory()
77 {
79 }
80
81
82 protected function buildInputFactory()
83 {
84 $df = new Data\Factory();
86 new SignalGenerator(),
87 $df,
88 new Validation\Factory($df, $this->createMock(\ilLanguage::class)),
90 );
91 }
92
93
94 protected function buildButtonFactory()
95 {
97 }
98
99
100 protected function buildTransformation(\Closure $trafo)
101 {
102 $f = new TransformationFactory();
103
104 return $f->custom($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 tearDown()
121 {
122 \Mockery::close();
123 }
124
125
126 public function test_getInputs()
127 {
128 $f = $this->buildFactory();
129 $if = $this->buildInputFactory();
130 $name_source = new FixedNameSource();
131
132 $inputs = [$if->text(""), $if->text("")];
133 $form = new ConcreteForm($this->buildInputFactory(), $inputs);
134
135 $seen_names = [];
136 $inputs = $form->getInputs();
137 $this->assertEquals(count($inputs), count($inputs));
138
139 foreach ($inputs as $input) {
140 $name = $input->getName();
141 $name_source->name = $name;
142
143 // name is a string
144 $this->assertInternalType("string", $name);
145
146 // only name is attached
147 $input = array_shift($inputs);
148 $this->assertEquals($input->withNameFrom($name_source), $input);
149
150 // every name can only be contained once.
151 $this->assertNotContains($name, $seen_names);
152 $seen_names[] = $name;
153 }
154 }
155
156
157 public function test_extractPostData()
158 {
159 $form = new ConcreteForm($this->buildInputFactory(), []);
160 $request = \Mockery::mock(ServerRequestInterface::class);
161 $request->shouldReceive("getParsedBody")->once()->andReturn([]);
162 $post_data = $form->_extractPostData($request);
163 $this->assertInstanceOf(PostData::class, $post_data);
164 }
165
166
167 public function test_withRequest()
168 {
169 $request = \Mockery::mock(ServerRequestInterface::class);
170 $post_data = \Mockery::Mock(PostData::class);
171 $post_data->shouldReceive("getOr")->once()->andReturn("");
172
173 $df = $this->buildDataFactory();
174
175 $input_1 = \Mockery::mock(InputInternal::class);
176 $input_1->shouldReceive("withInput")->once()->with($post_data)->andReturn($input_1);
177
178 $input_1->shouldReceive("getContent")->once()->andReturn($df->ok(0));
179
180 $input_2 = \Mockery::mock(InputInternal::class);
181 $input_2->shouldReceive("withInput")->once()->with($post_data)->andReturn($input_2);
182
183 $input_2->shouldReceive("getContent")->once()->andReturn($df->ok(0));
184
185 $form = new ConcreteForm($this->buildInputFactory(), []);
186 $form->setInputs([$input_1, $input_2]);
187 $form->post_data = $post_data;
188
189 $form2 = $form->withRequest($request);
190
191 $this->assertNotSame($form2, $form);
192 $this->assertInstanceOf(Form::class, $form2);
193 $this->assertEquals([$input_1, $input_2], $form2->getInputs());
194 }
195
196
198 {
199 $request = \Mockery::mock(ServerRequestInterface::class);
200 $post_data = \Mockery::Mock(PostData::class);
201 $post_data->shouldReceive("getOr")->once()->andReturn("");
202
203 $df = $this->buildDataFactory();
204
205 $input_1 = \Mockery::mock(InputInternal::class);
206 $input_1->shouldReceive("withInput")->once()->with($post_data)->andReturn($input_1);
207 $input_1->shouldReceive("getContent")->once()->andReturn($df->ok(0));
208
209 $input_2 = \Mockery::mock(InputInternal::class);
210 $input_2->shouldReceive("withInput")->once()->with($post_data)->andReturn($input_2);
211 $input_2->shouldReceive("getContent")->once()->andReturn($df->ok(0));
212
213 $form = new ConcreteForm($this->buildInputFactory(), []);
214 $form->setInputs(["foo" => $input_1, "bar" => $input_2]);
215 $form->post_data = $post_data;
216
217 $form2 = $form->withRequest($request);
218
219 $this->assertNotSame($form2, $form);
220 $this->assertInstanceOf(Form::class, $form2);
221 $this->assertEquals(["foo" => $input_1, "bar" => $input_2], $form2->getInputs());
222 }
223
224
225 public function test_getData()
226 {
227 $df = $this->buildDataFactory();
228 $request = \Mockery::mock(ServerRequestInterface::class);
229 $request->shouldReceive("getParsedBody")->once()->andReturn([]);
230
231 $input_1 = \Mockery::mock(InputInternal::class);
232 $input_1->shouldReceive("getContent")->once()->andReturn($df->ok(1));
233
234 $input_1->shouldReceive("withInput")->once()->andReturn($input_1);
235
236 $input_2 = \Mockery::mock(InputInternal::class);
237 $input_2->shouldReceive("getContent")->once()->andReturn($df->ok(2));
238
239 $input_2->shouldReceive("withInput")->once()->andReturn($input_2);
240
241 $form = new ConcreteForm($this->buildInputFactory(), []);
242 $form->setInputs([$input_1, $input_2]);
243 $form = $form->withRequest($request);
244 $this->assertEquals([1, 2], $form->getData());
245 }
246
247
249 {
250 $df = $this->buildDataFactory();
251 $request = \Mockery::mock(ServerRequestInterface::class);
252 $request->shouldReceive("getParsedBody")->once()->andReturn([]);
253
254 $input_1 = \Mockery::mock(InputInternal::class);
255 $input_1->shouldReceive("getContent")->once()->andReturn($df->ok(1));
256
257 $input_1->shouldReceive("withInput")->once()->andReturn($input_1);
258
259 $input_2 = \Mockery::mock(InputInternal::class);
260 $input_2->shouldReceive("getContent")->once()->andReturn($df->ok(2));
261
262 $input_2->shouldReceive("withInput")->once()->andReturn($input_2);
263
264 $form = new ConcreteForm($this->buildInputFactory(), []);
265 $form->setInputs(["foo" => $input_1, "bar" => $input_2]);
266 $form = $form->withRequest($request);
267 $this->assertEquals(["foo" => 1, "bar" => 2], $form->getData());
268 }
269
270
271 public function test_getData_faulty()
272 {
273 $df = $this->buildDataFactory();
274 $request = \Mockery::mock(ServerRequestInterface::class);
275 $request->shouldReceive("getParsedBody")->once()->andReturn([]);
276
277 $input_1 = \Mockery::mock(InputInternal::class);
278 $input_1->shouldReceive("getContent")->once()->andReturn($df->error("error"));
279
280 $input_1->shouldReceive("withInput")->once()->andReturn($input_1);
281
282 $input_2 = \Mockery::mock(InputInternal::class);
283 $input_2->shouldReceive("getContent")->once()->andReturn($df->ok(2));
284
285 $input_2->shouldReceive("withInput")->once()->andReturn($input_2);
286
287 $form = new ConcreteForm($this->buildInputFactory(), []);
288 $form->setInputs(["foo" => $input_1, "bar" => $input_2]);
289
290 //Todo: This is not good, this should throw an error or similar.
291 $form = $form->withRequest($request);
292 $this->assertEquals(null, null);
293 }
294
295
297 {
298 $df = $this->buildDataFactory();
299 $request = \Mockery::mock(ServerRequestInterface::class);
300 $request->shouldReceive("getParsedBody")->once()->andReturn([]);
301
302 $input_1 = \Mockery::mock(InputInternal::class);
303 $input_1->shouldReceive("getContent")->once()->andReturn($df->ok(1));
304
305 $input_1->shouldReceive("withInput")->once()->andReturn($input_1);
306
307 $input_2 = \Mockery::mock(InputInternal::class);
308 $input_2->shouldReceive("getContent")->once()->andReturn($df->ok(2));
309
310 $input_2->shouldReceive("withInput")->once()->andReturn($input_2);
311
312 $form = new ConcreteForm($this->buildInputFactory(), []);
313 $form->setInputs([$input_1, $input_2]);
314
315 $form2 = $form->withAdditionalTransformation($this->buildTransformation(function ($v) {
316 return "transformed";
317 }));
318
319 $this->assertNotSame($form2, $form);
320 $form2 = $form2->withRequest($request);
321
322 $this->assertEquals("transformed", $form2->getData());
323 }
324
325
327 {
328 $if = $this->buildInputFactory();
329 $inputs = [
330 2 => $if->text(""),
331 "foo" => $if->text(""),
332 1 => $if->text(""),
333 $if->text(""),
334 ];
335 $form = new ConcreteForm($this->buildInputFactory(), []);
336 $form->setInputs($inputs);
337 $named_inputs = $form->getInputs();
338 $this->assertEquals(array_keys($inputs), array_keys($named_inputs));
339 }
340}
foreach($paths as $path) $request
Definition: asyncclient.php:32
An exception for terminatinating execution or to throw for unit testing.
_getPostInput(ServerRequestInterface $request)
Definition: FormTest.php:65
__construct(Input\Field\Factory $field_factory, array $inputs)
Definition: FormTest.php:36
setInputs(array $inputs)
Definition: FormTest.php:58
extractPostData(ServerRequestInterface $request)
Extract post data from request.
Definition: FormTest.php:48
_extractPostData(ServerRequestInterface $request)
Definition: FormTest.php:42
getNewName()
Generates a unique name on every call.
Definition: FormTest.php:26
Test on form implementation.
Definition: FormTest.php:75
buildButtonFactory()
Definition: FormTest.php:94
test_nameInputs_respects_keys()
Definition: FormTest.php:326
buildDataFactory()
Definition: FormTest.php:114
test_getInputs()
Definition: FormTest.php:126
buildFactory()
Definition: FormTest.php:76
buildInputFactory()
Definition: FormTest.php:82
tearDown()
Definition: FormTest.php:120
test_withRequest()
Definition: FormTest.php:167
buildTransformation(\Closure $trafo)
Definition: FormTest.php:100
test_getData()
Definition: FormTest.php:225
test_getData_faulty()
Definition: FormTest.php:271
test_withRequest_respects_keys()
Definition: FormTest.php:197
test_getData_respects_keys()
Definition: FormTest.php:248
test_extractPostData()
Definition: FormTest.php:157
test_withAdditionalTransformation()
Definition: FormTest.php:296
getUIFactory()
Definition: FormTest.php:108
Builds data types.
Definition: Factory.php:15
This implements commonalities between all forms.
Definition: Form.php:23
Provides common functionality for UI tests.
Definition: Base.php:192
A transformation is a function from one datatype to another.
Describes a source for input names.
Definition: NameSource.php:11
Representation of an incoming, server-side HTTP request.
if(isset($_POST['submit'])) $form