1010use Magento \Store \Model \Store ;
1111use Magento \CatalogInventory \Api \Data \StockItemInterface ;
1212use Magento \Store \Model \Website ;
13+ use Magento \Store \Model \WebsiteRepository ;
1314use Magento \TestFramework \Helper \Bootstrap ;
1415use Magento \TestFramework \TestCase \WebapiAbstract ;
1516use Magento \Framework \Api \FilterBuilder ;
1617use Magento \Framework \Api \SearchCriteriaBuilder ;
1718use Magento \Framework \Api \SortOrder ;
1819use Magento \Framework \Api \SortOrderBuilder ;
1920use Magento \Framework \Webapi \Exception as HTTPExceptionCodes ;
21+ use Magento \Framework \Exception \NoSuchEntityException ;
2022
2123/**
2224 * @magentoAppIsolation enabled
@@ -136,6 +138,24 @@ public function productCreationProvider()
136138 ];
137139 }
138140
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+
139159 /**
140160 * Test removing association between product and website 1
141161 * @magentoApiDataFixture Magento/Catalog/_files/product_with_two_websites.php
@@ -144,12 +164,7 @@ public function testUpdateWithDeleteWebsites()
144164 {
145165 $ productBuilder [ProductInterface::SKU ] = 'unique-simple-azaza ' ;
146166 /** @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 ' );
153168
154169 $ websitesData = [
155170 'website_ids ' => [
@@ -171,13 +186,6 @@ public function testUpdateWithDeleteWebsites()
171186 public function testDeleteAllWebsiteAssociations ()
172187 {
173188 $ 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- }
181189
182190 $ websitesData = [
183191 'website_ids ' => []
@@ -198,14 +206,9 @@ public function testCreateWithMultipleWebsites()
198206 $ productBuilder = $ this ->getSimpleProductData ();
199207 $ productBuilder [ProductInterface::SKU ] = 'test-test-sku ' ;
200208 $ productBuilder [ProductInterface::TYPE_ID ] = 'simple ' ;
201-
202209 /** @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 ' );
205211
206- if (!$ website ->getId ()) {
207- $ this ->fail ("Couldn`t load website " );
208- }
209212 $ websitesData = [
210213 'website_ids ' => [
211214 1 ,
@@ -218,6 +221,84 @@ public function testCreateWithMultipleWebsites()
218221 $ response [ProductInterface::EXTENSION_ATTRIBUTES_KEY ]["website_ids " ],
219222 $ websitesData ["website_ids " ]
220223 );
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+ );
221302 }
222303
223304 /**
@@ -727,8 +808,7 @@ public function testGetList()
727808 */
728809 public function testGetListWithFilteringByWebsite ()
729810 {
730- $ website = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ()->create (Website::class);
731- $ website ->load ('test ' , 'code ' );
811+ $ website = $ this ->loadWebsiteByCode ('test ' );
732812 $ searchCriteria = [
733813 'searchCriteria ' => [
734814 'filter_groups ' => [
0 commit comments