Skip to content

Commit b54846f

Browse files
committed
Merge remote-tracking branch 'origin/MC-33026-upgrade' into MC-33394-pr-delivery
2 parents 67a21cf + b635415 commit b54846f

File tree

19 files changed

+395
-158
lines changed

19 files changed

+395
-158
lines changed

app/code/Magento/Elasticsearch/Setup/InstallConfig.php

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77

88
namespace Magento\Elasticsearch\Setup;
99

10-
use Magento\Framework\App\Config\ReinitableConfigInterface;
1110
use Magento\Framework\App\Config\Storage\WriterInterface;
12-
use Magento\Framework\Exception\InputException;
1311
use Magento\Setup\Model\SearchConfigOptionsList;
1412
use Magento\Search\Setup\InstallConfigInterface;
1513

@@ -27,36 +25,20 @@ class InstallConfig implements InstallConfigInterface
2725
SearchConfigOptionsList::INPUT_KEY_SEARCH_ENGINE => 'engine'
2826
];
2927

30-
/**
31-
* @var ConnectionValidator
32-
*/
33-
private $validator;
34-
3528
/**
3629
* @var WriterInterface
3730
*/
3831
private $configWriter;
3932

40-
/**
41-
* @var ReinitableConfigInterface
42-
*/
43-
private $reinitableConfig;
44-
4533
/**
4634
* @param WriterInterface $configWriter
47-
* @param ConnectionValidator $validator
48-
* @param ReinitableConfigInterface $reinitableConfig
4935
* @param array $searchConfigMapping
5036
*/
5137
public function __construct(
5238
WriterInterface $configWriter,
53-
ConnectionValidator $validator,
54-
ReinitableConfigInterface $reinitableConfig,
5539
array $searchConfigMapping = []
5640
) {
5741
$this->configWriter = $configWriter;
58-
$this->validator = $validator;
59-
$this->reinitableConfig = $reinitableConfig;
6042
$this->searchConfigMapping = array_merge($this->searchConfigMapping, $searchConfigMapping);
6143
}
6244

@@ -72,15 +54,5 @@ public function configure(array $inputOptions)
7254
$configKey = $this->searchConfigMapping[$inputKey];
7355
$this->configWriter->save(self::CATALOG_SEARCH . $configKey, $inputValue);
7456
}
75-
$this->reinitableConfig->reinit();
76-
$searchEngine = $inputOptions[SearchConfigOptionsList::INPUT_KEY_SEARCH_ENGINE] ?? null;
77-
if (!$this->validator->validate($searchEngine)) {
78-
throw new InputException(
79-
__(
80-
'Connection to Elasticsearch cannot be established. '
81-
. 'Please check the configuration and try again.'
82-
)
83-
);
84-
}
8557
}
8658
}

app/code/Magento/Elasticsearch/Setup/ConnectionValidator.php renamed to app/code/Magento/Elasticsearch/Setup/Validator.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
namespace Magento\Elasticsearch\Setup;
99

1010
use Magento\AdvancedSearch\Model\Client\ClientResolver;
11+
use Magento\Search\Model\SearchEngine\ValidatorInterface;
1112

1213
/**
1314
* Validate Elasticsearch connection
1415
*/
15-
class ConnectionValidator
16+
class Validator implements ValidatorInterface
1617
{
1718
/**
1819
* @var ClientResolver
@@ -30,16 +31,21 @@ public function __construct(ClientResolver $clientResolver)
3031
/**
3132
* Checks Elasticsearch Connection
3233
*
33-
* @param string|null $searchEngine if empty, uses the currently configured engine
34-
* @return bool true if the connection succeeded, false otherwise
34+
* @param array $searchConfig
35+
* @return array
3536
*/
36-
public function validate(string $searchEngine = null): bool
37+
public function validate(array $searchConfig = []): array
3738
{
39+
$errors = [];
40+
$searchEngine = $searchConfig['search-engine'] ?? null;
3841
try {
3942
$client = $this->clientResolver->create($searchEngine);
40-
return $client->testConnection();
43+
if (!$client->testConnection()) {
44+
$errors[] = 'Elasticsearch connection validation failed';
45+
}
4146
} catch (\Exception $e) {
42-
return false;
47+
$errors[] = 'Elasticsearch connection validation failed: ' . $e->getMessage();
4348
}
49+
return $errors;
4450
}
4551
}

app/code/Magento/Elasticsearch/Test/Unit/Setup/InstallConfigTest.php

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
namespace Magento\Elasticsearch\Test\Unit\Setup;
99

10-
use Magento\Elasticsearch\Setup\ConnectionValidator;
1110
use Magento\Elasticsearch\Setup\InstallConfig;
1211
use Magento\Framework\App\Config\Storage\WriterInterface;
1312
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
@@ -21,29 +20,20 @@ class InstallConfigTest extends TestCase
2120
*/
2221
private $installConfig;
2322

