Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use site fallback for links on TYPO3v10 #29

Merged
merged 5 commits into from
Jan 12, 2021
Merged
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
5 changes: 1 addition & 4 deletions Classes/DataCollector/Utility/OverlayUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PAGEmachine\Searchable\Utility\TsfeUtility;
use TYPO3\CMS\Core\SingletonInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Frontend\Page\PageRepository;

/*
Expand Down Expand Up @@ -33,9 +32,7 @@ public static function getInstance()
*/
public function __construct(PageRepository $pageRepository = null)
{
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '10', '<')) {
TsfeUtility::createTSFE();
}
TsfeUtility::createTSFE();

$this->pageRepository = $pageRepository ?: GeneralUtility::makeInstance(PageRepository::class);
}
Expand Down
2 changes: 1 addition & 1 deletion Classes/LinkBuilder/PageLinkBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class PageLinkBuilder extends AbstractLinkBuilder
'noCacheHash' => false,
'section' => '',
'linkAccessRestrictedPages' => false,
'absolute' => false,
'absolute' => true,
'addQueryString' => false,
'argumentsToBeExcludedFromQueryString' => [],
'addQueryStringMethod' => null,
Expand Down
13 changes: 13 additions & 0 deletions Classes/Middleware/UriBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Psr\Http\Server\RequestHandlerInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Site\Entity\NullSite;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand Down Expand Up @@ -62,6 +65,16 @@ private function bootFrontendController(ServerRequestInterface $request): void
return;
}

$site = $request->getAttribute('site');

if ($site instanceof NullSite) {
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
$site = array_values($siteFinder->getAllSites())[0] ?? $site;
$request = $request->withAttribute('site', $site);
$request = $request->withAttribute('language', $site->getDefaultLanguage());
$request = $request->withAttribute('routing', new PageArguments($site->getRootPageId(), '0', []));
}

$frontendController = GeneralUtility::makeInstance(
TypoScriptFrontendController::class,
GeneralUtility::makeInstance(Context::class),
Expand Down
70 changes: 54 additions & 16 deletions Classes/Utility/TsfeUtility.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<?php
namespace PAGEmachine\Searchable\Utility;

use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Http\ServerRequestFactory;
use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Utility\EidUtility;

Expand All @@ -21,27 +27,59 @@ class TsfeUtility
*/
public static function createTSFE()
{
// @extensionScannerIgnoreLine
if (class_exists(EidUtility::class)) {
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '10', '<')) {
// @extensionScannerIgnoreLine
EidUtility::initTCA(); // @phpstan-ignore-line
}
if (class_exists(EidUtility::class)) {
// @extensionScannerIgnoreLine
EidUtility::initTCA(); // @phpstan-ignore-line
}

if (!is_object($GLOBALS['TSFE'])) {
$GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], 1, 0);
if (!is_object($GLOBALS['TSFE'])) {
$GLOBALS['TSFE'] = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $GLOBALS['TYPO3_CONF_VARS'], 1, 0);

if (method_exists($GLOBALS['TSFE'], 'connectToDB')) { // TYPO3v9+
// @extensionScannerIgnoreLine
$GLOBALS['TSFE']->connectToDB();
if (method_exists($GLOBALS['TSFE'], 'connectToDB')) { // TYPO3v9+
// @extensionScannerIgnoreLine
$GLOBALS['TSFE']->connectToDB();
}

$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->determineId();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->getConfigArray();
$_SERVER['HTTP_HOST'] = 'localhost';
GeneralUtility::flushInternalRuntimeCaches();
$GLOBALS['TSFE']->preparePageContentGeneration();
}

$GLOBALS['TSFE']->initFEuser();
$GLOBALS['TSFE']->determineId();
$GLOBALS['TSFE']->initTemplate();
$GLOBALS['TSFE']->getConfigArray();
$_SERVER['HTTP_HOST'] = 'localhost';
GeneralUtility::flushInternalRuntimeCaches();
$GLOBALS['TSFE']->preparePageContentGeneration();
return;
}

$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
$site = array_values($siteFinder->getAllSites())[0] ?? null;

if ($site === null) {
throw new \RuntimeException('No site found for TSFE setup', 1610444900);
}

$requestFactory = GeneralUtility::makeInstance(ServerRequestFactory::class);
$request = $requestFactory->createServerRequest('get', 'http://localhost')
->withAttribute('site', $site)
->withAttribute('language', $site->getDefaultLanguage())
->withAttribute('routing', new PageArguments($site->getRootPageId(), '0', []))
->withAttribute('frontend.user', GeneralUtility::makeInstance(FrontendUserAuthentication::class));
$GLOBALS['TYPO3_REQUEST'] = $request;

$frontendController = GeneralUtility::makeInstance(
TypoScriptFrontendController::class,
GeneralUtility::makeInstance(Context::class),
$request->getAttribute('site'),
$request->getAttribute('language'),
$request->getAttribute('routing'),
$request->getAttribute('frontend.user')
);
$frontendController->fetch_the_id($request);
$frontendController->getConfigArray($request);

