Skip to content

Commit a9ba5b8

Browse files
committed
MAGETWO-91934: Unlock Locales Editing when SCD on Demand Mode is Enabled
- remove duplication
1 parent 822ba0c commit a9ba5b8

File tree

5 files changed

+166
-195
lines changed

5 files changed

+166
-195
lines changed

app/code/Magento/Backend/etc/adminhtml/di.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@
149149
</argument>
150150
</arguments>
151151
</type>
152-
<type name="Magento\Config\Model\Config\Structure\ElementVisibility\ConcealScdField">
152+
<type name="Magento\Config\Model\Config\Structure\ElementVisibility\ConcealInProductionWithoutScdOnDemand">
153153
<arguments>
154154
<argument name="configs" xsi:type="array">
155155
<item name="general/locale/code" xsi:type="const">Magento\Config\Model\Config\Structure\ElementVisibilityInterface::DISABLED</item>

app/code/Magento/Config/Model/Config/Structure/ElementVisibility/ConcealScdField.php renamed to app/code/Magento/Config/Model/Config/Structure/ElementVisibility/ConcealInProductionWithoutScdOnDemand.php

Lines changed: 15 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
/**
1616
* Defines status of visibility of form elements on Stores > Settings > Configuration page
17-
* in Admin Panel in Production mode.
17+
* when Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION is enabled.
1818
* @api
1919
*/
20-
class ConcealScdField implements ElementVisibilityInterface
20+
class ConcealInProductionWithoutScdOnDemand implements ElementVisibilityInterface
2121
{
2222
/**
2323
* The list of form element paths with concrete visibility status.
@@ -39,13 +39,6 @@ class ConcealScdField implements ElementVisibilityInterface
3939
*/
4040
private $configs = [];
4141

42-
/**
43-
* The object that has information about the state of the system.
44-
*
45-
* @var State
46-
*/
47-
private $state;
48-
4942
/**
5043
*
5144
* The list of form element paths which ignore visibility status.
@@ -66,24 +59,31 @@ class ConcealScdField implements ElementVisibilityInterface
6659
*/
6760
private $exemptions = [];
6861

62+
/**
63+
* @var ConcealInProduction
64+
*/
65+
private $concealInProduction;
66+
6967
/**
7068
* @var DeploymentConfig
7169
*/
7270
private $deploymentConfig;
7371

7472
/**
73+
* @param ConcealInProductionFactory $concealInProductionFactory
7574
* @param State $state The object that has information about the state of the system
7675
* @param DeploymentConfig $deploymentConfig Deployment configuration reader
7776
* @param array $configs The list of form element paths with concrete visibility status.
7877
* @param array $exemptions The list of form element paths which ignore visibility status.
7978
*/
8079
public function __construct(
81-
State $state,
80+
ConcealInProductionFactory $concealInProductionFactory,
8281
DeploymentConfig $deploymentConfig,
8382
array $configs = [],
8483
array $exemptions = []
8584
) {
86-
$this->state = $state;
85+
$this->concealInProduction = $concealInProductionFactory
86+
->create(['configs' => $configs, 'exemptions' => $exemptions]);
8787
$this->deploymentConfig = $deploymentConfig;
8888
$this->configs = $configs;
8989
$this->exemptions = $exemptions;
@@ -94,23 +94,9 @@ public function __construct(
9494
*/
9595
public function isHidden($path): bool
9696
{
97-
$path = $this->normalizePath($path);
98-
if ($this->state->getMode() === State::MODE_PRODUCTION
99-
&& !$this->deploymentConfig->getConfigData(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
100-
&& preg_match('/(?<group>(?<section>.*?)\/.*?)\/.*?/', $path, $match)) {
101-
$group = $match['group'];
102-
$section = $match['section'];
103-
$exemptions = array_keys($this->exemptions);
104-
$checkedItems = [];
105-
foreach ([$path, $group, $section] as $itemPath) {
106-
$checkedItems[] = $itemPath;
107-
if (!empty($this->configs[$itemPath])) {
108-
return $this->configs[$itemPath] === static::HIDDEN
109-
&& empty(array_intersect($checkedItems, $exemptions));
110-
}
111-
}
97+
if (!$this->deploymentConfig->getConfigData(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)) {
98+
return $this->concealInProduction->isHidden($path);
11299
}
113-
114100
return false;
115101
}
116102

@@ -119,33 +105,9 @@ public function isHidden($path): bool
119105
*/
120106
public function isDisabled($path): bool
121107
{
122-
$path = $this->normalizePath($path);
123-
if ($this->state->getMode() === State::MODE_PRODUCTION
124-
&& !$this->deploymentConfig->getConfigData(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)) {
125-
while ($path) {
126-
if (!empty($this->configs[$path])) {
127-
return $this->configs[$path] === static::DISABLED;
128-
}
129-
130-
$position = strripos($path, '/');
131-
if ($position === false) {
132-
break;
133-
}
134-
$path = $position !== false ? substr($path, 0, $position) : null;
135-
}
108+
if (!$this->deploymentConfig->getConfigData(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)) {
109+
return $this->concealInProduction->isDisabled($path);
136110
}
137-
138111
return false;
139112
}
140-
141-
/**
142-
* Returns normalized path.
143-
*
144-
* @param string $path The path to be normalized
145-
* @return string The normalized path
146-
*/
147-
private function normalizePath($path)
148-
{
149-
return trim($path, '/');
150-
}
151113
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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\Config\Test\Unit\Model\Config\Structure\ElementVisibility;
9+
10+
use Magento\Framework\App\DeploymentConfig;
11+
use \Magento\Config\Model\Config\Structure\ElementVisibility\ConcealInProduction;
12+
use \Magento\Config\Model\Config\Structure\ElementVisibility\ConcealInProductionFactory;
13+
use Magento\Framework\Config\ConfigOptionsListConstants as Constants;
14+
use Magento\Config\Model\Config\Structure\ElementVisibility\ConcealInProductionWithoutScdOnDemand;
15+
use Magento\Config\Model\Config\Structure\ElementVisibilityInterface;
16+
17+
class ConcealInProductionWithoutScdOnDemandTest extends \PHPUnit\Framework\TestCase
18+
{
19+
/**
20+
* @var ConcealInProduction|\PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
private $concealInProductionMock;
23+
24+
/**
25+
* @var ConcealInProductionWithoutScdOnDemand
26+
*/
27+
private $model;
28+
29+
/**
30+
* @var DeploymentConfig|\PHPUnit_Framework_MockObject_MockObject
31+
*/
32+
private $deploymentConfigMock;
33+
34+
protected function setUp()
35+
{
36+
$concealInProductionFactoryMock = $this->createMock(ConcealInProductionFactory::class);
37+
38+
$this->concealInProductionMock = $this->createMock(ConcealInProduction::class);
39+
40+
$this->deploymentConfigMock = $this->createMock(\Magento\Framework\App\DeploymentConfig::class);
41+
42+
$configs = [
43+
'section1/group1/field1' => ElementVisibilityInterface::DISABLED,
44+
'section1/group1' => ElementVisibilityInterface::HIDDEN,
45+
'section1' => ElementVisibilityInterface::DISABLED,
46+
'section1/group2' => 'no',
47+
'section2/group1' => ElementVisibilityInterface::DISABLED,
48+
'section2/group2' => ElementVisibilityInterface::HIDDEN,
49+
'section3' => ElementVisibilityInterface::HIDDEN,
50+
'section3/group1/field1' => 'no',
51+
];
52+
$exemptions = [
53+
'section1/group1/field3' => '',
54+
'section1/group2/field1' => '',
55+
'section2/group2/field1' => '',
56+
'section3/group2' => '',
57+
];
58+
59+
$concealInProductionFactoryMock->expects($this->any())
60+
->method('create')
61+
->with(['configs' => $configs, 'exemptions' => $exemptions])
62+
->willReturn($this->concealInProductionMock);
63+
64+
$this->model = new ConcealInProductionWithoutScdOnDemand(
65+
$concealInProductionFactoryMock,
66+
$this->deploymentConfigMock,
67+
$configs,
68+
$exemptions
69+
);
70+
}
71+
72+
public function testIsHiddenScdOnDemandEnabled(): void
73+
{
74+
$path = 'section1/group1/field1';
75+
$this->deploymentConfigMock->expects($this->once())
76+
->method('getConfigData')
77+
->with(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
78+
->willReturn(true);
79+
$this->concealInProductionMock->expects($this->never())
80+
->method('isHidden');
81+
82+
$this->assertFalse($this->model->isHidden($path));
83+
}
84+
85+
public function testIsDisabledScdOnDemandEnabled(): void
86+
{
87+
$path = 'section1/group1/field1';
88+
$this->deploymentConfigMock->expects($this->once())
89+
->method('getConfigData')
90+
->with(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
91+
->willReturn(true);
92+
$this->concealInProductionMock->expects($this->never())
93+
->method('isDisabled');
94+
95+
$this->assertFalse($this->model->isDisabled($path));
96+
}
97+
98+
/**
99+
* @param bool $isHidden
100+
*
101+
* @dataProvider visibilityDataProvider
102+
*/
103+
public function testIsHiddenScdOnDemandDisabled($isHidden): void
104+
{
105+
$path = 'section1/group1/field1';
106+
$this->deploymentConfigMock->expects($this->once())
107+
->method('getConfigData')
108+
->with(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
109+
->willReturn(false);
110+
$this->concealInProductionMock->expects($this->once())
111+
->method('isHidden')
112+
->with($path)
113+
->willReturn($isHidden);
114+
115+
$this->assertSame($isHidden, $this->model->isHidden($path));
116+
}
117+
118+
/**
119+
* @param bool $isDisabled
120+
*
121+
* @dataProvider visibilityDataProvider
122+
*/
123+
public function testIsDisabledScdOnDemandDisabled($isDisabled): void
124+
{
125+
$path = 'section1/group1/field1';
126+
$this->deploymentConfigMock->expects($this->once())
127+
->method('getConfigData')
128+
->with(Constants::CONFIG_PATH_SCD_ON_DEMAND_IN_PRODUCTION)
129+
->willReturn(false);
130+
$this->concealInProductionMock->expects($this->once())
131+
->method('isDisabled')
132+
->with($path)
133+
->willReturn($isDisabled);
134+
135+
$this->assertSame($isDisabled, $this->model->isDisabled($path));
136+
}
137+
138+
/**
139+
* @return array
140+
*/
141+
public function visibilityDataProvider(): array
142+
{
143+
return [
144+
[true],
145+
[false],
146+
];
147+
}
148+
}

0 commit comments

Comments
 (0)