Skip to content

Commit 12ccd95

Browse files
author
Bohdan Korablov
committed
MAGETWO-61786: Implementation
1 parent 3694d54 commit 12ccd95

File tree

10 files changed

+141
-91
lines changed

10 files changed

+141
-91
lines changed

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,12 @@
141141
<type name="Magento\Backend\Model\Menu\Builder">
142142
<plugin name="SetupMenuBuilder" type="Magento\Backend\Model\Setup\MenuBuilder" />
143143
</type>
144-
<type name="Magento\Config\Model\Config\Structure\ProductionVisibility">
144+
<type name="Magento\Config\Model\Config\Structure\ConcealInProductionConfigList">
145145
<arguments>
146146
<argument name="configs" xsi:type="array">
147147
<item name="dev" xsi:type="const">Magento\Config\Model\Config\Structure\ElementVisibilityInterface::HIDDEN</item>
148148
<item name="general/locale/code" xsi:type="const">Magento\Config\Model\Config\Structure\ElementVisibilityInterface::DISABLED</item>
149149
</argument>
150150
</arguments>
151151
</type>
152-
<type name="Magento\Config\Model\Config\Structure\ElementVisibility">
153-
<arguments>
154-
<argument name="visibility" xsi:type="array">
155-
<item name="productionVisibility" xsi:type="object">Magento\Config\Model\Config\Structure\ProductionVisibility</item>
156-
</argument>
157-
</arguments>
158-
</type>
159152
</config>

