PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/functional/tests/app/Magento/Bundle/Test/Handler/BundleProduct/Curl.php

https://bitbucket.org/crismablanco/magento
PHP | 206 lines | 141 code | 20 blank | 45 comment | 3 complexity | c699384256a2ec766df78c9f616f7cb3 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Test\Handler\BundleProduct;
  7. use Magento\Bundle\Test\Fixture\BundleProduct;
  8. use Magento\Catalog\Test\Handler\CatalogProductSimple\Curl as ProductCurl;
  9. use Magento\Mtf\Fixture\FixtureInterface;
  10. use Magento\Mtf\Config\DataInterface;
  11. use Magento\Mtf\System\Event\EventManagerInterface;
  12. /**
  13. * Create new bundle product via curl.
  14. */
  15. class Curl extends ProductCurl implements BundleProductInterface
  16. {
  17. /**
  18. * @constructor
  19. * @param DataInterface $configuration
  20. * @param EventManagerInterface $eventManager
  21. */
  22. public function __construct(DataInterface $configuration, EventManagerInterface $eventManager)
  23. {
  24. parent::__construct($configuration, $eventManager);
  25. $this->mappingData += [
  26. 'selection_can_change_qty' => [
  27. 'Yes' => 1,
  28. 'No' => 0,
  29. ],
  30. 'sku_type' => [
  31. 'Yes' => 0,
  32. 'No' => 1,
  33. ],
  34. 'price_type' => [
  35. 'Yes' => 0,
  36. 'No' => 1,
  37. ],
  38. 'weight_type' => [
  39. 'Yes' => 0,
  40. 'No' => 1,
  41. ],
  42. 'shipment_type' => [
  43. 'Together' => 0,
  44. 'Separately' => 1,
  45. ],
  46. 'type' => [
  47. 'Drop-down' => 'select',
  48. 'Radio Buttons' => 'radio',
  49. 'Checkbox' => 'checkbox',
  50. 'Multiple Select' => 'multi',
  51. ],
  52. 'selection_price_type' => [
  53. 'Fixed' => 0,
  54. 'Percent' => 1,
  55. ],
  56. 'price_view' => [
  57. 'Price Range' => 0,
  58. 'As Low as' => 1,
  59. ],
  60. 'use_config_gift_message_available' => [
  61. 'Yes' => 1,
  62. 'No' => 0
  63. ],
  64. 'gift_message_available' => [
  65. 'Yes' => 1,
  66. 'No' => 0
  67. ],
  68. 'user_defined' => [
  69. 'Yes' => 1,
  70. 'No' => 0
  71. ]
  72. ];
  73. }
  74. /**
  75. * Prepare POST data for creating product request.
  76. *
  77. * @param FixtureInterface $fixture
  78. * @return array
  79. */
  80. public function prepareData(FixtureInterface $fixture)
  81. {
  82. $this->fields = parent::prepareData($fixture);
  83. $this->prepareBundleItems();
  84. $this->fields = $this->replaceMappingData($this->fields);
  85. return $this->fields;
  86. }
  87. /**
  88. * Preparation of "Product Details" tab data.
  89. *
  90. * @return void
  91. */
  92. protected function prepareProductDetails()
  93. {
  94. parent::prepareProductDetails();
  95. if (!isset($this->fields['product']['price_type'])) {
  96. $this->fields['product']['price_type'] = 'Yes';
  97. }
  98. }
  99. /**
  100. * Preparation of "Advanced Pricing" tab data.
  101. *
  102. * @return void
  103. */
  104. protected function prepareAdvancedPricing()
  105. {
  106. parent::prepareAdvancedPricing();
  107. if (!isset($this->fields['product']['price_view'])) {
  108. $this->fields['product']['price_view'] = 'Price Range';
  109. }
  110. }
  111. /**
  112. * Preparation of selections data.
  113. *
  114. * @return void
  115. */
  116. protected function prepareBundleItems()
  117. {
  118. $selections = [];
  119. $bundleSelections = [];
  120. if (!empty($this->fields['product']['bundle_selections'])) {
  121. $selections = $this->fields['product']['bundle_selections'];
  122. $products = $selections['products'];
  123. unset($this->fields['product']['selections'], $selections['products']);
  124. foreach ($selections['bundle_options'] as $key => &$option) {
  125. $option['delete'] = '';
  126. $option['position'] = $key;
  127. foreach ($option['assigned_products'] as $productKey => $assignedProduct) {
  128. $assignedProduct['data'] += [
  129. 'product_id' => $products[$key][$productKey]->getId(),
  130. 'delete' => '',
  131. 'position' => $productKey
  132. ];
  133. $bundleSelections[$key][] = $assignedProduct['data'];
  134. }
  135. unset($option['assigned_products']);
  136. }
  137. }
  138. $this->fields = array_merge($this->fields, $selections);
  139. $this->fields['bundle_selections'] = $bundleSelections;
  140. }
  141. /**
  142. * Parse response.
  143. *
  144. * @param string $response
  145. * @return array
  146. */
  147. protected function parseResponse($response)
  148. {
  149. return array_replace_recursive(parent::parseResponse($response), $this->parseResponseSelections($response));
  150. }
  151. /**
  152. * Parse bundle selections in response.
  153. *
  154. * @param string $response
  155. * @return array
  156. */
  157. protected function parseResponseSelections($response)
  158. {
  159. $selectionIdKey = 1;
  160. $optionIdKey = 2;
  161. $productNameKey = 3;
  162. $responseSelections = [];
  163. $bundleSelections = $this->fixture->getBundleSelections();
  164. preg_match_all(
  165. '/{.*"selection_id":"(\d+)".*"option_id":"(\d+)".*"name":"([^"]+)".*}/',
  166. $response,
  167. $matches,
  168. PREG_SET_ORDER
  169. );
  170. foreach ($matches as $match) {
  171. $productName = $match[$productNameKey];
  172. $responseSelections[$productName] = [
  173. 'selection_id' => $match[$selectionIdKey],
  174. 'option_id' => $match[$optionIdKey],
  175. ];
  176. }
  177. foreach ($bundleSelections['bundle_options'] as $optionKey => $option) {
  178. foreach ($option['assigned_products'] as $assignedKey => $optionValue) {
  179. $productName = $optionValue['search_data']['name'];
  180. $bundleSelections['bundle_options'][$optionKey]['assigned_products'][$assignedKey] +=
  181. $responseSelections[$productName];
  182. }
  183. }
  184. return ['bundle_selections' => $bundleSelections];
  185. }
  186. }