Skip to content

Commit

Permalink
Merge pull request #1070 from openeuropa/EWPP-2099
Browse files Browse the repository at this point in the history
EWPP-2099: Fix event date formats.
  • Loading branch information
imanoleguskiza authored Mar 29, 2022
2 parents 5be9906 + 9a8350e commit 5eb642b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ function oe_theme_content_event_post_update_30004(): void {
];
foreach ($date_formats as $date_format_name) {
$config = $storage->read($date_format_name);
$date_format = DateFormat::load($config['id']);
// If the date format already exists, we skip it.
if ($date_format) {
continue;
}
$config['_core']['default_config_hash'] = Crypt::hashBase64(serialize($config));
$date_format = DateFormat::create($config);
$date_format->save();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,15 @@ public static function create(ContainerInterface $container, array $configuratio
*
* @param int $timestamp
* The timestamp of start date.
* @param string|null $timezone
* The timezone id.
*
* @return bool
* True if timestamp is within current day.
*/
protected function isCurrentDay(int $timestamp): bool {
$current_date = $this->dateFormatter->format($this->requestDateTime->getTimestamp(), 'custom', 'Ymd');
$start_day = $this->dateFormatter->format($timestamp, 'custom', 'Ymd');
protected function isCurrentDay(int $timestamp, string $timezone = NULL): bool {
$current_date = $this->dateFormatter->format($this->requestDateTime->getTimestamp(), 'custom', 'Ymd', $timezone);
$start_day = $this->dateFormatter->format($timestamp, 'custom', 'Ymd', $timezone);
return $current_date === $start_day;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function viewElements(ContentEntityInterface $entity) {
// render the date only.
if ($event->isOnlinePeriodYetToCome($this->requestDateTime)) {
$this->applyHourTag($build, $event->getOnlineStartDate());
if ($this->isCurrentDay($event->getOnlineStartDate()->getTimestamp())) {
if ($this->isCurrentDay($event->getOnlineStartDate()->getTimestamp(), $event->getOnlineTimezone())) {
$build += $link;
$build['#hide_link'] = TRUE;
$this->attachDisclosureScript($build, $event->getOnlineStartDate()->getTimestamp());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function viewElements(ContentEntityInterface $entity) {
$this->applyHourTag($build, $event->getOnlineStartDate());
// Do not send field value to browser if it is not yet day online
// livestreaming should be started.
if (!$this->isCurrentDay($event->getOnlineStartDate()->getTimestamp())) {
if (!$this->isCurrentDay($event->getOnlineStartDate()->getTimestamp(), $event->getOnlineTimezone())) {
$this->isEmpty = TRUE;
return $build;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function viewElements(ContentEntityInterface $entity) {

// If the request time is on the same day as the start day we need to
// show different message.
if ($this->isCurrentDay($datetime_start->getTimestamp())) {
if ($this->isCurrentDay($datetime_start->getTimestamp(), $event->getRegistrationTimezone())) {
$build['#registration_day_description'] = $this->t('Registration will open today, @start_date.', [
'@start_date' => $this->dateFormatter->format($datetime_start->getTimestamp(), 'oe_event_date_hour_timezone', '', $event->getRegistrationTimezone()),
]);
Expand Down Expand Up @@ -103,7 +103,7 @@ public function viewElements(ContentEntityInterface $entity) {
if ($event->hasRegistrationDates()) {
// If the request time is on the same day as the end day we need to
// show different message.
if ($this->isCurrentDay($datetime_end->getTimestamp())) {
if ($this->isCurrentDay($datetime_end->getTimestamp(), $event->getRegistrationTimezone())) {
$build['#description'] = $this->t('Book your seat, the registration will end today, @end_date', [
'@end_date' => $this->dateFormatter->format($datetime_end->getTimestamp(), 'oe_event_date_hour_timezone', '', $event->getRegistrationTimezone()),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ public function testTimeSensitiveFieldsDisplay(): void {
$node->set('oe_event_online_dates', [
'value' => $start_date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'end_value' => (clone $start_date)->modify('+3 hours')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'timezone' => 'Europe/Brussels',
]);
$node->set('oe_event_registration_dates', [
'value' => $start_date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'end_value' => (clone $start_date)->modify('+3 hours')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'timezone' => 'Europe/Brussels',
]);
$node->save();
$this->drupalGet($node->toUrl());
Expand Down Expand Up @@ -159,10 +161,12 @@ public function testTimeSensitiveFieldsDisplay(): void {
$node->set('oe_event_online_dates', [
'value' => $start_date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'end_value' => (clone $start_date)->modify('+3 hours')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'timezone' => 'Europe/Brussels',
]);
$node->set('oe_event_registration_dates', [
'value' => $start_date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'end_value' => (clone $start_date)->modify('+3 hours')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'timezone' => 'Europe/Brussels',
]);
$node->save();
$this->drupalGet($node->toUrl());
Expand Down Expand Up @@ -203,10 +207,12 @@ public function testTimeSensitiveFieldsDisplay(): void {
$node->set('oe_event_online_dates', [
'value' => (clone $static_time)->modify('-1 hour')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'end_value' => (clone $static_time)->modify('+3 hours')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'timezone' => 'Europe/Brussels',
]);
$node->set('oe_event_registration_dates', [
'value' => (clone $static_time)->modify('-1 hour')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'end_value' => (clone $static_time)->modify('+3 hours')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'timezone' => 'Europe/Brussels',
]);
$node->save();
$this->drupalGet($node->toUrl());
Expand All @@ -230,10 +236,12 @@ public function testTimeSensitiveFieldsDisplay(): void {
$node->set('oe_event_online_dates', [
'value' => (clone $static_time)->modify('-4 hours')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'end_value' => (clone $static_time)->modify('-1 hour')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'timezone' => 'Europe/Brussels',
]);
$node->set('oe_event_registration_dates', [
'value' => (clone $static_time)->modify('-4 hours')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'end_value' => (clone $static_time)->modify('-1 hour')->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'timezone' => 'Europe/Brussels',
]);
$node->save();
$this->drupalGet($node->toUrl());
Expand Down
26 changes: 13 additions & 13 deletions tests/src/Functional/ContentEventRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function testEventFeaturedMediaTranslation(): void {
*/
public function testEventRendering(): void {
// Freeze the time at a specific point.
$static_time = new DrupalDateTime('2020-02-17 14:00:00', DateTimeItemInterface::STORAGE_TIMEZONE);
$static_time = new DrupalDateTime('2020-02-17 14:00:00', 'Europe/Brussels');
$this->freezeTime($static_time);
$start_date = (clone $static_time)->modify('+ 10 days');

Expand All @@ -190,7 +190,7 @@ public function testEventRendering(): void {
'oe_event_dates' => [
'value' => $start_date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'end_value' => $start_date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
'timezone' => DateTimeItemInterface::STORAGE_TIMEZONE,
'timezone' => 'Europe/Brussels',
],
'oe_event_languages' => [
['target_id' => 'http://publications.europa.eu/resource/authority/language/EST'],
Expand Down Expand Up @@ -230,7 +230,7 @@ public function testEventRendering(): void {
'size' => 'm',
], [
'icon' => 'calendar',
'text' => '27 February 2020, 14:00 UTC',
'text' => '27 February 2020, 15:00 CET',
'size' => 'm',
],
],
Expand All @@ -247,7 +247,7 @@ public function testEventRendering(): void {
'items' => [
[
'label' => 'When',
'body' => 'Thursday 27 February 2020, 14:00 UTC',
'body' => 'Thursday 27 February 2020, 15:00 CET',
], [
'label' => 'Languages',
'body' => 'Estonian, French',
Expand Down Expand Up @@ -542,7 +542,7 @@ public function testEventRendering(): void {

// Assert "Registration date" field when registration will start today in
// one hour.
$static_time = new DrupalDateTime('2020-02-18 13:00:00', DateTimeItemInterface::STORAGE_TIMEZONE);
$static_time = new DrupalDateTime('2020-02-18 14:00:00', 'Europe/Brussels');
$this->freezeTime($static_time);
$this->cronRun();
$this->drupalGet($node->toUrl());
Expand All @@ -551,7 +551,7 @@ public function testEventRendering(): void {
$this->assertEquals('Registration will open today, 18 February 2020, 15:00 CET.', $registration_info_content->getText());

// Assert "Registration date" field when registration is in progress.
$static_time = new DrupalDateTime('2020-02-20 14:00:00', DateTimeItemInterface::STORAGE_TIMEZONE);
$static_time = new DrupalDateTime('2020-02-20 15:00:00', 'Europe/Brussels');
$this->freezeTime($static_time);
$this->cronRun();
$this->drupalGet($node->toUrl());
Expand All @@ -561,7 +561,7 @@ public function testEventRendering(): void {

// Assert "Registration date" field when registration will finish today in
// one hour.
$static_time = new DrupalDateTime('2020-02-21 13:00:00', DateTimeItemInterface::STORAGE_TIMEZONE);
$static_time = new DrupalDateTime('2020-02-21 13:00:00', 'Europe/Brussels');
$this->freezeTime($static_time);
$this->cronRun();
$this->drupalGet($node->toUrl());
Expand All @@ -570,7 +570,7 @@ public function testEventRendering(): void {
$this->assertEquals('Book your seat, the registration will end today, 21 February 2020, 15:00 CET', $registration_info_content->getText());

// Assert "Registration date" field in the past.
$static_time = new DrupalDateTime('2020-02-24 13:00:00', DateTimeItemInterface::STORAGE_TIMEZONE);
$static_time = new DrupalDateTime('2020-02-24 13:00:00', 'Europe/Brussels');
$this->freezeTime($static_time);
$this->cronRun();
$this->drupalGet($node->toUrl());
Expand All @@ -580,7 +580,7 @@ public function testEventRendering(): void {

// Assert "Report text" and "Summary for report" fields when event is
// finished.
$static_time = new DrupalDateTime('2020-04-15 13:00:00', DateTimeItemInterface::STORAGE_TIMEZONE);
$static_time = new DrupalDateTime('2020-04-15 13:00:00', 'Europe/Brussels');
$this->freezeTime($static_time);
$this->cronRun();
$this->drupalGet($node->toUrl());
Expand Down Expand Up @@ -1005,21 +1005,21 @@ public function testEventRendering(): void {
$this->assertEmpty($status_container->find('css', 'div.ecl-message__content div.ecl-message__title'));

// Event is ongoing, but livestream is not.
$static_time = new DrupalDateTime('2020-04-17 14:00:00', DateTimeItemInterface::STORAGE_TIMEZONE);
$static_time = new DrupalDateTime('2020-04-17 14:00:00', 'Europe/Brussels');
$this->freezeTime($static_time);
$this->cronRun();
$this->drupalGet($node->toUrl());
$this->assertStringContainsString('This event has started. The livestream will start at 18 April 2020, 23:00 AEST.', $status_container->find('css', 'div.ecl-message__content div.ecl-message__title')->getText());

// Event is ongoing and livestream also.
$static_time = new DrupalDateTime('2020-04-18 20:00:00', DateTimeItemInterface::STORAGE_TIMEZONE);
$static_time = new DrupalDateTime('2020-04-18 20:00:00', 'Europe/Brussels');
$this->freezeTime($static_time);
$this->cronRun();
$this->drupalGet($node->toUrl());
$this->assertStringContainsString('This event has started. You can also watch it via livestream.', $status_container->find('css', 'div.ecl-message__content div.ecl-message__title')->getText());

// Event is ongoing but livestream is finished.
$static_time = new DrupalDateTime('2020-04-20 22:00:00', DateTimeItemInterface::STORAGE_TIMEZONE);
$static_time = new DrupalDateTime('2020-04-20 22:00:00', 'Europe/Brussels');
$this->freezeTime($static_time);
$this->cronRun();
$this->drupalGet($node->toUrl());
Expand Down Expand Up @@ -1062,7 +1062,7 @@ public function testEventRendering(): void {
$this->assertSession()->elementTextNotContains('css', 'div.ecl-message__content div.ecl-message__description', 'Event status message.');

// Set current time after the event ends.
$static_time = new DrupalDateTime('2020-05-15 13:00:00', DateTimeItemInterface::STORAGE_TIMEZONE);
$static_time = new DrupalDateTime('2020-05-15 13:00:00', 'Europe/Brussels');
$this->freezeTime($static_time);
$this->cronRun();
$this->drupalGet($node->toUrl());
Expand Down

0 comments on commit 5eb642b

Please sign in to comment.