Skip to content

Commit 4e86507

Browse files
committed
MC-33026: Upgrade magento with MySQL search engine removed
1 parent 32ce065 commit 4e86507

File tree

11 files changed

+210
-121
lines changed

11 files changed

+210
-121
lines changed

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

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

88
namespace Magento\Elasticsearch\Setup;
99

10-
use Magento\Framework\App\Config\ReinitableConfigInterface;
1110
use Magento\Framework\App\Config\Storage\WriterInterface;
1211
use Magento\Setup\Model\SearchConfigOptionsList;
1312
use Magento\Search\Setup\InstallConfigInterface;
@@ -31,23 +30,15 @@ class InstallConfig implements InstallConfigInterface
3130
*/
3231
private $configWriter;
3332

34-
/**
35-
* @var ReinitableConfigInterface
36-
*/
37-
private $reinitableConfig;
38-
3933
/**
4034
* @param WriterInterface $configWriter
41-
* @param ReinitableConfigInterface $reinitableConfig
4235
* @param array $searchConfigMapping
4336
*/
4437
public function __construct(
4538
WriterInterface $configWriter,
46-
ReinitableConfigInterface $reinitableConfig,
4739
array $searchConfigMapping = []
4840
) {
4941
$this->configWriter = $configWriter;
50-
$this->reinitableConfig = $reinitableConfig;
5142
$this->searchConfigMapping = array_merge($this->searchConfigMapping, $searchConfigMapping);
5243
}
5344

@@ -63,6 +54,5 @@ public function configure(array $inputOptions)
6354
$configKey = $this->searchConfigMapping[$inputKey];
6455
$this->configWriter->save(self::CATALOG_SEARCH . $configKey, $inputValue);
6556
}
66-
$this->reinitableConfig->reinit();
6757
}
6858
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ public function __construct(ClientResolver $clientResolver)
3737
public function validate(array $searchConfig = []): array
3838
{
3939
$errors = [];
40-
$searchEngine = $searchConfig['engine'] ?? null;
40+
$searchEngine = $searchConfig['search-engine'] ?? null;
4141
try {
4242
$client = $this->clientResolver->create($searchEngine);
43-
$client->testConnection();
43+
if (!$client->testConnection()) {
44+
$errors[] = 'Elasticsearch connection validation failed';
45+
}
4446
} catch (\Exception $e) {
4547
$errors[] = 'Elasticsearch connection validation failed: ' . $e->getMessage();
4648
}

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

Lines changed: 1 addition & 44 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\Validator;
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 Validator|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(Validator::class)
37-
->disableOriginalConstructor()
38-
->getMock();
3930
$this->configWriterMock = $this->getMockBuilder(WriterInterface::class)->getMockForAbstractClass();
4031

4132
$objectManager = new ObjectManager($this);
4233
$this->installConfig = $objectManager->getObject(
43-
InstallConfigInterface::class,
34+
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: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use PHPUnit\Framework\TestCase;
1616

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

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

4343
$objectManager = new ObjectManager($this);
44-
$this->connectionValidator = $objectManager->getObject(
44+
$this->validator = $objectManager->getObject(
4545
Validator::class,
4646
[
4747
'clientResolver' => $this->clientResolverMock
@@ -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(['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/Search/Model/SearchEngine/Validator.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,15 @@ public function __construct(
4444
$this->engineBlacklist = array_merge($this->engineBlacklist, $engineBlacklist);
4545
}
4646

47-
4847
/**
4948
* @inheritDoc
5049
*/
5150
public function validate(array $searchConfig = []): array
5251
{
5352
$errors = [];
5453

55-
$currentEngine = isset($searchConfig['engine'])
56-
? $searchConfig['engine']
54+
$currentEngine = isset($searchConfig['search-engine'])
55+
? $searchConfig['search-engine']
5756
: $this->scopeConfig->getValue('catalog/search/engine');
5857

5958
if (in_array($currentEngine, $this->engineBlacklist)) {

app/code/Magento/Search/Setup/CompositeInstallConfig.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Search\Setup;
99

10+
use Magento\Framework\App\Config;
1011
use Magento\Framework\App\Config\ScopeConfigInterface;
1112

1213
/**
@@ -47,9 +48,14 @@ public function configure(array $inputOptions)
4748
$searchEngine = $this->scopeConfig->getValue('catalog/search/engine');
4849
}
4950

50-
if (isset($this->installConfigList[$searchEngine])) {
51+
if (isset($this->installConfigList[$searchEngine]) && !empty($inputOptions)) {
5152
$installConfig = $this->installConfigList[$searchEngine];
5253
$installConfig->configure($inputOptions);
54+
55+
//Clean config so new configuration is loaded
56+
if ($this->scopeConfig instanceof Config) {
57+
$this->scopeConfig->clean();
58+
}
5359
}
5460
}
5561
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\Test\Unit\Model\SearchEngine;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
12+
use Magento\Search\Model\SearchEngine\Validator;
13+
use Magento\Search\Model\SearchEngine\ValidatorInterface;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test search engine validator
18+
*/
19+
class ValidatorTest extends TestCase
20+
{
21+
private $validator;
22+
23+
private $otherEngineValidatorMock;
24+
25+
private $scopeConfigMock;
26+
27+
protected function setUp()
28+
{
29+
$objectManager = new ObjectManager($this);
30+
$this->otherEngineValidatorMock = $this->getMockForAbstractClass(ValidatorInterface::class);
31+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)->getMock();
32+
$this->validator = $objectManager->getObject(
33+
Validator::class,
34+
[
35+
'scopeConfig' => $this->scopeConfigMock,
36+
'engineValidators' => ['other-engine' => $this->otherEngineValidatorMock],
37+
'engineBlacklist' => ['badEngine']
38+
]
39+
);
40+
}
41+
42+
public function testValidateValid()
43+
{
44+
$expectedErrors = [];
45+
46+
$this->scopeConfigMock
47+
->expects($this->once())
48+
->method('getValue')
49+
->with('catalog/search/engine')
50+
->willReturn('other-engine');
51+
52+
$this->otherEngineValidatorMock->expects($this->once())->method('validate')->willReturn([]);
53+
54+
$this->assertEquals($expectedErrors, $this->validator->validate());
55+
}
56+
57+
public function testValidateBlacklist()
58+
{
59+
$expectedErrors = ["Search engine 'badEngine' is not supported. Fix search configuration and try again."];
60+
61+
$this->assertEquals($expectedErrors, $this->validator->validate(['engine' => 'badEngine']));
62+
}
63+
64+
public function testValidateInvalid()
65+
{
66+
$expectedErrors = ['Validation failed for other-engine'];
67+
68+
$this->otherEngineValidatorMock
69+
->expects($this->once())
70+
->method('validate')
71+
->willReturn($expectedErrors);
72+
73+
$this->assertEquals($expectedErrors, $this->validator->validate(['engine' => 'other-engine']));
74+
}
75+
}

0 commit comments

Comments
 (0)