Skip to content

Commit

Permalink
[TASK] Adapt Unit Tests for TYPO3 v13
Browse files Browse the repository at this point in the history
  • Loading branch information
bmack authored and dkd-kaehm committed Jun 6, 2024
1 parent 6d0f57a commit c9958ea
Show file tree
Hide file tree
Showing 19 changed files with 179 additions and 115 deletions.
1 change: 0 additions & 1 deletion Classes/Domain/Index/Queue/QueueItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
use ApacheSolrForTypo3\Solr\System\Records\AbstractRepository;
use ApacheSolrForTypo3\Solr\System\Util\SiteUtility;
use Doctrine\DBAL\Exception as DBALException;
use PDO;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

use ApacheSolrForTypo3\Solr\System\Records\AbstractRepository;
use Doctrine\DBAL\Exception as DBALException;
use PDO;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnsupportedMethodException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use ApacheSolrForTypo3\Solr\Domain\Index\Queue\GarbageRemover\StrategyFactory;
use ApacheSolrForTypo3\Solr\Domain\Site\Exception\UnexpectedTYPO3SiteInitializationException;
use Doctrine\DBAL\Exception as DBALException;
use PDO;
use Throwable;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
Expand Down
22 changes: 14 additions & 8 deletions Classes/Domain/Search/ApacheSolrDocument/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
use ApacheSolrForTypo3\Solr\Typo3PageContentExtractor;
use ApacheSolrForTypo3\Solr\Util;
use Doctrine\DBAL\Exception as DBALException;
use TYPO3\CMS\Core\Routing\PageArguments;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
use TYPO3\CMS\Frontend\Page\PageInformation;

