ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DurationInputTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
22require_once(__DIR__ . "/../../../Base.php");
23require_once(__DIR__ . "/CommonFieldRendering.php");
24
28use ILIAS\Data;
29use ILIAS\Refinery\Factory as Refinery;
32
34{
35 use CommonFieldRendering;
36
38 protected Data\Factory $data_factory;
39 protected I\Input\Field\Factory $factory;
40 protected Language $lng;
41
42 public function setUp(): void
43 {
44 $this->name_source = new DefNamesource();
45 $this->data_factory = new Data\Factory();
46 $this->factory = $this->buildFactory();
47 }
48
49 protected function buildLanguage(): Language
50 {
51 $this->lng = $this->createMock(Language::class);
52 $this->lng->method("txt")
53 ->will($this->returnArgument(0));
54
55 return $this->lng;
56 }
57
58 protected function buildRefinery(): Refinery
59 {
60 return new Refinery($this->data_factory, $this->buildLanguage());
61 }
62
63 protected function buildFactory(): I\Input\Field\Factory
64 {
65 return new I\Input\Field\Factory(
66 $this->createMock(\ILIAS\UI\Implementation\Component\Input\Field\Node\Factory::class),
67 $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
68 new SignalGenerator(),
69 $this->data_factory,
70 $this->buildRefinery(),
71 $this->buildLanguage()
72 );
73 }
74
75 public function getUIFactory(): NoUIFactory
76 {
77 return new class () extends NoUIFactory {
78 public function symbol(): I\Symbol\Factory
79 {
80 return new S\Factory(
81 new S\Icon\Factory(),
82 new S\Glyph\Factory(),
83 new S\Avatar\Factory()
84 );
85 }
86 };
87 }
88
89 public function testWithFormat(): void
90 {
91 $format = $this->data_factory->dateFormat()->germanShort();
92 $duration = $this->factory->duration('label', 'byline')
93 ->withFormat($format);
94
95 $this->assertEquals(
96 $format,
97 $duration->getFormat()
98 );
99 }
100
101 public function testWithMinValue(): void
102 {
103 $dat = new DateTimeImmutable('2019-01-09');
104 $duration = $this->factory->duration('label', 'byline')
105 ->withMinValue($dat);
106
107 $this->assertEquals(
108 $dat,
109 $duration->getMinValue()
110 );
111 }
112
113 public function testWithMaxValue(): void
114 {
115 $dat = new DateTimeImmutable('2019-01-09');
116 $duration = $this->factory->duration('label', 'byline')
117 ->withMaxValue($dat);
118
119 $this->assertEquals(
120 $dat,
121 $duration->getMaxValue()
122 );
123 }
124
125 public function testWithUseTime(): void
126 {
127 $datetime = $this->factory->duration('label', 'byline');
128 $this->assertFalse($datetime->getUseTime());
129 $this->assertTrue($datetime->withUseTime(true)->getUseTime());
130 }
131
132 public function testWithTimeOnly(): void
133 {
134 $datetime = $this->factory->duration('label', 'byline');
135 $this->assertFalse($datetime->getTimeOnly());
136 $this->assertTrue($datetime->withTimeOnly(true)->getTimeOnly());
137 }
138
139 public function testWithTimeZone(): void
140 {
141 $datetime = $this->factory->duration('label', 'byline');
142 $this->assertNull($datetime->getTimeZone());
143 $tz = 'Europe/Moscow';
144 $this->assertEquals(
145 $tz,
146 $datetime->withTimeZone($tz)->getTimeZone()
147 );
148 }
149
150 public function testWithInvalidTimeZone(): void
151 {
152 $this->expectException(InvalidArgumentException::class);
153 $datetime = $this->factory->duration('label', 'byline');
154 $tz = 'NOT/aValidTZ';
155 $datetime->withTimeZone($tz);
156 }
157
158 public function testWithoutByline(): void
159 {
160 $datetime = $this->factory->duration('label');
161 $this->assertInstanceOf(C\Input\Field\Duration::class, $datetime);
162 }
163
164 public function testRender(): \ILIAS\UI\Component\Input\Field\Duration
165 {
166 $duration = $this->factory->duration('label', 'byline')
167 ->withNameFrom($this->name_source);
168 $label_start = 'duration_default_label_start';
169 $label_end = 'duration_default_label_end';
170
171 $f1 = $this->getFormWrappedHtml(
172 'date-time-field-input',
173 $label_start,
174 '<div class="c-input-group">
175 <input id="id_1" type="date" name="name_0/start_1" class="c-field-datetime" />
176 </div>
177 ',
178 null,
179 'id_1',
180 null,
181 'name_0/start_1'
182 );
183 $f2 = $this->getFormWrappedHtml(
184 'date-time-field-input',
185 $label_end,
186 '<div class="c-input-group">
187 <input id="id_2" type="date" name="name_0/end_2" class="c-field-datetime" />
188 </div>
189 ',
190 null,
191 'id_2',
192 null,
193 'name_0/end_2'
194 );
195
196 $expected = $this->getFormWrappedHtml(
197 'duration-field-input',
198 'label',
199 '<div class="c-field-duration">' . $f1 . $f2 . '</div>',
200 'byline',
201 );
202 $this->assertEquals($expected, $this->render($duration));
203 return $duration;
204 }
205
206 #[\PHPUnit\Framework\Attributes\Depends('testRender')]
208 {
209 $other_start_label = 'other startlabel';
210 $other_end_label = 'other endlabel';
211
212 $duration = $duration->withLabels($other_start_label, $other_end_label);
213
214 $f1 = $this->getFormWrappedHtml(
215 'date-time-field-input',
216 $other_start_label,
217 '<div class="c-input-group">
218 <input id="id_1" type="date" name="name_0/start_1" class="c-field-datetime" />
219 </div>
220 ',
221 null,
222 'id_1',
223 null,
224 'name_0/start_1'
225 );
226 $f2 = $this->getFormWrappedHtml(
227 'date-time-field-input',
228 $other_end_label,
229 '<div class="c-input-group">
230 <input id="id_2" type="date" name="name_0/end_2" class="c-field-datetime" />
231 </div>
232 ',
233 null,
234 'id_2',
235 null,
236 'name_0/end_2'
237 );
238
239 $expected = $this->getFormWrappedHtml(
240 'duration-field-input',
241 'label',
242 '<div class="c-field-duration">' . $f1 . $f2 . '</div>',
243 'byline'
244 );
245 $this->assertEquals($expected, $this->render($duration));
246 }
247
248 public function testCommonRendering(): void
249 {
250 $duration = $this->factory->duration('label')
251 ->withNameFrom($this->name_source);
252 $this->testWithError($duration);
253 $this->testWithNoByline($duration);
254 $this->testWithRequired($duration);
255 $this->testWithDisabled($duration);
256 $this->testWithAdditionalOnloadCodeRendersId($duration);
257 }
258}
$datetime
$duration
factory()
DefNamesource $name_source
testRenderWithDifferentLabels($duration)
I Input Field Factory $factory
Data Factory $data_factory
Builds data types.
Definition: Factory.php:36
Definition: UI.php:24
Provides common functionality for UI tests.
Definition: Base.php:337
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.