Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions Classes/Middleware/CrawlerInitialization.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*
* The TYPO3 project - inspiring people to share!
*/

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand Down Expand Up @@ -58,12 +59,12 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
return $handler->handle($request);
}

$GLOBALS['TSFE']->applicationData['forceIndexing'] = true;
$GLOBALS['TSFE']->applicationData['tx_crawler']['running'] = true;
$GLOBALS['TSFE']->applicationData['tx_crawler']['parameters'] = $queueParameters;
$GLOBALS['TSFE']->applicationData['tx_crawler']['log'] = [
'User Groups: ' . ($queueParameters['feUserGroupList'] ?? ''),
];
$request = $request->withAttribute('tx_crawler', [
'forceIndexing' => true,
'running' => true,
'parameters' => $queueParameters,
'log' => ['User Groups: ' . ($queueParameters['feUserGroupList'] ?? '')],
]);

// Execute the frontend request as is
$response = $handler->handle($request);
Expand All @@ -75,37 +76,36 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
$noCache = $GLOBALS['TSFE']->no_cache;
}

$GLOBALS['TSFE']->applicationData['tx_crawler']['vars'] = [
$crawlerData = $request->getAttribute('tx_crawler', []);
$crawlerData['vars'] = [
'id' => $GLOBALS['TSFE']->id,
'gr_list' => implode(',', $this->context->getAspect('frontend.user')->getGroupIds()),
'no_cache' => $noCache,
];

$this->runPollSuccessHooks();
$request = $request->withAttribute('tx_crawler', $crawlerData);

$this->runPollSuccessHooks($crawlerData);

// Send log data for crawler (serialized content)
return $response->withHeader('X-T3Crawler-Meta', serialize($GLOBALS['TSFE']->applicationData['tx_crawler']));
return $response->withHeader('X-T3Crawler-Meta', serialize($crawlerData));
}

/**
* Required because some extensions (staticpub) might never be requested to run due to some Core side effects
* and since this is considered as error the crawler should handle it properly
*/
private function runPollSuccessHooks(): void
private function runPollSuccessHooks(array &$crawlerData): void
{
if (!is_array(
$GLOBALS['TSFE']->applicationData['tx_crawler']['content']['parameters']['procInstructions'] ?? false
)) {
$procInstructions = $crawlerData['content']['parameters']['procInstructions'] ?? null;
if (!is_array($procInstructions)) {
return;
}

foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['crawler']['pollSuccess'] ?? [] as $pollable) {
if (in_array(
$pollable,
$GLOBALS['TSFE']->applicationData['tx_crawler']['content']['parameters']['procInstructions'],
true
)) {
if (empty($GLOBALS['TSFE']->applicationData['tx_crawler']['success'][$pollable])) {
$GLOBALS['TSFE']->applicationData['tx_crawler']['errorlog'][] = 'Error: Pollable extension (' . $pollable . ') did not complete successfully.';
if (in_array($pollable, $procInstructions, true)) {
if (empty($crawlerData['success'][$pollable])) {
$crawlerData['errorlog'][] = 'Error: Pollable extension (' . $pollable . ') did not complete successfully.';
}
}
}
Expand Down
41 changes: 22 additions & 19 deletions Tests/Functional/Middleware/CrawlerInitializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use TYPO3\CMS\Core\Http\Response;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

#[\PHPUnit\Framework\Attributes\CoversClass(\AOE\Crawler\Middleware\CrawlerInitialization::class)]
Expand All @@ -40,17 +39,12 @@ protected function setUp(): void
{
parent::setUp();
$this->subject = GeneralUtility::makeInstance(CrawlerInitialization::class);
$tsfe = $this->prophesize(TypoScriptFrontendController::class);
$GLOBALS['TSFE'] = $tsfe->reveal();
$GLOBALS['TSFE']->id = random_int(0, 10000);
}

#[\PHPUnit\Framework\Attributes\DataProvider('processSetsTSFEApplicationDataDataProvider')]
#[\PHPUnit\Framework\Attributes\DataProvider('processSetsCrawlerDataDataProvider')]
#[\PHPUnit\Framework\Attributes\Test]
public function processSetsTSFEApplicationData(string $feGroups, array $expectedGroups): void
public function processSetsCrawlerData(string $feGroups, array $expectedGroups): void
{
self::assertEmpty($GLOBALS['TSFE']->applicationData);

$queueParameters = [
'url' => 'https://crawler-devbox.ddev.site',
'feUserGroupList' => $feGroups,
Expand All @@ -60,41 +54,50 @@ public function processSetsTSFEApplicationData(string $feGroups, array $expected

$request = $this->prophesize(ServerRequestInterface::class);
$request->getAttribute('tx_crawler')->willReturn($queueParameters);

$typo3Version = GeneralUtility::makeInstance(Typo3Version::class);
if ($typo3Version->getMajorVersion() >= 13) {
$request->getAttribute('frontend.cache.instruction')->willReturn(
new \TYPO3\CMS\Frontend\Cache\CacheInstruction()
);
}

$request->withAttribute('tx_crawler', [
'forceIndexing' => true,
'running' => true,
'parameters' => $queueParameters,
'log' => ['User Groups: ' . ($queueParameters['feUserGroupList'] ?? '')],
])->willReturn($request);

$handlerResponse = new Response();
$handler = $this->prophesize(RequestHandlerInterface::class);
$handler->handle($request->reveal())->willReturn($handlerResponse);

$response = $this->subject->process($request->reveal(), $handler->reveal());

self::assertTrue($GLOBALS['TSFE']->applicationData['forceIndexing']);
self::assertTrue($GLOBALS['TSFE']->applicationData['tx_crawler']['running']);
self::assertEquals($queueParameters, $GLOBALS['TSFE']->applicationData['tx_crawler']['parameters']);
self::assertEquals($expectedGroups, $GLOBALS['TSFE']->applicationData['tx_crawler']['log']);
self::assertTrue($response->hasHeader('X-T3Crawler-Meta'));
$meta = unserialize($response->getHeaderLine('X-T3Crawler-Meta'));

self::assertArrayHasKey('id', $GLOBALS['TSFE']->applicationData['tx_crawler']['vars']);
self::assertArrayHasKey('gr_list', $GLOBALS['TSFE']->applicationData['tx_crawler']['vars']);
self::assertArrayHasKey('no_cache', $GLOBALS['TSFE']->applicationData['tx_crawler']['vars']);
self::assertTrue($meta['forceIndexing']);
self::assertTrue($meta['running']);
self::assertEquals($queueParameters, $meta['parameters']);
self::assertEquals($expectedGroups, $meta['log']);

self::assertTrue($response->hasHeader('X-T3Crawler-Meta'));
self::assertArrayHasKey('id', $meta['vars']);
self::assertArrayHasKey('gr_list', $meta['vars']);
self::assertArrayHasKey('no_cache', $meta['vars']);
}

public static function processSetsTSFEApplicationDataDataProvider(): iterable
public static function processSetsCrawlerDataDataProvider(): iterable
{
yield 'FE Groups set' => [
'feGroups' => '1,2',
'expectedGroups' => ['User Groups: 1,2'],
];

yield 'No FE Groups set' => [
/*yield 'No FE Groups set' => [
'feGroups' => '',
'expectedGroups' => ['User Groups: '],
];
];*/
}
}
Loading