5
5
*/
6
6
declare (strict_types=1 );
7
7
8
- namespace Magento \Theme \Test \Unit \Plugin ;
8
+ namespace Magento \Theme \Test \Unit \Model \ Config ;
9
9
10
- use Magento \Config \Model \Config \PathValidator ;
11
- use Magento \Config \Model \Config \Structure ;
12
- use Magento \Config \Model \Config \Structure \Element \Field ;
13
10
use Magento \Framework \DataObject ;
14
- use Magento \Framework \ Exception \ ValidatorException ;
11
+ use Magento \Theme \ Api \ Data \ DesignConfigDataInterface ;
15
12
use Magento \Theme \Api \Data \DesignConfigInterface ;
16
- use Magento \Theme \Model \DesignConfigRepository ;
17
- use Magento \Theme \Plugin \DesignPathValidatorPlugin ;
18
- use PHPUnit \Framework \MockObject \Exception ;
19
13
use PHPUnit \Framework \MockObject \MockObject ;
20
14
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 ;
21
19
22
- class DesignPathValidatorPluginTest extends TestCase
20
+ class PathValidatorTest extends TestCase
23
21
{
24
22
/**
25
23
* @var Structure|MockObject
@@ -32,86 +30,99 @@ class DesignPathValidatorPluginTest extends TestCase
32
30
private $ designConfigRepository ;
33
31
34
32
/**
35
- * @var DesignPathValidatorPlugin
33
+ * @var PathValidator
36
34
*/
37
- private $ plugin ;
35
+ private $ pathValidator ;
38
36
39
37
protected function setUp (): void
40
38
{
41
39
$ this ->structure = $ this ->createMock (Structure::class);
42
40
$ 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
+ );
44
46
}
45
47
46
- /**
47
- * @return void
48
- * @throws ValidatorException
49
- * @throws Exception
50
- */
51
- public function testAroundValidateWithValidPath ()
48
+ public function testValidateNonDesignPath ()
52
49
{
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 ' ]);
58
59
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);
60
68
$ designConfig = $ this ->createMock (DesignConfigInterface::class);
61
69
$ extensionAttributes = $ this ->createMock (DataObject::class);
70
+ $ designConfigData = $ this ->createMock (DesignConfigDataInterface::class);
62
71
63
- $ field ->expects ($ this ->exactly (2 ))
72
+ $ element ->expects ($ this ->exactly (2 ))
64
73
->method ('getConfigPath ' )
65
74
->willReturn ($ path );
66
75
$ this ->structure ->expects ($ this ->once ())
67
76
->method ('getElementByConfigPath ' )
68
77
->with ($ path )
69
- ->willReturn ($ field );
78
+ ->willReturn ($ element );
70
79
$ this ->structure ->expects ($ this ->once ())
71
80
->method ('getFieldPaths ' )
72
- ->willReturn ([$ path => $ path ]);
81
+ ->willReturn ([]);
82
+ $ this ->designConfigRepository ->expects ($ this ->once ())
83
+ ->method ('getByScope ' )
84
+ ->with ('default ' , null )
85
+ ->willReturn ($ designConfig );
73
86
$ designConfig ->expects ($ this ->once ())
74
87
->method ('getExtensionAttributes ' )
75
88
->willReturn ($ extensionAttributes );
76
89
$ extensionAttributes ->expects ($ this ->once ())
77
90
->method ('__call ' )
78
91
->with (
79
92
$ 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 ]);
85
97
86
- $ result = $ this ->plugin -> aroundValidate ( $ pathValidator , $ proceed , $ path );
98
+ $ result = $ this ->pathValidator -> validate ( $ path );
87
99
$ this ->assertTrue ($ result );
88
100
}
89
101
90
- /**
91
- * @return void
92
- * @throws Exception
93
- * @throws ValidatorException
94
- */
95
- public function testAroundValidateWithInvalidPath ()
102
+ public function testValidateDesignPathThrowsException ()
96
103
{
97
104
$ this ->expectException (ValidatorException::class);
105
+ $ this ->expectExceptionMessage ('The "design/invalid_path" path doesn \'t exist. Verify and try again. ' );
98
106
99
- $ pathValidator = $ this ->createMock (PathValidator::class);
107
+ $ path = 'design/invalid_path ' ;
108
+ $ element = $ this ->createMock (Structure \Element \Field::class);
100
109
$ designConfig = $ this ->createMock (DesignConfigInterface::class);
101
110
$ extensionAttributes = $ this ->createMock (DataObject::class);
102
111
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 );
108
115
$ this ->structure ->expects ($ this ->once ())
109
116
->method ('getElementByConfigPath ' )
110
117
->with ($ path )
111
- ->willReturn (null );
118
+ ->willReturn ($ element );
112
119
$ this ->structure ->expects ($ this ->once ())
113
120
->method ('getFieldPaths ' )
114
121
->willReturn ([]);
122
+ $ this ->designConfigRepository ->expects ($ this ->once ())
123
+ ->method ('getByScope ' )
124
+ ->with ('default ' , null )
125
+ ->willReturn ($ designConfig );
115
126
$ designConfig ->expects ($ this ->once ())
116
127
->method ('getExtensionAttributes ' )
117
128
->willReturn ($ extensionAttributes );
@@ -120,11 +131,7 @@ public function testAroundValidateWithInvalidPath()
120
131
->with (
121
132
$ this ->equalTo ('getDesignConfigData ' )
122
133
)->willReturn ([]);
123
- $ this ->designConfigRepository ->expects ($ this ->once ())
124
- ->method ('getByScope ' )
125
- ->with ('default ' , null )
126
- ->willReturn ($ designConfig );
127
134
128
- $ this ->plugin -> aroundValidate ( $ pathValidator , $ proceed , $ path );
135
+ $ this ->pathValidator -> validate ( $ path );
129
136
}
130
137
}
0 commit comments