Skip to content

Commit

Permalink
[BUGFIX] Fixed calendar display issue for events on DST days
Browse files Browse the repository at this point in the history
Refs #1219
  • Loading branch information
derhansen committed Apr 13, 2024
1 parent 9ae846a commit 07a8ef7
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Classes/Service/CalendarService.php
Expand Up @@ -103,21 +103,29 @@ public function getCalendarDateRange(int $month, int $year, int $firstDayOfWeek
protected function getEventsForDay($events, DateTime $currentDay): array
{
$foundEvents = [];
$day = date('Y-m-d', $currentDay->getTimestamp());

/** @var Event $event */
foreach ($events as $event) {
$eventBeginDate = $event->getStartdate()->format('Y-m-d');
$day = date('Y-m-d', $currentDay->getTimestamp());
if (!is_a($event->getEnddate(), DateTime::class)) {
if ($eventBeginDate === $day) {
$foundEvents[] = $event;
}
} else {
// Create the compare date by cloning the event startdate to prevent timezone/DST issue
$dayParts = explode('-', $day);
$currentDayCompare = clone $event->getStartdate();
$currentDayCompare->setDate((int)$dayParts[0], (int)$dayParts[1], (int)$dayParts[2]);
$currentDayCompare->setTime(0, 0);

$eventEndDate = clone $event->getEnddate();
$eventEndDate->setTime(23, 59, 59);
$eventBeginDate = clone $event->getStartdate();
$eventBeginDate->setTime(0, 0);
$currentDay->setTime(0, 0);
if ($eventBeginDate <= $currentDay && $eventEndDate >= $currentDay) {

if ($eventBeginDate <= $currentDayCompare && $eventEndDate >= $currentDayCompare) {
$foundEvents[] = $event;
}
}
Expand Down
5 changes: 5 additions & 0 deletions Tests/Functional/Fixtures/events_calendarservice.csv
@@ -0,0 +1,5 @@
"tx_sfeventmgt_domain_model_event"
,"pid","startdate","enddate","title","description","link","rowDescription"
,4,1730019600 ,1730026800,Test 27.10.2024 10:00 - 12:00,Test,link,27.10.2024 10:00 - 12:00
,4,1729990800,1729991400,Test 27.10.2024 02:00 - 2:10,Test,link,27.10.2024 - 02:00 - 02:10
,4,1711846800,1711850400,Test 31.03.2024 - 3:00 - 4:00,Test,link,31.03.2024 - 3:00 - 4:00
65 changes: 65 additions & 0 deletions Tests/Functional/Service/CalendarServiceTest.php
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*/

namespace DERHANSEN\SfEventMgt\Tests\Functional\Service;

use DERHANSEN\SfEventMgt\Domain\Model\Dto\EventDemand;
use DERHANSEN\SfEventMgt\Domain\Repository\EventRepository;
use DERHANSEN\SfEventMgt\Service\CalendarService;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

class CalendarServiceTest extends FunctionalTestCase
{
protected EventRepository $eventRepository;
protected array $testExtensionsToLoad = ['typo3conf/ext/sf_event_mgt'];

public function setUp(): void
{
parent::setUp();
$this->importCSVDataSet(__DIR__ . '/../Fixtures/events_calendarservice.csv');

$this->eventRepository = $this->getContainer()->get(EventRepository::class);
}


/**
* Test, if events for the 27.10.2024 (date with timeshift) will be returned in the array
*
* @test
*/
public function getCalendarArrayReturnsArrayWithExpectedEventOnADayWithTimeshiftOctober(): void
{
$demand = new EventDemand();
$demand->setStoragePage('4');
$events = $this->eventRepository->findDemanded($demand);

$calendarService = GeneralUtility::makeInstance(CalendarService::class);
$calendarArray = $calendarService->getCalendarArray(10, 2024, mktime(0, 0, 0, 10, 27, 2024), 1, $events);
self::assertCount(2, $calendarArray[43][6]['events']);
}

/**
* Test, if events for the 31.03.2024 (date with timeshift) will be returned in the array
*
* @test
*/
public function getCalendarArrayReturnsArrayWithExpectedEventOnADayWithTimeshiftMarch(): void
{
$demand = new EventDemand();
$demand->setStoragePage('4');
$events = $this->eventRepository->findDemanded($demand);

$calendarService = GeneralUtility::makeInstance(CalendarService::class);
$calendarArray = $calendarService->getCalendarArray(3, 2024, mktime(0, 0, 0, 3, 31, 2024), 1, $events);
self::assertCount(1, $calendarArray[13][6]['events']);
}
}

0 comments on commit 07a8ef7

Please sign in to comment.