Skip to content

Commit

Permalink
[TASK] Clean up various test scenarios (#3637)
Browse files Browse the repository at this point in the history
* [TASK] Clean up various test scenarios

This change removes unused imports,
and additional magic within the main
"IntegrationTest" to make use of TYPO3's
Testing Framework API as much as possible.

The last tests need proper changes
within the actual code base to
remove direct dependencies to TSFE.

Other parts have been moved to the
respective test classes in order to
reduce legacy code functionality.

Relates: #3376
  • Loading branch information
bmack authored May 16, 2023
1 parent a90a453 commit 689ad06
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 539 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function __invoke(ModifyResolvedFrontendGroupsEvent $event): void
$groupData[] = [
'title' => 'group_(' . $groupUid . ')',
'uid' => $groupUid,
'pig' => 0,
'pid' => 0,
];
}
$event->getUser()->user[$event->getUser()->username_column] = AuthorizationService::SOLR_INDEXER_USERNAME;
Expand Down
2 changes: 0 additions & 2 deletions Classes/IndexQueue/FrontendHelper/PageIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ protected function registerAuthorizationService(): void

'os' => '',
'exec' => '',

'classFile' => ExtensionManagementUtility::extPath('solr') . 'Classes/IndexQueue/FrontendHelper/AuthorizationService.php',
'className' => AuthorizationService::class,
]
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"pages",
,"uid","pid","is_siteroot","doktype","slug","title","fe_group","module"
,2,1,0,1,"/public","Public page",,""
,3,1,0,1,"/restricted","Private page",1,""
,3,1,0,1,"/restricted","Private page","1",""
,4,1,0,254,"/folder","FE Users",,"fe_users"
"tt_content",
,"uid","pid","colPos","CType","bodytext"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use ApacheSolrForTypo3\Solr\System\Solr\ResponseAdapter;
use ApacheSolrForTypo3\Solr\Tests\Integration\IntegrationTest;
use PHPUnit\Framework\MockObject\MockObject;
use TYPO3\CMS\Core\Http\ServerRequest;

