Skip to content

Commit b41f23c

Browse files
Merge branch '6.0' into 6.1
* 6.0: Remove former core members from code owners [Form] fix populating single widget time view data with different timezones [DomCrawler][VarDumper] Fix html-encoding emojis
2 parents 746e6df + 3eecdd4 commit b41f23c

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

Extension/Core/DataTransformer/DateTimeToStringTransformer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ class DateTimeToStringTransformer extends BaseDateTimeTransformer
4343
* @param string|null $inputTimezone The name of the input timezone
4444
* @param string|null $outputTimezone The name of the output timezone
4545
* @param string $format The date format
46+
* @param string|null $parseFormat The parse format when different from $format
4647
*/
47-
public function __construct(string $inputTimezone = null, string $outputTimezone = null, string $format = 'Y-m-d H:i:s')
48+
public function __construct(string $inputTimezone = null, string $outputTimezone = null, string $format = 'Y-m-d H:i:s', string $parseFormat = null)
4849
{
4950
parent::__construct($inputTimezone, $outputTimezone);
5051

51-
$this->generateFormat = $this->parseFormat = $format;
52+
$this->generateFormat = $format;
53+
$this->parseFormat = $parseFormat ?? $format;
5254

5355
// See https://php.net/datetime.createfromformat
5456
// The character "|" in the format makes sure that the parts of a date

Extension/Core/Type/TimeType.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
7474
}
7575
});
7676

77+
$parseFormat = null;
78+
7779
if (null !== $options['reference_date']) {
78-
$format = 'Y-m-d '.$format;
80+
$parseFormat = 'Y-m-d '.$format;
7981

8082
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
8183
$data = $event->getData();
@@ -86,7 +88,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
8688
});
8789
}
8890

89-
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format));
91+
$builder->addViewTransformer(new DateTimeToStringTransformer($options['model_timezone'], $options['view_timezone'], $format, $parseFormat));
9092
} else {
9193
$hourOptions = $minuteOptions = $secondOptions = [
9294
'error_bubbling' => true,

Tests/Extension/Core/Type/TimeTypeTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,76 @@ public function testSubmitWithSecondsAndBrowserOmissionSeconds()
280280
$this->assertEquals('03:04:00', $form->getViewData());
281281
}
282282

283+
public function testPreSetDataDifferentTimezones()
284+
{
285+
$form = $this->factory->create(static::TESTED_TYPE, null, [
286+
'model_timezone' => 'UTC',
287+
'view_timezone' => 'Europe/Berlin',
288+
'input' => 'datetime',
289+
'with_seconds' => true,
290+
'reference_date' => new \DateTimeImmutable('2019-01-01', new \DateTimeZone('UTC')),
291+
]);
292+
$form->setData(new \DateTime('2022-01-01 15:09:10', new \DateTimeZone('UTC')));
293+
294+
$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
295+
$this->assertSame([
296+
'hour' => '16',
297+
'minute' => '9',
298+
'second' => '10',
299+
], $form->getViewData());
300+
}
301+
302+
public function testPreSetDataDifferentTimezonesDuringDaylightSavingTime()
303+
{
304+
$form = $this->factory->create(static::TESTED_TYPE, null, [
305+
'model_timezone' => 'UTC',
306+
'view_timezone' => 'Europe/Berlin',
307+
'input' => 'datetime',
308+
'with_seconds' => true,
309+
'reference_date' => new \DateTimeImmutable('2019-07-12', new \DateTimeZone('UTC')),
310+
]);
311+
$form->setData(new \DateTime('2022-04-29 15:09:10', new \DateTimeZone('UTC')));
312+
313+
$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
314+
$this->assertSame([
315+
'hour' => '17',
316+
'minute' => '9',
317+
'second' => '10',
318+
], $form->getViewData());
319+
}
320+
321+
public function testPreSetDataDifferentTimezonesUsingSingleTextWidget()
322+
{
323+
$form = $this->factory->create(static::TESTED_TYPE, null, [
324+
'model_timezone' => 'UTC',
325+
'view_timezone' => 'Europe/Berlin',
326+
'input' => 'datetime',
327+
'with_seconds' => true,
328+
'reference_date' => new \DateTimeImmutable('2019-01-01', new \DateTimeZone('UTC')),
329+
'widget' => 'single_text',
330+
]);
331+
$form->setData(new \DateTime('2022-01-01 15:09:10', new \DateTimeZone('UTC')));
332+
333+
$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
334+
$this->assertSame('16:09:10', $form->getViewData());
335+
}
336+
337+
public function testPreSetDataDifferentTimezonesDuringDaylightSavingTimeUsingSingleTextWidget()
338+
{
339+
$form = $this->factory->create(static::TESTED_TYPE, null, [
340+
'model_timezone' => 'UTC',
341+
'view_timezone' => 'Europe/Berlin',
342+
'input' => 'datetime',
343+
'with_seconds' => true,
344+
'reference_date' => new \DateTimeImmutable('2019-07-12', new \DateTimeZone('UTC')),
345+
'widget' => 'single_text',
346+
]);
347+
$form->setData(new \DateTime('2022-04-29 15:09:10', new \DateTimeZone('UTC')));
348+
349+
$this->assertSame('15:09:10', $form->getData()->format('H:i:s'));
350+
$this->assertSame('17:09:10', $form->getViewData());
351+
}
352+
283353
public function testSubmitDifferentTimezones()
284354
{
285355
$form = $this->factory->create(static::TESTED_TYPE, null, [

0 commit comments

Comments
 (0)