$GLOBALS['TSFE'] = $frontendController;
}
}
40 changes: 37 additions & 3 deletions Tests/Functional/AbstractElasticsearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,39 @@ protected function setUp()
],
],
'indexers' => [
'pages' => [
'foo_pages' => [
'className' => PagesIndexer::class,
'config' => [
'type' => 'test_pages',
'type' => 'foo_pages',
'collector' => [
'config' => [
'pid' => 1,
],
],
],
],
'bar_pages' => [
'className' => PagesIndexer::class,
'config' => [
'type' => 'bar_pages',
'collector' => [
'config' => [
'pid' => 100,
],
],
],
],
'qux_pages' => [
'className' => PagesIndexer::class,
'config' => [
'type' => 'qux_pages',
'collector' => [
'config' => [
'pid' => 200,
],
],
],
],
'content' => [
'className' => TcaIndexer::class,
'config' => [
Expand All @@ -109,7 +131,17 @@ protected function setUp()
$this->getDatabaseConnection()->insertArray('pages', [
'uid' => 1,
'doktype' => PageRepository::DOKTYPE_DEFAULT,
'title' => 'Root',
'title' => 'Foo Root',
]);
$this->getDatabaseConnection()->insertArray('pages', [
'uid' => 100,
'doktype' => PageRepository::DOKTYPE_DEFAULT,
'title' => 'Bar Root',
]);
$this->getDatabaseConnection()->insertArray('pages', [
'uid' => 200,
'doktype' => PageRepository::DOKTYPE_DEFAULT,
'title' => 'Qux Root',
]);

$typoScriptConstantsFile = 'EXT:searchable/Configuration/TypoScript/constants.typoscript';
Expand All @@ -122,6 +154,8 @@ protected function setUp()
__DIR__ . '/Fixtures/TypoScript/page.typoscript',
$typoScriptSetupFile,
]);
$this->setUpFrontendRootPage(100);
$this->setUpFrontendRootPage(200);
$this->getDatabaseConnection()->updateArray(
'sys_template',
[
Expand Down
66 changes: 64 additions & 2 deletions Tests/Functional/Service/IndexingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use PAGEmachine\Searchable\Service\IndexingService;
use PAGEmachine\Searchable\Tests\Functional\AbstractElasticsearchTest;
use TYPO3\CMS\Core\Configuration\SiteConfiguration;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Log\LogLevel;
use TYPO3\CMS\Core\Log\Writer\FileWriter;
Expand Down Expand Up @@ -69,7 +71,7 @@ public function indexesRecordsFully(): void
[
'title' => 'Test page',
'searchable_meta' => [
'renderedLink' => version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9', '>=') ? '/test-page/' : 'index.php?id=2',
'renderedLink' => version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9', '>=') ? '/test-page/' : 'http://localhost/index.php?id=2',
],
]
);
Expand Down Expand Up @@ -124,7 +126,7 @@ public function indexesRecordTranslations(): void
[
'title' => 'Translated test page',
'searchable_meta' => [
'renderedLink' => version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9', '>=') ? '/da/translated-test-page/' : 'index.php?id=2&L=1',
'renderedLink' => version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9', '>=') ? '/da/translated-test-page/' : 'http://localhost/index.php?id=2&L=1',
],
],
1
Expand Down Expand Up @@ -272,6 +274,66 @@ public function skipsPagesWithNoSearchFromIndexing(): void
$this->assertDocumentInIndex(5);
}

/**
* @test
*/
public function respectsSiteBase(): void
{
if (version_compare(VersionNumberUtility::getCurrentTypo3Version(), '9', '<')) {
$this->markTestSkipped('TYPO3v9+ only');
}

$siteConfiguration = GeneralUtility::makeInstance(
SiteConfiguration::class,
Environment::getConfigPath() . '/sites'
);
$configuration = $siteConfiguration->load('100');
$configuration['base'] = 'https://bar.example.org/';
$siteConfiguration->write('100', $configuration);

$configuration = $siteConfiguration->load('200');
$configuration['base'] = 'https://qux.example.org/';
$siteConfiguration->write('200', $configuration);

$this->getDatabaseConnection()->insertArray('pages', [
'uid' => 101,
'pid' => 100,
'doktype' => PageRepository::DOKTYPE_DEFAULT,
'title' => 'Bar test page',
'slug' => '/bar-test-page/',
]);
$this->getDatabaseConnection()->insertArray('pages', [
'uid' => 201,
'pid' => 200,
'doktype' => PageRepository::DOKTYPE_DEFAULT,
'title' => 'Qux test page',
'slug' => '/qux-test-page/',
]);

$this->assertIndexEmpty();

$this->indexingService->indexFull();

$this->assertDocumentInIndex(
101,
[
'title' => 'Bar test page',
'searchable_meta' => [
'renderedLink' => 'https://bar.example.org/bar-test-page/',
],
]
);
$this->assertDocumentInIndex(
201,
[
'title' => 'Qux test page',
'searchable_meta' => [
'renderedLink' => 'https://qux.example.org/qux-test-page/',
],
]
);
}

/**
* @return void
*/
Expand Down
12 changes: 10 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ parameters:
path: Classes/Middleware/UriBuilder.php
- message: '#TYPO3\\CMS\\Core\\Context\\.+#'
paths:
- Classes/Middleware/UriBuilder.php
- Classes/Service/IndexingService.php
- Classes/Utility/TsfeUtility.php
- Classes/ViewHelpers/SiteLanguageViewHelper.php
- message: '#TYPO3\\CMS\\Core\\Database\\PostProcessQueryHookInterface#'
paths:
- ext_localconf.php
- Classes/Hook/DatabaseConnectionHook.php
- message: '#TYPO3\\CMS\\Core\\Context\\Context#'
path: Classes/Middleware/UriBuilder.php
- message: '#TYPO3\\CMS\\Core\\Http\\JsonResponse#'
path: Classes/Middleware/UriBuilder.php
- message: '#TYPO3\\CMS\\Core\\Routing\\.+#'
paths:
- Classes/Middleware/UriBuilder.php
- Classes/Utility/TsfeUtility.php
- message: '#TYPO3\\CMS\\Core\\Site\\.+#'
paths:
- Classes/Middleware/UriBuilder.php
- Classes/Utility/TsfeUtility.php

reportUnmatchedIgnoredErrors: false