diff --git a/application/Espo/Core/Record/SearchParamsFetcher.php b/application/Espo/Core/Record/SearchParamsFetcher.php index 7d766ec704..a7d790c51e 100644 --- a/application/Espo/Core/Record/SearchParamsFetcher.php +++ b/application/Espo/Core/Record/SearchParamsFetcher.php @@ -29,14 +29,14 @@ namespace Espo\Core\Record; -use Espo\Core\{ - Exceptions\Forbidden, - Exceptions\BadRequest, - Utils\Config, - Api\Request, - Select\SearchParams, - Utils\Json, -}; +use Espo\Core\Exceptions\Forbidden; +use Espo\Core\Exceptions\BadRequest; + +use Espo\Core\Utils\Config; +use Espo\Core\Api\Request; +use Espo\Core\Select\SearchParams; +use Espo\Core\Select\Text\MetadataProvider as TextMetadataProvider; +use Espo\Core\Utils\Json; use JsonException; @@ -46,9 +46,12 @@ class SearchParamsFetcher private Config $config; - public function __construct(Config $config) + private TextMetadataProvider $textMetadataProvider; + + public function __construct(Config $config, TextMetadataProvider $textMetadataProvider) { $this->config = $config; + $this->textMetadataProvider = $textMetadataProvider; } public function fetch(Request $request): SearchParams @@ -67,7 +70,7 @@ class SearchParamsFetcher $this->fetchRawJsonSearchParams($request): $this->fetchRawMultipleParams($request); - $this->handleRawParams($params); + $this->handleRawParams($params, $request); return $params; } @@ -164,7 +167,7 @@ class SearchParamsFetcher /** * @param array $params */ - private function handleRawParams(array &$params): void + private function handleRawParams(array &$params, Request $request): void { if (isset($params['maxSize']) && !is_int($params['maxSize'])) { throw new BadRequest('maxSize must be integer.'); @@ -178,7 +181,8 @@ class SearchParamsFetcher strpos($q, '*') === false && strpos($q, '"') === false && strpos($q, '+') === false && - strpos($q, '-') === false + strpos($q, '-') === false && + $this->hasFullTextSearch($request) ) { $params['q'] = $q . '*'; } @@ -186,6 +190,21 @@ class SearchParamsFetcher $this->handleMaxSize($params); } + private function hasFullTextSearch(Request $request): bool + { + $scope = $request->getRouteParam('controller'); + + if (!$scope) { + return false; + } + + if ($request->getRouteParam('action') !== 'index') { + return false; + } + + return $this->textMetadataProvider->hasFullTextSearch($scope); + } + /** * @param array $params */ diff --git a/tests/unit/Espo/Core/Record/SearchParamsFetcherTest.php b/tests/unit/Espo/Core/Record/SearchParamsFetcherTest.php index ae4b22693b..d0444ace89 100644 --- a/tests/unit/Espo/Core/Record/SearchParamsFetcherTest.php +++ b/tests/unit/Espo/Core/Record/SearchParamsFetcherTest.php @@ -33,10 +33,10 @@ use Espo\Core\Record\SearchParamsFetcher; use Espo\Core\Api\RequestWrapper; use Espo\Core\Utils\Config; +use Espo\Core\Select\Text\MetadataProvider as TextMetadataProvider; use Slim\Psr7\Factory\RequestFactory; - class SearchParamsFetcherTest extends \PHPUnit\Framework\TestCase { private $config; @@ -44,6 +44,7 @@ class SearchParamsFetcherTest extends \PHPUnit\Framework\TestCase protected function setUp(): void { $this->config = $this->createMocK(Config::class); + $this->textMetadataProvider = $this->createMocK(TextMetadataProvider::class); $this->config ->method('get') @@ -62,7 +63,7 @@ class SearchParamsFetcherTest extends \PHPUnit\Framework\TestCase $request = (new RequestFactory)->createRequest('GET', 'http://localhost/?' . $q); - $fetcher = new SearchParamsFetcher($this->config); + $fetcher = new SearchParamsFetcher($this->config, $this->textMetadataProvider); $params = $fetcher->fetch(new RequestWrapper($request));