diff --git a/modules/oe_media_circabc/src/CircaBcMediaMapper.php b/modules/oe_media_circabc/src/CircaBcMediaMapper.php index d7c3757f..2dc185af 100644 --- a/modules/oe_media_circabc/src/CircaBcMediaMapper.php +++ b/modules/oe_media_circabc/src/CircaBcMediaMapper.php @@ -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. @@ -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()); } } diff --git a/modules/oe_media_circabc/tests/src/Kernel/DocumentMediaTest.php b/modules/oe_media_circabc/tests/src/Kernel/DocumentMediaTest.php index be98657b..f53534ce 100644 --- a/modules/oe_media_circabc/tests/src/Kernel/DocumentMediaTest.php +++ b/modules/oe_media_circabc/tests/src/Kernel/DocumentMediaTest.php @@ -5,11 +5,11 @@ namespace Drupal\Tests\oe_media_circabc\Kernel; use Drupal\Core\Site\Settings; +use Drupal\Tests\oe_media\Kernel\MediaTestBase; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\field\Entity\FieldConfig; use Drupal\language\Entity\ConfigurableLanguage; use Drupal\oe_media_circabc\Plugin\views\query\CircaBcQuery; -use Drupal\Tests\oe_media\Kernel\MediaTestBase; use Drupal\views\ViewExecutable; use Drupal\views\Views; @@ -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). @@ -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(); @@ -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());