Skip to content

Commit

Permalink
show and edit date in the same timezone
Browse files Browse the repository at this point in the history
add convert timezone test

optimize convert timezone

not use assign operation inside the if condition

test change datetime on change timezone

create named datasets
  • Loading branch information
peter-gribanov authored and jordisala1991 committed Jul 24, 2020
1 parent 224144d commit 17083d3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/Action/SetObjectFieldValueAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,15 @@ public function __invoke(Request $request): JsonResponse

// Handle date and datetime types have setter expecting a DateTime object
if ('' !== $value && \in_array($fieldDescription->getType(), ['date', 'datetime'], true)) {
$value = new \DateTime($value);
$inputTimezone = new \DateTimeZone(date_default_timezone_get());
$outputTimezone = $fieldDescription->getOption('timezone');

if ($outputTimezone && !$outputTimezone instanceof \DateTimeZone) {
$outputTimezone = new \DateTimeZone($outputTimezone);
}

$value = new \DateTime($value, $outputTimezone ?: $inputTimezone);
$value->setTimezone($inputTimezone);
}

// Handle boolean type transforming the value into a boolean
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/views/CRUD/base_list_field.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ file that was distributed with this source code.
) %}

{% if field_description.type == constant('Sonata\\AdminBundle\\Templating\\TemplateRegistry::TYPE_DATE') and value is not empty %}
{% set data_value = value.format('Y-m-d') %}
{% set data_value = value|date('Y-m-d', options.timezone|default(null)) %}
{% elseif field_description.type == constant('Sonata\\AdminBundle\\Templating\\TemplateRegistry::TYPE_BOOLEAN') and value is empty %}
{% set data_value = 0 %}
{% elseif value is iterable %}
Expand Down
2 changes: 1 addition & 1 deletion tests/Action/Bafoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function setDateProp(\DateTime $dateProp): self
return $this;
}

public function getDatetimeProp(): \DateTime
public function getDatetimeProp(): ?\DateTime
{
return $this->datetimeProp;
}
Expand Down
43 changes: 41 additions & 2 deletions tests/Action/SetObjectFieldValueActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,25 @@ public function testSetObjectFieldValueAction(): void
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
}

public function testSetObjectFieldValueActionWithDate(): void
public function getTimeZones(): iterable
{
$default = new \DateTimeZone(date_default_timezone_get());
$custom = new \DateTimeZone('Europe/Rome');

return [
'empty timezone' => [null, $default],
'disabled timezone' => [false, $default],
'default timezone by name' => [$default->getName(), $default],
'default timezone by object' => [$default, $default],
'custom timezone by name' => [$custom->getName(), $custom],
'custom timezone by object' => [$custom, $custom],
];
}

/**
* @dataProvider getTimeZones
*/
public function testSetObjectFieldValueActionWithDate($timezone, \DateTimeZone $expectedTimezone): void
{
$object = new Bafoo();
$request = new Request([
Expand Down Expand Up @@ -161,6 +179,7 @@ public function testSetObjectFieldValueActionWithDate(): void
$container->reveal()
));
$fieldDescription->getOption('editable')->willReturn(true);
$fieldDescription->getOption('timezone')->willReturn($timezone);
$fieldDescription->getAdmin()->willReturn($this->admin->reveal());
$fieldDescription->getType()->willReturn('date');
$fieldDescription->getTemplate()->willReturn('field_template');
Expand All @@ -171,9 +190,20 @@ public function testSetObjectFieldValueActionWithDate(): void
$response = ($this->action)($request);

$this->assertSame(Response::HTTP_OK, $response->getStatusCode());

$defaultTimezone = new \DateTimeZone(date_default_timezone_get());
$expectedDate = new \DateTime($request->query->get('value'), $expectedTimezone);
$expectedDate->setTimezone($defaultTimezone);

$this->assertInstanceOf(\DateTime::class, $object->getDateProp());
$this->assertSame($expectedDate->format('Y-m-d'), $object->getDateProp()->format('Y-m-d'));
$this->assertSame($defaultTimezone->getName(), $object->getDateProp()->getTimezone()->getName());
}

public function testSetObjectFieldValueActionWithDateTime(): void
/**
* @dataProvider getTimeZones
*/
public function testSetObjectFieldValueActionWithDateTime($timezone, \DateTimeZone $expectedTimezone): void
{
$object = new Bafoo();
$request = new Request([
Expand Down Expand Up @@ -208,6 +238,7 @@ public function testSetObjectFieldValueActionWithDateTime(): void
$container->reveal()
));
$fieldDescription->getOption('editable')->willReturn(true);
$fieldDescription->getOption('timezone')->willReturn($timezone);
$fieldDescription->getAdmin()->willReturn($this->admin->reveal());
$fieldDescription->getType()->willReturn('datetime');
$fieldDescription->getTemplate()->willReturn('field_template');
Expand All @@ -218,6 +249,14 @@ public function testSetObjectFieldValueActionWithDateTime(): void
$response = ($this->action)($request);

$this->assertSame(Response::HTTP_OK, $response->getStatusCode());

$defaultTimezone = new \DateTimeZone(date_default_timezone_get());
$expectedDate = new \DateTime($request->query->get('value'), $expectedTimezone);
$expectedDate->setTimezone($defaultTimezone);

$this->assertInstanceOf(\DateTime::class, $object->getDatetimeProp());
$this->assertSame($expectedDate->format('Y-m-d H:i:s'), $object->getDatetimeProp()->format('Y-m-d H:i:s'));
$this->assertSame($defaultTimezone->getName(), $object->getDatetimeProp()->getTimezone()->getName());
}

public function testSetObjectFieldValueActionOnARelationField(): void
Expand Down

0 comments on commit 17083d3

Please sign in to comment.