From 3d3aae9e34f7116ba4480b5c069e0fbfaaddee31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20K=C3=A4hm?= Date: Fri, 3 Mar 2023 17:55:45 +0100 Subject: [PATCH] [TASK] Fix Ingration tests: ResultSetReconstitutionProcessorTest and ApacheSolrDocumentRepositoryTest Relates: #3376 --- .../Search/ApacheSolrDocument/Repository.php | 21 +++++++----- Classes/Routing/RoutingService.php | 6 ++-- .../ApacheSolrDocumentRepositoryTest.php | 29 ++++++++++++++-- .../ResultSetReconstitutionProcessorTest.php | 8 +++-- Tests/Integration/IntegrationTest.php | 13 ++++++++ Tests/Integration/TSFETestBootstrapper.php | 33 +++++++++++++++++-- 6 files changed, 90 insertions(+), 20 deletions(-) diff --git a/Classes/Domain/Search/ApacheSolrDocument/Repository.php b/Classes/Domain/Search/ApacheSolrDocument/Repository.php index 9a56717e16..e13b09fa8f 100644 --- a/Classes/Domain/Search/ApacheSolrDocument/Repository.php +++ b/Classes/Domain/Search/ApacheSolrDocument/Repository.php @@ -25,7 +25,7 @@ use ApacheSolrForTypo3\Solr\System\Solr\SolrCommunicationException; use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection; use ApacheSolrForTypo3\Solr\Util; -use Exception; +use Doctrine\DBAL\Driver\Exception as DBALDriverException; use TYPO3\CMS\Core\SingletonInterface; use TYPO3\CMS\Core\Utility\GeneralUtility; @@ -79,9 +79,10 @@ public function __construct( * @param $pageId * @param $languageId * @return Document|false - * @throws Exception + * + * @throws DBALDriverException */ - public function findOneByPageIdAndByLanguageId($pageId, $languageId) + public function findOneByPageIdAndByLanguageId($pageId, $languageId): Document|false { $documentCollection = $this->findByPageIdAndByLanguageId($pageId, $languageId); return reset($documentCollection); @@ -94,7 +95,8 @@ public function findOneByPageIdAndByLanguageId($pageId, $languageId) * @param int $pageId * @param int $languageId * @return Document[] - * @throws Exception + * + * @throws DBALDriverException */ public function findByPageIdAndByLanguageId(int $pageId, int $languageId): array { @@ -102,7 +104,7 @@ public function findByPageIdAndByLanguageId(int $pageId, int $languageId): array $this->initializeSearch($pageId, $languageId); $pageQuery = $this->queryBuilder->buildPageQuery($pageId); $response = $this->search->search($pageQuery, 0, 10000); - } catch (NoSolrConnectionFoundException|SolrCommunicationException $exception) { + } catch (NoSolrConnectionFoundException|SolrCommunicationException) { return []; } $data = $response->getParsedData(); @@ -115,8 +117,9 @@ public function findByPageIdAndByLanguageId(int $pageId, int $languageId): array * @param int $uid * @param int $pageId * @param int $languageId - * @return Document[]|array - * @throws Exception + * @return Document[] + * + * @throws DBALDriverException */ public function findByTypeAndPidAndUidAndLanguageId( string $type, @@ -128,7 +131,7 @@ public function findByTypeAndPidAndUidAndLanguageId( $this->initializeSearch($pageId, $languageId); $recordQuery = $this->queryBuilder->buildRecordQuery($type, $uid, $pageId); $response = $this->search->search($recordQuery, 0, 10000); - } catch (NoSolrConnectionFoundException|SolrCommunicationException $exception) { + } catch (NoSolrConnectionFoundException|SolrCommunicationException) { return []; } $data = $response->getParsedData(); @@ -141,6 +144,8 @@ public function findByTypeAndPidAndUidAndLanguageId( * * @param int $pageId * @param int $languageId + * + * @throws DBALDriverException * @throws NoSolrConnectionFoundException */ protected function initializeSearch(int $pageId, int $languageId = 0) diff --git a/Classes/Routing/RoutingService.php b/Classes/Routing/RoutingService.php index 8947978de5..da91244721 100644 --- a/Classes/Routing/RoutingService.php +++ b/Classes/Routing/RoutingService.php @@ -1147,15 +1147,15 @@ protected function processUriPathArgument( */ public function getSiteMatcher(): SiteMatcher { - return GeneralUtility::makeInstance(SiteMatcher::class, $this->getSiteFinder()); + return GeneralUtility::makeInstance(SiteMatcher::class); } /** * Returns the site finder * - * @return SiteFinder|null + * @return SiteFinder */ - protected function getSiteFinder(): ?SiteFinder + protected function getSiteFinder(): SiteFinder { return GeneralUtility::makeInstance(SiteFinder::class); } diff --git a/Tests/Integration/Domain/Search/ApacheSolrDocument/ApacheSolrDocumentRepositoryTest.php b/Tests/Integration/Domain/Search/ApacheSolrDocument/ApacheSolrDocumentRepositoryTest.php index 5bdc9d84f4..03a3746926 100644 --- a/Tests/Integration/Domain/Search/ApacheSolrDocument/ApacheSolrDocumentRepositoryTest.php +++ b/Tests/Integration/Domain/Search/ApacheSolrDocument/ApacheSolrDocumentRepositoryTest.php @@ -18,7 +18,15 @@ use ApacheSolrForTypo3\Solr\Domain\Search\ApacheSolrDocument\Repository; use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; use ApacheSolrForTypo3\Solr\Tests\Integration\IntegrationTest; +use Doctrine\DBAL\Driver\Exception as DBALDriverException; +use Doctrine\DBAL\Exception as DBALException; +use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException; +use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException; +use TYPO3\CMS\Core\Error\Http\InternalServerErrorException; +use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException; +use TYPO3\CMS\Core\Exception\SiteNotFoundException; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\TestingFramework\Core\Exception as TestingFrameworkCoreException; class ApacheSolrDocumentRepositoryTest extends IntegrationTest { @@ -29,10 +37,20 @@ class ApacheSolrDocumentRepositoryTest extends IntegrationTest protected bool $skipImportRootPagesAndTemplatesForConfiguredSites = true; /** - * @var Repository + * @var Repository|null */ - protected $apacheSolrDocumentRepository; + protected ?Repository $apacheSolrDocumentRepository = null; + /** + * @throws AspectNotFoundException + * @throws DBALDriverException + * @throws DBALException + * @throws InternalServerErrorException + * @throws NoSuchCacheException + * @throws ServiceUnavailableException + * @throws SiteNotFoundException + * @throws TestingFrameworkCoreException + */ protected function setUp(): void { parent::setUp(); @@ -50,16 +68,19 @@ protected function setUp(): void } /** - * Executed after each test. Emptys solr and checks if the index is empty + * Executed after each test. Empties solr and checks if the index is empty */ protected function tearDown(): void { $this->cleanUpSolrServerAndAssertEmpty(); + unset($this->apacheSolrDocumentRepository); parent::tearDown(); } /** * @test + * + * @throws DBALDriverException */ public function canFindByPageIdAndByLanguageId() { @@ -72,6 +93,8 @@ public function canFindByPageIdAndByLanguageId() /** * @test + * + * @throws DBALDriverException */ public function canReturnEmptyCollectionIfNoConnectionToSolrServerIsEstablished() { diff --git a/Tests/Integration/Domain/Search/ResultSet/ResultSetReconstitutionProcessorTest.php b/Tests/Integration/Domain/Search/ResultSet/ResultSetReconstitutionProcessorTest.php index f0341543de..21fdde36e0 100644 --- a/Tests/Integration/Domain/Search/ResultSet/ResultSetReconstitutionProcessorTest.php +++ b/Tests/Integration/Domain/Search/ResultSet/ResultSetReconstitutionProcessorTest.php @@ -15,6 +15,7 @@ namespace ApacheSolrForTypo3\Solr\Tests\Integration\Domain\Search\ResultSet; +use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\InvalidFacetPackageException; use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options\Option; use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Facets\OptionBased\Options\OptionsFacet; use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\ResultSetReconstitutionProcessor; @@ -39,10 +40,11 @@ class ResultSetReconstitutionProcessorTest extends IntegrationTest /** * @test * - * @throws TestingFrameworkCoreException * @throws InternalServerErrorException * @throws ServiceUnavailableException * @throws SiteNotFoundException + * @throws TestingFrameworkCoreException + * @throws InvalidFacetPackageException */ public function canApplyRenderingInstructionsOnOptions() { @@ -95,6 +97,7 @@ public function canApplyRenderingInstructionsOnOptions() * @test * * @throws InternalServerErrorException + * @throws InvalidFacetPackageException * @throws ServiceUnavailableException * @throws SiteNotFoundException * @throws TestingFrameworkCoreException @@ -174,7 +177,6 @@ protected function getConfiguredReconstitutionProcessor(array $configuration, Se $usedSearchRequestMock->expects(self::any())->method('getContextTypoScriptConfiguration')->willReturn($typoScriptConfiguration); $usedSearchRequestMock->expects(self::any())->method('getActiveFacetNames')->willReturn([]); - $processor = new ResultSetReconstitutionProcessor(); - return $processor; + return new ResultSetReconstitutionProcessor(); } } diff --git a/Tests/Integration/IntegrationTest.php b/Tests/Integration/IntegrationTest.php index 6ee6aa31c4..e9eebe84c9 100644 --- a/Tests/Integration/IntegrationTest.php +++ b/Tests/Integration/IntegrationTest.php @@ -22,6 +22,7 @@ use Doctrine\DBAL\Exception as DoctrineDBALException; use Doctrine\DBAL\Schema\SchemaException; use InvalidArgumentException; +use PHPUnit\Framework\MockObject\MockObject; use ReflectionClass; use ReflectionException; use ReflectionObject; @@ -41,6 +42,7 @@ use TYPO3\CMS\Core\Error\Http\InternalServerErrorException; use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException; use TYPO3\CMS\Core\Exception\SiteNotFoundException; +use TYPO3\CMS\Core\Http\NormalizedParams; use TYPO3\CMS\Core\Http\ServerRequest; use TYPO3\CMS\Core\Information\Typo3Version; use TYPO3\CMS\Core\Localization\LanguageService; @@ -396,7 +398,18 @@ protected function fakeTSFE(int $pageId, array $feUserGroupArray = [0]): TypoScr $GLOBALS['TSFE'] = $fakeTSFE; $this->simulateFrontedUserGroups($feUserGroupArray); + /* @var MockObject|ServerRequest $request */ $request = $GLOBALS['TYPO3_REQUEST']; + $request = $GLOBALS['TYPO3_REQUEST'] = + $request + ->withAttribute( + 'frontend.controller', + $fakeTSFE + )->withAttribute( + 'normalizedParams', + NormalizedParams::createFromRequest($request) + ); + $requestHandler = GeneralUtility::makeInstance(RequestHandler::class); $requestHandler->handle($request); diff --git a/Tests/Integration/TSFETestBootstrapper.php b/Tests/Integration/TSFETestBootstrapper.php index 2402602a14..a4bdd0c3a2 100644 --- a/Tests/Integration/TSFETestBootstrapper.php +++ b/Tests/Integration/TSFETestBootstrapper.php @@ -2,8 +2,13 @@ namespace ApacheSolrForTypo3\Solr\Tests\Integration; +use DateTimeImmutable; +use Exception; use TYPO3\CMS\Core\Context\Context; +use TYPO3\CMS\Core\Context\DateTimeAspect; +use TYPO3\CMS\Core\Context\VisibilityAspect; use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder; +use TYPO3\CMS\Core\Error\Http\AbstractServerErrorException; use TYPO3\CMS\Core\Error\Http\InternalServerErrorException; use TYPO3\CMS\Core\Error\Http\ServiceUnavailableException; use TYPO3\CMS\Core\Exception\SiteNotFoundException; @@ -15,6 +20,7 @@ use TYPO3\CMS\Core\Site\SiteFinder; use TYPO3\CMS\Core\TypoScript\TemplateService; use TYPO3\CMS\Core\Utility\GeneralUtility; +use TYPO3\CMS\Frontend\Aspect\PreviewAspect; use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController; @@ -28,9 +34,12 @@ class TSFETestBootstrapper * @param string $MP * @param int $language * @return TSFEBootstrapResult + * * @throws SiteNotFoundException * @throws InternalServerErrorException * @throws ServiceUnavailableException + * @throws AbstractServerErrorException + * @throws Exception */ public function bootstrap(int $pageId, string $MP = '', int $language = 0): TSFEBootstrapResult { @@ -41,7 +50,7 @@ public function bootstrap(int $pageId, string $MP = '', int $language = 0): TSFE $siteFinder = GeneralUtility::makeInstance(SiteFinder::class); $request = GeneralUtility::makeInstance(ServerRequest::class); - $pageArguments = GeneralUtility::makeInstance(PageArguments::class, $pageId, 0, ['MP' => $MP]); + $pageArguments = GeneralUtility::makeInstance(PageArguments::class, $pageId, '0', ['MP' => $MP]); if ($MP !== '') { [$origPageId, $pageIdToUse] = GeneralUtility::trimExplode('-', $MP); $site = $siteFinder->getSiteByPageId($pageIdToUse); @@ -68,14 +77,32 @@ public function bootstrap(int $pageId, string $MP = '', int $language = 0): TSFE $TSFE = GeneralUtility::makeInstance(TypoScriptFrontendController::class, $context, $site, $siteLanguage, $pageArguments, $feUser); $TSFE->set_no_cache('', true); $GLOBALS['TSFE'] = $TSFE; - $TSFE->clear_preview(); + + $GLOBALS['SIM_EXEC_TIME'] = $GLOBALS['EXEC_TIME']; + $GLOBALS['SIM_ACCESS_TIME'] = $GLOBALS['ACCESS_TIME']; + $context->setAspect( + 'frontend.preview', + GeneralUtility::makeInstance(PreviewAspect::class) + ); + $context->setAspect( + 'date', + GeneralUtility::makeInstance( + DateTimeAspect::class, + new DateTimeImmutable('@' . $GLOBALS['SIM_EXEC_TIME']) + ) + ); + $context->setAspect( + 'visibility', + GeneralUtility::makeInstance(VisibilityAspect::class) + ); $template = GeneralUtility::makeInstance(TemplateService::class); $GLOBALS['TSFE']->tmpl = $template; try { $GLOBALS['TSFE']->determineId($GLOBALS['TYPO3_REQUEST']); - $GLOBALS['TSFE']->getConfigArray(); + $GLOBALS['TYPO3_REQUEST'] = $GLOBALS['TSFE']->getFromCache($request); + $GLOBALS['TSFE']->releaseLocks(); } catch (ImmediateResponseException $e) { $result->addExceptions($e); }