Skip to content

Commit fceacab

Browse files
MC-36960: Create automated test for "Create product for "all" store views using API service"
1 parent 4527d76 commit fceacab

File tree

1 file changed

+236
-0
lines changed

1 file changed

+236
-0
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Catalog\Api;
9+
10+
use Magento\Catalog\Model\Product\Type;
11+
use Magento\Catalog\Model\ResourceModel\Product\Website\Link;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\Registry;
14+
use Magento\Framework\Webapi\Rest\Request;
15+
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\TestCase\WebapiAbstract;
18+
19+
/**
20+
* Tests for products creation for all store views.
21+
*
22+
* @magentoAppIsolation enabled
23+
*/
24+
class ProductRepositoryAllStoreViewsTest extends WebapiAbstract
25+
{
26+
const PRODUCT_SERVICE_NAME = 'catalogProductRepositoryV1';
27+
const SERVICE_VERSION = 'V1';
28+
const PRODUCTS_RESOURCE_PATH = '/V1/products';
29+
30+
/**
31+
* @var ObjectManagerInterface
32+
*/
33+
private $objectManager;
34+
35+
/**
36+
* @var ProductRepositoryInterface
37+
*/
38+
private $productRepository;
39+
40+
/**
41+
* @var Registry
42+
*/
43+
private $registry;
44+
45+
/**
46+
* @var StoreManagerInterface
47+
*/
48+
private $storeManager;
49+
50+
/**
51+
* @var string
52+
*/
53+
private $productSku = 'simple';
54+
55+
/**
56+
* @var Link
57+
*/
58+
private $productWebsiteLink;
59+
60+
/**
61+
* @inheritdoc
62+
*/
63+
protected function setUp(): void
64+
{
65+
parent::setUp();
66+
$this->objectManager = Bootstrap::getObjectManager();
67+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
68+
$this->productRepository->cleanCache();
69+
$this->registry = $this->objectManager->get(Registry::class);
70+
$this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
71+
$this->productWebsiteLink = $this->objectManager->get(Link::class);
72+
}
73+
74+
/**
75+
* @inheritdoc
76+
*/
77+
protected function tearDown(): void
78+
{
79+
$this->registry->unregister('isSecureArea');
80+
$this->registry->register('isSecureArea', true);
81+
$this->productRepository->delete(
82+
$this->productRepository->get($this->productSku)
83+
);
84+
$this->registry->unregister('isSecureArea');
85+
$this->registry->register('isSecureArea', false);
86+
87+
parent::tearDown();
88+
}
89+
90+
/**
91+
* @magentoApiDataFixture Magento/Catalog/_files/category.php
92+
*/
93+
public function testCreateProduct(): void
94+
{
95+
$productData = $this->getProductData();
96+
$resultData = $this->saveProduct($productData);
97+
$this->assertProductWebsites($this->productSku, $this->getAllWebsiteIds());
98+
$this->assertProductData($productData, $resultData, $this->getAllWebsiteIds());
99+
}
100+
101+
/**
102+
* @magentoApiDataFixture Magento/Catalog/_files/category.php
103+
* @magentoApiDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
104+
*/
105+
public function testCreateProductOnMultipleWebsites(): void
106+
{
107+
$productData = $this->getProductData();
108+
$resultData = $this->saveProduct($productData);
109+
$this->assertProductWebsites($this->productSku, $this->getAllWebsiteIds());
110+
$this->assertProductData($productData, $resultData, $this->getAllWebsiteIds());
111+
}
112+
113+
/**
114+
* Saves Product via API.
115+
*
116+
* @param $product
117+
* @return array
118+
*/
119+
private function saveProduct($product): array
120+
{
121+
$serviceInfo = [
122+
'rest' => ['resourcePath' =>self::PRODUCTS_RESOURCE_PATH, 'httpMethod' => Request::HTTP_METHOD_POST],
123+
'soap' => [
124+
'service' => self::PRODUCT_SERVICE_NAME,
125+
'serviceVersion' => self::SERVICE_VERSION,
126+
'operation' => self::PRODUCT_SERVICE_NAME . 'Save'
127+
]
128+
];
129+
$requestData = ['product' => $product];
130+
return $this->_webApiCall($serviceInfo, $requestData, null, 'all');
131+
}
132+
133+
/**
134+
* Returns product data.
135+
*
136+
* @return array
137+
*/
138+
private function getProductData(): array
139+
{
140+
return [
141+
'sku' => $this->productSku,
142+
'name' => 'simple',
143+
'type_id' => Type::TYPE_SIMPLE,
144+
'weight' => 1,
145+
'attribute_set_id' => 4,
146+
'price' => 10,
147+
'status' => 1,
148+
'visibility' => 4,
149+
'extension_attributes' => [
150+
'stock_item' => ['is_in_stock' => true, 'qty' => 1000]
151+
],
152+
'custom_attributes' => [
153+
['attribute_code' => 'url_key', 'value' => 'simple'],
154+
['attribute_code' => 'tax_class_id', 'value' => 2],
155+
['attribute_code' => 'category_ids', 'value' => [333]]
156+
]
157+
];
158+
}
159+
160+
/**
161+
* Asserts that product is linked to websites in 'catalog_product_website' table.
162+
*
163+
* @param string $sku
164+
* @param array $websiteIds
165+
* @return void
166+
*/
167+
private function assertProductWebsites(string $sku, array $websiteIds): void
168+
{
169+
$productId = $this->productRepository->get($sku)->getId();
170+
$this->assertEquals($websiteIds, $this->productWebsiteLink->getWebsiteIdsByProductId($productId));
171+
}
172+
173+
/**
174+
* Asserts result after product creation.
175+
*
176+
* @param array $productData
177+
* @param array $resultData
178+
* @param array $websiteIds
179+
* @return void
180+
*/
181+
private function assertProductData(array $productData, array $resultData, array $websiteIds): void
182+
{
183+
foreach ($productData as $key => $value) {
184+
if ($key == 'extension_attributes' || $key == 'custom_attributes') {
185+
continue;
186+
}
187+
$this->assertEquals($value, $resultData[$key]);
188+
}
189+
foreach ($productData['custom_attributes'] as $attribute) {
190+
$resultAttribute = $this->getCustomAttributeByCode(
191+
$resultData['custom_attributes'],
192+
$attribute['attribute_code']
193+
);
194+
$this->assertEquals($attribute['value'], $resultAttribute['value']);
195+
}
196+
foreach ($productData['extension_attributes']['stock_item'] as $key => $value) {
197+
$this->assertEquals($value, $resultData['extension_attributes']['stock_item'][$key]);
198+
}
199+
$this->assertEquals($websiteIds, $resultData['extension_attributes']['website_ids']);
200+
}
201+
202+
/**
203+
* Get list of all websites IDs.
204+
*
205+
* @return array
206+
*/
207+
private function getAllWebsiteIds(): array
208+
{
209+
$websiteIds = [];
210+
$websites = $this->storeManager->getWebsites();
211+
foreach ($websites as $website) {
212+
$websiteIds[] = $website->getId();
213+
}
214+
215+
return $websiteIds;
216+
}
217+
218+
/**
219+
* Returns custom attribute data by given code.
220+
*
221+
* @param array $attributes
222+
* @param string $attributeCode
223+
* @return array
224+
*/
225+
private function getCustomAttributeByCode(array $attributes, string $attributeCode): array
226+
{
227+
$items = array_filter(
228+
$attributes,
229+
function ($attribute) use ($attributeCode) {
230+
return $attribute['attribute_code'] == $attributeCode;
231+
}
232+
);
233+
234+
return reset($items);
235+
}
236+
}

0 commit comments

Comments
 (0)