ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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  $language = $this->createMock(ilLanguage::class);
35  return new I\Input\Field\Factory(
36  $this->createMock(UploadLimitResolver::class),
37  new SignalGenerator(),
38  $df,
39  new Refinery($df, $language),
40  $language
41  );
42  }
43 
44  protected function render(FormInput $component): string
45  {
46  return $this->brutallyTrimHTML(
47  $this->getDefaultRenderer()->render($component)
48  );
49  }
50 
51  protected function testWithError(FormInput $component): void
52  {
53  $error = "an_error";
54  $expected = '<div class="c-input__error-msg alert alert-danger"';
55  $expected2 = 'ui_error:</span>' . $error . '</div>';
56  $html = $this->render($component->withError($error));
57  $this->assertStringContainsString($expected, $html);
58  $this->assertStringContainsString($expected2, $html);
59  }
60 
61  protected function testWithRequired(FormInput $component): void
62  {
63  $expected = '<span class="asterisk" aria-label="required_field">*</span></label>';
64  $this->assertStringContainsString($expected, $this->render($component->withRequired(true)));
65  }
66 
67  protected function testWithNoByline(FormInput $component): void
68  {
69  $expected = '<div class="c-input__help-byline">';
70  $this->assertStringNotContainsString($expected, $this->render($component));
71  }
72 
73  protected function testWithDisabled(FormInput $component): void
74  {
75  $type = $this->getDefaultRenderer()->getComponentCanonicalNameAttribute($component);
76  $expected = '<fieldset class="c-input" data-il-ui-component="' . $type . '" data-il-ui-input-name="name_0" disabled="disabled"';
77  $this->assertStringContainsString($expected, $this->render($component->withDisabled(true)));
78  }
79 
80  protected function testWithAdditionalOnloadCodeRendersId(FormInput $component): void
81  {
82  $component = $component->withAdditionalOnLoadCode(
83  function (string $id): string {
84  return '';
85  }
86  );
87 
88  $js_binding = new class () implements JavaScriptBinding {
89  public function createId(): string
90  {
91  return 'THE COMPONENT ID';
92  }
93  public function addOnLoadCode(string $code): void
94  {
95  }
96  public function getOnLoadCodeAsync(): string
97  {
98  return '';
99  }
100  };
101 
102 
103  $renderer = $this->getDefaultRenderer($js_binding);
104  $outerhtml = $this->brutallyTrimHTML($renderer->render($component));
105  if (method_exists($component, 'getInputs')) {
106  $innerhtml = $this->brutallyTrimHTML($renderer->render($component->getInputs()));
107  $outerhtml = str_replace($innerhtml, '', $outerhtml);
108  }
109 
110  $this->assertStringContainsString(
111  'id="THE COMPONENT ID"',
112  $outerhtml
113  );
114  }
115 
116  protected function getFormWrappedHtml(
117  string $type,
118  string $label,
119  string $payload_field,
120  ?string $byline = null,
121  ?string $label_id = null,
122  ?string $js_id = null,
123  ?string $name = 'name_0',
124  ): string {
125  $label_id = $label_id ? " for=\"$label_id\"" : '';
126  $tab = $label_id ? '' : ' tabindex="0"';
127  $js_id = $js_id ? " id=\"$js_id\"" : '';
128  if ($type === 'section-field-input') {
129  $headline_tag_open = "<h2>";
130  $headline_tag_close = "</h2>";
131  } else {
132  $headline_tag_open = "";
133  $headline_tag_close = "";
134  }
135 
136  $html = '
137  <fieldset class="c-input" data-il-ui-component="' . $type . '" data-il-ui-input-name="' . $name . '"' . $js_id . $tab . '>
138  <label' . $label_id . '>' . $headline_tag_open . $label . $headline_tag_close . '</label>
139  <div class="c-input__field">';
140  $html .= $payload_field;
141  $html .= '
142  </div>';
143  if ($byline) {
144  $html .= '
145  <div class="c-input__help-byline">' . $byline . '</div>';
146  }
147  $html .= '
148  </fieldset>
149  ';
150  return $this->brutallyTrimHTML($html);
151  }
152 }
$renderer
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
withDisabled(bool $is_disabled)
Get an input like this, but set it to a disabled state.
Builds data types.
Definition: Factory.php:35
Provides methods to interface with javascript.
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
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:32
withAdditionalOnLoadCode(Closure $binder)
Add some onload-code to the component instead of replacing the existing one.