@@ -24,6 +24,16 @@ class CategoryUrlPathAutogeneratorObserverTest extends \PHPUnit_Framework_TestCa
24
24
/** @var \PHPUnit_Framework_MockObject_MockObject */
25
25
protected $ category ;
26
26
27
+ /**
28
+ * @var \Magento\CatalogUrlRewrite\Service\V1\StoreViewService|\PHPUnit_Framework_MockObject_MockObject
29
+ */
30
+ protected $ storeViewService ;
31
+
32
+ /**
33
+ * @var \Magento\Catalog\Model\ResourceModel\Category|\PHPUnit_Framework_MockObject_MockObject
34
+ */
35
+ protected $ categoryResource ;
36
+
27
37
protected function setUp ()
28
38
{
29
39
$ this ->observer = $ this ->getMock (
@@ -33,13 +43,15 @@ protected function setUp()
33
43
'' ,
34
44
false
35
45
);
46
+ $ this ->categoryResource = $ this ->getMock ('Magento\Catalog\Model\ResourceModel\Category ' , [], [], '' , false );
36
47
$ this ->category = $ this ->getMock (
37
48
'Magento\Catalog\Model\Category ' ,
38
- ['setUrlKey ' , 'setUrlPath ' , 'dataHasChangedFor ' , 'isObjectNew ' , 'getResource ' , 'getUrlKey ' ],
49
+ ['setUrlKey ' , 'setUrlPath ' , 'dataHasChangedFor ' , 'isObjectNew ' , 'getResource ' , 'getUrlKey ' , ' getStoreId ' ],
39
50
[],
40
51
'' ,
41
52
false
42
53
);
54
+ $ this ->category ->expects ($ this ->any ())->method ('getResource ' )->willReturn ($ this ->categoryResource );
43
55
$ this ->observer ->expects ($ this ->any ())->method ('getEvent ' )->willReturnSelf ();
44
56
$ this ->observer ->expects ($ this ->any ())->method ('getCategory ' )->willReturn ($ this ->category );
45
57
$ this ->categoryUrlPathGenerator = $ this ->getMock (
@@ -53,19 +65,23 @@ protected function setUp()
53
65
'Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider '
54
66
);
55
67
68
+ $ this ->storeViewService = $ this ->getMock ('Magento\CatalogUrlRewrite\Service\V1\StoreViewService ' , [], [], '' ,
69
+ false );
70
+
56
71
$ this ->categoryUrlPathAutogeneratorObserver = (new ObjectManagerHelper ($ this ))->getObject (
57
72
'Magento\CatalogUrlRewrite\Observer\CategoryUrlPathAutogeneratorObserver ' ,
58
73
[
59
74
'categoryUrlPathGenerator ' => $ this ->categoryUrlPathGenerator ,
60
- 'childrenCategoriesProvider ' => $ this ->childrenCategoriesProvider
75
+ 'childrenCategoriesProvider ' => $ this ->childrenCategoriesProvider ,
76
+ 'storeViewService ' => $ this ->storeViewService ,
61
77
]
62
78
);
63
79
}
64
80
65
81
public function testSetCategoryUrlAndCategoryPath ()
66
82
{
67
83
$ this ->category ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn ('category ' );
68
- $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('generateUrlKey ' )->willReturn ('urk_key ' );
84
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn ('urk_key ' );
69
85
$ this ->category ->expects ($ this ->once ())->method ('setUrlKey ' )->with ('urk_key ' )->willReturnSelf ();
70
86
$ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlPath ' )->willReturn ('url_path ' );
71
87
$ this ->category ->expects ($ this ->once ())->method ('setUrlPath ' )->with ('url_path ' )->willReturnSelf ();
@@ -74,38 +90,111 @@ public function testSetCategoryUrlAndCategoryPath()
74
90
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
75
91
}
76
92
77
- public function testExecuteWithoutGeneration ()
93
+ public function testExecuteWithoutUrlKeyAndUrlPathUpdating ()
78
94
{
79
95
$ this ->category ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn (false );
80
96
$ this ->category ->expects ($ this ->never ())->method ('setUrlKey ' );
81
97
$ this ->category ->expects ($ this ->never ())->method ('setUrlPath ' );
82
98
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
83
99
}
84
100
85
- public function testUpdateUrlPathForChildren ()
101
+ public function testUrlKeyAndUrlPathUpdating ()
86
102
{
87
- $ this ->category ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn ('category ' );
88
- $ this ->category ->expects ($ this ->once ())->method ('setUrlKey ' )->willReturnSelf ();
89
- $ this ->category ->expects ($ this ->once ())->method ('setUrlPath ' )->willReturnSelf ();
103
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlKey ' )->with ($ this ->category )
104
+ ->willReturn ('url_key ' );
105
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->once ())->method ('getUrlPath ' )->with ($ this ->category )
106
+ ->willReturn ('url_path ' );
107
+
108
+ $ this ->category ->expects ($ this ->once ())->method ('getUrlKey ' )->willReturn ('not_formatted_url_key ' );
109
+ $ this ->category ->expects ($ this ->once ())->method ('setUrlKey ' )->with ('url_key ' )->willReturnSelf ();
110
+ $ this ->category ->expects ($ this ->once ())->method ('setUrlPath ' )->with ('url_path ' )->willReturnSelf ();
111
+ // break code execution
112
+ $ this ->category ->expects ($ this ->once ())->method ('isObjectNew ' )->willReturn (true );
113
+
114
+ $ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
115
+ }
116
+
117
+ public function testUrlPathAttributeNoUpdatingIfCategoryIsNew ()
118
+ {
119
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('url_key ' );
120
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('url_path ' );
121
+
122
+ $ this ->category ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('not_formatted_url_key ' );
123
+ $ this ->category ->expects ($ this ->any ())->method ('setUrlKey ' )->willReturnSelf ();
124
+ $ this ->category ->expects ($ this ->any ())->method ('setUrlPath ' )->willReturnSelf ();
125
+
126
+ $ this ->category ->expects ($ this ->once ())->method ('isObjectNew ' )->willReturn (true );
127
+ $ this ->categoryResource ->expects ($ this ->never ())->method ('saveAttribute ' );
128
+
129
+ $ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
130
+ }
131
+
132
+ public function testUrlPathAttributeUpdating ()
133
+ {
134
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('url_key ' );
135
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('url_path ' );
136
+
137
+ $ this ->category ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('not_formatted_url_key ' );
138
+ $ this ->category ->expects ($ this ->any ())->method ('setUrlKey ' )->willReturnSelf ();
139
+ $ this ->category ->expects ($ this ->any ())->method ('setUrlPath ' )->willReturnSelf ();
90
140
$ this ->category ->expects ($ this ->once ())->method ('isObjectNew ' )->willReturn (false );
91
- $ this ->category ->expects ($ this ->once ())->method ('dataHasChangedFor ' )->with ('url_path ' )->willReturn (true );
92
- $ categoryResource = $ this ->getMockBuilder ('Magento\Catalog\Model\ResourceModel\Category ' )
93
- ->disableOriginalConstructor ()->getMock ();
94
- $ this ->category ->expects ($ this ->once ())->method ('getResource ' )->willReturn ($ categoryResource );
95
- $ categoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ this ->category , 'url_path ' );
96
141
142
+ $ this ->categoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ this ->category , 'url_path ' );
143
+
144
+ // break code execution
145
+ $ this ->category ->expects ($ this ->once ())->method ('dataHasChangedFor ' )->with ('url_path ' )->willReturn (false );
146
+
147
+ $ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
148
+ }
149
+
150
+ public function testChildrenUrlPathAttributeNoUpdatingIfParentUrlPathIsNotChanged ()
151
+ {
152
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('url_key ' );
153
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('url_path ' );
154
+
155
+ $ this ->categoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ this ->category , 'url_path ' );
156
+
157
+ $ this ->category ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('not_formatted_url_key ' );
158
+ $ this ->category ->expects ($ this ->any ())->method ('setUrlKey ' )->willReturnSelf ();
159
+ $ this ->category ->expects ($ this ->any ())->method ('setUrlPath ' )->willReturnSelf ();
160
+ $ this ->category ->expects ($ this ->once ())->method ('isObjectNew ' )->willReturn (false );
161
+ // break code execution
162
+ $ this ->category ->expects ($ this ->once ())->method ('dataHasChangedFor ' )->with ('url_path ' )->willReturn (false );
163
+
164
+ $ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
165
+ }
166
+
167
+ public function testChildrenUrlPathAttributeUpdatingForSpecificStore ()
168
+ {
169
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('generated_url_key ' );
170
+ $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('generated_url_path ' );
171
+
172
+ $ this ->category ->expects ($ this ->any ())->method ('getUrlKey ' )->willReturn ('not_formatted_url_key ' );
173
+ $ this ->category ->expects ($ this ->any ())->method ('setUrlKey ' )->willReturnSelf ();
174
+ $ this ->category ->expects ($ this ->any ())->method ('setUrlPath ' )->willReturnSelf ();
175
+ $ this ->category ->expects ($ this ->any ())->method ('isObjectNew ' )->willReturn (false );
176
+ $ this ->category ->expects ($ this ->any ())->method ('dataHasChangedFor ' )->willReturn (true );
177
+ // only for specific store
178
+ $ this ->category ->expects ($ this ->atLeastOnce ())->method ('getStoreId ' )->willReturn (1 );
179
+
180
+ $ childCategoryResource = $ this ->getMockBuilder ('Magento\Catalog\Model\ResourceModel\Category ' )
181
+ ->disableOriginalConstructor ()->getMock ();
97
182
$ childCategory = $ this ->getMockBuilder ('Magento\Catalog\Model\Category ' )
98
- ->setMethods (['getUrlPath ' , 'setUrlPath ' , 'getResource ' , 'unsUrlPath ' ])
183
+ ->setMethods ([
184
+ 'getUrlPath ' ,
185
+ 'setUrlPath ' ,
186
+ 'getResource ' ,
187
+ 'getStore ' ,
188
+ 'getStoreId ' ,
189
+ 'setStoreId '
190
+ ])
99
191
->disableOriginalConstructor ()->getMock ();
192
+ $ childCategory ->expects ($ this ->any ())->method ('getResource ' )->willReturn ($ childCategoryResource );
193
+ $ childCategory ->expects ($ this ->once ())->method ('setStoreId ' )->with (1 );
100
194
101
195
$ this ->childrenCategoriesProvider ->expects ($ this ->once ())->method ('getChildren ' )->willReturn ([$ childCategory ]);
102
- $ childCategoryResource = $ this ->getMockBuilder ('Magento\Catalog\Model\ResourceModel\Category ' )
103
- ->disableOriginalConstructor ()->getMock ();
104
- $ childCategory ->expects ($ this ->once ())->method ('unsUrlPath ' )->willReturnSelf ();
105
- $ childCategory ->expects ($ this ->once ())->method ('getResource ' )->willReturn ($ childCategoryResource );
196
+ $ childCategory ->expects ($ this ->once ())->method ('setUrlPath ' )->with ('generated_url_path ' )->willReturnSelf ();
106
197
$ childCategoryResource ->expects ($ this ->once ())->method ('saveAttribute ' )->with ($ childCategory , 'url_path ' );
107
- $ childCategory ->expects ($ this ->once ())->method ('setUrlPath ' )->with ('category-url_path ' )->willReturnSelf ();
108
- $ this ->categoryUrlPathGenerator ->expects ($ this ->any ())->method ('getUrlPath ' )->willReturn ('category-url_path ' );
109
198
110
199
$ this ->categoryUrlPathAutogeneratorObserver ->execute ($ this ->observer );
111
200
}
0 commit comments