@@ -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,9 @@ 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 ())
123
+ $ this ->categoryMock ->expects ($ this ->exactly (2 ))
119
124
->method ('getParentId ' )
120
- ->willReturn (1 );
125
+ ->willReturn (2 );
121
126
$ this ->initObjectRegistryFactory ([]);
122
127
$ canonical = new \Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ([], $ this ->serializer );
123
128
$ canonical ->setRequestPath ('category-1 ' )
@@ -149,25 +154,23 @@ public function testGenerationForGlobalScope()
149
154
'category-3_3 ' => $ current ,
150
155
'category-4_4 ' => $ anchorCategories
151
156
],
152
- $ this ->productScopeGenerator ->generateForGlobalScope ([$ categoryMock ], $ product , 1 )
157
+ $ this ->productScopeGenerator ->generateForGlobalScope ([$ this -> categoryMock ], $ product , 1 )
153
158
);
154
159
}
155
160
156
161
public function testGenerationForSpecificStore ()
157
162
{
163
+ $ storeRootCategoryId = 2 ;
164
+ $ parent_id = 3 ;
165
+ $ category_id = 4 ;
158
166
$ product = $ this ->createMock (\Magento \Catalog \Model \Product::class);
159
167
$ product ->expects ($ this ->any ())->method ('getStoreId ' )->will ($ this ->returnValue (1 ));
160
168
$ 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 ' )
169
+ $ this ->categoryMock ->expects ($ this ->any ())->method ('getParentIds ' )
164
170
->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 ]);
171
+ $ this ->categoryMock ->expects ($ this ->any ())->method ('getParentId ' )->will ($ this ->returnValue ($ parent_id ));
172
+ $ this ->categoryMock ->expects ($ this ->any ())->method ('getId ' )->will ($ this ->returnValue ($ category_id ));
173
+ $ this ->initObjectRegistryFactory ([$ this ->categoryMock ]);
171
174
$ canonical = new \Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ([], $ this ->serializer );
172
175
$ canonical ->setRequestPath ('category-1 ' )
173
176
->setStoreId (1 );
@@ -184,7 +187,7 @@ public function testGenerationForSpecificStore()
184
187
185
188
$ this ->assertEquals (
186
189
['category-1_1 ' => $ canonical ],
187
- $ this ->productScopeGenerator ->generateForSpecificStoreView (1 , [$ category ], $ product , 1 )
190
+ $ this ->productScopeGenerator ->generateForSpecificStoreView (1 , [$ this -> categoryMock ], $ product , 1 )
188
191
);
189
192
}
190
193
@@ -212,4 +215,42 @@ protected function initObjectRegistryFactory($entities)
212
215
->with (['entities ' => $ entities ])
213
216
->will ($ this ->returnValue ($ objectRegistry ));
214
217
}
218
+
219
+ /**
220
+ * Test the possibility of url rewrite generation.
221
+ *
222
+ * @param int $parentId
223
+ * @param array $parentIds
224
+ * @param bool $expectedResult
225
+ * @dataProvider isCategoryProperForGeneratingDataProvider
226
+ */
227
+ public function testIsCategoryProperForGenerating ($ parentId , $ parentIds , $ expectedResult )
228
+ {
229
+ $ storeId = 1 ;
230
+ $ this ->categoryMock ->expects (self ::any ())->method ('getParentId ' )->willReturn ($ parentId );
231
+ $ this ->categoryMock ->expects (self ::any ())->method ('getParentIds ' )->willReturn ($ parentIds );
232
+ $ result = $ this ->productScopeGenerator ->isCategoryProperForGenerating (
233
+ $ this ->categoryMock ,
234
+ $ storeId
235
+ );
236
+ self ::assertEquals (
237
+ $ expectedResult ,
238
+ $ result
239
+ );
240
+ }
241
+
242
+ /**
243
+ * Data provider for testIsCategoryProperForGenerating.
244
+ *
245
+ * @return array
246
+ */
247
+ public function isCategoryProperForGeneratingDataProvider ()
248
+ {
249
+ return [
250
+ ['0 ' , ['0 ' ], false ],
251
+ ['1 ' , ['1 ' ], false ],
252
+ ['2 ' , ['1 ' , '2 ' ], true ],
253
+ ['2 ' , ['1 ' , '3 ' ], false ],
254
+ ];
255
+ }
215
256
}
0 commit comments