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 ' => [
@@ -171,13 +186,6 @@ public function testUpdateWithDeleteWebsites()
171
186
public function testDeleteAllWebsiteAssociations ()
172
187
{
173
188
$ productBuilder [ProductInterface::SKU ] = 'unique-simple-azaza ' ;
174
- /** @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
- }
181
189
182
190
$ websitesData = [
183
191
'website_ids ' => []
@@ -198,14 +206,9 @@ public function testCreateWithMultipleWebsites()
198
206
$ productBuilder = $ this ->getSimpleProductData ();
199
207
$ productBuilder [ProductInterface::SKU ] = 'test-test-sku ' ;
200
208
$ productBuilder [ProductInterface::TYPE_ID ] = 'simple ' ;
201
-
202
209
/** @var Website $website */
203
- $ website = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->get (Website::class);
204
- $ website ->load ('test_website ' , 'code ' );
210
+ $ website = $ this ->loadWebsiteByCode ('test_website ' );
205
211
206
- if (!$ website ->getId ()) {
207
- $ this ->fail ("Couldn`t load website " );
208
- }
209
212
$ websitesData = [
210
213
'website_ids ' => [
211
214
1 ,
@@ -218,6 +221,84 @@ public function testCreateWithMultipleWebsites()
218
221
$ response [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ],
219
222
$ websitesData ["website_ids " ]
220
223
);
224
+ $ this ->deleteProduct ($ productBuilder [ProductInterface::SKU ]);
225
+ }
226
+
227
+ /**
228
+ * Add product associated with website that is not associated with default store
229
+ *
230
+ * @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
231
+ */
232
+ public function testCreateWithNonDefaultStoreWebsite ()
233
+ {
234
+ $ productBuilder = $ this ->getSimpleProductData ();
235
+ $ productBuilder [ProductInterface::SKU ] = 'test-sku-second-site-123 ' ;
236
+ $ productBuilder [ProductInterface::TYPE_ID ] = 'simple ' ;
237
+ /** @var Website $website */
238
+ $ website = $ this ->loadWebsiteByCode ('test ' );
239
+
240
+ $ websitesData = [
241
+ 'website_ids ' => [
242
+ $ website ->getId (),
243
+ ]
244
+ ];
245
+ $ productBuilder [ProductInterface::EXTENSION_ATTRIBUTES_KEY ] = $ websitesData ;
246
+ $ response = $ this ->saveProduct ($ productBuilder );
247
+ $ this ->assertEquals (
248
+ $ websitesData ["website_ids " ],
249
+ $ response [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ]
250
+ );
251
+ $ this ->deleteProduct ($ productBuilder [ProductInterface::SKU ]);
252
+ }
253
+
254
+ /**
255
+ * Update product to be associated with website that is not associated with default store
256
+ *
257
+ * @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
258
+ * @magentoApiDataFixture Magento/Store/_files/second_website_with_two_stores.php
259
+ */
260
+ public function testUpdateWithNonDefaultStoreWebsite ()
261
+ {
262
+ $ productBuilder [ProductInterface::SKU ] = 'unique-simple-azaza ' ;
263
+ /** @var Website $website */
264
+ $ website = $ this ->loadWebsiteByCode ('test ' );
265
+
266
+ $ this ->assertNotContains (Store::SCOPE_DEFAULT , $ website ->getStoreCodes ());
267
+
268
+ $ websitesData = [
269
+ 'website_ids ' => [
270
+ $ website ->getId (),
271
+ ]
272
+ ];
273
+ $ productBuilder [ProductInterface::EXTENSION_ATTRIBUTES_KEY ] = $ websitesData ;
274
+ $ response = $ this ->updateProduct ($ productBuilder );
275
+ $ this ->assertEquals (
276
+ $ websitesData ["website_ids " ],
277
+ $ response [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ]
278
+ );
279
+ }
280
+
281
+ /**
282
+ * Update product without specifying websites
283
+ *
284
+ * @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
285
+ */
286
+ public function testUpdateWithoutWebsiteIds ()
287
+ {
288
+ $ productBuilder [ProductInterface::SKU ] = 'unique-simple-azaza ' ;
289
+ $ originalProduct = $ this ->getProduct ($ productBuilder [ProductInterface::SKU ]);
290
+ $ newName = 'Updated Product ' ;
291
+
292
+ $ productBuilder [ProductInterface::NAME ] = $ newName ;
293
+ $ response = $ this ->updateProduct ($ productBuilder );
294
+ $ this ->assertEquals (
295
+ $ newName ,
296
+ $ response [ProductInterface::NAME ]
297
+ );
298
+ $ this ->assertEquals (
299
+ $ originalProduct [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ],
300
+ $ response [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ]
301
+ );
221
302
}
222
303
223
304
/**
@@ -727,8 +808,7 @@ public function testGetList()
727
808
*/
728
809
public function testGetListWithFilteringByWebsite ()
729
810
{
730
- $ website = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (Website::class);
731
- $ website ->load ('test ' , 'code ' );
811
+ $ website = $ this ->loadWebsiteByCode ('test ' );
732
812
$ searchCriteria = [
733
813
'searchCriteria ' => [
734
814
'filter_groups ' => [
0 commit comments