Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWPP-4861: Fixing CircaBC date timezone mapping. #254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 7 additions & 7 deletions modules/oe_media_circabc/tests/src/Kernel/DocumentMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down 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