Slim daily: minor bugfix with empty data

This commit is contained in:
ArthurHoaro 2020-05-17 11:29:17 +02:00
parent 07f99432b7
commit e3d28be967
2 changed files with 10 additions and 12 deletions

View file

@ -30,10 +30,13 @@ public function index(Request $request, Response $response): Response
$nbAvailableDates = count($availableDates); $nbAvailableDates = count($availableDates);
$index = array_search($day, $availableDates); $index = array_search($day, $availableDates);
if ($index === false && $nbAvailableDates > 0) { if ($index === false) {
// no bookmarks for day, but at least one day with bookmarks // no bookmarks for day, but at least one day with bookmarks
$index = $nbAvailableDates - 1; $day = $availableDates[$nbAvailableDates - 1] ?? $day;
$day = $availableDates[$index]; $previousDay = $availableDates[$nbAvailableDates - 2] ?? '';
} else {
$previousDay = $availableDates[$index - 1] ?? '';
$nextDay = $availableDates[$index + 1] ?? '';
} }
if ($day === date('Ymd')) { if ($day === date('Ymd')) {
@ -42,15 +45,6 @@ public function index(Request $request, Response $response): Response
$this->assignView('dayDesc', t('Yesterday')); $this->assignView('dayDesc', t('Yesterday'));
} }
if ($index !== false) {
if ($index >= 1) {
$previousDay = $availableDates[$index - 1];
}
if ($index < $nbAvailableDates - 1) {
$nextDay = $availableDates[$index + 1];
}
}
try { try {
$linksToDisplay = $this->container->bookmarkService->filterDay($day); $linksToDisplay = $this->container->bookmarkService->filterDay($day);
} catch (\Exception $exc) { } catch (\Exception $exc) {

View file

@ -112,6 +112,8 @@ public function testValidControllerInvokeDefault(): void
'Daily - '. format_date($currentDay, false, true) .' - Shaarli', 'Daily - '. format_date($currentDay, false, true) .' - Shaarli',
$assignedVariables['pagetitle'] $assignedVariables['pagetitle']
); );
static::assertEquals($currentDay, $assignedVariables['dayDate']);
static::assertEquals($currentDay->getTimestamp(), $assignedVariables['day']);
static::assertCount(3, $assignedVariables['linksToDisplay']); static::assertCount(3, $assignedVariables['linksToDisplay']);
$link = $assignedVariables['linksToDisplay'][0]; $link = $assignedVariables['linksToDisplay'][0];
@ -358,6 +360,8 @@ public function testValidControllerInvokeNoBookmark(): void
static::assertSame('daily', (string) $result->getBody()); static::assertSame('daily', (string) $result->getBody());
static::assertCount(0, $assignedVariables['linksToDisplay']); static::assertCount(0, $assignedVariables['linksToDisplay']);
static::assertSame('Today', $assignedVariables['dayDesc']); static::assertSame('Today', $assignedVariables['dayDesc']);
static::assertEquals((new \DateTime())->setTime(0, 0)->getTimestamp(), $assignedVariables['day']);
static::assertEquals((new \DateTime())->setTime(0, 0), $assignedVariables['dayDate']);
} }
protected function createValidContainerMockSet(): void protected function createValidContainerMockSet(): void