Skip to content

Commit 282b878

Browse files
committed
Merge branch 'ACP2E-3324-new' of https://github.com/adobe-commerce-tier-4/magento2ce into ACP2E-3324
2 parents 2124104 + 734db2a commit 282b878

File tree

4 files changed

+92
-80
lines changed

4 files changed

+92
-80
lines changed

app/code/Magento/Theme/Plugin/DesignPathValidatorPlugin.php renamed to app/code/Magento/Theme/Model/Config/PathValidator.php

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\Theme\Plugin;
8+
namespace Magento\Theme\Model\Config;
99

10-
use Magento\Config\Model\Config\PathValidator;
1110
use Magento\Config\Model\Config\Structure;
12-
use Magento\Config\Model\Config\Structure\Element\Field;
1311
use Magento\Framework\Exception\ValidatorException;
1412
use Magento\Theme\Model\DesignConfigRepository;
1513

16-
class DesignPathValidatorPlugin
14+
class PathValidator extends \Magento\Config\Model\Config\PathValidator
1715
{
1816
/**
1917
* @param Structure $structure
@@ -23,35 +21,19 @@ public function __construct(
2321
private readonly Structure $structure,
2422
private readonly DesignConfigRepository $designConfigRepository
2523
) {
24+
parent::__construct($structure);
2625
}
2726

2827
/**
29-
* Allow setting design configuration from cli
30-
*
31-
* @param PathValidator $subject
32-
* @param callable $proceed
33-
* @param string $path
34-
* @return true
35-
* @throws ValidatorException
36-
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
28+
* @inheritdoc
3729
*/
38-
public function aroundValidate(PathValidator $subject, callable $proceed, $path): bool
30+
public function validate($path)
3931
{
4032
if (stripos($path, 'design/') !== 0) {
41-
return $proceed($path);
42-
}
43-
44-
$element = $this->structure->getElementByConfigPath($path);
45-
if ($element instanceof Field && $element->getConfigPath()) {
46-
$path = $element->getConfigPath();
33+
return parent::validate($path);
4734
}
4835

49-
$allPaths = array_merge($this->structure->getFieldPaths(), $this->getDesignFieldPaths());
50-
51-
if (!array_key_exists($path, $allPaths)) {
52-
throw new ValidatorException(__('The "%1" path doesn\'t exist. Verify and try again.', $path));
53-
}
54-
return true;
36+
return $this->validateDesignPath($path);
5537
}
5638

5739
/**
@@ -69,4 +51,26 @@ private function getDesignFieldPaths(): array
6951
}
7052
return $data;
7153
}
54+
55+
/**
56+
* Validate design path configurations
57+
*
58+
* @param string $path
59+
* @return bool
60+
* @throws ValidatorException
61+
*/
62+
private function validateDesignPath(string $path): bool
63+
{
64+
$element = $this->structure->getElementByConfigPath($path);
65+
if ($element instanceof Structure\Element\Field && $element->getConfigPath()) {
66+
$path = $element->getConfigPath();
67+
}
68+
69+
$allPaths = array_merge($this->structure->getFieldPaths(), $this->getDesignFieldPaths());
70+
71+
if (!array_key_exists($path, $allPaths)) {
72+
throw new ValidatorException(__('The "%1" path doesn\'t exist. Verify and try again.', $path));
73+
}
74+
return true;
75+
}
7276
}

app/code/Magento/Theme/Test/Unit/Plugin/DesignPathValidatorPluginTest.php renamed to app/code/Magento/Theme/Test/Unit/Model/Config/PathValidatorTest.php

Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\Theme\Test\Unit\Plugin;
8+
namespace Magento\Theme\Test\Unit\Model\Config;
99

10-
use Magento\Config\Model\Config\PathValidator;
11-
use Magento\Config\Model\Config\Structure;
12-
use Magento\Config\Model\Config\Structure\Element\Field;
1310
use Magento\Framework\DataObject;
14-
use Magento\Framework\Exception\ValidatorException;
11+
use Magento\Theme\Api\Data\DesignConfigDataInterface;
1512
use Magento\Theme\Api\Data\DesignConfigInterface;
16-
use Magento\Theme\Model\DesignConfigRepository;
17-
use Magento\Theme\Plugin\DesignPathValidatorPlugin;
18-
use PHPUnit\Framework\MockObject\Exception;
1913
use PHPUnit\Framework\MockObject\MockObject;
2014
use PHPUnit\Framework\TestCase;
15+
use Magento\Theme\Model\Config\PathValidator;
16+
use Magento\Config\Model\Config\Structure;
17+
use Magento\Theme\Model\DesignConfigRepository;
18+
use Magento\Framework\Exception\ValidatorException;
2119

