Skip to content

Commit

Permalink
EWPP-4861: Fixing CircaBC date timezone mapping.
Browse files Browse the repository at this point in the history
  • Loading branch information
upchuk committed Dec 11, 2024
1 parent 660a142 commit c0b9c40
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions modules/oe_media_circabc/src/CircaBcMediaMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public function mapDocumentMedia(CircaBcDocument $document, MediaInterface $medi
}

// Set the created and changed dates.
$created = \DateTime::createFromFormat('Y-m-d\TH:iZ', $document->getProperty('created'));
$created = \DateTime::createFromFormat('Y-m-d\TH:iZ', $document->getProperty('created'), new \DateTimeZone('UTC'));
$media->setCreatedTime($created->getTimestamp());
$changed = \DateTime::createFromFormat('Y-m-d\TH:iZ', $document->getProperty('modified'));
$changed = \DateTime::createFromFormat('Y-m-d\TH:iZ', $document->getProperty('modified'), new \DateTimeZone('UTC'));
$media->setChangedTime($changed->getTimestamp());

// Handle translations.
Expand Down Expand Up @@ -137,12 +137,12 @@ protected function mapTranslations(CircaBcDocument $document, MediaInterface $me

// Set the created and changed dates if they are translatable.
if ($media->getFieldDefinition('created')->isTranslatable()) {
$created = \DateTime::createFromFormat('Y-m-d\TH:iZ', $document_translation->getProperty('created'));
$created = \DateTime::createFromFormat('Y-m-d\TH:iZ', $document_translation->getProperty('created'), new \DateTimeZone('UTC'));
$media_translation->setCreatedTime($created->getTimestamp());
}

if ($media->getFieldDefinition('created')->isTranslatable()) {
$changed = \DateTime::createFromFormat('Y-m-d\TH:iZ', $document_translation->getProperty('modified'));
if ($media->getFieldDefinition('changed')->isTranslatable()) {
$changed = \DateTime::createFromFormat('Y-m-d\TH:iZ', $document_translation->getProperty('modified'), new \DateTimeZone('UTC'));
$media_translation->setChangedTime($changed->getTimestamp());
}
}
Expand Down
12 changes: 6 additions & 6 deletions modules/oe_media_circabc/tests/src/Kernel/DocumentMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public function testPivotCircaBcSync(): void {
$this->assertEquals('application/pdf', $reference['mime']);
$this->assertEquals('sample_pdf.pdf', $reference['filename']);
$this->assertEquals('Test sample file', $media->label());
$this->assertEquals('2023-10-25T05:55:00', (new \DateTime())->setTimestamp((int) $media->getCreatedTime())->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
$this->assertEquals('2023-10-26T05:55:00', (new \DateTime())->setTimestamp((int) $media->getChangedTime())->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
$this->assertEquals('2023-10-25T05:55:00', (new \DateTime())->setTimestamp((int) $media->getCreatedTime())->setTimezone(new \DateTimeZone('UTC'))->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
$this->assertEquals('2023-10-26T05:55:00', (new \DateTime())->setTimestamp((int) $media->getChangedTime())->setTimezone(new \DateTimeZone('UTC'))->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));

// Assert the translations (no translations as the media is not
// translatable).
Expand Down Expand Up @@ -134,8 +134,8 @@ public function testPivotCircaBcSync(): void {
$media_storage->resetCache();
/** @var \Drupal\media\MediaInterface $media */
$media = $media_storage->load($media->id());
$this->assertEquals('2023-10-25T05:55:00', (new \DateTime())->setTimestamp((int) $media->getCreatedTime())->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
$this->assertEquals('2023-10-26T05:55:00', (new \DateTime())->setTimestamp((int) $media->getChangedTime())->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
$this->assertEquals('2023-10-25T05:55:00', (new \DateTime())->setTimestamp((int) $media->getCreatedTime())->setTimezone(new \DateTimeZone('UTC'))->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
$this->assertEquals('2023-10-26T05:55:00', (new \DateTime())->setTimestamp((int) $media->getChangedTime())->setTimezone(new \DateTimeZone('UTC'))->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
$french = $media->getTranslation('fr');
$this->assertEquals('Test sample file FR', $french->label());
$reference = $french->get('oe_media_circabc_reference')->first()->getValue();
Expand All @@ -145,8 +145,8 @@ public function testPivotCircaBcSync(): void {
$this->assertEquals('sample_pdf_FR.pdf', $reference['filename']);
$this->assertTrue($media->getFieldDefinition('created')->isTranslatable());
$this->assertTrue($media->getFieldDefinition('changed')->isTranslatable());
$this->assertEquals('2023-10-23T05:55:00', (new \DateTime())->setTimestamp((int) $french->getCreatedTime())->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
$this->assertEquals('2023-10-27T08:05:00', (new \DateTime())->setTimestamp((int) $french->getChangedTime())->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
$this->assertEquals('2023-10-23T05:55:00', (new \DateTime())->setTimestamp((int) $french->getCreatedTime())->setTimezone(new \DateTimeZone('UTC'))->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
$this->assertEquals('2023-10-27T08:05:00', (new \DateTime())->setTimestamp((int) $french->getChangedTime())->setTimezone(new \DateTimeZone('UTC'))->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));

$portuguese = $media->getTranslation('pt-pt');
$this->assertEquals('Test sample file PT', $portuguese->label());
Expand Down

0 comments on commit c0b9c40

Please sign in to comment.