diff --git a/Classes/Indexer/Indexer.php b/Classes/Indexer/Indexer.php index d871a21e..db6e7a85 100644 --- a/Classes/Indexer/Indexer.php +++ b/Classes/Indexer/Indexer.php @@ -275,7 +275,7 @@ protected function addSystemFields($record = []) */ public function run() { - TsfeUtility::createTSFE($this->config['siteIdentifier'] ?? null); + TsfeUtility::createTSFE($this->config['siteIdentifier'] ?? null, $this->language); $bulkSize = ($this->config['bulkSize'] ?? null) ?: 20; diff --git a/Classes/Service/IndexingService.php b/Classes/Service/IndexingService.php index 79ae8eab..de975c60 100644 --- a/Classes/Service/IndexingService.php +++ b/Classes/Service/IndexingService.php @@ -11,10 +11,7 @@ use PAGEmachine\Searchable\PipelineManager; use PAGEmachine\Searchable\Service\ExtconfService; use Psr\EventDispatcher\EventDispatcherInterface; -use TYPO3\CMS\Core\Context\Context; -use TYPO3\CMS\Core\Context\LanguageAspect; use TYPO3\CMS\Core\Log\LogManager; -use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface; use TYPO3\CMS\Extbase\SignalSlot\Dispatcher; @@ -329,19 +326,11 @@ protected function applyEnvironment(int $languageUid, array $environment): \Clos $GLOBALS['BE_USER']->uc['lang'] = $environment['language']; setlocale(LC_ALL, $environment['locale']); - $context = GeneralUtility::makeInstance(Context::class); - $originalLanguageAspect = $context->getAspect('language'); - - $restoreEnvironment = function () use ($originalUserLanguage, $originalLocale, $originalLanguageAspect): void { + $restoreEnvironment = function () use ($originalUserLanguage, $originalLocale): void { $GLOBALS['BE_USER']->uc['lang'] = $originalUserLanguage; setlocale(LC_ALL, $originalLocale); - - $context = GeneralUtility::makeInstance(Context::class); - $context->setAspect('language', $originalLanguageAspect); }; - $context->setAspect('language', new LanguageAspect($languageUid)); - return $restoreEnvironment; } diff --git a/Classes/Utility/TsfeUtility.php b/Classes/Utility/TsfeUtility.php index a34187d2..88fea693 100644 --- a/Classes/Utility/TsfeUtility.php +++ b/Classes/Utility/TsfeUtility.php @@ -2,6 +2,7 @@ namespace PAGEmachine\Searchable\Utility; use TYPO3\CMS\Core\Context\Context; +use TYPO3\CMS\Core\Context\LanguageAspectFactory; use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder; use TYPO3\CMS\Core\Http\ServerRequestFactory; use TYPO3\CMS\Core\Routing\PageArguments; @@ -24,7 +25,7 @@ class TsfeUtility * * @return void */ - public static function createTSFE(string $siteIdentifier = null) + public static function createTSFE(string $siteIdentifier = null, int $languageId = null) { $siteFinder = GeneralUtility::makeInstance(SiteFinder::class); $site = $siteIdentifier ? $siteFinder->getSiteByIdentifier($siteIdentifier) : array_values($siteFinder->getAllSites())[0] ?? null; @@ -33,15 +34,20 @@ public static function createTSFE(string $siteIdentifier = null) throw new \RuntimeException('No site found for TSFE setup', 1610444900); } + $siteLanguage = $languageId !== null ? $site->getLanguageById($languageId) : $site->getDefaultLanguage(); + $requestFactory = GeneralUtility::makeInstance(ServerRequestFactory::class); $request = $requestFactory->createServerRequest('get', 'http://localhost') ->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE) ->withAttribute('site', $site) - ->withAttribute('language', $site->getDefaultLanguage()) + ->withAttribute('language', $siteLanguage) ->withAttribute('routing', new PageArguments($site->getRootPageId(), '0', [])) ->withAttribute('frontend.user', GeneralUtility::makeInstance(FrontendUserAuthentication::class)); $GLOBALS['TYPO3_REQUEST'] = $request; + $context = GeneralUtility::makeInstance(Context::class); + $context->setAspect('language', LanguageAspectFactory::createFromSiteLanguage($siteLanguage)); + $frontendController = GeneralUtility::makeInstance( TypoScriptFrontendController::class, GeneralUtility::makeInstance(Context::class), diff --git a/Tests/Functional/AbstractElasticsearchTest.php b/Tests/Functional/AbstractElasticsearchTest.php index add2d00a..197ab90c 100644 --- a/Tests/Functional/AbstractElasticsearchTest.php +++ b/Tests/Functional/AbstractElasticsearchTest.php @@ -161,6 +161,14 @@ protected function setUp(): void 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'Foo Root', ]); + $this->getDatabaseConnection()->insertArray('pages', [ + 'uid' => 2, + 'pid' => 1, + 'sys_language_uid' => 1, + 'l10n_parent' => 1, + 'doktype' => PageRepository::DOKTYPE_DEFAULT, + 'title' => 'Dansk Foo Root', + ]); $this->getDatabaseConnection()->insertArray('pages', [ 'uid' => 100, 'doktype' => PageRepository::DOKTYPE_DEFAULT, diff --git a/Tests/Functional/Query/SearchQueryTest.php b/Tests/Functional/Query/SearchQueryTest.php index 3bbb9082..e103063b 100644 --- a/Tests/Functional/Query/SearchQueryTest.php +++ b/Tests/Functional/Query/SearchQueryTest.php @@ -19,19 +19,19 @@ final class SearchQueryTest extends AbstractElasticsearchTest public function searchesByTerm(): void { $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 2, + 'uid' => 3, 'pid' => 1, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'Test page', ]); $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 3, + 'uid' => 4, 'pid' => 1, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'Another test page', ]); $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 4, + 'uid' => 5, 'pid' => 1, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'Unrelated page', @@ -46,7 +46,7 @@ public function searchesByTerm(): void $result = $query->execute(); $this->assertEquals(2, $result['hits']['total']); - $this->assertEquals('Test page', $result['hits']['hits'][0]['_source']['title']); - $this->assertEquals('Another test page', $result['hits']['hits'][1]['_source']['title']); + $this->assertEquals('Test page', $result['hits']['hits'][1]['_source']['title']); + $this->assertEquals('Another test page', $result['hits']['hits'][0]['_source']['title']); } } diff --git a/Tests/Functional/Service/IndexingServiceTest.php b/Tests/Functional/Service/IndexingServiceTest.php index c1e21a93..0079e835 100644 --- a/Tests/Functional/Service/IndexingServiceTest.php +++ b/Tests/Functional/Service/IndexingServiceTest.php @@ -51,7 +51,7 @@ final class IndexingServiceTest extends AbstractElasticsearchTest public function indexesRecordsFully(): void { $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 2, + 'uid' => 3, 'pid' => 1, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'Test page', @@ -63,7 +63,7 @@ public function indexesRecordsFully(): void $this->indexingService->indexFull(); $this->assertDocumentInIndex( - 2, + 3, [ 'title' => 'Test page', 'searchable_meta' => [ @@ -79,17 +79,17 @@ public function indexesRecordsFully(): void public function indexesRecordTranslations(): void { $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 2, + 'uid' => 3, 'pid' => 1, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'Test page', 'slug' => '/test-page/', ]); $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 3, + 'uid' => 4, 'pid' => 1, 'sys_language_uid' => 1, - 'l10n_parent' => 2, + 'l10n_parent' => 3, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'Translated test page', 'slug' => '/translated-test-page/', @@ -101,7 +101,7 @@ public function indexesRecordTranslations(): void $this->indexingService->indexFull(); $this->assertDocumentInIndex( - 2, + 3, [ 'title' => 'Translated test page', 'searchable_meta' => [ @@ -164,7 +164,7 @@ public function appliesLanguageForRecordTranslationIndexing(): void public function indexesRecordsPartially(): void { $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 2, + 'uid' => 3, 'pid' => 1, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'Test page', @@ -174,7 +174,7 @@ public function indexesRecordsPartially(): void $this->indexingService->indexFull(); $this->assertDocumentInIndex( - 2, + 3, [ 'title' => 'Test page', ] @@ -188,7 +188,7 @@ public function indexesRecordsPartially(): void 'title' => 'Updated test page', ], [ - 'uid' => 2, + 'uid' => 3, ] ); @@ -197,7 +197,7 @@ public function indexesRecordsPartially(): void $this->indexingService->indexPartial(); $this->assertDocumentInIndex( - 2, + 3, [ 'title' => 'Updated test page', ] @@ -210,28 +210,28 @@ public function indexesRecordsPartially(): void public function skipsPagesWithNoSearchFromIndexing(): void { $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 2, + 'uid' => 3, 'pid' => 1, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'First page to exclude', 'no_search' => 1, ]); $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 3, - 'pid' => 2, + 'uid' => 4, + 'pid' => 3, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'First regular page', ]); $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 4, - 'pid' => 3, + 'uid' => 5, + 'pid' => 4, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'Second page to exclude', 'no_search' => 1, ]); $this->getDatabaseConnection()->insertArray('pages', [ - 'uid' => 5, - 'pid' => 4, + 'uid' => 6, + 'pid' => 5, 'doktype' => PageRepository::DOKTYPE_DEFAULT, 'title' => 'Second regular page', ]); @@ -240,10 +240,10 @@ public function skipsPagesWithNoSearchFromIndexing(): void $this->indexingService->indexFull(); - $this->assertDocumentNotInIndex(2); - $this->assertDocumentInIndex(3); - $this->assertDocumentNotInIndex(4); - $this->assertDocumentInIndex(5); + $this->assertDocumentNotInIndex(3); + $this->assertDocumentInIndex(4); + $this->assertDocumentNotInIndex(5); + $this->assertDocumentInIndex(6); } /**