@@ -47,6 +47,9 @@ class ProductScopeRewriteGeneratorTest extends \PHPUnit\Framework\TestCase
47
47
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
48
48
private $ serializer ;
49
49
50
+ /** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
51
+ private $ categoryMock ;
52
+
50
53
public function setUp ()
51
54
{
52
55
$ this ->serializer = $ this ->createMock (\Magento \Framework \Serialize \Serializer \Json::class);
@@ -83,6 +86,10 @@ function ($value) {
83
86
$ this ->storeViewService = $ this ->getMockBuilder (\Magento \CatalogUrlRewrite \Service \V1 \StoreViewService::class)
84
87
->disableOriginalConstructor ()->getMock ();
85
88
$ this ->storeManager = $ this ->createMock (StoreManagerInterface::class);
89
+ $ storeRootCategoryId = 2 ;
90
+ $ store = $ this ->getMockBuilder (\Magento \Store \Model \Store::class)->disableOriginalConstructor ()->getMock ();
91
+ $ store ->expects ($ this ->any ())->method ('getRootCategoryId ' )->will ($ this ->returnValue ($ storeRootCategoryId ));
92
+ $ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )->will ($ this ->returnValue ($ store ));
86
93
$ mergeDataProviderFactory = $ this ->createPartialMock (
87
94
\Magento \UrlRewrite \Model \MergeDataProviderFactory::class,
88
95
['create ' ]
@@ -103,6 +110,7 @@ function ($value) {
103
110
'mergeDataProviderFactory ' => $ mergeDataProviderFactory
104
111
]
105
112
);
113
+ $ this ->categoryMock = $ this ->getMockBuilder (Category::class)->disableOriginalConstructor ()->getMock ();
106
114
}
107
115
108
116
public function testGenerationForGlobalScope ()
@@ -112,12 +120,6 @@ public function testGenerationForGlobalScope()
112
120
$ product ->expects ($ this ->any ())->method ('getStoreIds ' )->will ($ this ->returnValue ([1 ]));
113
121
$ this ->storeViewService ->expects ($ this ->once ())->method ('doesEntityHaveOverriddenUrlKeyForStore ' )
114
122
->will ($ this ->returnValue (false ));
115
- $ categoryMock = $ this ->getMockBuilder (Category::class)
116
- ->disableOriginalConstructor ()
117
- ->getMock ();
118
- $ categoryMock ->expects ($ this ->once ())
119
- ->method ('getParentId ' )
120
- ->willReturn (1 );
121
123
$ this ->initObjectRegistryFactory ([]);
122
124
$ canonical = new \Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ([], $ this ->serializer );
123
125
$ canonical ->setRequestPath ('category-1 ' )
@@ -149,25 +151,21 @@ public function testGenerationForGlobalScope()
149
151
'category-3_3 ' => $ current ,
150
152
'category-4_4 ' => $ anchorCategories
151
153
],
152
- $ this ->productScopeGenerator ->generateForGlobalScope ([$ categoryMock ], $ product , 1 )
154
+ $ this ->productScopeGenerator ->generateForGlobalScope ([$ this -> categoryMock ], $ product , 1 )
153
155
);
154
156
}
155
157
156
158
public function testGenerationForSpecificStore ()
157
159
{
160
+ $ storeRootCategoryId = 2 ;
161
+ $ category_id = 4 ;
158
162
$ product = $ this ->createMock (\Magento \Catalog \Model \Product::class);
159
163
$ product ->expects ($ this ->any ())->method ('getStoreId ' )->will ($ this ->returnValue (1 ));
160
164
$ product ->expects ($ this ->never ())->method ('getStoreIds ' );
161
- $ storeRootCategoryId = 'root-for-store-id ' ;
162
- $ category = $ this ->createMock (\Magento \Catalog \Model \Category::class);
163
- $ category ->expects ($ this ->any ())->method ('getParentIds ' )
165
+ $ this ->categoryMock ->expects ($ this ->any ())->method ('getParentIds ' )
164
166
->will ($ this ->returnValue (['root-id ' , $ storeRootCategoryId ]));
165
- $ category ->expects ($ this ->any ())->method ('getParentId ' )->will ($ this ->returnValue ('parent_id ' ));
166
- $ category ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ('category_id ' ));
167
- $ store = $ this ->getMockBuilder (\Magento \Store \Model \Store::class)->disableOriginalConstructor ()->getMock ();
168
- $ store ->expects ($ this ->any ())->method ('getRootCategoryId ' )->will ($ this ->returnValue ($ storeRootCategoryId ));
169
- $ this ->storeManager ->expects ($ this ->any ())->method ('getStore ' )->will ($ this ->returnValue ($ store ));
170
- $ this ->initObjectRegistryFactory ([$ category ]);
167
+ $ this ->categoryMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ($ category_id ));
168
+ $ this ->initObjectRegistryFactory ([$ this ->categoryMock ]);
171
169
$ canonical = new \Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ([], $ this ->serializer );
172
170
$ canonical ->setRequestPath ('category-1 ' )
173
171
->setStoreId (1 );
@@ -184,7 +182,7 @@ public function testGenerationForSpecificStore()
184
182
185
183
$ this ->assertEquals (
186
184
['category-1_1 ' => $ canonical ],
187
- $ this ->productScopeGenerator ->generateForSpecificStoreView (1 , [$ category ], $ product , 1 )
185
+ $ this ->productScopeGenerator ->generateForSpecificStoreView (1 , [$ this -> categoryMock ], $ product , 1 )
188
186
);
189
187
}
190
188
@@ -212,4 +210,40 @@ protected function initObjectRegistryFactory($entities)
212
210
->with (['entities ' => $ entities ])
213
211
->will ($ this ->returnValue ($ objectRegistry ));
214
212
}
213
+
214
+ /**
215
+ * Test the possibility of url rewrite generation.
216
+ *
217
+ * @param array $parentIds
218
+ * @param bool $expectedResult
219
+ * @dataProvider isCategoryProperForGeneratingDataProvider
220
+ */
221
+ public function testIsCategoryProperForGenerating ($ parentIds , $ expectedResult )
222
+ {
223
+ $ storeId = 1 ;
224
+ $ this ->categoryMock ->expects (self ::any ())->method ('getParentIds ' )->willReturn ($ parentIds );
225
+ $ result = $ this ->productScopeGenerator ->isCategoryProperForGenerating (
226
+ $ this ->categoryMock ,
227
+ $ storeId
228
+ );
229
+ self ::assertEquals (
230
+ $ expectedResult ,
231
+ $ result
232
+ );
233
+ }
234
+
235
+ /**
236
+ * Data provider for testIsCategoryProperForGenerating.
237
+ *
238
+ * @return array
239
+ */
240
+ public function isCategoryProperForGeneratingDataProvider ()
241
+ {
242
+ return [
243
+ [['0 ' ], false ],
244
+ [['1 ' ], false ],
245
+ [['1 ' , '2 ' ], true ],
246
+ [['1 ' , '3 ' ], false ],
247
+ ];
248
+ }
215
249
}
0 commit comments