ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
FilterTest.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;
34 
36 {
37  public string $name = "name";
38 
39  public function getNewName(): string
40  {
41  return $this->name;
42  }
43 }
44 
45 class ConcreteFilter extends Filter
46 {
47  public array $inputs;
48  public ?InputData $input_data = null;
49  protected Input\Field\Factory $input_factory;
50  protected Group $input_group;
51 
52  public function __construct(
53  SignalGenerator $signal_generator,
54  Input\Field\Factory $field_factory,
55  $toggle_action_on,
56  $toggle_action_off,
57  $expand_action,
58  $collapse_action,
59  $apply_action,
60  $reset_action,
61  array $inputs,
62  array $is_input_rendered,
63  bool $is_activated,
64  bool $is_expanded
65  ) {
66  $this->input_factory = $field_factory;
68  $signal_generator,
69  $field_factory,
70  $toggle_action_on,
71  $toggle_action_off,
72  $expand_action,
73  $collapse_action,
74  $apply_action,
75  $reset_action,
76  $inputs,
77  $is_input_rendered,
78  $is_activated,
79  $is_expanded
80  );
81  }
82 
84  {
85  return $this->extractParamData($request);
86  }
87 
89  {
90  if ($this->input_data !== null) {
91  return $this->input_data;
92  }
93 
94  return parent::extractParamData($request);
95  }
96 
97  public function setInputs(array $inputs): void
98  {
99  $this->input_group = $this->input_factory->group($inputs);
100  $this->inputs = $inputs;
101  }
102 
103  // TODO: DW perhaps, this can be removed
104  // public function _getInput(ServerRequestInterface $request)
105  // {
106  // return $this->getInput($request);
107  // }
108 }
109 
114 {
115  protected function buildFactory(): Input\Container\Filter\Factory
116  {
118  new SignalGenerator(),
119  $this->buildInputFactory()
120  );
121  }
122 
123  protected function buildInputFactory(): Input\Field\Factory
124  {
125  $df = new Data\Factory();
126  $language = $this->createMock(ILIAS\Language\Language::class);
128  $this->createMock(\ILIAS\UI\Implementation\Component\Input\Field\Node\Factory::class),
129  $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
130  new SignalGenerator(),
131  $df,
132  new ILIAS\Refinery\Factory($df, $language),
133  $language
134  );
135  }
136 
137  protected function buildButtonFactory(): I\Button\Factory
138  {
140  }
141 
142  protected function buildGlyphFactory(): I\Symbol\Factory
143  {
145  }
146 
147  protected function buildPopoverFactory(): I\Popover\Factory
148  {
150  }
151 
152  protected function buildLegacyFactory(): I\Legacy\Factory
153  {
155  }
156 
157  protected function buildListingFactory(): I\Listing\Factory
158  {
160  }
161 
162  public function getUIFactory(): NoUIFactory
163  {
164  return new WithNoUIFactories(
165  $this->buildButtonFactory(),
166  $this->buildGlyphFactory(),
167  $this->buildPopoverFactory(),
168  $this->buildLegacyFactory(),
169  $this->buildListingFactory()
170  );
171  }
172 
173  public function buildDataFactory(): Data\Factory
174  {
175  return new Data\Factory();
176  }
177 
178  public function testGetInputs(): void
179  {
180  $f = $this->buildFactory();
181  $if = $this->buildInputFactory();
182  $name_source = new FixedNameSourceFilter();
183 
184  $inputs = [$if->text(""), $if->select("", [])];
185  $inputs_rendered = [true, true];
186  $filter = $f->standard(
187  "#",
188  "#",
189  "#",
190  "#",
191  "#",
192  "#",
193  $inputs,
194  $inputs_rendered,
195  false,
196  false
197  );
198 
199  $seen_names = [];
200  $inputs = $filter->getInputs();
201 
202  foreach ($inputs as $input) {
203  $name = $input->getName();
204  $name_source->name = $name;
205 
206  // name is a string
207  $this->assertIsString($name);
208 
209  // only name is attached
210  $input = array_shift($inputs);
211  $this->assertEquals($input->withNameFrom($name_source), $input);
212 
213  // every name can only be contained once.
214  $this->assertNotContains($name, $seen_names);
215  $seen_names[] = $name;
216  }
217  }
218 
219  public function testExtractParamData(): void
220  {
221  $filter = new ConcreteFilter(
222  new SignalGenerator(),
223  $this->buildInputFactory(),
224  "#",
225  "#",
226  "#",
227  "#",
228  "#",
229  "#",
230  [],
231  [],
232  false,
233  false
234  );
235  $request = $this->createMock(ServerRequestInterface::class);
236  $request
237  ->expects($this->once())
238  ->method("getQueryParams")
239  ->willReturn([]);
240  $input_data = $filter->_extractParamData($request);
241  $this->assertInstanceOf(InputData::class, $input_data);
242  }
243 
244  public function testWithRequest(): void
245  {
246  $request = $this->createMock(ServerRequestInterface::class);
247  $input_data = $this->createMock(InputData::class);
248 
249  $df = $this->buildDataFactory();
250 
251  $input_1 = $this->inputMock();
252  $input_1
253  ->expects($this->once())
254  ->method("withInput")
255  ->with($input_data)
256  ->willReturn($input_1);
257  $input_1
258  ->expects($this->once())
259  ->method("getContent")
260  ->willReturn($df->ok(0));
261 
262  $input_2 = $this->inputMock();
263  $input_2
264  ->expects($this->once())
265  ->method("withInput")
266  ->with($input_data)
267  ->willReturn($input_2);
268  $input_2
269  ->expects($this->once())
270  ->method("getContent")
271  ->willReturn($df->ok(0));
272 
273  $filter = new ConcreteFilter(
274  new SignalGenerator(),
275  $this->buildInputFactory(),
276  "#",
277  "#",
278  "#",
279  "#",
280  "#",
281  "#",
282  [],
283  [],
284  false,
285  false
286  );
287  $filter->setInputs([$input_1, $input_2]);
288  $filter->input_data = $input_data;
289 
290  $filter2 = $filter->withRequest($request);
291 
292  $this->assertNotSame($filter2, $filter);
293  $this->assertInstanceOf(Filter::class, $filter2);
294  $this->assertEquals([$input_1, $input_2], $filter2->getInputs());
295  }
296 
297  public function testGetData(): void
298  {
299  $df = $this->buildDataFactory();
300  $request = $this->createMock(ServerRequestInterface::class);
301  $request
302  ->expects($this->once())
303  ->method("getQueryParams")
304  ->willReturn([]);
305 
306  $input_1 = $this->inputMock();
307  $input_1
308  ->expects($this->once())
309  ->method("getContent")
310  ->willReturn($df->ok(1));
311  $input_1
312  ->expects($this->once())
313  ->method("withInput")
314  ->willReturn($input_1);
315 
316  $input_2 = $this->inputMock();
317  $input_2
318  ->expects($this->once())
319  ->method("getContent")
320  ->willReturn($df->ok(2));
321  $input_2
322  ->expects($this->once())
323  ->method("withInput")
324  ->willReturn($input_2);
325 
326  $filter = new ConcreteFilter(
327  new SignalGenerator(),
328  $this->buildInputFactory(),
329  "#",
330  "#",
331  "#",
332  "#",
333  "#",
334  "#",
335  [],
336  [],
337  false,
338  false
339  );
340  $filter->setInputs([$input_1, $input_2]);
341  $filter = $filter->withRequest($request);
342  $this->assertEquals([1, 2], $filter->getData());
343  }
344 
345  public function testWithActivated(): void
346  {
347  $f = $this->buildFactory();
348  $if = $this->buildInputFactory();
349  $inputs = [$if->text(""), $if->text("")];
350  $inputs_rendered = [true, true];
351 
352  $filter = $f->standard(
353  "#",
354  "#",
355  "#",
356  "#",
357  "#",
358  "#",
359  $inputs,
360  $inputs_rendered,
361  false,
362  false
363  );
364  $filter1 = $filter->withActivated();
365 
366  $this->assertFalse($filter->isActivated());
367  $this->assertTrue($filter1->isActivated());
368  }
369 
370  public function testWithDeactivated(): void
371  {
372  $f = $this->buildFactory();
373  $if = $this->buildInputFactory();
374  $inputs = [$if->text(""), $if->text("")];
375  $inputs_rendered = [true, true];
376 
377  $filter = $f->standard(
378  "#",
379  "#",
380  "#",
381  "#",
382  "#",
383  "#",
384  $inputs,
385  $inputs_rendered,
386  true,
387  false
388  );
389  $filter1 = $filter->withDeactivated();
390 
391  $this->assertTrue($filter->isActivated());
392  $this->assertFalse($filter1->isActivated());
393  }
394 
395  public function testWithExpanded(): void
396  {
397  $f = $this->buildFactory();
398  $if = $this->buildInputFactory();
399  $inputs = [$if->text(""), $if->text("")];
400  $inputs_rendered = [true, true];
401 
402  $filter = $f->standard(
403  "#",
404  "#",
405  "#",
406  "#",
407  "#",
408  "#",
409  $inputs,
410  $inputs_rendered,
411  false,
412  false
413  );
414  $filter1 = $filter->withExpanded();
415 
416  $this->assertFalse($filter->isExpanded());
417  $this->assertTrue($filter1->isExpanded());
418  }
419 
420  public function testWithCollapsed(): void
421  {
422  $f = $this->buildFactory();
423  $if = $this->buildInputFactory();
424  $inputs = [$if->text(""), $if->text("")];
425  $inputs_rendered = [true, true];
426 
427  $filter = $f->standard(
428  "#",
429  "#",
430  "#",
431  "#",
432  "#",
433  "#",
434  $inputs,
435  $inputs_rendered,
436  false,
437  true
438  );
439  $filter1 = $filter->withCollapsed();
440 
441  $this->assertTrue($filter->isExpanded());
442  $this->assertFalse($filter1->isExpanded());
443  }
444 
448  protected function inputMock()
449  {
450  static $no = 2000;
451  return $this
452  ->getMockBuilder(Input\Field\FormInputInternal::class)
453  ->onlyMethods([
454  "getName",
455  "withDedicatedName",
456  "withNameFrom",
457  "withInput",
458  "getContent",
459  "getLabel",
460  "withLabel",
461  "getByline",
462  "withByline",
463  "isRequired",
464  "withRequired",
465  "isDisabled",
466  "withDisabled",
467  "getValue",
468  "withValue",
469  "getError",
470  "withError",
471  "withAdditionalTransformation",
472  "getUpdateOnLoadCode",
473  "getCanonicalName",
474  "withOnLoadCode",
475  "withAdditionalOnLoadCode",
476  "getOnLoadCode",
477  "withOnUpdate",
478  "appendOnUpdate",
479  "withResetTriggeredSignals",
480  "getTriggeredSignals",
481  "reduceWith"
482  ])
483  ->setMockClassName("Mock_InputNo" . ($no++))
484  ->getMock();
485  }
486 }
setInputs(array $inputs)
Definition: FilterTest.php:97
Input Field Factory $input_factory
Definition: FilterTest.php:49
buildDataFactory()
Definition: FilterTest.php:173
buildLegacyFactory()
Definition: FilterTest.php:152
Describes how Input-Elements want to interact with posted data.
Definition: InputData.php:29
Interface Observer Contains several chained tasks and infos about them.
getNewName()
Generates a unique name on every call.
Definition: FilterTest.php:39
testWithExpanded()
Definition: FilterTest.php:395
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
_extractParamData(ServerRequestInterface $request)
Definition: FilterTest.php:83
testWithActivated()
Definition: FilterTest.php:345
buildInputFactory()
Definition: FilterTest.php:123
__construct(SignalGenerator $signal_generator, Input\Field\Factory $field_factory, $toggle_action_on, $toggle_action_off, $expand_action, $collapse_action, $apply_action, $reset_action, array $inputs, array $is_input_rendered, bool $is_activated, bool $is_expanded)
Definition: FilterTest.php:52
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
testWithCollapsed()
Definition: FilterTest.php:420
buildPopoverFactory()
Definition: FilterTest.php:147
Test on filter implementation.
Definition: FilterTest.php:113
extractParamData(ServerRequestInterface $request)
Definition: FilterTest.php:88
buildListingFactory()
Definition: FilterTest.php:157
testExtractParamData()
Definition: FilterTest.php:219
Builds data types.
Definition: Factory.php:35
buildGlyphFactory()
Definition: FilterTest.php:142
Group $input_group
Definition: FilterTest.php:50
__construct(Container $dic, ilPlugin $plugin)
This describes commonalities between all inputs.
Definition: Input.php:46
buildButtonFactory()
Definition: FilterTest.php:137
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...
testWithDeactivated()
Definition: FilterTest.php:370