Skip to content

Commit d5aa1cc

Browse files
committed
MC-19282: Add Elasticsearch configuration parameters to web setup
1 parent e600d64 commit d5aa1cc

File tree

5 files changed

+250
-17
lines changed

5 files changed

+250
-17
lines changed

setup/src/Magento/Setup/Controller/ConfigureCatalogSearch.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Setup\Controller;
79

8-
use Magento\Setup\Model\SearchConfigOptionsList;
910
use Laminas\Mvc\Controller\AbstractActionController;
10-
use Laminas\View\Model\ViewModel;
1111
use Laminas\View\Model\JsonModel;
12+
use Laminas\View\Model\ViewModel;
13+
use Magento\Setup\Model\SearchConfigOptionsList;
1214

1315
/**
1416
* ConfigureCatalogSearch controller
@@ -33,7 +35,7 @@ public function __construct(SearchConfigOptionsList $searchConfigOptionsList)
3335
*
3436
* @return ViewModel
3537
*/
36-
public function indexAction()
38+
public function indexAction(): ViewModel
3739
{
3840
$view = new ViewModel([
3941
'availableSearchEngines' => $this->searchConfigOptionsList->getAvailableSearchEngineList(),
@@ -47,7 +49,7 @@ public function indexAction()
4749
*
4850
* @return JsonModel
4951
*/
50-
public function defaultParametersAction()
52+
public function defaultParametersAction(): JsonModel
5153
{
5254
$defaults = [
5355
'engine' => SearchConfigOptionsList::DEFAULT_SEARCH_ENGINE,
@@ -56,17 +58,10 @@ public function defaultParametersAction()
5658
'port' => SearchConfigOptionsList::DEFAULT_ELASTICSEARCH_PORT,
5759
'timeout' => SearchConfigOptionsList::DEFAULT_ELASTICSEARCH_TIMEOUT,
5860
'indexPrefix' => SearchConfigOptionsList::DEFAULT_ELASTICSEARCH_INDEX_PREFIX,
59-
'enableAuth' => false,
60-
'username' => null,
61-
'password' => null
61+
'enableAuth' => false
6262
]
6363
];
6464

6565
return new JsonModel($defaults);
6666
}
67-
68-
public function saveAction()
69-
{
70-
71-
}
7267
}

setup/src/Magento/Setup/Controller/SearchEngineCheck.php

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Setup\Controller;
79

810
use Laminas\Json\Json;
911
use Laminas\Mvc\Controller\AbstractActionController;
1012
use Laminas\View\Model\JsonModel;
13+
use Magento\Framework\Exception\InputException;
14+
use Magento\Setup\Model\SearchConfigOptionsList;
1115
use Magento\Setup\Validator\ElasticsearchConnectionValidator;
1216

1317
/**
@@ -20,24 +24,34 @@ class SearchEngineCheck extends AbstractActionController
2024
*/
2125
private $connectionValidator;
2226

27+
/**
28+
* @var SearchConfigOptionsList
29+
*/
30+
private $searchConfigOptionsList;
31+
2332
/**
2433
* @param ElasticsearchConnectionValidator $connectionValidator
34+
* @param SearchConfigOptionsList $searchConfigOptionsList
2535
*/
26-
public function __construct(ElasticsearchConnectionValidator $connectionValidator)
27-
{
36+
public function __construct(
37+
ElasticsearchConnectionValidator $connectionValidator,
38+
SearchConfigOptionsList $searchConfigOptionsList
39+
) {
2840
$this->connectionValidator = $connectionValidator;
41+
$this->searchConfigOptionsList = $searchConfigOptionsList;
2942
}
3043

3144
/**
3245
* Result of checking Elasticsearch connection
3346
*
3447
* @return JsonModel
3548
*/
36-
public function indexAction()
49+
public function indexAction(): JsonModel
3750
{
3851
try {
3952
$params = Json::decode($this->getRequest()->getContent(), Json::TYPE_ARRAY);
40-
$this->connectionValidator->isValidConnection(
53+
$this->isValidSearchEngine($params);
54+
$isValid = $this->connectionValidator->isValidConnection(
4155
[
4256
'hostname' => $params['elasticsearch']['hostname'] ?? null,
4357
'port' => $params['elasticsearch']['port'] ?? null,
@@ -47,9 +61,27 @@ public function indexAction()
4761
'indexPrefix' => $params['elasticsearch']['indexPrefix'] ?? ''
4862
]
4963
);
50-
return new JsonModel(['success' => true]);
64+
return new JsonModel(['success' => $isValid]);
5165
} catch (\Exception $e) {
5266
return new JsonModel(['success' => false, 'error' => $e->getMessage()]);
5367
}
5468
}
69+
70+
/**
71+
* Check search engine parameter is valid
72+
*
73+
* @param $requestParams
74+
* @return bool
75+
* @throws InputException
76+
*/
77+
private function isValidSearchEngine($requestParams): bool
78+
{
79+
$selectedEngine = $requestParams['engine'] ?? null;
80+
$availableSearchEngines = $this->searchConfigOptionsList->getAvailableSearchEngineList();
81+
if (empty($selectedEngine) || !isset($availableSearchEngines[$selectedEngine])) {
82+
throw new InputException(__('Please select a valid search engine.'));
83+
}
84+
85+
return true;
86+
}
5587
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Setup\Test\Unit\Controller;
9+
10+
use Laminas\View\Model\JsonModel;
11+
use Magento\Setup\Controller\ConfigureCatalogSearch;
12+
use Magento\Setup\Model\SearchConfigOptionsList;
13+
use PHPUnit\Framework\TestCase;
14+
use Zend\View\Model\ViewModel;
15+
16+
class ConfigureCatalogSearchTest extends TestCase
17+
{
18+
/**
19+
* @var ConfigureCatalogSearch
20+
*/
21+
private $controller;
22+
23+
/**
24+
* @var SearchConfigOptionsList
25+
*/
26+
private $searchConfigOptionsList;
27+
28+
protected function setup()
29+
{
30+
$this->searchConfigOptionsList = new SearchConfigOptionsList();
31+
$this->controller = new ConfigureCatalogSearch($this->searchConfigOptionsList);
32+
}
33+
34+
public function testIndexAction()
35+
{
36+
$viewModel = $this->controller->indexAction();
37+
$this->assertInstanceOf(ViewModel::class, $viewModel);
38+
$this->assertNotEmpty($viewModel->getVariables());
39+
$expectedAvailableSearchEngines = [
40+
'elasticsearch5' => 'Elasticsearch 5.x (deprecated)',
41+
'elasticsearch6' => 'Elasticsearch 6.x',
42+
'elasticsearch7' => 'Elasticsearch 7.x',
43+
];
44+
$this->assertEquals($expectedAvailableSearchEngines, $viewModel->getVariable('availableSearchEngines'));
45+
}
46+
47+
public function testDefaultParametersAction()
48+
{
49+
$jsonModel = $this->controller->defaultParametersAction();
50+
$this->assertInstanceOf(JsonModel::class, $jsonModel);
51+
52+
$expectedDefaultParameters = [
53+
'engine' => SearchConfigOptionsList::DEFAULT_SEARCH_ENGINE,
54+
'elasticsearch' => [
55+
'hostname' => SearchConfigOptionsList::DEFAULT_ELASTICSEARCH_HOST,
56+
'port' => SearchConfigOptionsList::DEFAULT_ELASTICSEARCH_PORT,
57+
'timeout' => SearchConfigOptionsList::DEFAULT_ELASTICSEARCH_TIMEOUT,
58+
'indexPrefix' => SearchConfigOptionsList::DEFAULT_ELASTICSEARCH_INDEX_PREFIX,
59+
'enableAuth' => false
60+
]
61+
];
62+
$this->assertEquals($expectedDefaultParameters, $jsonModel->getVariables());
63+
}
64+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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\Setup\Test\Unit\Controller;
9+
10+
use Laminas\Stdlib\RequestInterface;
11+
use Laminas\View\Model\JsonModel;
12+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
13+
use Magento\Setup\Controller\SearchEngineCheck;
14+
use Magento\Setup\Model\SearchConfigOptionsList;
15+
use Magento\Setup\Validator\ElasticsearchConnectionValidator;
16+
use PHPUnit\Framework\MockObject\MockObject;
17+
use PHPUnit\Framework\TestCase;
18+
19+
class SearchEngineCheckTest extends TestCase
20+
{
21+
/**
22+
* @var SearchEngineCheck
23+
*/
24+
private $controller;
25+
26+
/**
27+
* @var ElasticsearchConnectionValidator|MockObject
28+
*/
29+
private $connectionValidatorMock;
30+
31+
/**
32+
* @var ObjectManager
33+
*/
34+
private $objectManagerHelper;
35+
36+
protected function setUp()
37+
{
38+
$searchConfigOptionsList = new SearchConfigOptionsList();
39+
$this->objectManagerHelper = new ObjectManager($this);
40+
$this->connectionValidatorMock = $this->getMockBuilder(ElasticsearchConnectionValidator::class)
41+
->disableOriginalConstructor()
42+
->getMock();
43+
44+
$this->controller = new SearchEngineCheck(
45+
$this->connectionValidatorMock,
46+
$searchConfigOptionsList
47+
);
48+
}
49+
50+
public function testIndexAction()
51+
{
52+
$requestData = [
53+
'engine' => 'elasticsearch7',
54+
'elasticsearch' => [
55+
'hostname' => 'localhost',
56+
'port' => '9200',
57+
'timeout' => '15',
58+
'indexPrefix' => 'prefix',
59+
'enableAuth' => false,
60+
'username' => '',
61+
'password' => ''
62+
]
63+
];
64+
/** @var RequestInterface|MockObject $requestMock */
65+
$requestMock = $this->getMockBuilder(RequestInterface::class)
66+
->getMockForAbstractClass();
67+
$requestMock->expects($this->once())
68+
->method('getContent')
69+
->willReturn(json_encode($requestData));
70+
$this->objectManagerHelper->setBackwardCompatibleProperty($this->controller, 'request', $requestMock);
71+
72+
$this->connectionValidatorMock->expects($this->once())->method('isValidConnection')->willReturn(true);
73+
74+
$jsonModel = $this->controller->indexAction();
75+
$this->assertInstanceOf(JsonModel::class, $jsonModel);
76+
$this->assertEquals(['success' => true], $jsonModel->getVariables());
77+
}
78+
79+
public function testIndexActionFailure()
80+
{
81+
$requestData = [
82+
'engine' => 'elasticsearch7',
83+
'elasticsearch' => [
84+
'hostname' => 'other.host',
85+
'port' => '9200',
86+
'timeout' => '15',
87+
'indexPrefix' => 'prefix',
88+
'enableAuth' => false,
89+
'username' => '',
90+
'password' => ''
91+
]
92+
];
93+
/** @var RequestInterface|MockObject $requestMock */
94+
$requestMock = $this->getMockBuilder(RequestInterface::class)
95+
->getMockForAbstractClass();
96+
$requestMock->expects($this->once())
97+
->method('getContent')
98+
->willReturn(json_encode($requestData));
99+
$this->objectManagerHelper->setBackwardCompatibleProperty($this->controller, 'request', $requestMock);
100+
101+
$exceptionMessage = 'Could not connect to Elasticsearch server.';
102+
$this->connectionValidatorMock
103+
->expects($this->once())
104+
->method('isValidConnection')
105+
->willThrowException(new \Exception($exceptionMessage));
106+
107+
$jsonModel = $this->controller->indexAction();
108+
$this->assertInstanceOf(JsonModel::class, $jsonModel);
109+
$this->assertEquals(['success' => false, 'error' => $exceptionMessage], $jsonModel->getVariables());
110+
}
111+
112+
public function testIndexActionInvalidEngine()
113+
{
114+
$requestData = [
115+
'engine' => 'other-engine',
116+
'elasticsearch' => [
117+
'hostname' => 'other.host',
118+
'port' => '9200',
119+
'timeout' => '15',
120+
'indexPrefix' => 'prefix',
121+
'enableAuth' => false,
122+
'username' => '',
123+
'password' => ''
124+
]
125+
];
126+
/** @var RequestInterface|MockObject $requestMock */
127+
$requestMock = $this->getMockBuilder(RequestInterface::class)
128+
->getMockForAbstractClass();
129+
$requestMock->expects($this->once())
130+
->method('getContent')
131+
->willReturn(json_encode($requestData));
132+
$this->objectManagerHelper->setBackwardCompatibleProperty($this->controller, 'request', $requestMock);
133+
$this->connectionValidatorMock->expects($this->never())->method('isValidConnection');
134+
135+
$expectedErrorMessage = 'Please select a valid search engine.';
136+
$jsonModel = $this->controller->indexAction();
137+
$this->assertInstanceOf(JsonModel::class, $jsonModel);
138+
$this->assertEquals(['success' => false, 'error' => $expectedErrorMessage], $jsonModel->getVariables());
139+
}
140+
}

setup/src/Magento/Setup/Validator/ElasticsearchConnectionValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class ElasticsearchConnectionValidator
1616
/**
1717
* Validate elasticsearch connection
1818
*
19+
* Throws exception if unable to connect to Elasticsearch server
20+
*
1921
* @param array $options
2022
* @return bool
2123
* @throws \Exception

0 commit comments

Comments
 (0)