/**
* Builder class to build an ApacheSolrDocument
Expand All @@ -43,19 +46,22 @@ public function __construct(
* This method can be used to build a Document from a TYPO3 page.
*/
public function fromPage(
TypoScriptFrontendController $page,
PageInformation $pageInformation,
PageArguments $pageArguments,
SiteLanguage $siteLanguage,
TypoScriptFrontendController $tsfe,
string $url,
Rootline $pageAccessRootline,
string $mountPointParameter = '',
): Document {
$pageId = $page->id;
$pageRecord = $page->page;
$pageId = $pageInformation->getId();
$pageRecord = $pageInformation->getPageRecord();

$document = GeneralUtility::makeInstance(Document::class);
$site = $this->getSiteByPageId($pageId);

$accessGroups = $this->getDocumentIdGroups($pageAccessRootline);
$documentId = $this->getPageDocumentId($page, $accessGroups, $mountPointParameter);
$documentId = $this->getPageDocumentId($pageInformation, $pageArguments, $siteLanguage, $accessGroups, $mountPointParameter);

$document->setField('id', $documentId);
$document->setField('site', $site->getDomain());
Expand All @@ -71,7 +77,7 @@ public function fromPage(
$variantId = $this->variantIdBuilder->buildFromTypeAndUid('pages', $pageId, $pageRecord, $site, $document);
$document->setField('variantId', $variantId);

$document->setField('typeNum', (int)$page->getPageArguments()->getPageType());
$document->setField('typeNum', (int)$pageArguments->getPageType());
$document->setField('created', $pageRecord['crdate']);
$document->setField('changed', $pageRecord['SYS_LASTCHANGED']);

Expand All @@ -84,7 +90,7 @@ public function fromPage(

// content
// @extensionScannerIgnoreLine
$contentExtractor = $this->getExtractorForPageContent($page->content);
$contentExtractor = $this->getExtractorForPageContent($tsfe->content);
$document->setField('title', $contentExtractor->getPageTitle());
$document->setField('subTitle', $pageRecord['subtitle']);
$document->setField('navTitle', $pageRecord['nav_title']);
Expand Down Expand Up @@ -153,9 +159,9 @@ public function fromRecord(array $itemRecord, string $type, int $rootPageUid, st
/**
* @throws DBALException
*/
protected function getPageDocumentId(TypoScriptFrontendController $frontendController, string $accessGroups, string $mountPointParameter): string
protected function getPageDocumentId(PageInformation $pageInformation, PageArguments $pageArguments, SiteLanguage $siteLanguage, string $accessGroups, string $mountPointParameter): string
{
return Util::getPageDocumentId($frontendController->id, (int)$frontendController->getPageArguments()->getPageType(), $frontendController->getLanguage()->getLanguageId(), $accessGroups, $mountPointParameter);
return Util::getPageDocumentId($pageInformation->getId(), (int)$pageArguments->getPageType(), $siteLanguage->getLanguageId(), $accessGroups, $mountPointParameter);
}

/**
Expand Down
24 changes: 10 additions & 14 deletions Classes/Domain/Search/FrequentSearches/FrequentSearchesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
use ApacheSolrForTypo3\Solr\Domain\Search\Statistics\StatisticsRepository;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use Doctrine\DBAL\Exception as DBALException;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* The FrequentSearchesService is used to retrieve the frequent searches from the database or cache.
Expand All @@ -36,21 +36,17 @@ class FrequentSearchesService
*/
protected ?AbstractFrontend $cache;

protected ?TypoScriptFrontendController $tsfe;

protected ?StatisticsRepository $statisticsRepository;

protected TypoScriptConfiguration $configuration;

public function __construct(
TypoScriptConfiguration $typoscriptConfiguration,
AbstractFrontend $cache = null,
TypoScriptFrontendController $tsfe = null,
StatisticsRepository $statisticsRepository = null
) {
$this->configuration = $typoscriptConfiguration;
$this->cache = $cache;
$this->tsfe = $tsfe;
$this->statisticsRepository = $statisticsRepository ?? GeneralUtility::makeInstance(StatisticsRepository::class);
}

Expand All @@ -60,16 +56,16 @@ public function __construct(
* @throws AspectNotFoundException
* @throws DBALException
*/
public function getFrequentSearchTerms(): array
public function getFrequentSearchTerms(ServerRequestInterface $typo3Request): array
{
$frequentSearchConfiguration = $this->configuration->getSearchFrequentSearchesConfiguration();

$identifier = $this->getCacheIdentifier($frequentSearchConfiguration);
$identifier = $this->getCacheIdentifier($typo3Request, $frequentSearchConfiguration);

if ($this->hasValidCache() && $this->cache->has($identifier)) {
$terms = $this->cache->get($identifier);
} else {
$terms = $this->getFrequentSearchTermsFromStatistics($frequentSearchConfiguration);
$terms = $this->getFrequentSearchTermsFromStatistics($typo3Request, $frequentSearchConfiguration);

if (isset($frequentSearchConfiguration['sortBy']) && $frequentSearchConfiguration['sortBy'] === 'hits') {
arsort($terms);
Expand All @@ -95,17 +91,17 @@ public function getFrequentSearchTerms(): array
*
* @throws DBALException
*/
protected function getFrequentSearchTermsFromStatistics(array $frequentSearchConfiguration = []): array
protected function getFrequentSearchTermsFromStatistics(ServerRequestInterface $serverRequest, array $frequentSearchConfiguration): array
{
$terms = [];

if ($frequentSearchConfiguration['select.']['checkRootPageId']) {
$checkRootPidWhere = 'root_pid = ' . $this->tsfe->tmpl->rootLine[0]['uid'];
$checkRootPidWhere = 'root_pid = ' . $serverRequest->getAttribute('frontend.page.information')?->getLocalRootLine()[0]['uid'];
} else {
$checkRootPidWhere = '1';
}
if ($frequentSearchConfiguration['select.']['checkLanguage']) {
$checkLanguageWhere = ' AND language =' . $this->tsfe->getLanguage()->getLanguageId();
$checkLanguageWhere = ' AND language =' . $serverRequest->getAttribute('language')?->getLanguageId();
} else {
$checkLanguageWhere = '';
}
Expand All @@ -128,16 +124,16 @@ protected function getFrequentSearchTermsFromStatistics(array $frequentSearchCon
/**
* Returns cache identifier for given $frequentSearchConfiguration
*/
protected function getCacheIdentifier(array $frequentSearchConfiguration): string
protected function getCacheIdentifier(ServerRequestInterface $serverRequest, array $frequentSearchConfiguration): string
{
// Use configuration as cache identifier
$identifier = 'frequentSearchesTags';

if (isset($frequentSearchConfiguration['select.']['checkRootPageId']) && $frequentSearchConfiguration['select.']['checkRootPageId']) {
$identifier .= '_RP' . (int)$this->tsfe->tmpl->rootLine[0]['uid'];
$identifier .= '_RP' . (int)$serverRequest->getAttribute('frontend.page.information')?->getLocalRootLine()[0]['uid'];
}
if (isset($frequentSearchConfiguration['select.']['checkLanguage']) && $frequentSearchConfiguration['select.']['checkLanguage']) {
$identifier .= '_L' . $this->tsfe->getLanguage()->getLanguageId();
$identifier .= '_L' . $serverRequest->getAttribute('language')?->getLanguageId();
}

$identifier .= '_' . md5(serialize($frequentSearchConfiguration));
Expand Down
2 changes: 1 addition & 1 deletion Classes/Domain/Search/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public function useFiltersFromTypoScript(): QueryBuilder
$searchQueryFilters['__pageSections.']
);
}
$pageIds = GeneralUtility::trimExplode(',', $searchQueryFilters['__pageSections']);
$pageIds = GeneralUtility::trimExplode(',', (string)$searchQueryFilters['__pageSections']);
$this->usePageSectionsFromPageIds($pageIds);
$this->typoScriptConfiguration->removeSearchQueryFilterForPageSections();
}
Expand Down
19 changes: 11 additions & 8 deletions Classes/Domain/Search/Statistics/StatisticsWriterProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
use ApacheSolrForTypo3\Solr\HtmlContentExtractor;
use Doctrine\DBAL\Exception as DBALException;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\IpAnonymizationUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;

/**
* Writes statistics after searches have been conducted.
Expand Down Expand Up @@ -66,13 +66,16 @@ public function process(SearchResultSet $resultSet): SearchResultSet
$page = (int)$searchRequest->getPage();
$ipMaskLength = $configuration->getStatisticsAnonymizeIP();

$TSFE = $this->getTSFE();
$root_pid = $this->siteRepository->getSiteByPageId($TSFE->id)->getRootPageId();
$serverRequest = $this->getServerRequest();
$pageId = $serverRequest->getAttribute('routing')->getPageId();
$siteLanguage = $serverRequest->getAttribute('language');
$frontendUser = $serverRequest->getAttribute('frontend.user');
$root_pid = $this->siteRepository->getSiteByPageId($pageId)->getRootPageId();
$statisticData = [
'pid' => $TSFE->id,
'pid' => $pageId,
'root_pid' => $root_pid,
'tstamp' => $this->getTime(),
'language' => $TSFE->getLanguage()->getLanguageId(),
'language' => $siteLanguage->getLanguageId(),
// @extensionScannerIgnoreLine
'num_found' => $resultSet->getAllResultCount(),
'suggestions_shown' => is_object($response->spellcheck->suggestions ?? null) ? (int)get_object_vars($response->spellcheck->suggestions) : 0,
Expand All @@ -83,7 +86,7 @@ public function process(SearchResultSet $resultSet): SearchResultSet
// @extensionScannerIgnoreLine
'time_processing' => $response->debug->timing->process->time ?? 0,
/** @phpstan-ignore-next-line */
'feuser_id' => isset($TSFE->fe_user->user) ? (int)$TSFE->fe_user->user['uid'] ?? 0 : 0,
'feuser_id' => isset($frontendUser?->user) ? (int)$frontendUser->user['uid'] ?? 0 : 0,
'ip' => IpAnonymizationUtility::anonymizeIp($this->getUserIp(), $ipMaskLength),
'page' => $page,
'keywords' => $keywords,
Expand Down Expand Up @@ -123,9 +126,9 @@ protected function sanitizeString(string $string): string
return trim($string);
}

protected function getTSFE(): ?TypoScriptFrontendController
protected function getServerRequest(): ?ServerRequestInterface
{
return $GLOBALS['TSFE'];
return $GLOBALS['TYPO3_REQUEST'] ?? null;
}

protected function getUserIp(): string
Expand Down
15 changes: 7 additions & 8 deletions Classes/Domain/Search/Suggest/SuggestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use ApacheSolrForTypo3\Solr\Util;
use Doctrine\DBAL\Exception as DBALException;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Context\Exception\AspectNotFoundException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
Expand All @@ -52,12 +53,10 @@ class SuggestService
protected QueryBuilder $queryBuilder;

public function __construct(
TypoScriptFrontendController $tsfe,
SearchResultSetService $searchResultSetService,
TypoScriptConfiguration $typoScriptConfiguration,
QueryBuilder $queryBuilder = null
) {
$this->tsfe = $tsfe;
$this->searchService = $searchResultSetService;
$this->typoScriptConfiguration = $typoScriptConfiguration;
$this->queryBuilder = $queryBuilder ?? GeneralUtility::makeInstance(
Expand All @@ -74,13 +73,13 @@ public function __construct(
* @throws NoSolrConnectionFoundException
* @throws DBALException
*/
public function getSuggestions(SearchRequest $searchRequest, array $additionalFilters = []): array
public function getSuggestions(ServerRequestInterface $serverRequest, SearchRequest $searchRequest, array $additionalFilters = []): array
{
$requestId = $this->tsfe->getRequestedId();
$requestId = (int)$serverRequest->getAttribute('routing')?->getPageId();
$frontendUserGroupIds = Util::getFrontendUserGroups();

$suggestQuery = $this->queryBuilder->buildSuggestQuery($searchRequest->getRawUserQuery(), $additionalFilters, $requestId, $frontendUserGroupIds);
$solrSuggestions = $this->getSolrSuggestions($suggestQuery, $searchRequest);
$solrSuggestions = $this->getSolrSuggestions($serverRequest, $suggestQuery, $searchRequest);

if ($solrSuggestions === []) {
return ['status' => false];
Expand Down Expand Up @@ -145,10 +144,10 @@ protected function addTopResultsToSuggestions(SearchRequest $searchRequest, arra
* @throws NoSolrConnectionFoundException
* @throws DBALException
*/
protected function getSolrSuggestions(SuggestQuery $suggestQuery, SearchRequest $searchRequest): array
protected function getSolrSuggestions(ServerRequestInterface $request, SuggestQuery $suggestQuery, SearchRequest $searchRequest): array
{
$pageId = $this->tsfe->getRequestedId();
$languageId = $this->tsfe->getLanguage()->getLanguageId();
$pageId = (int)$request->getAttribute('routing')?->getPageId();
$languageId = (int)$request->getAttribute('language')->getLanguageId();
$solr = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId($pageId, $languageId);
$search = GeneralUtility::makeInstance(Search::class, $solr);

Expand Down
3 changes: 2 additions & 1 deletion Classes/Domain/Variants/IdBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ApacheSolrForTypo3\Solr\Exception\InvalidArgumentException;
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Core\Crypto\HashService;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
Expand Down Expand Up @@ -61,6 +62,6 @@ protected function getSystemHash(): string

$siteName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'];
$systemKey = 'tx_solr' . $siteName;
return GeneralUtility::hmac($systemKey);
return GeneralUtility::makeInstance(HashService::class)->hmac($systemKey, $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] ?? '');
}
}
Loading

0 comments on commit c9958ea

Please sign in to comment.