Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX:BP:11.5] Retry Uri Building after exception #3789

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions Classes/Domain/Search/Uri/SearchUriBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use ApacheSolrForTypo3\Solr\System\Url\UrlHelper;
use ApacheSolrForTypo3\Solr\Utility\ParameterSortingUtility;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use TYPO3\CMS\Core\Http\Uri;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
Expand Down Expand Up @@ -368,11 +369,26 @@ protected function buildLinkWithInMemoryCache(?int $pageUid, array $arguments):
} else {
self::$missCount++;
$this->uriBuilder->reset()->setTargetPageUid($pageUid);
$uriCacheTemplate = $this->uriBuilder->setArguments($structure)->build();

/* @var UrlHelper $urlHelper */
$urlHelper = GeneralUtility::makeInstance(UrlHelper::class, $uriCacheTemplate);
self::$preCompiledLinks[$hash] = (string)$urlHelper;
try {
$uriCacheTemplate = $this->uriBuilder->setArguments($structure)->build();

/** @var UrlHelper $urlHelper */
$urlHelper = GeneralUtility::makeInstance(UrlHelper::class, $uriCacheTemplate);
self::$preCompiledLinks[$hash] = (string)$urlHelper;
} catch (InvalidParameterException $exception) {
// the placeholders may result in an exception when route enhancers with requirements are active
// In this case, try to build the URL with original arguments
$hash = md5($pageUid . json_encode($arguments));
if (isset(self::$preCompiledLinks[$hash])) {
self::$hitCount++;
$uriCacheTemplate = self::$preCompiledLinks[$hash];
} else {
$uriCacheTemplate = $this->uriBuilder->setArguments($arguments)->build();
/** @var UrlHelper $urlHelper */
$urlHelper = GeneralUtility::makeInstance(UrlHelper::class, $uriCacheTemplate);
self::$preCompiledLinks[$hash] = (string)$urlHelper;
}
}
}

$keys = array_map(function ($value) {
Expand Down
67 changes: 67 additions & 0 deletions Tests/Unit/Domain/Search/Uri/SearchUriBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use ApacheSolrForTypo3\Solr\Routing\RoutingService;
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration;
use ApacheSolrForTypo3\Solr\Tests\Unit\UnitTest;
use Symfony\Component\Routing\Exception\InvalidParameterException;
use Symfony\Component\Yaml\Yaml;
use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
Expand Down Expand Up @@ -480,4 +481,70 @@ public function siteConfigurationModifyUriKeepUnmappedFilterTest()
$uri = $this->searchUrlBuilder->getResultPageUri($previousRequest, 0);
self::assertEquals($linkBuilderResult, $uri);
}

/**
* @test
*/
public function uriErrorsResultInNonMappedProcessing(): void
{
$configuration = Yaml::parse($this->getFixtureContentByName('siteConfiguration.yaml'));
$routingServiceMock = $this->createMock(RoutingService::class);
$routingServiceMock->expects(self::any())
->method('fetchEnhancerByPageUid')
->willReturn($configuration['routeEnhancers']['example']);
$queryParameters = [
'tx_solr' => [
'filter' => [
'type:pages',
'color:green',
'color:red',
'color:yellow',
'taste:matcha',
'taste:sour',
'product:candy',
'product:sweets',
'quantity:20',
],
],
];
$subsitutedQueryParameters = [
'tx_solr' => [
'filter' => [
'###tx_solr:filter:0:type###',
'###tx_solr:filter:1:color###',
'###tx_solr:filter:2:color###',
'###tx_solr:filter:3:color###',
'###tx_solr:filter:4:taste###',
'###tx_solr:filter:5:taste###',
'###tx_solr:filter:6:product###',
'###tx_solr:filter:7:product###',
'###tx_solr:filter:8:quantity###',
],
],
];
$linkBuilderResult = '/index.php?id=42&color=' . urlencode('green,red,yellow') .
'&taste=' . urlencode('matcha,sour') .
'&product=' . urlencode('candy,sweets') .
'&' . urlencode('tx_solr[filter][0]') . '=' . urlencode('quantity:20');
$configurationMock = $this->createMock(TypoScriptConfiguration::class);
$configurationMock->expects(self::any())->method('getSearchPluginNamespace')->willReturn('tx_solr');
$configurationMock->expects(self::once())->method('getSearchTargetPage')->willReturn(42);

$previousRequest = new SearchRequest($queryParameters, 42, 0, $configurationMock);
$this->extBaseUriBuilderMock->expects(self::any())->method('setArguments')
->withConsecutive([$subsitutedQueryParameters], [$queryParameters])
->willReturn($this->extBaseUriBuilderMock);
$this->extBaseUriBuilderMock->expects(self::once())->method('reset')->with()->willReturn($this->extBaseUriBuilderMock);
$buildCounter = 0;
$this->extBaseUriBuilderMock->expects(self::exactly(2))->method('build')
->willReturnCallback(function () use ($linkBuilderResult, &$buildCounter) {
if (++$buildCounter === 1) {
throw new InvalidParameterException('First call fails, should reprocess with regular arguments');
}
return $linkBuilderResult;
});
$this->searchUrlBuilder->injectRoutingService($routingServiceMock);
$uri = $this->searchUrlBuilder->getResultPageUri($previousRequest, 0);
self::assertEquals($linkBuilderResult, $uri);
}
}
Loading