9
9
10
10
use Magento \Catalog \Api \ProductRepositoryInterface ;
11
11
use Magento \Catalog \Model \Product \Visibility ;
12
+ use Magento \Store \Model \StoreManagerInterface ;
12
13
use Magento \TestFramework \Helper \Bootstrap ;
13
14
use Magento \TestFramework \ObjectManager ;
15
+ use Magento \UrlRewrite \Model \ResourceModel \UrlRewriteCollection ;
16
+ use Magento \UrlRewrite \Model \ResourceModel \UrlRewriteCollectionFactory ;
14
17
use Magento \UrlRewrite \Model \UrlFinderInterface ;
18
+ use Magento \UrlRewrite \Model \UrlRewrite as UrlRewriteItem ;
15
19
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
16
20
use PHPUnit \Framework \TestCase ;
17
21
@@ -30,11 +34,21 @@ class FindByUrlRewriteTest extends TestCase
30
34
*/
31
35
private $ urlFinder ;
32
36
37
+ /**
38
+ * @var StoreManagerInterface
39
+ */
40
+ private $ storeManager ;
41
+
33
42
/**
34
43
* @var ProductRepositoryInterface
35
44
*/
36
45
private $ productRepository ;
37
46
47
+ /**
48
+ * @var UrlRewriteCollectionFactory
49
+ */
50
+ private $ urlRewriteCollectionFactory ;
51
+
38
52
/**
39
53
* @inheritdoc
40
54
*/
@@ -43,42 +57,191 @@ protected function setUp()
43
57
$ this ->objectManger = Bootstrap::getObjectManager ();
44
58
$ this ->urlFinder = $ this ->objectManger ->get (UrlFinderInterface::class);
45
59
$ this ->productRepository = $ this ->objectManger ->get (ProductRepositoryInterface::class);
60
+ $ this ->storeManager = $ this ->objectManger ->get (StoreManagerInterface::class);
61
+ $ this ->urlRewriteCollectionFactory = $ this ->objectManger ->get (UrlRewriteCollectionFactory::class);
46
62
parent ::setUp ();
47
63
}
48
64
49
65
/**
50
- * Assert that URL rewrite for child product of configurable was not created .
66
+ * Assert that product is available by URL rewrite with different visibility .
51
67
*
52
68
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_two_child_products.php
69
+ * @dataProvider visibilityWithExpectedResultDataProvider
53
70
* @magentoDbIsolation enabled
54
71
*
72
+ * @param array $productsData
55
73
* @return void
56
74
*/
57
- public function testCheckUrlRewriteForChildWasNotCreated ( ): void
75
+ public function testCheckIsUrlRewriteForChildrenProductsAreCreated ( array $ productsData ): void
58
76
{
59
77
$ this ->checkConfigurableUrlRewriteWasCreated ();
60
- $ this ->assertNull ($ this ->urlFinder ->findOneByData ([UrlRewrite::REQUEST_PATH => 'configurable-option-1.html ' ]));
61
- $ this ->assertNull ($ this ->urlFinder ->findOneByData ([UrlRewrite::REQUEST_PATH => 'configurable-option-2.html ' ]));
78
+ $ this ->updateProductsVisibility ($ productsData );
79
+ $ urlRewritesCollection = $ this ->getUrlRewritesCollectionByPaths ([
80
+ 'configurable-option-1.html ' ,
81
+ 'configurable-option-2.html ' ,
82
+ ]);
83
+ $ expectedCount = 0 ;
84
+ foreach ($ productsData as $ productData ) {
85
+ $ product = $ this ->productRepository ->get ($ productData ['sku ' ]);
86
+ /** @var UrlRewriteItem $urlRewrite */
87
+ $ urlRewrite = $ urlRewritesCollection ->getItemByColumnValue (
88
+ UrlRewrite::TARGET_PATH ,
89
+ "catalog/product/view/id/ {$ product ->getId ()}"
90
+ );
91
+ if ($ productData ['url_rewrite_created ' ]) {
92
+ $ this ->assertNotNull ($ urlRewrite );
93
+ $ this ->assertEquals ($ product ->getId (), $ urlRewrite ->getEntityId ());
94
+ $ this ->assertEquals ('product ' , $ urlRewrite ->getEntityType ());
95
+ $ expectedCount ++;
96
+ } else {
97
+ $ this ->assertNull ($ urlRewrite );
98
+ }
99
+ }
100
+ $ this ->assertCount ($ expectedCount , $ urlRewritesCollection );
62
101
}
63
102
64
103
/**
65
- * Assert that URL rewrite for one of child product of configurable was created .
104
+ * Return products visibility, expected result and other product additional data .
66
105
*
67
- * @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_two_child_products.php
68
- * @magentoDbIsolation enabled
106
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
69
107
*
108
+ * @return array
109
+ */
110
+ public function visibilityWithExpectedResultDataProvider (): array
111
+ {
112
+ return [
113
+ 'visibility_for_both_product_only_catalog ' => [
114
+ [
115
+ [
116
+ 'sku ' => 'Simple option 1 ' ,
117
+ 'visibility ' => Visibility::VISIBILITY_IN_CATALOG ,
118
+ 'url_rewrite_created ' => true ,
119
+ ],
120
+ [
121
+ 'sku ' => 'Simple option 2 ' ,
122
+ 'visibility ' => Visibility::VISIBILITY_IN_CATALOG ,
123
+ 'url_rewrite_created ' => true ,
124
+ ],
125
+ ],
126
+ ],
127
+ 'visibility_for_both_product_catalog_search ' => [
128
+ [
129
+ [
130
+ 'sku ' => 'Simple option 1 ' ,
131
+ 'visibility ' => Visibility::VISIBILITY_BOTH ,
132
+ 'url_rewrite_created ' => true ,
133
+ ],
134
+ [
135
+ 'sku ' => 'Simple option 2 ' ,
136
+ 'visibility ' => Visibility::VISIBILITY_BOTH ,
137
+ 'url_rewrite_created ' => true ,
138
+ ],
139
+ ],
140
+ ],
141
+ 'visibility_for_both_product_only_search ' => [
142
+ [
143
+ [
144
+ 'sku ' => 'Simple option 1 ' ,
145
+ 'visibility ' => Visibility::VISIBILITY_IN_SEARCH ,
146
+ 'url_rewrite_created ' => true ,
147
+ ],
148
+ [
149
+ 'sku ' => 'Simple option 2 ' ,
150
+ 'visibility ' => Visibility::VISIBILITY_IN_SEARCH ,
151
+ 'url_rewrite_created ' => true ,
152
+ ],
153
+ ],
154
+ ],
155
+ 'visibility_for_both_product_not_visible_individuality ' => [
156
+ [
157
+ [
158
+ 'sku ' => 'Simple option 1 ' ,
159
+ 'visibility ' => Visibility::VISIBILITY_NOT_VISIBLE ,
160
+ 'url_rewrite_created ' => false ,
161
+ ],
162
+ [
163
+ 'sku ' => 'Simple option 2 ' ,
164
+ 'visibility ' => Visibility::VISIBILITY_NOT_VISIBLE ,
165
+ 'url_rewrite_created ' => false ,
166
+ ],
167
+ ],
168
+ ],
169
+ 'visibility_for_one_product_only_catalog ' => [
170
+ [
171
+ [
172
+ 'sku ' => 'Simple option 1 ' ,
173
+ 'visibility ' => Visibility::VISIBILITY_IN_CATALOG ,
174
+ 'url_rewrite_created ' => true ,
175
+ ],
176
+ [
177
+ 'sku ' => 'Simple option 2 ' ,
178
+ 'visibility ' => Visibility::VISIBILITY_NOT_VISIBLE ,
179
+ 'url_rewrite_created ' => false ,
180
+ ],
181
+ ],
182
+ ],
183
+ 'visibility_for_one_product_catalog_search ' => [
184
+ [
185
+ [
186
+ 'sku ' => 'Simple option 1 ' ,
187
+ 'visibility ' => Visibility::VISIBILITY_BOTH ,
188
+ 'url_rewrite_created ' => true ,
189
+ ],
190
+ [
191
+ 'sku ' => 'Simple option 2 ' ,
192
+ 'visibility ' => Visibility::VISIBILITY_NOT_VISIBLE ,
193
+ 'url_rewrite_created ' => false ,
194
+ ],
195
+ ],
196
+ ],
197
+ 'visibility_for_one_product_only_search ' => [
198
+ [
199
+ [
200
+ 'sku ' => 'Simple option 1 ' ,
201
+ 'visibility ' => Visibility::VISIBILITY_IN_SEARCH ,
202
+ 'url_rewrite_created ' => true ,
203
+ ],
204
+ [
205
+ 'sku ' => 'Simple option 2 ' ,
206
+ 'visibility ' => Visibility::VISIBILITY_NOT_VISIBLE ,
207
+ 'url_rewrite_created ' => false ,
208
+ ],
209
+ ],
210
+ ],
211
+ ];
212
+ }
213
+
214
+ /**
215
+ * Update products visibility.
216
+ *
217
+ * @param array $productsData
70
218
* @return void
71
219
*/
72
- public function testCheckUrlRewriteForOneOfChildWasCreated ( ): void
220
+ private function updateProductsVisibility ( array $ productsData ): void
73
221
{
74
- $ this ->checkConfigurableUrlRewriteWasCreated ();
75
- $ childProduct = $ this ->productRepository ->get ('Simple option 1 ' );
76
- $ childProduct ->setVisibility (Visibility::VISIBILITY_BOTH );
77
- $ this ->productRepository ->save ($ childProduct );
78
- $ childUrlRewrite = $ this ->urlFinder ->findOneByData ([UrlRewrite::REQUEST_PATH => 'configurable-option-1.html ' ]);
79
- $ this ->assertNotNull ($ childUrlRewrite );
80
- $ this ->assertEquals ($ childUrlRewrite ->getTargetPath (), "catalog/product/view/id/ {$ childProduct ->getId ()}" );
81
- $ this ->assertNull ($ this ->urlFinder ->findOneByData ([UrlRewrite::REQUEST_PATH => 'configurable-option-2.html ' ]));
222
+ foreach ($ productsData as $ productData ) {
223
+ $ product = $ this ->productRepository ->get ($ productData ['sku ' ]);
224
+ $ product ->setVisibility ($ productData ['visibility ' ]);
225
+ $ this ->productRepository ->save ($ product );
226
+ }
227
+ }
228
+
229
+ /**
230
+ * Get URL rewrite collection by requested products paths.
231
+ *
232
+ * @param array $requestPaths
233
+ * @param string $storeCode
234
+ * @return UrlRewriteCollection
235
+ */
236
+ private function getUrlRewritesCollectionByPaths (
237
+ array $ requestPaths ,
238
+ string $ storeCode = 'default '
239
+ ): UrlRewriteCollection {
240
+ $ collection = $ this ->urlRewriteCollectionFactory ->create ();
241
+ $ collection ->addStoreFilter ($ storeCode );
242
+ $ collection ->addFieldToFilter (UrlRewrite::REQUEST_PATH , ['in ' => $ requestPaths ]);
243
+
244
+ return $ collection ;
82
245
}
83
246
84
247
/**
0 commit comments