ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
CommonFieldRendering.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
25 use ILIAS\Data;
28 
30 {
31  protected function getFieldFactory(): I\Input\Field\Factory
32  {
33  $df = new Data\Factory();
34  return new I\Input\Field\Factory(
35  $this->createMock(UploadLimitResolver::class),
36  new SignalGenerator(),
37  $df,
38  new Refinery($df, $this->getLanguage()),
39  $this->getLanguage()
40  );
41  }
42 
43  protected function render(FormInput $component): string
44  {
45  return $this->brutallyTrimHTML(
46  $this->getDefaultRenderer()->render($component)
47  );
48  }
49 
50  protected function testWithError(FormInput $component): void
51  {
52  $error = "an_error";
53  $expected = '<div class="c-input__error-msg alert alert-danger"';
54  $expected2 = 'ui_error:</span>' . $error . '</div>';
55  $html = $this->render($component->withError($error));
56  $this->assertStringContainsString($expected, $html);
57  $this->assertStringContainsString($expected2, $html);
58  }
59 
60  protected function testWithRequired(FormInput $component): void
61  {
62  $expected = '<span class="asterisk" aria-label="required_field">*</span></label>';
63  $this->assertStringContainsString($expected, $this->render($component->withRequired(true)));
64  }
65 
66  protected function testWithNoByline(FormInput $component): void
67  {
68  $expected = '<div class="c-input__help-byline">';
69  $this->assertStringNotContainsString($expected, $this->render($component));
70  }
71 
72  protected function testWithDisabled(FormInput $component): void
73  {
74  $type = $this->getDefaultRenderer()->getComponentCanonicalNameAttribute($component);
75  $expected = '<fieldset class="c-input" data-il-ui-component="' . $type . '" data-il-ui-input-name="name_0" disabled="disabled"';
76  $this->assertStringContainsString($expected, $this->render($component->withDisabled(true)));
77  }
78 
79  protected function testWithAdditionalOnloadCodeRendersId(FormInput $component): void
80  {
81  $component = $component->withAdditionalOnLoadCode(
82  function (string $id): string {
83  return '';
84  }
85  );
86 
87  $js_binding = new class () implements JavaScriptBinding {
88  public function createId(): string
89  {
90  return 'THE COMPONENT ID';
91  }
92  public function addOnLoadCode(string $code): void
93  {
94  }
95  public function getOnLoadCodeAsync(): string
96  {
97  return '';
98  }
99  };
100 
101 
102  $renderer = $this->getDefaultRenderer($js_binding);
103  $outerhtml = $this->brutallyTrimHTML($renderer->render($component));
104  if (method_exists($component, 'getInputs')) {
105  $innerhtml = $this->brutallyTrimHTML($renderer->render($component->getInputs()));
106  $outerhtml = str_replace($innerhtml, '', $outerhtml);
107  }
108 
109  $this->assertStringContainsString(
110  'id="THE COMPONENT ID"',
111  $outerhtml
112  );
113  }
114 
115  protected function getFormWrappedHtml(
116  string $type,
117  string $label,
118  string $payload_field,
119  ?string $byline = null,
120  ?string $label_id = null,
121  ?string $js_id = null,
122  ?string $name = 'name_0',
123  ): string {
124  $label_id = $label_id ? " for=\"$label_id\"" : '';
125  $tab = $label_id ? '' : ' tabindex="0"';
126  $js_id = $js_id ? " id=\"$js_id\"" : '';
127  if ($type === 'section-field-input') {
128  $headline_tag_open = "<h2>";
129  $headline_tag_close = "</h2>";
130  } else {
131  $headline_tag_open = "";
132  $headline_tag_close = "";
133  }
134 
135  $html = '
136  <fieldset class="c-input" data-il-ui-component="' . $type . '" data-il-ui-input-name="' . $name . '"' . $js_id . $tab . '>
137  <label' . $label_id . '>' . $headline_tag_open . $label . $headline_tag_close . '</label>
138  <div class="c-input__field">';
139  $html .= $payload_field;
140  $html .= '
141  </div>';
142  if ($byline) {
143  $html .= '
144  <div class="c-input__help-byline">' . $byline . '</div>';
145  }
146  $html .= '
147  </fieldset>
148  ';
149  return $this->brutallyTrimHTML($html);
150  }
151 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$renderer
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.
getLanguage()
Builds data types.
Definition: Factory.php:35
Provides methods to interface with javascript.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
withRequired(bool $is_required, ?Constraint $requirement_constraint=null)
Get an input like this, but set the field to be required (or not).
This describes inputs that can be used in forms.
Definition: FormInput.php:31
withAdditionalOnLoadCode(Closure $binder)
Add some onload-code to the component instead of replacing the existing one.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...