app/code/Magento/Config/Block/System/Config/Form.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,7 @@ protected function _initElement(
361361
$sharedClass = $this->_getSharedCssClass($field);
362362
$requiresClass = $this->_getRequiresCssClass($field, $fieldPrefix);
363363

364-
$path = trim($field->getPath(), '/');
365-
$isReadOnly = $this->getElementVisibility()->isDisabled($path);
364+
$isReadOnly = $this->getElementVisibility()->isDisabled($field->getPath());
366365
$isReadOnly = $isReadOnly
367366
?: $this->getSettingChecker()->isReadOnly($path, $this->getScope(), $this->getStringScopeCode());
368367
$formField = $fieldset->addField(
@@ -816,10 +815,12 @@ private function getAppConfigDataValue($path)
816815
}
817816

818817
/**
819-
* Get instance of ElementVisibilityInterface.
818+
* Gets instance of ElementVisibilityInterface.
820819
*
821820
* @return ElementVisibilityInterface
822-
* @deprecated
821+
* @deprecated Added to not break backward compatibility of the constructor signature
822+
* by injecting the new dependency directly.
823+
* The method can be removed in a future major release, when constructor signature can be changed.
823824
*/
824825
public function getElementVisibility()
825826
{

app/code/Magento/Config/Model/Config/Structure/AbstractElement.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ public function getAttribute($key)
147147
*/
148148
public function isVisible()
149149
{
150-
$path = trim($this->getPath(), '/');
151-
if ($this->getElementVisibility()->isHidden($path)) {
150+
if ($this->getElementVisibility()->isHidden($this->getPath())) {
152151
return false;
153152
}
154153

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Config\Model\Config\Structure;
7+
8+
use Magento\Framework\App\State;
9+
10+
/**
11+
* Contains list of elements paths which should be hidden or disabled on Configuration page in Production mode.
12+
*/
13+
class ConcealInProductionConfigList implements ElementVisibilityInterface
14+
{
15+
/**
16+
* The list of paths of form elements in the structure.
17+
*
18+
* @var array
19+
*/
20+
private $configs = [];
21+
22+
/**
23+
* The object that has information about the state of the system.
24+
*
25+
* @var State
26+
*/
27+
private $state;
28+
29+
/**
30+
* @param State $state The object that has information about the state of the system
31+
* @param array $configs The list of paths of form elements in the structure
32+
*/
33+
public function __construct(State $state, array $configs = [])
34+
{
35+
$this->state = $state;
36+
$this->configs = $configs;
37+
}
38+
39+
/**
40+
* @inheritdoc
41+
*/
42+
public function isHidden($path)
43+
{
44+
$path = $this->normalizePath($path);
45+
return $this->state->getMode() === State::MODE_PRODUCTION
46+
&& !empty($this->configs[$path])
47+
&& $this->configs[$path] === static::HIDDEN;
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function isDisabled($path)
54+
{
55+
$path = $this->normalizePath($path);
56+
if ($this->state->getMode() === State::MODE_PRODUCTION) {
57+
while (true) {
58+
if (!empty($this->configs[$path])) {
59+
return $this->configs[$path] === static::DISABLED;
60+
}
61+
62+
$position = strripos($path, '/');
63+
if ($position === false) {
64+
break;
65+
}
66+
$path = substr($path, 0, $position);
67+
}
68+
}
69+
70+
return false;
71+
}
72+
73+
/**
74+
* Returns normalized path.
75+
*
76+
* @param string $path The path will be normalized
77+
* @return string The normalized path
78+
*/
79+
private function normalizePath($path)
80+
{
81+
return trim($path ,'/');
82+
}
83+
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,34 @@
88
use Magento\Framework\Exception\ConfigurationMismatchException;
99

1010
/**
11-
* Contains list of classes which implement ElementVisibilityInterface for checking of visibility of form elements.
11+
* Contains list of classes which implement ElementVisibilityInterface for
12+
* checking of visibility of form elements on Configuration page.
1213
*/
13-
class ElementVisibility implements ElementVisibilityInterface
14+
class ElementVisibilityComposite implements ElementVisibilityInterface
1415
{
1516
/**
17+
* List of objects which implements ElementVisibilityInterface for
18+
* checking of visibility of form elements on Configuration page.
19+
*
1620
* @var ElementVisibilityInterface[]
1721
*/
1822
private $visibility = [];
1923

2024
/**
21-
* @param ElementVisibilityInterface[] $visibility
22-
* @throws ConfigurationMismatchException
25+
* @param ElementVisibilityInterface[] $visibility List of objects which implement ElementVisibilityInterface.
26+
* @throws ConfigurationMismatchException It is thrown if some object from list $visibility
27+
* implements the wrong interface.
2328
*/
2429
public function __construct(array $visibility = [])
2530
{
2631
foreach ($visibility as $name => $item) {
2732
if (!$item instanceof ElementVisibilityInterface) {
2833
throw new ConfigurationMismatchException(
29-
__('%1 is not instance on %2', $name, ElementVisibilityInterface::class)
34+
__(
35+
'%1: Instance of %2 is expected, got %3 instead',
36+
$name,
37+
ElementVisibilityInterface::class, get_class($item)
38+
)
3039
);
3140
}
3241
}
@@ -35,7 +44,7 @@ public function __construct(array $visibility = [])
3544
}
3645

3746
/**
38-
* {@inheritdoc}
47+
* @inheritdoc
3948
*/
4049
public function isHidden($path)
4150
{
@@ -49,7 +58,7 @@ public function isHidden($path)
4958
}
5059

5160
/**
52-
* {@inheritdoc}
61+
* @inheritdoc
5362
*/
5463
public function isDisabled($path)
5564
{

app/code/Magento/Config/Model/Config/Structure/ElementVisibilityInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Magento\Config\Model\Config\Structure;
77

88
/**
9-
* Checks visibility of form elements by their paths in the structure.
9+
* Checks visibility of form elements on Configuration page by their paths in the structure.
1010
*/
1111
interface ElementVisibilityInterface
1212
{
@@ -20,15 +20,15 @@ interface ElementVisibilityInterface
2020
/**
2121
* Check whether form element is disabled by path.
2222
*
23-
* @param string $path
23+
* @param string $path The path of form element in the structure
2424
* @return bool
2525
*/
2626
public function isDisabled($path);
2727

2828
/**
2929
* Check whether form element is hidden from form by path.
3030
*
31-
* @param string $path
31+
* @param string $path The path of form element in the structure
3232
* @return bool
3333
*/
3434
public function isHidden($path);

app/code/Magento/Config/Model/Config/Structure/ProductionVisibility.php

Lines changed: 0 additions & 54 deletions
This file was deleted.

app/code/Magento/Config/Test/Unit/Model/Config/Structure/ProductionVisibilityTest.php renamed to app/code/Magento/Config/Test/Unit/Model/Config/Structure/ConcealInProductionConfigListTest.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
*/
66
namespace Magento\Config\Test\Unit\Model\Config\Structure;
77

8-
use Magento\Config\Model\Config\Structure\ProductionVisibility;
8+
use Magento\Config\Model\Config\Structure\ConcealInProductionConfigList;
99
use Magento\Framework\App\State;
1010

11-
class ProductionVisibilityTest extends \PHPUnit_Framework_TestCase
11+
class ConcealInProductionConfigListTest extends \PHPUnit_Framework_TestCase
1212
{
1313
/**
1414
* @var State|\PHPUnit_Framework_MockObject_MockObject
1515
*/
1616
private $stateMock;
1717

1818
/**
19-
* @var ProductionVisibility
19+
* @var ConcealInProductionConfigList
2020
*/
2121
private $model;
2222

@@ -27,11 +27,15 @@ protected function setUp()
2727
->getMock();
2828

2929
$configs = [
30-
'first/path' => ProductionVisibility::DISABLED,
31-
'second/path' => ProductionVisibility::HIDDEN,
30+
'first/path' => ConcealInProductionConfigList::DISABLED,
31+
'second/path' => ConcealInProductionConfigList::HIDDEN,
32+
'third' => ConcealInProductionConfigList::DISABLED,
33+
'third/path' => 'no',
34+
'third/path/field' => ConcealInProductionConfigList::DISABLED,
35+
'first/path/field' => 'no',
3236
];
3337

34-
$this->model = new ProductionVisibility($this->stateMock, $configs);
38+
$this->model = new ConcealInProductionConfigList($this->stateMock, $configs);
3539
}
3640

3741
/**
@@ -55,9 +59,17 @@ public function disabledDataProvider()
5559
{
5660
return [
5761
['first/path', State::MODE_PRODUCTION, true],
62+
['first/path/field', State::MODE_PRODUCTION, false],
63+
['first/path/field2', State::MODE_PRODUCTION, true],
5864
['first/path', State::MODE_DEFAULT, false],
5965
['some/path', State::MODE_PRODUCTION, false],
6066
['second/path', State::MODE_PRODUCTION, false],
67+
['third', State::MODE_PRODUCTION, true],
68+
['third/path2', State::MODE_PRODUCTION, true],
69+
['third/path2/field', State::MODE_PRODUCTION, true],
70+
['third/path', State::MODE_PRODUCTION, false],
71+
['third/path/field', State::MODE_PRODUCTION, true],
72+
['third/path/field2', State::MODE_PRODUCTION, false],
6173
];
6274
}
6375

app/code/Magento/Config/Test/Unit/Model/Config/Structure/ElementVisibilityTest.php renamed to app/code/Magento/Config/Test/Unit/Model/Config/Structure/ElementVisibilityCompositeTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
*/
66
namespace Magento\Config\Test\Unit\Model\Config\Structure;
77

8-
use Magento\Config\Model\Config\Structure\ElementVisibility;
8+
use Magento\Config\Model\Config\Structure\ElementVisibilityComposite;
99
use Magento\Config\Model\Config\Structure\ElementVisibilityInterface;
1010

11-
class ElementVisibilityTest extends \PHPUnit_Framework_TestCase
11+
class ElementVisibilityCompositeTest extends \PHPUnit_Framework_TestCase
1212
{
1313
/**
14-
* @var ElementVisibility
14+
* @var ElementVisibilityComposite
1515
*/
1616
private $model;
1717

@@ -32,13 +32,13 @@ protected function setUp()
3232
$this->secondVisibilityMock = $this->getMockBuilder(ElementVisibilityInterface::class)
3333
->getMockForAbstractClass();
3434

35-
$this->model = new ElementVisibility([$this->firstVisibilityMock, $this->secondVisibilityMock]);
35+
$this->model = new ElementVisibilityComposite([$this->firstVisibilityMock, $this->secondVisibilityMock]);
3636
}
3737

3838
/**
3939
* @expectedException \Magento\Framework\Exception\ConfigurationMismatchException
4040
* @codingStandardsIgnoreStart
41-
* @expectedExceptionMessage stdClass is not instance on Magento\Config\Model\Config\Structure\ElementVisibilityInterface
41+
* @expectedExceptionMessage stdClass: Instance of Magento\Config\Model\Config\Structure\ElementVisibilityInterface is expected, got stdClass instead
4242
* @codingStandardsIgnoreEnd
4343
*/
4444
public function testException()
@@ -47,7 +47,7 @@ public function testException()
4747
'stdClass' => new \StdClass()
4848
];
4949

50-
new ElementVisibility($visibility);
50+
new ElementVisibilityComposite($visibility);
5151
}
5252

5353
/**

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\Config\Model\Config\Structure\SearchInterface" type="Magento\Config\Model\Config\Structure" />
1010
<preference for="Magento\Config\Model\Config\Backend\File\RequestData\RequestDataInterface" type="Magento\Config\Model\Config\Backend\File\RequestData" />
11-
<preference for="Magento\Config\Model\Config\Structure\ElementVisibilityInterface" type="Magento\Config\Model\Config\Structure\ElementVisibility" />
11+
<preference for="Magento\Config\Model\Config\Structure\ElementVisibilityInterface" type="Magento\Config\Model\Config\Structure\ElementVisibilityComposite" />
1212
<type name="Magento\Config\Model\Config\Structure\Element\Iterator\Tab" shared="false" />
1313
<type name="Magento\Config\Model\Config\Structure\Element\Iterator\Section" shared="false" />
14+
<type name="Magento\Config\Model\Config\Structure\ElementVisibilityComposite">
15+
<arguments>
16+
<argument name="visibility" xsi:type="array">
17+
<item name="productionVisibility" xsi:type="object">Magento\Config\Model\Config\Structure\ConcealInProductionConfigList</item>
18+
</argument>
19+
</arguments>
20+
</type>
1421
</config>

0 commit comments

Comments
 (0)