@@ -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,9 @@ 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 ())
123+ $ this ->categoryMock ->expects ($ this ->exactly (2 ))
119124 ->method ('getParentId ' )
120- ->willReturn (1 );
125+ ->willReturn (2 );
121126 $ this ->initObjectRegistryFactory ([]);
122127 $ canonical = new \Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ([], $ this ->serializer );
123128 $ canonical ->setRequestPath ('category-1 ' )
@@ -149,25 +154,23 @@ public function testGenerationForGlobalScope()
149154 'category-3_3 ' => $ current ,
150155 'category-4_4 ' => $ anchorCategories
151156 ],
152- $ this ->productScopeGenerator ->generateForGlobalScope ([$ categoryMock ], $ product , 1 )
157+ $ this ->productScopeGenerator ->generateForGlobalScope ([$ this -> categoryMock ], $ product , 1 )
153158 );
154159 }
155160
156161 public function testGenerationForSpecificStore ()
157162 {
163+ $ storeRootCategoryId = 2 ;
164+ $ parent_id = 3 ;
165+ $ category_id = 4 ;
158166 $ product = $ this ->createMock (\Magento \Catalog \Model \Product::class);
159167 $ product ->expects ($ this ->any ())->method ('getStoreId ' )->will ($ this ->returnValue (1 ));
160168 $ 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 ' )
164170 ->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 ]);
171174 $ canonical = new \Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ([], $ this ->serializer );
172175 $ canonical ->setRequestPath ('category-1 ' )
173176 ->setStoreId (1 );
@@ -184,7 +187,7 @@ public function testGenerationForSpecificStore()
184187
185188 $ this ->assertEquals (
186189 ['category-1_1 ' => $ canonical ],
187- $ this ->productScopeGenerator ->generateForSpecificStoreView (1 , [$ category ], $ product , 1 )
190+ $ this ->productScopeGenerator ->generateForSpecificStoreView (1 , [$ this -> categoryMock ], $ product , 1 )
188191 );
189192 }
190193
@@ -212,4 +215,42 @@ protected function initObjectRegistryFactory($entities)
212215 ->with (['entities ' => $ entities ])
213216 ->will ($ this ->returnValue ($ objectRegistry ));
214217 }
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+ }
215256}
0 commit comments