PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/functional/tests/app/Magento/Catalog/Test/Handler/CatalogProductSimple/Curl.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 453 lines | 305 code | 35 blank | 113 comment | 21 complexity | c950e19557a35ee3210ec9e33da9a0cc MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Test\Handler\CatalogProductSimple;
  7. use Magento\Mtf\Fixture\FixtureInterface;
  8. use Magento\Mtf\Util\Protocol\CurlTransport;
  9. use Magento\Mtf\Handler\Curl as AbstractCurl;
  10. use Magento\Mtf\Util\Protocol\CurlTransport\BackendDecorator;
  11. /**
  12. * Create new simple product via curl.
  13. */
  14. class Curl extends AbstractCurl implements CatalogProductSimpleInterface
  15. {
  16. /**
  17. * Mapping values for data.
  18. *
  19. * @var array
  20. */
  21. protected $mappingData = [
  22. 'links_purchased_separately' => [
  23. 'Yes' => 1,
  24. 'No' => 0
  25. ],
  26. 'use_config_notify_stock_qty' => [
  27. 'Yes' => 1,
  28. 'No' => 0
  29. ],
  30. 'is_shareable' => [
  31. 'Yes' => 1,
  32. 'No' => 0,
  33. 'Use config' => 2
  34. ],
  35. 'required' => [
  36. 'Yes' => 1,
  37. 'No' => 0
  38. ],
  39. 'manage_stock' => [
  40. 'Yes' => 1,
  41. 'No' => 0
  42. ],
  43. 'use_config_manage_stock' => [
  44. 'Yes' => 1,
  45. 'No' => 0
  46. ],
  47. 'product_has_weight' => [
  48. 'Yes' => 1,
  49. 'No' => 0,
  50. ],
  51. 'use_config_enable_qty_increments' => [
  52. 'Yes' => 1,
  53. 'No' => 0
  54. ],
  55. 'use_config_qty_increments' => [
  56. 'Yes' => 1,
  57. 'No' => 0
  58. ],
  59. 'is_in_stock' => [
  60. 'In Stock' => 1,
  61. 'Out of Stock' => 0
  62. ],
  63. 'visibility' => [
  64. 'Not Visible Individually' => 1,
  65. 'Catalog' => 2,
  66. 'Search' => 3,
  67. 'Catalog, Search' => 4
  68. ],
  69. 'website_ids' => [
  70. 'Main Website' => 1
  71. ],
  72. 'status' => [
  73. 'Product offline' => 2,
  74. 'Product online' => 1
  75. ],
  76. 'is_require' => [
  77. 'Yes' => 1,
  78. 'No' => 0
  79. ],
  80. 'msrp_display_actual_price_type' => [
  81. 'Use config' => 0,
  82. 'On Gesture' => 1,
  83. 'In Cart' => 2,
  84. 'Before Order Confirmation' => 3
  85. ],
  86. 'enable_qty_increments' => [
  87. 'Yes' => 1,
  88. 'No' => 0,
  89. ],
  90. ];
  91. /**
  92. * Placeholder for price data sent Curl.
  93. *
  94. * @var array
  95. */
  96. protected $priceData = [
  97. 'website' => [
  98. 'name' => 'website_id',
  99. 'data' => [
  100. 'All Websites [USD]' => 0
  101. ]
  102. ],
  103. 'customer_group' => [
  104. 'name' => 'cust_group',
  105. 'data' => [
  106. 'ALL GROUPS' => 32000,
  107. 'NOT LOGGED IN' => 0,
  108. 'General' => 1
  109. ]
  110. ]
  111. ];
  112. /**
  113. * Placeholder for fpt data sent Curl
  114. *
  115. * @var array
  116. */
  117. protected $fptData = [
  118. 'website' => [
  119. 'name' => 'website_id',
  120. 'data' => [
  121. 'All Websites [USD]' => 0
  122. ]
  123. ],
  124. 'country_name' => [
  125. 'name' => 'country',
  126. 'data' => [
  127. 'United States' => 'US'
  128. ]
  129. ],
  130. 'state_name' => [
  131. 'name' => 'state',
  132. 'data' => [
  133. 'California' => 12,
  134. '*' => 0
  135. ]
  136. ]
  137. ];
  138. /**
  139. * Select custom options.
  140. *
  141. * @var array
  142. */
  143. protected $selectOptions = ['drop_down', 'radio', 'checkbox', 'multiple'];
  144. /**
  145. * Post request for creating simple product.
  146. *
  147. * @param FixtureInterface|null $fixture [optional]
  148. * @return array
  149. *
  150. * @SuppressWarnings(PHPMD.NPathComplexity)
  151. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  152. */
  153. public function persist(FixtureInterface $fixture = null)
  154. {
  155. $config = $fixture->getDataConfig();
  156. $prefix = isset($config['input_prefix']) ? $config['input_prefix'] : null;
  157. $data = $this->prepareData($fixture, $prefix);
  158. return $this->createProduct($data, $config);
  159. }
  160. /**
  161. * Prepare POST data for creating product request.
  162. *
  163. * @param FixtureInterface $fixture
  164. * @param string|null $prefix [optional]
  165. * @return array
  166. *
  167. * @SuppressWarnings(PHPMD.NPathComplexity)
  168. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  169. */
  170. protected function prepareData(FixtureInterface $fixture, $prefix = null)
  171. {
  172. $fields = $this->replaceMappingData($fixture->getData());
  173. if (!isset($fields['status'])) {
  174. // Default product is enabled
  175. $fields['status'] = 1;
  176. }
  177. if (!isset($fields['visibility'])) {
  178. // Default product is visible on Catalog, Search
  179. $fields['visibility'] = 4;
  180. }
  181. // Getting Tax class id
  182. if ($fixture->hasData('tax_class_id')) {
  183. $fields['tax_class_id'] = $fixture->getDataFieldConfig('tax_class_id')['source']->getTaxClassId();
  184. }
  185. if (!empty($fields['category_ids'])) {
  186. $categoryIds = [];
  187. foreach ($fixture->getDataFieldConfig('category_ids')['source']->getCategories() as $category) {
  188. $categoryIds[] = $category->getId();
  189. }
  190. $fields['category_ids'] = $categoryIds;
  191. }
  192. if (isset($fields['tier_price'])) {
  193. $fields['tier_price'] = $this->preparePriceData($fields['tier_price']);
  194. }
  195. if (isset($fields['fpt'])) {
  196. $attributeLabel = $fixture->getDataFieldConfig('attribute_set_id')['source']
  197. ->getAttributeSet()->getDataFieldConfig('assigned_attributes')['source']
  198. ->getAttributes()[0]->getFrontendLabel();
  199. $fields[$attributeLabel] = $this->prepareFptData($fields['fpt']);
  200. }
  201. if ($isCustomOptions = isset($fields['custom_options'])) {
  202. $fields = $this->prepareCustomOptionsData($fields);
  203. }
  204. if (!empty($fields['website_ids'])) {
  205. foreach ($fields['website_ids'] as &$value) {
  206. $value = isset($this->mappingData['website_ids'][$value])
  207. ? $this->mappingData['website_ids'][$value]
  208. : $value;
  209. }
  210. }
  211. // Getting Attribute Set id
  212. if ($fixture->hasData('attribute_set_id')) {
  213. $attributeSetId = $fixture
  214. ->getDataFieldConfig('attribute_set_id')['source']
  215. ->getAttributeSet()
  216. ->getAttributeSetId();
  217. $fields['attribute_set_id'] = $attributeSetId;
  218. }
  219. // Prepare assigned attribute
  220. if (isset($fields['attributes'])) {
  221. $fields += $fields['attributes'];
  222. unset($fields['attributes']);
  223. }
  224. if (isset($fields['custom_attribute'])) {
  225. $fields[$fields['custom_attribute']['code']] = $fields['custom_attribute']['value'];
  226. }
  227. $fields = $this->prepareStockData($fields);
  228. $fields = $prefix ? [$prefix => $fields] : $fields;
  229. if ($isCustomOptions) {
  230. $fields['affect_product_custom_options'] = 1;
  231. }
  232. return $fields;
  233. }
  234. /**
  235. * Preparation of custom options data.
  236. *
  237. * @param array $fields
  238. * @return array
  239. */
  240. protected function prepareCustomOptionsData(array $fields)
  241. {
  242. $options = [];
  243. foreach ($fields['custom_options'] as $key => $customOption) {
  244. $options[$key] = [
  245. 'is_delete' => '',
  246. 'option_id' => 0,
  247. 'type' => $this->optionNameConvert($customOption['type']),
  248. ];
  249. foreach ($customOption['options'] as $index => $option) {
  250. $customOption['options'][$index]['is_delete'] = '';
  251. $customOption['options'][$index]['price_type'] = strtolower($option['price_type']);
  252. }
  253. $options[$key] += in_array($options[$key]['type'], $this->selectOptions)
  254. ? ['values' => $customOption['options']]
  255. : $customOption['options'][0];
  256. unset($customOption['options']);
  257. $options[$key] += $customOption;
  258. }
  259. $fields['options'] = $options;
  260. unset($fields['custom_options']);
  261. return $fields;
  262. }
  263. /**
  264. * Convert option name.
  265. *
  266. * @param string $optionName
  267. * @return string
  268. */
  269. protected function optionNameConvert($optionName)
  270. {
  271. $optionName = substr($optionName, strpos($optionName, "/") + 1);
  272. $optionName = str_replace(['-', ' & '], "_", trim($optionName));
  273. $end = strpos($optionName, ' ');
  274. if ($end !== false) {
  275. $optionName = substr($optionName, 0, $end);
  276. }
  277. return strtolower($optionName);
  278. }
  279. /**
  280. * Preparation of stock data.
  281. *
  282. * @param array $fields
  283. * @return array
  284. *
  285. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  286. * @SuppressWarnings(PHPMD.NPathComplexity)
  287. */
  288. protected function prepareStockData(array $fields)
  289. {
  290. if (isset($fields['quantity_and_stock_status']) && !is_array($fields['quantity_and_stock_status'])) {
  291. $fields['quantity_and_stock_status'] = [
  292. 'qty' => $fields['qty'],
  293. 'is_in_stock' => $fields['quantity_and_stock_status']
  294. ];
  295. }
  296. if (!isset($fields['stock_data']['is_in_stock'])) {
  297. $fields['stock_data']['is_in_stock'] = isset($fields['quantity_and_stock_status']['is_in_stock'])
  298. ? $fields['quantity_and_stock_status']['is_in_stock']
  299. : (isset($fields['inventory_manage_stock']) ? $fields['inventory_manage_stock'] : null);
  300. }
  301. if (!isset($fields['stock_data']['qty'])) {
  302. $fields['stock_data']['qty'] = isset($fields['quantity_and_stock_status']['qty'])
  303. ? $fields['quantity_and_stock_status']['qty']
  304. : null;
  305. }
  306. if (!isset($fields['stock_data']['manage_stock'])) {
  307. $fields['stock_data']['manage_stock'] = (int)(!empty($fields['stock_data']['qty'])
  308. || !empty($fields['stock_data']['is_in_stock']));
  309. }
  310. return $this->filter($fields);
  311. }
  312. /**
  313. * Preparation of tier price data.
  314. *
  315. * @param array $fields
  316. * @return array
  317. *
  318. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  319. * @SuppressWarnings(PHPMD.NPathComplexity)
  320. */
  321. protected function preparePriceData(array $fields)
  322. {
  323. foreach ($fields as &$field) {
  324. foreach ($this->priceData as $key => $data) {
  325. $field[$data['name']] = $this->priceData[$key]['data'][$field[$key]];
  326. unset($field[$key]);
  327. }
  328. $field['delete'] = '';
  329. }
  330. return $fields;
  331. }
  332. /**
  333. * Preparation of fpt data
  334. *
  335. * @param array $fields
  336. * @return array
  337. */
  338. protected function prepareFptData(array $fields)
  339. {
  340. foreach ($fields as &$field) {
  341. foreach ($this->fptData as $key => $data) {
  342. $field[$data['name']] = $this->fptData[$key]['data'][$field[$key]];
  343. unset($field[$key]);
  344. }
  345. $field['delete'] = '';
  346. }
  347. return $fields;
  348. }
  349. /**
  350. * Remove items from a null.
  351. *
  352. * @param array $data
  353. * @return array
  354. */
  355. protected function filter(array $data)
  356. {
  357. foreach ($data as $key => $value) {
  358. if ($value === null) {
  359. unset($data[$key]);
  360. } elseif (is_array($data[$key])) {
  361. $data[$key] = $this->filter($data[$key]);
  362. }
  363. }
  364. return $data;
  365. }
  366. /**
  367. * Create product via curl.
  368. *
  369. * @param array $data
  370. * @param array $config
  371. * @return array
  372. * @throws \Exception
  373. */
  374. protected function createProduct(array $data, array $config)
  375. {
  376. $config['create_url_params']['set'] = isset($data['product']['attribute_set_id'])
  377. ? $data['product']['attribute_set_id']
  378. : $config['create_url_params']['set'];
  379. $url = $this->getUrl($config);
  380. $curl = new BackendDecorator(new CurlTransport(), $this->_configuration);
  381. $curl->addOption(CURLOPT_HEADER, 1);
  382. $curl->write($url, $data);
  383. $response = $curl->read();
  384. $curl->close();
  385. if (!strpos($response, 'data-ui-id="messages-message-success"')) {
  386. $this->_eventManager->dispatchEvent(['curl_failed'], [$response]);
  387. throw new \Exception('Product creation by curl handler was not successful!');
  388. }
  389. return $this->parseResponse($response);
  390. }
  391. /**
  392. * Parse data in response.
  393. *
  394. * @param string $response
  395. * @return array
  396. */
  397. protected function parseResponse($response)
  398. {
  399. preg_match('~Location: [^\s]*\/id\/(\d+)~', $response, $matches);
  400. $id = isset($matches[1]) ? $matches[1] : null;
  401. return ['id' => $id];
  402. }
  403. /**
  404. * Retrieve URL for request with all necessary parameters.
  405. *
  406. * @param array $config
  407. * @return string
  408. */
  409. protected function getUrl(array $config)
  410. {
  411. $requestParams = isset($config['create_url_params']) ? $config['create_url_params'] : [];
  412. $params = '';
  413. foreach ($requestParams as $key => $value) {
  414. $params .= $key . '/' . $value . '/';
  415. }
  416. return $_ENV['app_backend_url'] . 'catalog/product/save/' . $params . 'popup/1/back/edit';
  417. }
  418. }