ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
FileInputTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/../../../../../libs/composer/vendor/autoload.php");
22 require_once(__DIR__ . "/../../../Base.php");
23 require_once(__DIR__ . "/InputTest.php");
24 
25 use ILIAS\Data;
30 use ILIAS\UI\Component\Symbol\Factory as SymbolFactory;
32 
34 {
36  protected SymbolFactory $symbol_factory;
37 
38  public function __construct(ButtonFactory $button_factory, SymbolFactory $symbol_factory)
39  {
40  $this->button_factory = $button_factory;
41  $this->symbol_factory = $symbol_factory;
42  }
43 
44  public function button(): ButtonFactory
45  {
46  return $this->button_factory;
47  }
48 
49  public function symbol(): SymbolFactory
50  {
51  return $this->symbol_factory;
52  }
53 }
54 
56 {
58 
59  public function setUp(): void
60  {
61  $this->name_source = new DefNamesource();
62  }
63 
64  protected function brutallyTrimHTML(string $html): string
65  {
66  $html = str_replace(" />", "/>", $html);
67  return parent::brutallyTrimHTML($html);
68  }
69 
70  protected function buildFactory(): I\Input\Field\Factory
71  {
72  $df = new Data\Factory();
73  $language = $this->createMock(ilLanguage::class);
74 
75  return new I\Input\Field\Factory(
76  $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
77  new SignalGenerator(),
78  $df,
79  new ILIAS\Refinery\Factory($df, $language),
80  $language
81  );
82  }
83 
84 
85  private function getUploadHandler(?FileInfoResult $file = null): Field\UploadHandler
86  {
87  return new class ($file) implements Field\UploadHandler {
88  protected ?FileInfoResult $file;
89 
90  public function __construct(?FileInfoResult $file)
91  {
92  $this->file = $file;
93  }
94 
95  public function getFileIdentifierParameterName(): string
96  {
97  return 'file_id';
98  }
99 
100  public function getUploadURL(): string
101  {
102  return 'uploadurl';
103  }
104 
105  public function getFileRemovalURL(): string
106  {
107  return 'removalurl';
108  }
109 
113  public function getExistingFileInfoURL(): string
114  {
115  return 'infourl';
116  }
117 
121  public function getInfoForExistingFiles(array $file_ids): array
122  {
123  return [];
124  }
125 
126  public function getInfoResult(string $identifier): ?FileInfoResult
127  {
128  if (null !== $this->file && $identifier === $this->file->getFileIdentifier()) {
129  return $this->file;
130  }
131 
132  return null;
133  }
134 
135  public function supportsChunkedUploads(): bool
136  {
137  return false;
138  }
139  };
140  }
141 
142 
143  public function test_implements_factory_interface(): void
144  {
145  $f = $this->buildFactory();
146 
147  $text = $f->file($this->getUploadHandler(), "label", "byline");
148 
149  $this->assertInstanceOf(\ILIAS\UI\Component\Input\Container\Form\FormInput::class, $text);
150  $this->assertInstanceOf(Field\File::class, $text);
151  }
152 
153 
154  public function test_render(): void
155  {
156  $f = $this->buildFactory();
157  $label = "label";
158  $byline = "byline";
159  $text = $f->file($this->getUploadHandler(), $label, $byline)->withNameFrom($this->name_source);
160 
161  $r = $this->getDefaultRenderer();
162  $html = $this->brutallyTrimHTML($r->render($text));
163 
164  $expected = $this->brutallyTrimHTML('
165  <div class="form-group row">
166  <label class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
167  <div class="col-sm-8 col-md-9 col-lg-10">
168  <div id="id_3" class="ui-input-file">
169  <div class="ui-input-file-input-list ui-input-dynamic-inputs-list"></div>
170  <div class="ui-input-file-input-dropzone">
171  <button class="btn btn-link" data-action="#" id="id_2">select_files_from_computer</button>
172  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span>
173  </div>
174  </div>
175  <div class="help-block">byline</div>
176  </div>
177  </div>
178  ');
179  $this->assertEquals($expected, $html);
180  }
181 
182 
183  public function test_render_error(): void
184  {
185  $f = $this->buildFactory();
186  $label = "label";
187  $byline = "byline";
188  $error = "an_error";
189  $text = $f->file($this->getUploadHandler(), $label, $byline)->withNameFrom($this->name_source)->withError($error);
190 
191  $r = $this->getDefaultRenderer();
192  $html = $this->brutallyTrimHTML($r->render($text));
193 
194  $expected = $this->brutallyTrimHTML('
195  <div class="form-group row"><label class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
196  <div class="col-sm-8 col-md-9 col-lg-10">
197  <div class="help-block alert alert-danger" aria-describedby="id_3" role="alert">an_error</div>
198  <div id="id_3" class="ui-input-file">
199  <div class="ui-input-file-input-list ui-input-dynamic-inputs-list"></div>
200  <div class="ui-input-file-input-dropzone">
201  <button class="btn btn-link" data-action="#" id="id_2">select_files_from_computer</button>
202  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span></div>
203  </div>
204  <div class="help-block">byline</div>
205  </div>
206  </div>
207  ');
208  $this->assertEquals($expected, $html);
209  }
210 
211 
212  public function test_render_no_byline(): void
213  {
214  $f = $this->buildFactory();
215  $label = "label";
216  $text = $f->file($this->getUploadHandler(), $label)->withNameFrom($this->name_source);
217 
218  $r = $this->getDefaultRenderer();
219  $html = $this->brutallyTrimHTML($r->render($text));
220 
221  $expected = $this->brutallyTrimHTML('
222  <div class="form-group row"><label class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
223  <div class="col-sm-8 col-md-9 col-lg-10">
224  <div id="id_3" class="ui-input-file">
225  <div class="ui-input-file-input-list ui-input-dynamic-inputs-list"></div>
226  <div class="ui-input-file-input-dropzone">
227  <button class="btn btn-link" data-action="#" id="id_2">select_files_from_computer</button>
228  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span></div>
229  </div>
230  </div>
231  </div>
232  ');
233  $this->assertEquals($expected, $html);
234  }
235 
236 
237  public function test_render_value(): void
238  {
239  $test_file_id = "test_file_id_1";
240  $test_file_name = "test file name 1";
241 
242  $test_file_info = $this->createMock(FileInfoResult::class);
243  $test_file_info->method('getFileIdentifier')->willReturn("test_file_id_1");
244  $test_file_info->method('getName')->willReturn("test file name 1");
245  $test_file_info->method('getSize')->willReturn(1001);
246 
247  $file_input = $this->buildFactory()->file(
248  $this->getUploadHandler($test_file_info),
249  "",
250  )->withValue([
251  $test_file_id,
252  ])->withNameFrom($this->name_source);
253 
254  $html = $this->brutallyTrimHTML($this->getDefaultRenderer()->render($file_input));
255 
256  $expected = $this->brutallyTrimHTML('
257 <div class="form-group row">
258  <label class="control-label col-sm-4 col-md-3 col-lg-2"></label>
259  <div class="col-sm-8 col-md-9 col-lg-10">
260  <div id="id_4" class="ui-input-file">
261  <div class="ui-input-file-input-list ui-input-dynamic-inputs-list">
262  <div class="ui-input-file-input ui-input-dynamic-input">
263  <div class="ui-input-file-info">
264  <span data-action="expand"></span>
265  <span data-action="collapse"></span>
266  <span data-dz-name>test file name 1</span>
267  <span data-dz-size>1 KB</span>
268  <span data-action="remove">
269  <a tabindex="0" class="glyph" href="#" aria-label="close">
270  <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
271  </a>
272  </span>
273  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span>
274  </div>
275  <div class="ui-input-file-metadata" style="display: none;">
276  <input id="id_1" type="hidden" name="name_0[input_0][]" value="test_file_id_1"/>
277  </div>
278  <div class="ui-input-file-input-progress-container">
279  <div class="ui-input-file-input-progress-indicator"></div>
280  </div>
281  </div>
282  </div>
283  <div class="ui-input-file-input-dropzone">
284  <button class="btn btn-link" data-action="#" id="id_3">select_files_from_computer</button>
285  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span>
286  </div>
287  </div>
288  </div>
289 </div>
290  ');
291  $this->assertEquals($expected, $html);
292  }
293 
294 
295  public function test_render_with_metadata(): void
296  {
297  $factory = $this->buildFactory();
298 
299  $metadata_input = $factory->text("text_input");
300  $file_input = $factory->file(
301  ($u = $this->getUploadHandler()),
302  "file_input",
303  null,
304  $metadata_input
305  )->withValue([
306  [
307  $u->getFileIdentifierParameterName() => "file_id",
308  ""
309  ]
310  ])->withNameFrom($this->name_source);
311 
312  $r = $this->getDefaultRenderer();
313  $html = $this->brutallyTrimHTML($r->render($file_input));
314 
315  $expected = $this->brutallyTrimHTML('
316 <div class="form-group row">
317  <label class="control-label col-sm-4 col-md-3 col-lg-2">file_input</label>
318  <div class="col-sm-8 col-md-9 col-lg-10">
319  <div id="id_6" class="ui-input-file">
320  <div class="ui-input-file-input-list ui-input-dynamic-inputs-list">
321  <div class="ui-input-file-input ui-input-dynamic-input">
322  <div class="ui-input-file-info">
323  <span data-action="expand">
324  <a tabindex="0" class="glyph" href="#" aria-label="expand_content">
325  <span class="glyphicon glyphicon-triangle-right" aria-hidden="true"></span>
326  </a>
327  </span>
328  <span data-action="collapse">
329  <a tabindex="0" class="glyph" href="#" aria-label="collapse_content">
330  <span class="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></span>
331  </a>
332  </span>
333  <span data-dz-name></span>
334  <span data-dz-size></span>
335  <span data-action="remove">
336  <a tabindex="0" class="glyph" href="#" aria-label="close">
337  <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
338  </a>
339  </span>
340  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span>
341  </div>
342  <div class="ui-input-file-metadata" style="display: none;">
343  <div class="form-group row">
344  <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">text_input</label>
345  <div class="col-sm-8 col-md-9 col-lg-10">
346  <input id="id_1" type="text" name="name_0[input_1][]" class="form-control form-control-sm"/>
347  </div>
348  </div>
349  <input id="id_2" type="hidden" name="name_0[input_2][]" value="file_id"/>
350  </div>
351  <div class="ui-input-file-input-progress-container">
352  <div class="ui-input-file-input-progress-indicator"></div>
353  </div>
354  </div>
355  </div>
356  <div class="ui-input-file-input-dropzone">
357  <button class="btn btn-link" data-action="#" id="id_5">select_files_from_computer</button>
358  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span>
359  </div>
360  </div>
361  </div>
362 </div>
363  ');
364 
365  $this->assertEquals($expected, $html);
366  }
367 
368 
369  public function test_render_with_metadata_value(): void
370  {
371  $test_file_id = "test_file_id_1";
372  $test_file_name = "test file name 1";
373 
374  $test_file_info = $this->createMock(FileInfoResult::class);
375  $test_file_info->method('getFileIdentifier')->willReturn("test_file_id_1");
376  $test_file_info->method('getName')->willReturn("test file name 1");
377  $test_file_info->method('getSize')->willReturn(1000 * 1000 + 1);
378 
379  $factory = $this->buildFactory();
380 
381  $metadata_input = $factory->text("text_input");
382  $file_input = $factory->file(
383  $u = $this->getUploadHandler($test_file_info),
384  "file_input",
385  null,
386  $metadata_input
387  )->withValue([
388  [
389  $u->getFileIdentifierParameterName() => $test_file_id,
390  "test",
391  ]
392  ])->withNameFrom($this->name_source);
393 
394  $r = $this->getDefaultRenderer();
395  $html = $this->brutallyTrimHTML($r->render($file_input));
396 
397  $expected = $this->brutallyTrimHTML('
398 <div class="form-group row">
399  <label class="control-label col-sm-4 col-md-3 col-lg-2">file_input</label>
400  <div class="col-sm-8 col-md-9 col-lg-10">
401  <div id="id_6" class="ui-input-file">
402  <div class="ui-input-file-input-list ui-input-dynamic-inputs-list">
403  <div class="ui-input-file-input ui-input-dynamic-input">
404  <div class="ui-input-file-info">
405  <span data-action="expand">
406  <a tabindex="0" class="glyph" href="#" aria-label="expand_content">
407  <span class="glyphicon glyphicon-triangle-right" aria-hidden="true"></span>
408  </a>
409  </span>
410  <span data-action="collapse">
411  <a tabindex="0" class="glyph" href="#" aria-label="collapse_content">
412  <span class="glyphicon glyphicon-triangle-bottom" aria-hidden="true"></span>
413  </a>
414  </span>
415  <span data-dz-name>test file name 1</span>
416  <span data-dz-size>1 MB</span>
417  <span data-action="remove">
418  <a tabindex="0" class="glyph" href="#" aria-label="close">
419  <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
420  </a>
421  </span>
422  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span>
423  </div>
424  <div class="ui-input-file-metadata" style="display: none;">
425  <div class="form-group row">
426  <label for="id_1" class="control-label col-sm-4 col-md-3 col-lg-2">text_input</label>
427  <div class="col-sm-8 col-md-9 col-lg-10">
428  <input id="id_1" type="text" value="test" name="name_0[input_1][]" class="form-control form-control-sm"/>
429  </div>
430  </div>
431  <input id="id_2" type="hidden" name="name_0[input_2][]" value="test_file_id_1"/>
432  </div>
433  <div class="ui-input-file-input-progress-container">
434  <div class="ui-input-file-input-progress-indicator"></div>
435  </div>
436  </div>
437  </div>
438  <div class="ui-input-file-input-dropzone">
439  <button class="btn btn-link" data-action="#" id="id_5">select_files_from_computer</button>
440  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span>
441  </div>
442  </div>
443  </div>
444 </div>
445  ');
446 
447  $this->assertEquals($expected, $html);
448  }
449 
450 
451  public function test_render_required(): void
452  {
453  $f = $this->buildFactory();
454  $label = "label";
455  $text = $f->file($this->getUploadHandler(), $label)->withNameFrom($this->name_source)->withRequired(true);
456 
457  $r = $this->getDefaultRenderer();
458  $html = $this->brutallyTrimHTML($r->render($text));
459 
460  $expected = $this->brutallyTrimHTML('
461  <div class="form-group row"><label class="control-label col-sm-4 col-md-3 col-lg-2">label<span class="asterisk">*</span></label>
462  <div class="col-sm-8 col-md-9 col-lg-10">
463  <div id="id_3" class="ui-input-file">
464  <div class="ui-input-file-input-list ui-input-dynamic-inputs-list"></div>
465  <div class="ui-input-file-input-dropzone">
466  <button class="btn btn-link" data-action="#" id="id_2">select_files_from_computer</button>
467  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span></div>
468  </div>
469  </div>
470  </div>
471  ');
472  $this->assertEquals($expected, $html);
473  }
474 
475 
476  public function test_render_disabled(): void
477  {
478  $f = $this->buildFactory();
479  $label = "label";
480  $text = $f->file($this->getUploadHandler(), $label)->withNameFrom($this->name_source)->withDisabled(true);
481 
482  $r = $this->getDefaultRenderer();
483  $html = $this->brutallyTrimHTML($r->render($text));
484 
485  $expected = $this->brutallyTrimHTML('
486  <div class="form-group row"><label class="control-label col-sm-4 col-md-3 col-lg-2">label</label>
487  <div class="col-sm-8 col-md-9 col-lg-10">
488  <div id="id_3" class="ui-input-file">
489  <div class="ui-input-file-input-list ui-input-dynamic-inputs-list"></div>
490  <div class="ui-input-file-input-dropzone">
491  <button class="btn btn-link" data-action="#" id="id_2">select_files_from_computer</button>
492  <span class="ui-input-file-input-error-msg" data-dz-error-msg></span></div>
493  </div>
494  </div>
495  </div>
496  ');
497 
498  $this->assertEquals($expected, $html);
499  }
500 
501  protected function buildButtonFactory(): I\Button\Factory
502  {
503  return new I\Button\Factory();
504  }
505 
506  protected function buildSymbolFactory(): I\Symbol\Factory
507  {
508  return new I\Symbol\Factory(
509  new I\Symbol\Icon\Factory(),
510  new I\Symbol\Glyph\Factory(),
511  new I\Symbol\Avatar\Factory()
512  );
513  }
514 
516  {
518  $this->buildButtonFactory(),
519  $this->buildSymbolFactory()
520  );
521  }
522 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class Factory.
test_implements_factory_interface()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
DefNamesource $name_source
symbol()
description: purpose: > Symbols are graphical representations of concepts or contexts quickly compre...
Class ChatMainBarProvider .
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
button()
description: purpose: > Buttons trigger interactions that change the system’s or view&#39;s status...
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...
Definition: Checkbox.php:21
__construct(ButtonFactory $button_factory, SymbolFactory $symbol_factory)
test_render_with_metadata_value()
Provides common functionality for UI tests.
Definition: Base.php:298
withNameFrom(NameSource $source, ?string $parent_name=null)
getUploadHandler(?FileInfoResult $file=null)
brutallyTrimHTML(string $html)
$factory
Definition: metadata.php:75