10
10
use Magento \Store \Model \Store ;
11
11
use Magento \CatalogInventory \Api \Data \StockItemInterface ;
12
12
use Magento \Store \Model \Website ;
13
+ use Magento \Store \Model \WebsiteRepository ;
13
14
use Magento \TestFramework \Helper \Bootstrap ;
14
15
use Magento \TestFramework \TestCase \WebapiAbstract ;
15
16
use Magento \Framework \Api \FilterBuilder ;
16
17
use Magento \Framework \Api \SearchCriteriaBuilder ;
17
18
use Magento \Framework \Api \SortOrder ;
18
19
use Magento \Framework \Api \SortOrderBuilder ;
19
20
use Magento \Framework \Webapi \Exception as HTTPExceptionCodes ;
21
+ use Magento \Framework \Exception \NoSuchEntityException ;
20
22
21
23
/**
22
24
* @magentoAppIsolation enabled
@@ -136,6 +138,24 @@ public function productCreationProvider()
136
138
];
137
139
}
138
140
141
+ /**
142
+ * Load website by website code
143
+ *
144
+ * @param $websiteCode
145
+ * @return Website
146
+ */
147
+ private function loadWebsiteByCode ($ websiteCode )
148
+ {
149
+ $ websiteRepository = Bootstrap::getObjectManager ()->get (WebsiteRepository::class);
150
+ try {
151
+ $ website = $ websiteRepository ->get ($ websiteCode );
152
+ } catch (NoSuchEntityException $ e ){
153
+ $ this ->fail ("Couldn`t load website: {$ websiteCode }" );
154
+ }
155
+
156
+ return $ website ;
157
+ }
158
+
139
159
/**
140
160
* Test removing association between product and website 1
141
161
* @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
@@ -144,12 +164,7 @@ public function testUpdateWithDeleteWebsites()
144
164
{
145
165
$ productBuilder [ProductInterface::SKU ] = 'unique-simple-azaza ' ;
146
166
/** @var Website $website */
147
- $ website = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (Website::class);
148
- $ website ->load ('second_website ' , 'code ' );
149
-
150
- if (!$ website ->getId ()) {
151
- $ this ->fail ("Couldn`t load website " );
152
- }
167
+ $ website = $ this ->loadWebsiteByCode ('second_website ' );
153
168
154
169
$ websitesData = [
155
170
'website_ids ' => [
@@ -172,12 +187,7 @@ public function testDeleteAllWebsiteAssociations()
172
187
{
173
188
$ productBuilder [ProductInterface::SKU ] = 'unique-simple-azaza ' ;
174
189
/** @var Website $website */
175
- $ website = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (Website::class);
176
- $ website ->load ('second_website ' , 'code ' );
177
-
178
- if (!$ website ->getId ()) {
179
- $ this ->fail ("Couldn`t load website " );
180
- }
190
+ $ website = $ this ->loadWebsiteByCode ('second_website ' );
181
191
182
192
$ websitesData = [
183
193
'website_ids ' => []
@@ -198,14 +208,9 @@ public function testCreateWithMultipleWebsites()
198
208
$ productBuilder = $ this ->getSimpleProductData ();
199
209
$ productBuilder [ProductInterface::SKU ] = 'test-test-sku ' ;
200
210
$ productBuilder [ProductInterface::TYPE_ID ] = 'simple ' ;
201
-
202
211
/** @var Website $website */
203
- $ website = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (Website::class);
204
- $ website ->load ('test_website ' , 'code ' );
212
+ $ website = $ this ->loadWebsiteByCode ('test_website ' );
205
213
206
- if (!$ website ->getId ()) {
207
- $ this ->fail ("Couldn`t load website " );
208
- }
209
214
$ websitesData = [
210
215
'website_ids ' => [
211
216
1 ,
@@ -218,6 +223,104 @@ public function testCreateWithMultipleWebsites()
218
223
$ response [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ],
219
224
$ websitesData ["website_ids " ]
220
225
);
226
+ $ this ->deleteProduct ($ productBuilder [ProductInterface::SKU ]);
227
+ }
228
+
229
+ /**
230
+ * Add product associated with website that is not associated with default store
231
+ *
232
+ * @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
233
+ */
234
+ public function testCreateWithNonDefaultStoreWebsite ()
235
+ {
236
+ $ productBuilder = $ this ->getSimpleProductData ();
237
+ $ productBuilder [ProductInterface::SKU ] = 'test-sku-second-site-123 ' ;
238
+ $ productBuilder [ProductInterface::TYPE_ID ] = 'simple ' ;
239
+ /** @var Website $website */
240
+ $ website = $ this ->loadWebsiteByCode ('test ' );
241
+
242
+ $ websitesData = [
243
+ 'website_ids ' => [
244
+ $ website ->getId (),
245
+ ]
246
+ ];
247
+ $ productBuilder [ProductInterface::EXTENSION_ATTRIBUTES_KEY ] = $ websitesData ;
248
+ $ response = $ this ->saveProduct ($ productBuilder );
249
+ $ this ->assertEquals (
250
+ $ websitesData ["website_ids " ],
251
+ $ response [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ]
252
+ );
253
+ $ this ->deleteProduct ($ productBuilder [ProductInterface::SKU ]);
254
+ }
255
+
256
+ /**
257
+ * Update product to be associated with website that is not associated with default store
258
+ *
259
+ * @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
260
+ * @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
261
+ */
262
+ public function testUpdateWithNonDefaultStoreWebsite ()
263
+ {
264
+ $ productBuilder [ProductInterface::SKU ] = 'unique-simple-azaza ' ;
265
+ /** @var Website $website */
266
+ $ website = $ this ->loadWebsiteByCode ('test ' );
267
+
268
+ $ this ->assertNotContains (Store::SCOPE_DEFAULT , $ website ->getStoreCodes ());
269
+
270
+ $ websitesData = [
271
+ 'website_ids ' => [
272
+ $ website ->getId (),
273
+ ]
274
+ ];
275
+ $ productBuilder [ProductInterface::EXTENSION_ATTRIBUTES_KEY ] = $ websitesData ;
276
+ $ response = $ this ->updateProduct ($ productBuilder );
277
+ $ this ->assertEquals (
278
+ $ websitesData ["website_ids " ],
279
+ $ response [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ]
280
+ );
281
+ }
282
+
283
+ /**
284
+ * Update product to be associated with no websites
285
+ *
286
+ * @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
287
+ */
288
+ public function testUpdateWithNoWebsite ()
289
+ {
290
+ $ productBuilder [ProductInterface::SKU ] = 'unique-simple-azaza ' ;
291
+
292
+ $ websitesData = [
293
+ 'website_ids ' => []
294
+ ];
295
+ $ productBuilder [ProductInterface::EXTENSION_ATTRIBUTES_KEY ] = $ websitesData ;
296
+ $ response = $ this ->updateProduct ($ productBuilder );
297
+ $ this ->assertEquals (
298
+ $ websitesData ["website_ids " ],
299
+ $ response [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ]
300
+ );
301
+ }
302
+
303
+ /**
304
+ * Update product without specifying websites
305
+ *
306
+ * @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
307
+ */
308
+ public function testUpdateWithoutWebsiteIds ()
309
+ {
310
+ $ productBuilder [ProductInterface::SKU ] = 'unique-simple-azaza ' ;
311
+ $ originalProduct = $ this ->getProduct ($ productBuilder [ProductInterface::SKU ]);
312
+ $ newName = 'Updated Product ' ;
313
+
314
+ $ productBuilder [ProductInterface::NAME ] = $ newName ;
315
+ $ response = $ this ->updateProduct ($ productBuilder );
316
+ $ this ->assertEquals (
317
+ $ newName ,
318
+ $ response [ProductInterface::NAME ]
319
+ );
320
+ $ this ->assertEquals (
321
+ $ originalProduct [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ],
322
+ $ response [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ]
323
+ );
221
324
}
222
325
223
326
/**
@@ -727,8 +830,7 @@ public function testGetList()
727
830
*/
728
831
public function testGetListWithFilteringByWebsite ()
729
832
{
730
- $ website = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (Website::class);
731
- $ website ->load ('test ' , 'code ' );
833
+ $ website = $ this ->loadWebsiteByCode ('test ' );
732
834
$ searchCriteria = [
733
835
'searchCriteria ' => [
734
836
'filter_groups ' => [
0 commit comments