class ResultSetReconstitutionProcessorTest extends IntegrationTest
{
Expand All @@ -33,7 +34,7 @@ class ResultSetReconstitutionProcessorTest extends IntegrationTest
public function canApplyRenderingInstructionsOnOptions()
{
$this->writeDefaultSolrTestSiteConfiguration();
$this->fakeTSFE(1);
$GLOBALS['TYPO3_REQUEST'] = new ServerRequest('https://example.com');

$searchResultSet = $this->initializeSearchResultSetFromFakeResponse('fake_solr_response_with_multiple_fields_facets.json');

Expand Down Expand Up @@ -83,7 +84,7 @@ public function canApplyRenderingInstructionsOnOptions()
public function labelCanBeUsedAsCObject()
{
$this->writeDefaultSolrTestSiteConfiguration();
$this->fakeTSFE(1);
$GLOBALS['TYPO3_REQUEST'] = new ServerRequest('https://example.com');
$searchResultSet = $this->initializeSearchResultSetFromFakeResponse('fake_solr_response_with_multiple_fields_facets.json');

// before the reconstitution of the domain object from the response we expect that no facets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@

use ApacheSolrForTypo3\Solr\ConnectionManager;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\SearchResult;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\SearchResultCollection;
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\SearchResultSetService;
use ApacheSolrForTypo3\Solr\Domain\Search\SearchRequest;
use ApacheSolrForTypo3\Solr\Search;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\Tests\Integration\IntegrationTest;
use ApacheSolrForTypo3\Solr\Util;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\UserAspect;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;

class SearchResultSetServiceTest extends IntegrationTest
{
Expand All @@ -48,14 +53,13 @@ public function canGetDocumentById()
{
// trigger a search
$this->importCSVDataSet(__DIR__ . '/../../../Controller/Fixtures/indexing_data.csv');
$this->indexPageIds([1, 2, 3, 4, 5]);

$this->waitToBeVisibleInSolr();
$this->addSimpleFrontendRenderingToTypoScriptRendering(1);
$this->indexPages([1, 2, 3, 4, 5]);

$solrContent = file_get_contents($this->getSolrConnectionUriAuthority() . '/solr/core_en/select?q=*:*');
self::assertStringContainsString('002de2729efa650191f82900ea02a0a3189dfabb/pages/1/0/0/0', $solrContent);

$solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId(1, 0, 0);
$solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId(1);

$typoScriptConfiguration = Util::getSolrConfiguration();

Expand All @@ -74,10 +78,8 @@ public function canGetVariants()
{
$this->importCSVDataSet(__DIR__ . '/../../../Controller/Fixtures/indexing_data.csv');
$this->importCSVDataSet(__DIR__ . '/Fixtures/can_get_searchResultSet.csv');
$this->indexPageIds([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

$this->waitToBeVisibleInSolr();
$solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId(1, 0, 0);
$this->addSimpleFrontendRenderingToTypoScriptRendering(1);
$this->indexPages([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

$typoScriptConfiguration = Util::getSolrConfiguration();
$typoScriptConfiguration->mergeSolrConfiguration([
Expand All @@ -95,7 +97,7 @@ public function canGetVariants()
self::assertEquals('pid', $typoScriptConfiguration->getSearchVariantsField());
self::assertEquals(11, $typoScriptConfiguration->getSearchVariantsLimit());

$searchResults = $this->doSearchWithResultSetService($solrConnection, $typoScriptConfiguration);
$searchResults = $this->doSearchWithResultSetService($typoScriptConfiguration);
self::assertSame(4, count($searchResults), 'There should be three results at all');

// We assume that the first result (pid=0) has no variants.
Expand Down Expand Up @@ -124,10 +126,8 @@ public function canGetCaseSensitiveVariants()
{
$this->importCSVDataSet(__DIR__ . '/../../../Controller/Fixtures/indexing_data.csv');
$this->importCSVDataSet(__DIR__ . '/Fixtures/can_get_searchResultSet.csv');
$this->indexPageIds([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);

$this->waitToBeVisibleInSolr();
$solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId(1, 0, 0);
$this->addSimpleFrontendRenderingToTypoScriptRendering(1);
$this->indexPages([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]);

$typoScriptConfiguration = Util::getSolrConfiguration();
$typoScriptConfiguration->mergeSolrConfiguration([
Expand All @@ -150,7 +150,7 @@ public function canGetCaseSensitiveVariants()
self::assertEquals('author', $typoScriptConfiguration->getSearchVariantsField());
self::assertEquals(11, $typoScriptConfiguration->getSearchVariantsLimit());

$searchResults = $this->doSearchWithResultSetService($solrConnection, $typoScriptConfiguration);
$searchResults = $this->doSearchWithResultSetService($typoScriptConfiguration);
self::assertSame(3, count($searchResults), 'There should be three results at all');

// We assume that the first result has 6 variants.
Expand Down Expand Up @@ -196,9 +196,6 @@ public function canGetZeroResultsWithVariantsOnEmptyIndex()
{
$this->importCSVDataSet(__DIR__ . '/../../../Controller/Fixtures/indexing_data.csv');
$this->importCSVDataSet(__DIR__ . '/Fixtures/can_get_searchResultSet.csv');
$this->fakeTsfe(1);

$solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId(1, 0, 0);

$typoScriptConfiguration = Util::getSolrConfiguration();
$typoScriptConfiguration->mergeSolrConfiguration([
Expand All @@ -212,7 +209,7 @@ public function canGetZeroResultsWithVariantsOnEmptyIndex()
],
]);

$searchResults = $this->doSearchWithResultSetService($solrConnection, $typoScriptConfiguration, 'nomatchfound');
$searchResults = $this->doSearchWithResultSetService($typoScriptConfiguration, 'nomatchfound');
self::assertSame(0, count($searchResults), 'There should zero results when the index is empty');
}

Expand All @@ -221,15 +218,13 @@ public function canGetZeroResultsWithVariantsOnEmptyIndex()
*/
public function cantGetHiddenElementWithoutPermissions()
{
$this->applyUsingErrorControllerForCMS9andAbove();
$this->importFrontendRestrictedPageScenario();

$solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId(1, 0, 0);
$typoScriptConfiguration = Util::getSolrConfiguration();

// only the default group
$this->simulateFrontedUserGroups([0]);
$searchResults = $this->doSearchWithResultSetService($solrConnection, $typoScriptConfiguration);
$searchResults = $this->doSearchWithResultSetService($typoScriptConfiguration);

self::assertSame(2, count($searchResults), 'We should only see two documents because the restricted element should be filtered out');
}
Expand All @@ -239,15 +234,13 @@ public function cantGetHiddenElementWithoutPermissions()
*/
public function canGetHiddenElementWithPermissions()
{
$this->applyUsingErrorControllerForCMS9andAbove();
$this->importFrontendRestrictedPageScenario();

$solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId(1, 0, 0);
$typoScriptConfiguration = Util::getSolrConfiguration();

// user group 0 and 1 should see all elements
$this->simulateFrontedUserGroups([0, 1]);
$searchResults = $this->doSearchWithResultSetService($solrConnection, $typoScriptConfiguration);
$searchResults = $this->doSearchWithResultSetService($typoScriptConfiguration);

self::assertSame(3, count($searchResults), 'We should see all content, because nothing should be filtered');
}
Expand All @@ -258,40 +251,50 @@ public function canGetHiddenElementWithPermissions()
protected function importFrontendRestrictedPageScenario()
{
$this->importCSVDataSet(__DIR__ . '/Fixtures/fe_user_page.csv');
$this->indexPageIds([1, 2, 3], [1]);
$this->waitToBeVisibleInSolr();
$this->addSimpleFrontendRenderingToTypoScriptRendering(1);
$this->indexPages([1, 2, 3], 1);
$solrContent = file_get_contents($this->getSolrConnectionUriAuthority() . '/solr/core_en/select?q=*:*');
self::assertStringContainsString('"numFound":3', $solrContent);
}

/**
* @param $solrConnection
* @param $typoScriptConfiguration
* @param string $queryString
* @return array
*/
protected function doSearchWithResultSetService($solrConnection, $typoScriptConfiguration, $queryString = '*')
protected function doSearchWithResultSetService(TypoScriptConfiguration $typoScriptConfiguration, string $queryString = '*'): SearchResultCollection
{
$solrConnection = GeneralUtility::makeInstance(ConnectionManager::class)->getConnectionByPageId(1);
$search = GeneralUtility::makeInstance(Search::class, $solrConnection);

/* @var SearchResultSetService $searchResultsSetService */
$searchResultSetService = GeneralUtility::makeInstance(
SearchResultSetService::class,
$typoScriptConfiguration,
$search,
null,
null,
null
$search
);

/* @var SearchRequest $searchRequest */
$searchRequest = GeneralUtility::makeInstance(SearchRequest::class, [], 0, 0, $typoScriptConfiguration);
$searchRequest->setRawQueryString($queryString);
$searchRequest->setResultsPerPage(10);
$searchRequest->setPage(1);

// Simulate something as we still have some $GLOBALS[TSFE] dependency
$GLOBALS['TSFE'] = new \stdClass();
$GLOBALS['TSFE']->id = 1;
$searchResultSet = $searchResultSetService->search($searchRequest);

$searchResults = $searchResultSet->getSearchResults();
return $searchResults;
}

protected function simulateFrontedUserGroups(array $feUserGroupArray): void
{
/* @var Context $context */
$context = GeneralUtility::makeInstance(Context::class);
$userAuthentication = GeneralUtility::makeInstance(FrontendUserAuthentication::class);
// Simulate any user
$userAuthentication->user = [
'uid' => 1,
'usergroup' => implode(',', $feUserGroupArray),
];
$userAspect = new UserAspect($userAuthentication, $feUserGroupArray);
$context->setAspect('frontend.user', $userAspect);
}
}
12 changes: 0 additions & 12 deletions Tests/Integration/Domain/Site/SiteRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@
use ApacheSolrForTypo3\Solr\Domain\Site\Site;
use ApacheSolrForTypo3\Solr\Domain\Site\SiteRepository;
use ApacheSolrForTypo3\Solr\Tests\Integration\IntegrationTest;
use Exception;
use InvalidArgumentException;
use TYPO3\CMS\Core\Cache\Exception\NoSuchCacheException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\TestingFramework\Core\Exception as TestingFrameworkCoreException;

/**
* Testcase to check if the SiteRepository class works as expected.
Expand All @@ -36,10 +33,6 @@ class SiteRepositoryTest extends IntegrationTest
*/
protected $siteRepository;

/**
* @throws NoSuchCacheException
* @throws TestingFrameworkCoreException
*/
protected function setUp(): void
{
parent::setUp();
Expand All @@ -49,7 +42,6 @@ protected function setUp(): void

/**
* @test
* @throws Exception
*/
public function canGetAllSites()
{
Expand All @@ -59,7 +51,6 @@ public function canGetAllSites()

/**
* @test
* @throws Exception
*/
public function canGetAllPagesFromSite()
{
Expand Down Expand Up @@ -89,7 +80,6 @@ public function canGetSiteByRootPageIdNonExistingRoot()

/**
* @test
* @throws Exception
*/
public function canGetSiteByPageIdExistingPage()
{
Expand All @@ -100,7 +90,6 @@ public function canGetSiteByPageIdExistingPage()

/**
* @test
* @throws TestingFrameworkCoreException
*/
public function canGetSiteByPageIdNonExistingPage()
{
Expand All @@ -111,7 +100,6 @@ public function canGetSiteByPageIdNonExistingPage()

/**
* @test
* @throws TestingFrameworkCoreException
*/
public function canGetSiteWithDomainFromSiteConfiguration()
{
Expand Down
Loading

0 comments on commit 689ad06

Please sign in to comment.