12
12
use Magento \TestFramework \TestCase \GraphQlAbstract ;
13
13
use Magento \UrlRewrite \Model \UrlFinderInterface ;
14
14
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite as UrlRewriteDTO ;
15
+ use Magento \Eav \Model \Config as EavConfig ;
16
+ use Magento \Store \Model \StoreManagerInterface ;
15
17
16
18
/**
17
19
* Test of getting URL rewrites data from products
@@ -54,9 +56,22 @@ public function testProductWithNoCategoriesAssigned()
54
56
$ productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
55
57
$ product = $ productRepository ->get ('virtual-product ' , false , null , true );
56
58
59
+ $ storeId = ObjectManager::getInstance ()->get (StoreManagerInterface::class)->getStore ()->getId ();
57
60
$ urlFinder = ObjectManager::getInstance ()->get (UrlFinderInterface::class);
61
+ $ entityType = ObjectManager::getInstance ()->create (EavConfig::class)->getEntityType ('catalog_product ' );
58
62
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
+ );
60
75
61
76
/* There should be only one rewrite */
62
77
/** @var UrlRewriteDTO $urlRewrite */
@@ -110,18 +125,32 @@ public function testProductWithOneCategoryAssigned()
110
125
$ productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
111
126
$ product = $ productRepository ->get ('simple ' , false , null , true );
112
127
128
+ $ storeId = ObjectManager::getInstance ()->get (StoreManagerInterface::class)->getStore ()->getId ();
113
129
$ urlFinder = ObjectManager::getInstance ()->get (UrlFinderInterface::class);
130
+ $ entityType = ObjectManager::getInstance ()->create (EavConfig::class)->getEntityType ('catalog_product ' );
114
131
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
+ }
117
136
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 );
118
146
$ this ->assertArrayHasKey ('url_rewrites ' , $ response ['products ' ]['items ' ][0 ]);
147
+ $ this ->assertCount (1 , $ response ['products ' ]['items ' ][0 ]['url_rewrites ' ]);
119
148
$ this ->assertCount ($ rewritesCount , $ response ['products ' ]['items ' ][0 ]['url_rewrites ' ]);
120
149
121
- for ($ i = 0 ; $ i < $ rewritesCount ; $ i ++) {
122
- $ urlRewrite = $ rewritesCollection [$ i ];
150
+ for ($ index = 0 ; $ index < $ rewritesCount ; $ index ++) {
151
+ $ urlRewrite = $ rewritesCollection [$ index ];
123
152
$ this ->assertResponseFields (
124
- $ response ['products ' ]['items ' ][0 ]['url_rewrites ' ][$ i ],
153
+ $ response ['products ' ]['items ' ][0 ]['url_rewrites ' ][$ index ],
125
154
[
126
155
"url " => $ urlRewrite ->getRequestPath (),
127
156
"parameters " => $ this ->getUrlParameters ($ urlRewrite ->getTargetPath ())
@@ -141,11 +170,11 @@ private function getUrlParameters(string $targetPath): array
141
170
$ urlParameters = [];
142
171
$ targetPathParts = explode ('/ ' , trim ($ targetPath , '/ ' ));
143
172
$ 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 ) {
146
175
$ urlParameters [] = [
147
- 'name ' => $ targetPathParts [$ i ],
148
- 'value ' => $ targetPathParts [$ i + 1 ]
176
+ 'name ' => $ targetPathParts [$ index ],
177
+ 'value ' => $ targetPathParts [$ index + 1 ]
149
178
];
150
179
}
151
180
0 commit comments