ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
DateTimeInputTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 require_once(__DIR__ . "/../../../../../../../vendor/composer/vendor/autoload.php");
22 require_once(__DIR__ . "/../../../Base.php");
23 require_once(__DIR__ . "/CommonFieldRendering.php");
24 
27 use ILIAS\UI\Component as C;
28 use ILIAS\Data;
31 
33 {
35 
37  protected Data\Factory $data_factory;
38  protected I\Input\Field\Factory $factory;
39 
40  public function setUp(): void
41  {
42  $this->name_source = new DefNamesource();
43  $this->data_factory = new Data\Factory();
44  $this->factory = $this->buildFactory();
45  }
46 
47  public function getUIFactory(): NoUIFactory
48  {
49  return new class () extends NoUIFactory {
50  public function symbol(): I\Symbol\Factory
51  {
52  return new S\Factory(
53  new S\Icon\Factory(),
54  new S\Glyph\Factory(),
55  new S\Avatar\Factory()
56  );
57  }
58  };
59  }
60 
61  public function getLanguage(): LanguageMock
62  {
63  return new class () extends LanguageMock {
64  public function getLangKey(): string
65  {
66  return 'en';
67  }
68  };
69  }
70 
71  protected function buildFactory(): I\Input\Field\Factory
72  {
73  $df = new Data\Factory();
74  $language = $this->createMock(ILIAS\Language\Language::class);
75 
76  return new I\Input\Field\Factory(
77  $this->createMock(\ILIAS\UI\Implementation\Component\Input\Field\Node\Factory::class),
78  $this->createMock(\ILIAS\UI\Implementation\Component\Input\UploadLimitResolver::class),
79  new SignalGenerator(),
80  $this->data_factory,
81  new Refinery($df, $language),
82  $language
83  );
84  }
85 
86  public function testWithFormat(): void
87  {
88  $format = $this->data_factory->dateFormat()->germanShort();
89  $datetime = $this->factory->datetime('label', 'byline')
90  ->withFormat($format);
91 
92  $this->assertEquals(
93  $format,
94  $datetime->getFormat()
95  );
96  }
97 
98  public function testWithMinValue(): void
99  {
100  $dat = new DateTimeImmutable('2019-01-09');
101  $datetime = $this->factory->datetime('label', 'byline')
102  ->withMinValue($dat);
103 
104  $this->assertEquals(
105  $dat,
106  $datetime->getMinValue()
107  );
108  }
109 
110  public function testWithMaxValue(): void
111  {
112  $dat = new DateTimeImmutable('2019-01-09');
113  $datetime = $this->factory->datetime('label', 'byline')
114  ->withMaxValue($dat);
115 
116  $this->assertEquals(
117  $dat,
118  $datetime->getMaxValue()
119  );
120  }
121 
122  public function testWithUseTime(): void
123  {
124  $datetime = $this->factory->datetime('label', 'byline');
125  $this->assertFalse($datetime->getUseTime());
126  $this->assertTrue($datetime->withUseTime(true)->getUseTime());
127  }
128 
129  public function testWithTimeOnly(): void
130  {
131  $datetime = $this->factory->datetime('label', 'byline');
132  $this->assertFalse($datetime->getTimeOnly());
133  $this->assertTrue($datetime->withTimeOnly(true)->getTimeOnly());
134  }
135 
136  public function testWithTimeZone(): void
137  {
138  $datetime = $this->factory->datetime('label', 'byline');
139  $this->assertNull($datetime->getTimeZone());
140  $tz = 'Europe/Moscow';
141  $this->assertEquals(
142  $tz,
143  $datetime->withTimeZone($tz)->getTimeZone()
144  );
145  }
146 
147  public function testWithInvalidTimeZone(): void
148  {
149  $this->expectException(InvalidArgumentException::class);
150  $datetime = $this->factory->datetime('label', 'byline');
151  $tz = 'NOT/aValidTZ';
152  $datetime->withTimeZone($tz);
153  }
154 
155  public function testWithValueThatIsDateTimeImmutable(): void
156  {
157  $string_value = "1985-05-04 00:00";
158  $value = new \DateTimeImmutable($string_value);
159  $datetime = $this->factory->datetime('label', 'byline')
160  ->withValue($value);
161  $this->assertEquals(
162  $string_value,
163  $datetime->getValue()
164  );
165  }
166 
167  public function testWithInvalidValue(): void
168  {
169  $this->expectException(InvalidArgumentException::class);
170  $datetime = $this->factory->datetime('label', 'byline')
171  ->withValue("this is no datetime...");
172  }
173 
174  public function testRender(): void
175  {
176  $datetime = $this->factory->dateTime('label', 'byline');
177  $r = $this->getDefaultRenderer();
178  $html = $this->brutallyTrimHTML($r->render($datetime));
179 
180  $expected = $this->brutallyTrimHTML('
181  <fieldset class="c-input" data-il-ui-component="date-time-field-input" data-il-ui-input-name="">
182  <label for="id_1">label</label>
183  <div class="c-input__field">
184  <div class="c-input-group">
185  <input id="id_1" type="date" class="c-field-datetime" />
186  </div>
187  </div>
188  <div class="c-input__help-byline">byline</div>
189  </fieldset>
190  ');
191  $this->assertEquals($expected, $html);
192  }
193 
194  public function testCommonRendering(): void
195  {
196  $datetime = $this->factory->dateTime('label')
197  ->withNameFrom($this->name_source);
198 
199  $this->testWithError($datetime);
200  $this->testWithNoByline($datetime);
201  $this->testWithRequired($datetime);
202  $this->testWithDisabled($datetime);
203  $this->testWithAdditionalOnloadCodeRendersId($datetime);
204  }
205 }
Interface Observer Contains several chained tasks and infos about them.
factory()
$datetime
I Input Field Factory $factory
DefNamesource $name_source
Data Factory $data_factory
Builds data types.
Definition: Factory.php:35
$r