24-
/**
25-
* @var ConnectionValidator|MockObject
26-
*/
27-
private $validatorMock;
28-
2923
/**
3024
* @var WriterInterface|MockObject
3125
*/
3226
private $configWriterMock;
3327

3428
protected function setup()
3529
{
36-
$this->validatorMock = $this->getMockBuilder(ConnectionValidator::class)
37-
->disableOriginalConstructor()
38-
->getMock();
3930
$this->configWriterMock = $this->getMockBuilder(WriterInterface::class)->getMockForAbstractClass();
4031

4132
$objectManager = new ObjectManager($this);
4233
$this->installConfig = $objectManager->getObject(
4334
InstallConfig::class,
4435
[
4536
'configWriter' => $this->configWriterMock,
46-
'validator' => $this->validatorMock,
4737
'searchConfigMapping' => [
4838
'elasticsearch-host' => 'elasticsearch5_server_hostname',
4939
'elasticsearch-port' => 'elasticsearch5_server_port',
@@ -78,45 +68,12 @@ public function testConfigure()
7868
->method('save')
7969
->with('catalog/search/elasticsearch5_server_port', '9200');
8070

81-
$this->validatorMock->expects($this->once())->method('validate')->with('elasticsearch5')->willReturn(true);
82-
83-
$this->installConfig->configure($inputOptions);
84-
}
85-
86-
/**
87-
* @expectedException \Magento\Framework\Exception\InputException
88-
* @expectedExceptionMessage Connection to Elasticsearch cannot be established.
89-
*/
90-
public function testConfigureValidateFail()
91-
{
92-
$inputOptions = [
93-
'search-engine' => 'elasticsearch5',
94-
'elasticsearch-host' => 'es.domain.com',
95-
'elasticsearch-port' => '9200'
96-
];
97-
98-
$this->configWriterMock
99-
->expects($this->at(0))
100-
->method('save')
101-
->with('catalog/search/engine', 'elasticsearch5');
102-
$this->configWriterMock
103-
->expects($this->at(1))
104-
->method('save')
105-
->with('catalog/search/elasticsearch5_server_hostname', 'es.domain.com');
106-
$this->configWriterMock
107-
->expects($this->at(2))
108-
->method('save')
109-
->with('catalog/search/elasticsearch5_server_port', '9200');
110-
111-
$this->validatorMock->expects($this->once())->method('validate')->with('elasticsearch5')->willReturn(false);
112-
11371
$this->installConfig->configure($inputOptions);
11472
}
11573

11674
public function testConfigureWithEmptyInput()
11775
{
11876
$this->configWriterMock->expects($this->never())->method('save');
119-
$this->validatorMock->expects($this->once())->method('validate')->with(null)->willReturn(true);
12077
$this->installConfig->configure([]);
12178
}
12279
}

app/code/Magento/Elasticsearch/Test/Unit/Setup/ConnectionValidatorTest.php renamed to app/code/Magento/Elasticsearch/Test/Unit/Setup/ValidatorTest.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
namespace Magento\Elasticsearch\Test\Unit\Setup;
99

1010
use Magento\AdvancedSearch\Model\Client\ClientResolver;
11-
use Magento\Elasticsearch\Setup\ConnectionValidator;
11+
use Magento\Elasticsearch\Setup\Validator;
1212
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1313
use Magento\Elasticsearch\Elasticsearch5\Model\Client\Elasticsearch;
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use PHPUnit\Framework\TestCase;
1616

17-
class ConnectionValidatorTest extends TestCase
17+
class ValidatorTest extends TestCase
1818
{
1919
/**
20-
* @var ConnectionValidator
20+
* @var Validator
2121
*/
22-
private $connectionValidator;
22+
private $validator;
2323

2424
/**
2525
* @var ClientResolver|MockObject
@@ -41,8 +41,8 @@ protected function setUp()
4141
->getMock();
4242

4343
$objectManager = new ObjectManager($this);
44-
$this->connectionValidator = $objectManager->getObject(
45-
ConnectionValidator::class,
44+
$this->validator = $objectManager->getObject(
45+
Validator::class,
4646
[
4747
'clientResolver' => $this->clientResolverMock
4848
]
@@ -51,16 +51,14 @@ protected function setUp()
5151

5252
public function testValidate()
5353
{
54-
$searchEngine = 'elasticsearch5';
55-
5654
$this->clientResolverMock
5755
->expects($this->once())
5856
->method('create')
59-
->with($searchEngine)
57+
->with(null)
6058
->willReturn($this->elasticsearchClientMock);
6159
$this->elasticsearchClientMock->expects($this->once())->method('testConnection')->willReturn(true);
6260

63-
$this->assertTrue($this->connectionValidator->validate($searchEngine));
61+
$this->assertEquals([], $this->validator->validate());
6462
}
6563

6664
public function testValidateFail()
@@ -74,6 +72,23 @@ public function testValidateFail()
7472
->willReturn($this->elasticsearchClientMock);
7573
$this->elasticsearchClientMock->expects($this->once())->method('testConnection')->willReturn(false);
7674

77-
$this->assertFalse($this->connectionValidator->validate($searchEngine));
75+
$expected = ['Elasticsearch connection validation failed'];
76+
$this->assertEquals($expected, $this->validator->validate(['search-engine' => $searchEngine]));
77+
}
78+
79+
public function testValidateFailException()
80+
{
81+
$this->clientResolverMock
82+
->expects($this->once())
83+
->method('create')
84+
->willReturn($this->elasticsearchClientMock);
85+
$exceptionMessage = 'Could not ping host.';
86+
$this->elasticsearchClientMock
87+
->expects($this->once())
88+
->method('testConnection')
89+
->willThrowException(new \Exception($exceptionMessage));
90+
91+
$expected = ['Elasticsearch connection validation failed: ' . $exceptionMessage];
92+
$this->assertEquals($expected, $this->validator->validate());
7893
}
7994
}

app/code/Magento/Elasticsearch/etc/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,4 +534,14 @@
534534
</argument>
535535
</arguments>
536536
</type>
537+
<type name="Magento\Search\Model\SearchEngine\Validator">
538+
<arguments>
539+
<argument name="engineBlacklist" xsi:type="array">
540+
<item name="elasticsearch" xsi:type="string">Elasticsearch 2</item>
541+
</argument>
542+
<argument name="engineValidators" xsi:type="array">
543+
<item name="elasticsearch5" xsi:type="object">Magento\Elasticsearch\Setup\Validator</item>
544+
</argument>
545+
</arguments>
546+
</type>
537547
</config>

app/code/Magento/Elasticsearch6/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,11 @@
240240
</argument>
241241
</arguments>
242242
</type>
243+
<type name="Magento\Search\Model\SearchEngine\Validator">
244+
<arguments>
245+
<argument name="engineValidators" xsi:type="array">
246+
<item name="elasticsearch6" xsi:type="object">Magento\Elasticsearch\Setup\Validator</item>
247+
</argument>
248+
</arguments>
249+
</type>
243250
</config>

app/code/Magento/Elasticsearch7/etc/di.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,11 @@
243243
</argument>
244244
</arguments>
245245
</type>
246+
<type name="Magento\Search\Model\SearchEngine\Validator">
247+
<arguments>
248+
<argument name="engineValidators" xsi:type="array">
249+
<item name="elasticsearch7" xsi:type="object">Magento\Elasticsearch\Setup\Validator</item>
250+
</argument>
251+
</arguments>
252+
</type>
246253
</config>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Search\Model\SearchEngine;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
12+
/**
13+
* Validate search engine configuration
14+
*/
15+
class Validator implements ValidatorInterface
16+
{
17+
/**
18+
* @var ScopeConfigInterface
19+
*/
20+
private $scopeConfig;
21+
22+
/**
23+
* @var array
24+
*/
25+
private $engineBlacklist = ['mysql' => 'MySQL'];
26+
27+
/**
28+
* @var ValidatorInterface[]
29+
*/
30+
private $engineValidators;
31+
32+
/**
33+
* @param ScopeConfigInterface $scopeConfig
34+
* @param array $engineValidators
35+
* @param array $engineBlacklist
36+
*/
37+
public function __construct(
38+
ScopeConfigInterface $scopeConfig,
39+
array $engineValidators = [],
40+
array $engineBlacklist = []
41+
) {
42+
$this->scopeConfig = $scopeConfig;
43+
$this->engineValidators = $engineValidators;
44+
$this->engineBlacklist = array_merge($this->engineBlacklist, $engineBlacklist);
45+
}
46+
47+
/**
48+
* @inheritDoc
49+
*/
50+
public function validate(array $searchConfig = []): array
51+
{
52+
$errors = [];
53+
54+
$currentEngine = isset($searchConfig['search-engine'])
55+
? $searchConfig['search-engine']
56+
: $this->scopeConfig->getValue('catalog/search/engine');
57+
58+
if (isset($this->engineBlacklist[$currentEngine])) {
59+
$blacklistedEngine = $this->engineBlacklist[$currentEngine];
60+
$errors[] = "Search engine '{$blacklistedEngine}' is not supported. Fix search configuration and try again.";
61+
}
62+
63+
if (isset($this->engineValidators[$currentEngine])) {
64+
$validator = $this->engineValidators[$currentEngine];
65+
$validationErrors = $validator->validate($searchConfig);
66+
$errors = array_merge($errors, $validationErrors);
67+
}
68+
return $errors;
69+
}
70+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Search\Model\SearchEngine;
9+
10+
/**
11+
* Validate search engine configuration
12+
*/
13+
interface ValidatorInterface
14+
{
15+
/**
16+
* Validate search engine
17+
*
18+
* @param array $searchConfig
19+
* @return string[] array of errors, empty array if validation passed
20+
*/
21+
public function validate(array $searchConfig = []): array;
22+
}

0 commit comments

Comments
 (0)