Skip to content

Commit

Permalink
Adding Unit tests for the suggested terms provider
Browse files Browse the repository at this point in the history
  • Loading branch information
romainruaud committed Dec 19, 2024
1 parent b7904ad commit 75387ca
Show file tree
Hide file tree
Showing 10 changed files with 409 additions and 15 deletions.
5 changes: 0 additions & 5 deletions src/module-elasticsuite-catalog/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@
<category_autocomplete>
<max_size>3</max_size>
</category_autocomplete>
<advanced>
<extension_enabled>1</extension_enabled>
<extension_limit>0</extension_limit>
<stop_extension_on_match>0</stop_extension_on_match>
</advanced>
</smile_elasticsuite_autocomplete_settings>
<smile_elasticsuite_catalogsearch_settings>
<catalogsearch>
Expand Down
13 changes: 13 additions & 0 deletions src/module-elasticsuite-core/Helper/Autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,19 @@ public function isExtensionStoppedOnMatch()
);
}

/**
* Check if Autocomplete is supposed to always use the user raw query or not.
*
* @return bool
*/
public function isPreservingBaseQuery()
{
return (bool) $this->scopeConfig->isSetFlag(
self::AUTOCOMPLETE_SETTINGS_CONFIG_XML_PREFIX . "/advanced/preserve_base_query",
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
);
}

/**
* Retrieve a configuration value by its key
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* versions in the future.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <[email protected]>
* @copyright 2024 Smile
* @license Open Software License ("OSL") v. 3.0
Expand All @@ -24,7 +24,7 @@
* Based on the Term provider but will manipulate it according to configuration.
*
* @category Smile
* @package Smile\ElasticsuiteCatalog
* @package Smile\ElasticsuiteCore
* @author Romain Ruaud <[email protected]>
*/
class SuggestedTermsProvider
Expand All @@ -44,6 +44,11 @@ class SuggestedTermsProvider
*/
private $queryStringProviderFactory;

/**
* @var null|string
*/
private $queryString = null;

/**
* @var null
*/
Expand All @@ -67,6 +72,8 @@ public function __construct(
/**
* List of search terms suggested by the search terms data provider, and reworked according to configuration.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*
* @return array|string[]
*/
public function getSuggestedTerms()
Expand All @@ -84,32 +91,42 @@ function (TermItem $termItem) {

$hasAlreadyStoppedExtending = false;
if ($this->helper->isExtensionStoppedOnMatch()) {
if (array_search(trim($this->getQueryStringProvider()->get()), $terms) !== false) {
$terms = [$this->getQueryStringProvider()->get()];
if (array_search(trim($this->getQueryString()), $terms) !== false) {
$terms = [$this->getQueryString()];
$hasAlreadyStoppedExtending = true;
}
}

if ($this->helper->isExtensionLimited() && !$hasAlreadyStoppedExtending) {
$terms = array_slice($terms, 0, (int) $this->helper->getExtensionSize());
}

if ($this->helper->isPreservingBaseQuery() && !$hasAlreadyStoppedExtending) {
array_unshift($terms, $this->getQueryString());
}
}

if (empty($terms)) {
$terms = [$this->getQueryStringProvider()->get()];
$terms = [$this->getQueryString()];
}

$this->terms = array_unique($terms);
$this->terms = array_values(array_unique($terms));
}

return $this->terms;
}

/**
* @return \Smile\ElasticsuiteCore\Model\Search\QueryStringProvider
* Retrieve current query string
*
* @return string
*/
private function getQueryStringProvider()
private function getQueryString()
{
return $this->queryStringProviderFactory->create();
if ($this->queryString === null) {
$this->queryString = $this->queryStringProviderFactory->create()->get();
}

return $this->queryString;
}
}
Loading

0 comments on commit 75387ca

Please sign in to comment.