1212use Magento \TestFramework \TestCase \GraphQlAbstract ;
1313use Magento \UrlRewrite \Model \UrlFinderInterface ;
1414use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite as UrlRewriteDTO ;
15+ use Magento \Eav \Model \Config as EavConfig ;
16+ use Magento \Store \Model \StoreManagerInterface ;
1517
1618/**
1719 * Test of getting URL rewrites data from products
@@ -54,9 +56,22 @@ public function testProductWithNoCategoriesAssigned()
5456 $ productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
5557 $ product = $ productRepository ->get ('virtual-product ' , false , null , true );
5658
59+ $ storeId = ObjectManager::getInstance ()->get (StoreManagerInterface::class)->getStore ()->getId ();
5760 $ urlFinder = ObjectManager::getInstance ()->get (UrlFinderInterface::class);
61+ $ entityType = ObjectManager::getInstance ()->create (EavConfig::class)->getEntityType ('catalog_product ' );
5862
59- $ rewritesCollection = $ urlFinder ->findAllByData ([UrlRewriteDTO::ENTITY_ID => $ product ->getId ()]);
63+ $ entityTypeCode = $ entityType ->getEntityTypeCode ();
64+ if ($ entityTypeCode === 'catalog_product ' ) {
65+ $ entityTypeCode = 'product ' ;
66+ }
67+
68+ $ rewritesCollection = $ urlFinder ->findAllByData (
69+ [
70+ UrlRewriteDTO::ENTITY_ID => $ product ->getId (),
71+ UrlRewriteDTO::ENTITY_TYPE => $ entityTypeCode ,
72+ UrlRewriteDTO::STORE_ID => $ storeId
73+ ]
74+ );
6075
6176 /* There should be only one rewrite */
6277 /** @var UrlRewriteDTO $urlRewrite */
@@ -110,18 +125,32 @@ public function testProductWithOneCategoryAssigned()
110125 $ productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
111126 $ product = $ productRepository ->get ('simple ' , false , null , true );
112127
128+ $ storeId = ObjectManager::getInstance ()->get (StoreManagerInterface::class)->getStore ()->getId ();
113129 $ urlFinder = ObjectManager::getInstance ()->get (UrlFinderInterface::class);
130+ $ entityType = ObjectManager::getInstance ()->create (EavConfig::class)->getEntityType ('catalog_product ' );
114131
115- $ rewritesCollection = $ urlFinder ->findAllByData ([UrlRewriteDTO::ENTITY_ID => $ product ->getId ()]);
116- $ rewritesCount = count ($ rewritesCollection );
132+ $ entityTypeCode = $ entityType ->getEntityTypeCode ();
133+ if ($ entityTypeCode === 'catalog_product ' ) {
134+ $ entityTypeCode = 'product ' ;
135+ }
117136
137+ $ rewritesCollection = $ urlFinder ->findAllByData (
138+ [
139+ UrlRewriteDTO::ENTITY_ID => $ product ->getId (),
140+ UrlRewriteDTO::ENTITY_TYPE => $ entityTypeCode ,
141+ UrlRewriteDTO::STORE_ID => $ storeId
142+ ]
143+ );
144+
145+ $ rewritesCount = count ($ rewritesCollection );
118146 $ this ->assertArrayHasKey ('url_rewrites ' , $ response ['products ' ]['items ' ][0 ]);
147+ $ this ->assertCount (1 , $ response ['products ' ]['items ' ][0 ]['url_rewrites ' ]);
119148 $ this ->assertCount ($ rewritesCount , $ response ['products ' ]['items ' ][0 ]['url_rewrites ' ]);
120149
121- for ($ i = 0 ; $ i < $ rewritesCount ; $ i ++) {
122- $ urlRewrite = $ rewritesCollection [$ i ];
150+ for ($ index = 0 ; $ index < $ rewritesCount ; $ index ++) {
151+ $ urlRewrite = $ rewritesCollection [$ index ];
123152 $ this ->assertResponseFields (
124- $ response ['products ' ]['items ' ][0 ]['url_rewrites ' ][$ i ],
153+ $ response ['products ' ]['items ' ][0 ]['url_rewrites ' ][$ index ],
125154 [
126155 "url " => $ urlRewrite ->getRequestPath (),
127156 "parameters " => $ this ->getUrlParameters ($ urlRewrite ->getTargetPath ())
@@ -141,11 +170,11 @@ private function getUrlParameters(string $targetPath): array
141170 $ urlParameters = [];
142171 $ targetPathParts = explode ('/ ' , trim ($ targetPath , '/ ' ));
143172 $ count = count ($ targetPathParts ) - 1 ;
144-
145- for ($ i = 3 ; $ i < $ count ; $ i += 2 ) {
173+ //phpcs:ignore Generic.CodeAnalysis.ForLoopWithTestFunctionCall
174+ for ($ index = 3 ; $ index < $ count ; $ index += 2 ) {
146175 $ urlParameters [] = [
147- 'name ' => $ targetPathParts [$ i ],
148- 'value ' => $ targetPathParts [$ i + 1 ]
176+ 'name ' => $ targetPathParts [$ index ],
177+ 'value ' => $ targetPathParts [$ index + 1 ]
149178 ];
150179 }
151180
0 commit comments