Skip to content

Commit c469e00

Browse files
committed
ACP2E-99: "Search Synonyms" no longer works when value is added in "Minimum Terms to Match"
1 parent 73ba8cf commit c469e00

File tree

1 file changed

+101
-35
lines changed
  • app/code/Magento/Elasticsearch/Test/Unit/Model/Adapter/Index

1 file changed

+101
-35
lines changed

app/code/Magento/Elasticsearch/Test/Unit/Model/Adapter/Index/BuilderTest.php

Lines changed: 101 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ class BuilderTest extends TestCase
2222
/**
2323
* @var Builder
2424
*/
25-
protected $model;
25+
private $model;
2626

2727
/**
2828
* @var LocaleResolver|MockObject
2929
*/
30-
protected $localeResolver;
30+
private $localeResolver;
3131

3232
/**
3333
* @var EsConfigInterface|MockObject
3434
*/
35-
protected $esConfig;
35+
private $esConfig;
3636

3737
/**
3838
* @var SynonymReader|MockObject
@@ -87,6 +87,26 @@ protected function setUp(): void
8787
->disableOriginalConstructor()
8888
->getMock();
8989

90+
$this->synonymReaderMock->expects($this->once())
91+
->method('getConnection')
92+
->willReturn($this->connectionMock);
93+
94+
$this->connectionMock->expects($this->once())
95+
->method('select')
96+
->willReturn($this->selectMock);
97+
98+
$this->selectMock->expects($this->once())
99+
->method('from')
100+
->willReturn($this->selectMock);
101+
102+
$this->esConfig->expects($this->once())
103+
->method('getStemmerInfo')
104+
->willReturn([
105+
'type' => 'stemmer',
106+
'default' => 'english',
107+
'en_US' => 'english',
108+
]);
109+
90110
$objectManager = new ObjectManagerHelper($this);
91111
$this->model = $objectManager->getObject(
92112
Builder::class,
@@ -99,57 +119,103 @@ protected function setUp(): void
99119
}
100120

101121
/**
102-
* Test build() method
122+
* Test build() method without provided synonyms.
123+
*
124+
* In this case, synonyms filter must not be created or referenced
125+
* in the prefix_search and sku_prefix_search analyzers.
103126
*
104127
* @param string $locale
105128
* @dataProvider buildDataProvider
106129
*/
107-
public function testBuild($locale)
130+
public function testBuildWithoutSynonymsProvided(string $locale)
108131
{
109-
$synonymsArray = [
110-
'mp3,player,sound,audio',
111-
'tv,video,television,screen'
112-
];
132+
$synonymsFilterName = 'synonyms';
113133

114134
$this->localeResolver->expects($this->once())
115135
->method('getLocale')
116136
->willReturn($locale);
117137

118-
$this->esConfig->expects($this->once())
119-
->method('getStemmerInfo')
120-
->willReturn([
121-
'type' => 'stemmer',
122-
'default' => 'english',
123-
'en_US' => 'english',
124-
]);
125-
126-
$this->synonymReaderMock->expects($this->once())
127-
->method('getConnection')
128-
->willReturn($this->connectionMock);
129-
130-
$this->connectionMock->expects($this->once())
131-
->method('select')
132-
->willReturn($this->selectMock);
133-
134-
$this->selectMock->expects($this->once())
135-
->method('from')
136-
->willReturn($this->selectMock);
137-
138138
$this->connectionMock->expects($this->once())
139139
->method('fetchCol')
140-
->willReturn($synonymsArray);
140+
->willReturn([]);
141141

142142
$result = $this->model->build();
143-
$this->assertNotNull($result);
143+
144+
$analysisFilters = $result["analysis"]["filter"];
145+
$prefixSearchAnalyzerFilters = $result["analysis"]["analyzer"]["prefix_search"]["filter"];
146+
$skuPrefixSearchAnalyzerFilters = $result["analysis"]["analyzer"]["sku_prefix_search"]["filter"];
147+
148+
$this->assertArrayNotHasKey(
149+
$synonymsFilterName,
150+
$analysisFilters,
151+
'Analysis filters must not contain synonyms when they are not defined'
152+
);
153+
$this->assertNotContains($synonymsFilterName,
154+
$prefixSearchAnalyzerFilters,
155+
'The prefix_search analyzer must not include synonyms filter when it is not present'
156+
);
157+
$this->assertNotContains(
158+
$synonymsFilterName,
159+
$skuPrefixSearchAnalyzerFilters,
160+
'The sku_prefix_search analyzer must include synonyms filter when it is not present'
161+
);
144162
}
145163

146164
/**
147-
* Test setStoreId() method
165+
* Test build() method with synonyms provided.
166+
*
167+
* In this case synonyms filter should be created, populated with the list of available synonyms
168+
* and referenced in the prefix_search and sku_prefix_search analyzers.
169+
*
170+
* @param string $locale
171+
* @dataProvider buildDataProvider
148172
*/
149-
public function testSetStoreId()
173+
public function testBuildWithProvidedSynonyms(string $locale)
150174
{
151-
$result = $this->model->setStoreId(1);
152-
$this->assertNull($result);
175+
$synonymsFilterName = 'synonyms';
176+
177+
$synonyms = [
178+
'mp3,player,sound,audio',
179+
'tv,video,television,screen'
180+
];
181+
182+
$expectedFilter = [
183+
'type' => 'synonym_graph',
184+
'synonyms' => $synonyms
185+
];
186+
187+
$this->localeResolver->expects($this->once())
188+
->method('getLocale')
189+
->willReturn($locale);
190+
191+
$this->connectionMock->expects($this->once())
192+
->method('fetchCol')
193+
->willReturn($synonyms);
194+
195+
$result = $this->model->build();
196+
197+
$analysisFilters = $result["analysis"]["filter"];
198+
$prefixSearchAnalyzerFilters = $result["analysis"]["analyzer"]["prefix_search"]["filter"];
199+
$skuPrefixSearchAnalyzerFilters = $result["analysis"]["analyzer"]["sku_prefix_search"]["filter"];
200+
201+
$this->assertArrayHasKey(
202+
$synonymsFilterName,
203+
$analysisFilters,
204+
'Analysis filters must contain synonyms when defined'
205+
);
206+
$this->assertContains(
207+
$expectedFilter,
208+
$analysisFilters,
209+
'Analysis synonyms filter must match the expected result'
210+
);
211+
$this->assertContains($synonymsFilterName,
212+
$prefixSearchAnalyzerFilters,
213+
'The prefix_search analyzer must include synonyms filter'
214+
);
215+
$this->assertContains($synonymsFilterName,
216+
$skuPrefixSearchAnalyzerFilters,
217+
'The sku_prefix_search analyzer must include synonyms filter'
218+
);
153219
}
154220

155221
/**

0 commit comments

Comments
 (0)