2023-05-04 15:23:23 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 Daniel Siepmann <coding@daniel-siepmann.de>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Wrm\Events\Tests\Functional\Frontend;
|
|
|
|
|
|
|
|
use Codappix\Typo3PhpDatasets\PhpDataSet;
|
|
|
|
use DateTimeImmutable;
|
|
|
|
use DateTimeZone;
|
2023-06-19 10:13:26 +02:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
2023-05-04 15:23:23 +02:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2023-06-19 10:13:26 +02:00
|
|
|
use TYPO3\CMS\Core\Information\Typo3Version;
|
2023-05-04 15:23:23 +02:00
|
|
|
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
|
2023-06-19 10:13:26 +02:00
|
|
|
use Wrm\Events\Tests\Functional\AbstractFunctionalTestCase;
|
2023-05-04 15:23:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Wrm\Events\Caching\PageCacheTimeout
|
|
|
|
*/
|
2023-06-19 10:13:26 +02:00
|
|
|
class CacheTest extends AbstractFunctionalTestCase
|
2023-05-04 15:23:23 +02:00
|
|
|
{
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2023-06-19 10:13:26 +02:00
|
|
|
$this->testExtensionsToLoad = [
|
|
|
|
'typo3conf/ext/events/Tests/Functional/Frontend/Fixtures/Extensions/example',
|
|
|
|
];
|
|
|
|
|
2023-05-04 15:23:23 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
2023-05-15 11:58:19 +02:00
|
|
|
$this->importPHPDataSet(__DIR__ . '/Fixtures/Database/SiteStructure.php');
|
2023-05-04 15:23:23 +02:00
|
|
|
(new PhpDataSet())->import(['tt_content' => [[
|
|
|
|
'uid' => '1',
|
|
|
|
'pid' => '1',
|
|
|
|
'CType' => 'list',
|
|
|
|
'list_type' => 'events_datelisttest',
|
|
|
|
'header' => 'All Dates',
|
|
|
|
]]]);
|
2023-06-19 10:13:26 +02:00
|
|
|
$this->setUpFrontendRendering();
|
2023-05-04 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-05-23 11:46:18 +02:00
|
|
|
public function returnsSystemDefaults(): void
|
2023-05-04 15:23:23 +02:00
|
|
|
{
|
2023-05-16 10:15:05 +02:00
|
|
|
$response = $this->executeFrontendRequest($this->getRequestWithSleep());
|
2023-05-04 15:23:23 +02:00
|
|
|
|
|
|
|
self::assertSame(200, $response->getStatusCode());
|
|
|
|
self::assertSame('max-age=86400', $response->getHeaderLine('Cache-Control'));
|
|
|
|
self::assertSame('public', $response->getHeaderLine('Pragma'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-05-23 11:46:18 +02:00
|
|
|
public function returnsDefaultsIfEventsEndLater(): void
|
2023-05-04 15:23:23 +02:00
|
|
|
{
|
|
|
|
(new PhpDataSet())->import([
|
|
|
|
'tx_events_domain_model_event' => [
|
|
|
|
[
|
|
|
|
'uid' => '1',
|
|
|
|
'title' => 'Test Event 1',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'tx_events_domain_model_date' => [
|
|
|
|
[
|
|
|
|
'event' => '1',
|
|
|
|
'start' => time(),
|
|
|
|
'end' => time() + 86400 + 50,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2023-05-16 10:15:05 +02:00
|
|
|
$response = $this->executeFrontendRequest($this->getRequestWithSleep());
|
2023-05-04 15:23:23 +02:00
|
|
|
|
|
|
|
self::assertSame(200, $response->getStatusCode());
|
|
|
|
self::assertSame('max-age=86400', $response->getHeaderLine('Cache-Control'));
|
|
|
|
self::assertSame('public', $response->getHeaderLine('Pragma'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-05-23 11:46:18 +02:00
|
|
|
public function returnsEarlierIfEventsEndEarlier(): void
|
2023-05-04 15:23:23 +02:00
|
|
|
{
|
2023-05-16 10:15:05 +02:00
|
|
|
$end = (new DateTimeImmutable('tomorrow midnight', new DateTimeZone('UTC')))->modify('+2 hours');
|
|
|
|
|
2023-05-04 15:23:23 +02:00
|
|
|
(new PhpDataSet())->import([
|
|
|
|
'tx_events_domain_model_event' => [
|
|
|
|
[
|
|
|
|
'uid' => '1',
|
|
|
|
'pid' => '2',
|
|
|
|
'title' => 'Test Event 1',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'tx_events_domain_model_date' => [
|
|
|
|
[
|
|
|
|
'pid' => '2',
|
|
|
|
'event' => '1',
|
|
|
|
'start' => time(),
|
2023-05-16 10:15:05 +02:00
|
|
|
'end' => $end->format('U'),
|
2023-05-04 15:23:23 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2023-05-16 10:15:05 +02:00
|
|
|
$response = $this->executeFrontendRequest($this->getRequestWithSleep());
|
2023-05-04 15:23:23 +02:00
|
|
|
|
|
|
|
self::assertSame(200, $response->getStatusCode());
|
2023-05-16 10:15:05 +02:00
|
|
|
self::assertCacheHeaders($end, $response);
|
2023-05-04 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-05-23 11:46:18 +02:00
|
|
|
public function returnsEarlierIfStartEndEalierAndIsUpcoming(): void
|
|
|
|
{
|
|
|
|
$end = (new DateTimeImmutable('now', new DateTimeZone('UTC')))->modify('+2 hours');
|
|
|
|
|
|
|
|
(new PhpDataSet())->import([
|
|
|
|
'tx_events_domain_model_event' => [
|
|
|
|
[
|
|
|
|
'uid' => '1',
|
|
|
|
'pid' => '2',
|
|
|
|
'title' => 'Test Event 1',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'tx_events_domain_model_date' => [
|
|
|
|
[
|
|
|
|
'pid' => '2',
|
|
|
|
'event' => '1',
|
|
|
|
'start' => $end->format('U'),
|
|
|
|
'end' => '0',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response = $this->executeFrontendRequest($this->getRequestWithSleep([
|
|
|
|
'plugin.' => [
|
|
|
|
'tx_events.' => [
|
|
|
|
'settings.' => [
|
|
|
|
'upcoming' => 1,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]));
|
|
|
|
|
|
|
|
self::assertSame(200, $response->getStatusCode());
|
|
|
|
self::assertCacheHeaders($end, $response);
|
|
|
|
}
|
|
|
|
|
2023-05-24 09:45:15 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function usesEarliestTimeout(): void
|
|
|
|
{
|
|
|
|
$end = (new DateTimeImmutable('now', new DateTimeZone('UTC')))->modify('+2 hours');
|
|
|
|
|
|
|
|
(new PhpDataSet())->import([
|
|
|
|
'tx_events_domain_model_event' => [
|
|
|
|
[
|
|
|
|
'uid' => '1',
|
|
|
|
'pid' => '2',
|
|
|
|
'title' => 'Test Event 1',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'uid' => '2',
|
|
|
|
'pid' => '2',
|
|
|
|
'title' => 'Test Event 2',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'tx_events_domain_model_date' => [
|
|
|
|
[
|
|
|
|
'pid' => '2',
|
|
|
|
'event' => '1',
|
|
|
|
'start' => $end->format('U'),
|
|
|
|
'end' => '0',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'pid' => '2',
|
|
|
|
'event' => '2',
|
|
|
|
'start' => $end->modify('+2 hours')->format('U'),
|
|
|
|
'end' => '0',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response = $this->executeFrontendRequest($this->getRequestWithSleep([
|
|
|
|
'plugin.' => [
|
|
|
|
'tx_events.' => [
|
|
|
|
'settings.' => [
|
|
|
|
'upcoming' => 1,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]));
|
|
|
|
|
|
|
|
self::assertSame(200, $response->getStatusCode());
|
|
|
|
self::assertCacheHeaders($end, $response);
|
|
|
|
}
|
|
|
|
|
2023-05-23 11:46:18 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function returnsMidnightIfConfigured(): void
|
2023-05-04 15:23:23 +02:00
|
|
|
{
|
2023-05-16 10:15:05 +02:00
|
|
|
$midnight = (new DateTimeImmutable('tomorrow midnight', new DateTimeZone('UTC')));
|
|
|
|
|
2023-05-04 15:23:23 +02:00
|
|
|
(new PhpDataSet())->import([
|
|
|
|
'tx_events_domain_model_event' => [
|
|
|
|
[
|
|
|
|
'uid' => '1',
|
|
|
|
'pid' => '2',
|
|
|
|
'title' => 'Test Event 1',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'tx_events_domain_model_date' => [
|
|
|
|
[
|
|
|
|
'pid' => '2',
|
|
|
|
'event' => '1',
|
|
|
|
'start' => time(),
|
|
|
|
'end' => time() + 50,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->setUpFrontendRootPage(1, array_merge_recursive($this->getTypoScriptFiles(), [
|
|
|
|
'setup' => [
|
2023-06-07 08:56:42 +02:00
|
|
|
'EXT:events/Tests/Functional/Frontend/Fixtures/TypoScript/CachingMidnight.typoscript',
|
2023-05-04 15:23:23 +02:00
|
|
|
],
|
|
|
|
]));
|
|
|
|
|
2023-05-16 10:15:05 +02:00
|
|
|
$response = $this->executeFrontendRequest($this->getRequestWithSleep());
|
2023-05-04 15:23:23 +02:00
|
|
|
|
|
|
|
self::assertSame(200, $response->getStatusCode());
|
2023-05-16 10:15:05 +02:00
|
|
|
self::assertCacheHeaders($midnight, $response);
|
2023-05-04 15:23:23 +02:00
|
|
|
self::assertSame('public', $response->getHeaderLine('Pragma'));
|
|
|
|
}
|
|
|
|
|
2023-06-19 10:13:26 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function cachesAreClearedByImport(): void
|
|
|
|
{
|
|
|
|
// Assert frontend is cached
|
|
|
|
$this->assertResponseIsNotCached($this->executeFrontendRequest($this->getRequestWithSleep()));
|
|
|
|
$this->assertResponseIsCached($this->executeFrontendRequest($this->getRequestWithSleep()));
|
|
|
|
|
|
|
|
// Import
|
|
|
|
$this->importPHPDataSet(__DIR__ . '/../Import/DestinationDataTest/Fixtures/Database/DefaultImportConfiguration.php');
|
|
|
|
$this->setUpConfiguration([
|
|
|
|
'restUrl = https://example.com/some-path/',
|
|
|
|
'license = example-license',
|
|
|
|
'restType = Event',
|
|
|
|
'restLimit = 3',
|
|
|
|
'restMode = next_months,12',
|
|
|
|
'restTemplate = ET2014A.json',
|
|
|
|
]);
|
|
|
|
$this->setUpResponses([
|
|
|
|
new Response(200, [], file_get_contents(__DIR__ . '/../Import/DestinationDataTest/Fixtures/ResponseWithSingleImageForSingleEvent.json') ?: ''),
|
|
|
|
new Response(200, [], file_get_contents(__DIR__ . '/../Import/DestinationDataTest/Fixtures/ExampleImage.jpg') ?: ''),
|
|
|
|
]);
|
|
|
|
$this->executeCommand();
|
|
|
|
|
|
|
|
// Assert frontend is not cached on first hit
|
|
|
|
$this->setUpFrontendRendering();
|
|
|
|
$this->assertResponseIsNotCached($this->executeFrontendRequest($this->getRequestWithSleep()));
|
|
|
|
$this->assertResponseIsCached($this->executeFrontendRequest($this->getRequestWithSleep()));
|
|
|
|
}
|
|
|
|
|
2023-05-16 10:15:05 +02:00
|
|
|
private static function assertCacheHeaders(DateTimeImmutable $end, ResponseInterface $response): void
|
2023-05-04 15:23:23 +02:00
|
|
|
{
|
2023-05-16 10:15:05 +02:00
|
|
|
self::assertSame('public', $response->getHeaderLine('Pragma'));
|
2023-05-04 15:23:23 +02:00
|
|
|
|
2023-05-16 10:15:05 +02:00
|
|
|
$expectedExpires = $end
|
|
|
|
->setTimezone(new DateTimeZone('GMT'))
|
|
|
|
->format(DateTimeImmutable::RFC7231)
|
|
|
|
;
|
|
|
|
self::assertSame($expectedExpires, $response->getHeaderLine('Expires'));
|
|
|
|
|
|
|
|
[$prefix, $value] = explode('=', $response->getHeaderLine('Cache-Control'));
|
2023-05-04 15:23:23 +02:00
|
|
|
self::assertSame('max-age', $prefix);
|
|
|
|
|
2023-05-16 10:15:05 +02:00
|
|
|
// We might be seconds off due to our created offset within the rendering.
|
2023-05-04 15:23:23 +02:00
|
|
|
$value = (int)$value;
|
2023-06-07 08:56:42 +02:00
|
|
|
$age = ((int)$end->format('U')) - time();
|
2023-05-16 10:15:05 +02:00
|
|
|
self::assertLessThanOrEqual($age + 3, $value, 'Max age of cached response is higher than expected.');
|
|
|
|
self::assertGreaterThanOrEqual($age - 3, $value, 'Max age of cached response is less than expected.');
|
2023-05-04 15:23:23 +02:00
|
|
|
}
|
|
|
|
|
2023-06-19 10:13:26 +02:00
|
|
|
private function assertResponseIsNotCached(ResponseInterface $response): void
|
2023-05-04 15:23:23 +02:00
|
|
|
{
|
2023-06-19 10:13:26 +02:00
|
|
|
if ((new Typo3Version())->getMajorVersion() < 11) {
|
|
|
|
self::assertStringNotContainsString('Cached page', $response->getBody()->__toString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self::assertStringStartsNotWith('Cached page', $response->getHeaderLine('X-TYPO3-Debug-Cache'));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function assertResponseIsCached(ResponseInterface $response): void
|
|
|
|
{
|
|
|
|
if ((new Typo3Version())->getMajorVersion() < 11) {
|
|
|
|
self::assertStringContainsString('Cached page', $response->getBody()->__toString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self::assertStringStartsWith('Cached page', $response->getHeaderLine('X-TYPO3-Debug-Cache'));
|
2023-05-04 15:23:23 +02:00
|
|
|
}
|
2023-05-16 10:15:05 +02:00
|
|
|
|
2023-05-23 11:46:18 +02:00
|
|
|
private function getRequestWithSleep(array $typoScript = []): InternalRequest
|
2023-05-16 10:15:05 +02:00
|
|
|
{
|
|
|
|
$request = new InternalRequest();
|
|
|
|
$request = $request->withPageId(1);
|
|
|
|
$request = $request->withInstructions([
|
|
|
|
$this->getTypoScriptInstruction()
|
2023-05-23 11:46:18 +02:00
|
|
|
->withTypoScript(array_merge_recursive($typoScript, [
|
2023-05-16 10:15:05 +02:00
|
|
|
'page.' => [
|
|
|
|
'30.' => [
|
|
|
|
'userFunc.' => [
|
|
|
|
'sleep' => '2',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2023-06-07 08:56:42 +02:00
|
|
|
])),
|
2023-05-16 10:15:05 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
return $request;
|
|
|
|
}
|
2023-05-04 15:23:23 +02:00
|
|
|
}
|