@@ -47,6 +47,9 @@ class ProductScopeRewriteGeneratorTest extends \PHPUnit\Framework\TestCase
4747 /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
4848 private $ serializer ;
4949
50+ /** @var \Magento\Catalog\Model\Category|\PHPUnit_Framework_MockObject_MockObject */
51+ private $ categoryMock ;
52+
5053 public function setUp ()
5154 {
5255 $ this ->serializer = $ this ->createMock (\Magento \Framework \Serialize \Serializer \Json::class);
@@ -83,6 +86,10 @@ function ($value) {
8386 $ this ->storeViewService = $ this ->getMockBuilder (\Magento \CatalogUrlRewrite \Service \V1 \StoreViewService::class)
8487 ->disableOriginalConstructor ()->getMock ();
8588 $ 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 ));
8693 $ mergeDataProviderFactory = $ this ->createPartialMock (
8794 \Magento \UrlRewrite \Model \MergeDataProviderFactory::class,
8895 ['create ' ]
@@ -103,6 +110,7 @@ function ($value) {
103110 'mergeDataProviderFactory ' => $ mergeDataProviderFactory
104111 ]
105112 );
113+ $ this ->categoryMock = $ this ->getMockBuilder (Category::class)->disableOriginalConstructor ()->getMock ();
106114 }
107115
108116 public function testGenerationForGlobalScope ()
@@ -112,12 +120,6 @@ public function testGenerationForGlobalScope()
112120 $ product ->expects ($ this ->any ())->method ('getStoreIds ' )->will ($ this ->returnValue ([1 ]));
113121 $ this ->storeViewService ->expects ($ this ->once ())->method ('doesEntityHaveOverriddenUrlKeyForStore ' )
114122 ->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 );
121123 $ this ->initObjectRegistryFactory ([]);
122124 $ canonical = new \Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ([], $ this ->serializer );
123125 $ canonical ->setRequestPath ('category-1 ' )
@@ -149,25 +151,21 @@ public function testGenerationForGlobalScope()
149151 'category-3_3 ' => $ current ,
150152 'category-4_4 ' => $ anchorCategories
151153 ],
152- $ this ->productScopeGenerator ->generateForGlobalScope ([$ categoryMock ], $ product , 1 )
154+ $ this ->productScopeGenerator ->generateForGlobalScope ([$ this -> categoryMock ], $ product , 1 )
153155 );
154156 }
155157
156158 public function testGenerationForSpecificStore ()
157159 {
160+ $ storeRootCategoryId = 2 ;
161+ $ category_id = 4 ;
158162 $ product = $ this ->createMock (\Magento \Catalog \Model \Product::class);
159163 $ product ->expects ($ this ->any ())->method ('getStoreId ' )->will ($ this ->returnValue (1 ));
160164 $ 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 ' )
164166 ->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 ]);
171169 $ canonical = new \Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ([], $ this ->serializer );
172170 $ canonical ->setRequestPath ('category-1 ' )
173171 ->setStoreId (1 );
@@ -184,7 +182,7 @@ public function testGenerationForSpecificStore()
184182
185183 $ this ->assertEquals (
186184 ['category-1_1 ' => $ canonical ],
187- $ this ->productScopeGenerator ->generateForSpecificStoreView (1 , [$ category ], $ product , 1 )
185+ $ this ->productScopeGenerator ->generateForSpecificStoreView (1 , [$ this -> categoryMock ], $ product , 1 )
188186 );
189187 }
190188
@@ -212,4 +210,40 @@ protected function initObjectRegistryFactory($entities)
212210 ->with (['entities ' => $ entities ])
213211 ->will ($ this ->returnValue ($ objectRegistry ));
214212 }
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+ }
215249}
0 commit comments