fix datetime modify to now (#3511)

This commit is contained in:
Kevin Papst
2022-09-03 01:11:48 +02:00
committed by GitHub
parent 1763a2efff
commit 3f4f63dc33
5 changed files with 40 additions and 7 deletions

View File

@@ -118,7 +118,7 @@ class QuickEntryController extends AbstractController
];
}
$beginTime = $this->configuration->getTimesheetDefaultBeginTime();
$beginTime = $factory->createDateTime($this->configuration->getTimesheetDefaultBeginTime())->format('H:i:s');
// fill all rows and columns to make sure we do not have missing records
/** @var QuickEntryModel[] $models */

View File

@@ -55,7 +55,11 @@ final class DurationFixedBeginMode implements TrackingModeInterface
}
$newBegin = clone $timesheet->getBegin();
$newBegin->modify($this->configuration->getTimesheetDefaultBeginTime());
// this prevents the problem that "now" is being ignored in modify()
$beginTime = (new DateTime($this->configuration->getTimesheetDefaultBeginTime(), $newBegin->getTimezone()))->format('H:i:s');
$newBegin->modify($beginTime);
$timesheet->setBegin($newBegin);
}

View File

@@ -63,7 +63,12 @@ final class DurationOnlyMode extends AbstractTrackingMode
}
$newBegin = clone $timesheet->getBegin();
$newBegin->modify($this->configuration->getTimesheetDefaultBeginTime());
// this prevents the problem that "now" is being ignored in modify()
$beginTime = $this->configuration->getTimesheetDefaultBeginTime();
$beginTime = (new DateTime($this->configuration->getTimesheetDefaultBeginTime(), $newBegin->getTimezone()))->format('H:i:s');
$newBegin->modify($beginTime);
$timesheet->setBegin($newBegin);
parent::create($timesheet, $request);

View File

@@ -22,10 +22,10 @@ use Symfony\Component\HttpFoundation\Request;
*/
class DurationFixedBeginModeTest extends TestCase
{
protected function createSut()
protected function createSut($default = '13:47')
{
$loader = new TestConfigLoader([]);
$configuration = new SystemConfiguration($loader, ['timesheet' => ['default_begin' => '13:47']]);
$configuration = new SystemConfiguration($loader, ['timesheet' => ['default_begin' => $default]]);
return new DurationFixedBeginMode($configuration);
}
@@ -42,6 +42,18 @@ class DurationFixedBeginModeTest extends TestCase
self::assertEquals('duration_fixed_begin', $sut->getId());
}
public function testNow()
{
$seconds = (new \DateTime())->getTimestamp();
$timesheet = new Timesheet();
$timesheet->setBegin(new \DateTime('18:50:32'));
$mode = $this->createSut('now');
$mode->create($timesheet);
$diff = $timesheet->getBegin()->getTimestamp() - $seconds;
// amount of seconds doesn't really matter, it must only be near "now"
self::assertLessThanOrEqual(2, $diff);
}
public function testCreate()
{
$timesheet = new Timesheet();

View File

@@ -25,14 +25,26 @@ class DurationOnlyModeTest extends AbstractTrackingModeTest
self::assertEquals('13:45:37', $timesheet->getBegin()->format('H:i:s'));
}
protected function createSut()
protected function createSut($default = '13:45:37')
{
$loader = new TestConfigLoader([]);
$configuration = new SystemConfiguration($loader, ['timesheet' => ['default_begin' => '13:45:37']]);
$configuration = new SystemConfiguration($loader, ['timesheet' => ['default_begin' => $default]]);
return new DurationOnlyMode($configuration);
}
public function testNow()
{
$seconds = (new \DateTime())->getTimestamp();
$timesheet = new Timesheet();
$timesheet->setBegin(new \DateTime('18:50:32'));
$mode = $this->createSut('now');
$mode->create($timesheet);
$diff = $timesheet->getBegin()->getTimestamp() - $seconds;
// amount of seconds doesn't really matter, it must only be near "now"
self::assertLessThanOrEqual(2, $diff);
}
public function testDefaultValues()
{
$sut = $this->createSut();