Skip to content

Commit

Permalink
[WIP] ...
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrodala committed Feb 29, 2024
1 parent b1a238f commit 3db6c51
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Classes/Indexer/Indexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 1 addition & 12 deletions Classes/Service/IndexingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
10 changes: 8 additions & 2 deletions Classes/Utility/TsfeUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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),
Expand Down
8 changes: 8 additions & 0 deletions Tests/Functional/AbstractElasticsearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions Tests/Functional/Query/SearchQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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']);
}
}
42 changes: 21 additions & 21 deletions Tests/Functional/Service/IndexingServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -63,7 +63,7 @@ public function indexesRecordsFully(): void
$this->indexingService->indexFull();

$this->assertDocumentInIndex(
2,
3,
[
'title' => 'Test page',
'searchable_meta' => [
Expand All @@ -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/',
Expand All @@ -101,7 +101,7 @@ public function indexesRecordTranslations(): void
$this->indexingService->indexFull();

$this->assertDocumentInIndex(
2,
3,
[
'title' => 'Translated test page',
'searchable_meta' => [
Expand Down Expand Up @@ -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',
Expand All @@ -174,7 +174,7 @@ public function indexesRecordsPartially(): void
$this->indexingService->indexFull();

$this->assertDocumentInIndex(
2,
3,
[
'title' => 'Test page',
]
Expand All @@ -188,7 +188,7 @@ public function indexesRecordsPartially(): void
'title' => 'Updated test page',
],
[
'uid' => 2,
'uid' => 3,
]
);

Expand All @@ -197,7 +197,7 @@ public function indexesRecordsPartially(): void
$this->indexingService->indexPartial();

$this->assertDocumentInIndex(
2,
3,
[
'title' => 'Updated test page',
]
Expand All @@ -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',
]);
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit 3db6c51

Please sign in to comment.