22-
class DesignPathValidatorPluginTest extends TestCase
20+
class PathValidatorTest extends TestCase
2321
{
2422
/**
2523
* @var Structure|MockObject
@@ -32,86 +30,99 @@ class DesignPathValidatorPluginTest extends TestCase
3230
private $designConfigRepository;
3331

3432
/**
35-
* @var DesignPathValidatorPlugin
33+
* @var PathValidator
3634
*/
37-
private $plugin;
35+
private $pathValidator;
3836

3937
protected function setUp(): void
4038
{
4139
$this->structure = $this->createMock(Structure::class);
4240
$this->designConfigRepository = $this->createMock(DesignConfigRepository::class);
43-
$this->plugin = new DesignPathValidatorPlugin($this->structure, $this->designConfigRepository);
41+
42+
$this->pathValidator = new PathValidator(
43+
$this->structure,
44+
$this->designConfigRepository
45+
);
4446
}
4547

46-
/**
47-
* @return void
48-
* @throws ValidatorException
49-
* @throws Exception
50-
*/
51-
public function testAroundValidateWithValidPath()
48+
public function testValidateNonDesignPath()
5249
{
53-
$pathValidator = $this->createMock(PathValidator::class);
54-
$proceed = function ($path) {
55-
return true;
56-
};
57-
$path = 'design/header/default_title';
50+
$path = 'non_design/path';
51+
$this->structure->expects($this->once())
52+
->method('getElementByConfigPath')
53+
->with($path)
54+
->willReturn(null);
55+
56+
$this->structure->expects($this->once())
57+
->method('getFieldPaths')
58+
->willReturn(['non_design/path' => 'non_design/path']);
5859

59-
$field = $this->createMock(Field::class);
60+
$result = $this->pathValidator->validate($path);
61+
$this->assertTrue($result);
62+
}
63+
64+
public function testValidateDesignPath()
65+
{
66+
$path = 'design/path';
67+
$element = $this->createMock(Structure\Element\Field::class);
6068
$designConfig = $this->createMock(DesignConfigInterface::class);
6169
$extensionAttributes = $this->createMock(DataObject::class);
70+
$designConfigData = $this->createMock(DesignConfigDataInterface::class);
6271

63-
$field->expects($this->exactly(2))
72+
$element->expects($this->exactly(2))
6473
->method('getConfigPath')
6574
->willReturn($path);
6675
$this->structure->expects($this->once())
6776
->method('getElementByConfigPath')
6877
->with($path)
69-
->willReturn($field);
78+
->willReturn($element);
7079
$this->structure->expects($this->once())
7180
->method('getFieldPaths')
72-
->willReturn([$path => $path]);
81+
->willReturn([]);
82+
$this->designConfigRepository->expects($this->once())
83+
->method('getByScope')
84+
->with('default', null)
85+
->willReturn($designConfig);
7386
$designConfig->expects($this->once())
7487
->method('getExtensionAttributes')
7588
->willReturn($extensionAttributes);
7689
$extensionAttributes->expects($this->once())
7790
->method('__call')
7891
->with(
7992
$this->equalTo('getDesignConfigData')
80-
)->willReturn([]);
81-
$this->designConfigRepository->expects($this->once())
82-
->method('getByScope')
83-
->with('default', null)
84-
->willReturn($designConfig);
93+
)->willReturn([$designConfigData]);
94+
$designConfigData->expects($this->exactly(2))
95+
->method('getFieldConfig')
96+
->willReturn(['path' => $path]);
8597

86-
$result = $this->plugin->aroundValidate($pathValidator, $proceed, $path);
98+
$result = $this->pathValidator->validate($path);
8799
$this->assertTrue($result);
88100
}
89101

90-
/**
91-
* @return void
92-
* @throws Exception
93-
* @throws ValidatorException
94-
*/
95-
public function testAroundValidateWithInvalidPath()
102+
public function testValidateDesignPathThrowsException()
96103
{
97104
$this->expectException(ValidatorException::class);
105+
$this->expectExceptionMessage('The "design/invalid_path" path doesn\'t exist. Verify and try again.');
98106

99-
$pathValidator = $this->createMock(PathValidator::class);
107+
$path = 'design/invalid_path';
108+
$element = $this->createMock(Structure\Element\Field::class);
100109
$designConfig = $this->createMock(DesignConfigInterface::class);
101110
$extensionAttributes = $this->createMock(DataObject::class);
102111

103-
$proceed = function ($path) {
104-
return true;
105-
};
106-
$path = 'design/invalid_path';
107-
112+
$element->expects($this->exactly(2))
113+
->method('getConfigPath')
114+
->willReturn($path);
108115
$this->structure->expects($this->once())
109116
->method('getElementByConfigPath')
110117
->with($path)
111-
->willReturn(null);
118+
->willReturn($element);
112119
$this->structure->expects($this->once())
113120
->method('getFieldPaths')
114121
->willReturn([]);
122+
$this->designConfigRepository->expects($this->once())
123+
->method('getByScope')
124+
->with('default', null)
125+
->willReturn($designConfig);
115126
$designConfig->expects($this->once())
116127
->method('getExtensionAttributes')
117128
->willReturn($extensionAttributes);
@@ -120,11 +131,7 @@ public function testAroundValidateWithInvalidPath()
120131
->with(
121132
$this->equalTo('getDesignConfigData')
122133
)->willReturn([]);
123-
$this->designConfigRepository->expects($this->once())
124-
->method('getByScope')
125-
->with('default', null)
126-
->willReturn($designConfig);
127134

128-
$this->plugin->aroundValidate($pathValidator, $proceed, $path);
135+
$this->pathValidator->validate($path);
129136
}
130137
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,4 @@
4141
</argument>
4242
</arguments>
4343
</type>
44-
<type name="Magento\Config\Model\Config\PathValidator">
45-
<plugin name="validate_design_paths_plugin" type="Magento\Theme\Plugin\DesignPathValidatorPlugin"/>
46-
</type>
4744
</config>

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,5 +334,9 @@
334334
<type name="Magento\Config\Console\Command\LocaleEmulator">
335335
<plugin name="themeForLocaleEmulator" type="Magento\Theme\Plugin\LocaleEmulator"/>
336336
</type>
337-
337+
<type name="Magento\Config\Console\Command\ConfigSet\ProcessorFacade">
338+
<arguments>
339+
<argument name="pathValidator" xsi:type="object">Magento\Theme\Model\Config\PathValidator</argument>
340+
</arguments>
341+
</type>
338342
</config>

0 commit comments

Comments
 (0)