PageRenderTime 35ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/crazybutterfly815/magento2
PHP | 259 lines | 150 code | 27 blank | 82 comment | 5 complexity | b629bdfe67bf9e842aa9da1d18c7d7ad 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\Category;
  7. use Magento\Catalog\Test\Fixture\Category;
  8. use Magento\Mtf\Config\DataInterface;
  9. use Magento\Mtf\Fixture\FixtureInterface;
  10. use Magento\Mtf\Handler\Curl as AbstractCurl;
  11. use Magento\Mtf\System\Event\EventManagerInterface;
  12. use Magento\Mtf\Util\Protocol\CurlInterface;
  13. use Magento\Mtf\Util\Protocol\CurlTransport;
  14. use Magento\Mtf\Util\Protocol\CurlTransport\BackendDecorator;
  15. /**
  16. * Create new category via curl.
  17. */
  18. class Curl extends AbstractCurl implements CategoryInterface
  19. {
  20. /**
  21. * Curl transport for send request via backend.
  22. *
  23. * @var BackendDecorator
  24. */
  25. protected $backendTransport;
  26. /**
  27. * Category instance.
  28. *
  29. * @var Category
  30. */
  31. protected $fixture;
  32. /**
  33. * Prepared data for creating category.
  34. *
  35. * @var array
  36. */
  37. protected $fields;
  38. /**
  39. * Data use config for category.
  40. *
  41. * @var array
  42. */
  43. protected $dataUseConfig = [
  44. 'available_sort_by',
  45. 'default_sort_by',
  46. 'filter_price_range',
  47. ];
  48. /**
  49. * Mapping values for data.
  50. *
  51. * @var array
  52. */
  53. protected $mappingData = [
  54. 'is_active' => [
  55. 'Yes' => 1,
  56. 'No' => 0,
  57. ],
  58. 'include_in_menu' => [
  59. 'Yes' => 1,
  60. 'No' => 0,
  61. ],
  62. 'display_mode' => [
  63. 'Static block and products' => 'PRODUCTS_AND_PAGE',
  64. 'Static block only' => 'PAGE',
  65. 'Products only' => 'PRODUCTS',
  66. ],
  67. 'is_anchor' => [
  68. 'Yes' => 1,
  69. 'No' => 0,
  70. ],
  71. 'available_product_listing_config' => [
  72. 'Yes' => 1,
  73. 'No' => 0,
  74. ],
  75. 'custom_use_parent_settings' => [
  76. 'Yes' => 1,
  77. 'No' => 0,
  78. ],
  79. 'custom_apply_to_products' => [
  80. 'Yes' => 1,
  81. 'No' => 0,
  82. ],
  83. 'page_layout' => [
  84. '1 column' => '1column',
  85. '2 columns with left bar' => '2columns-left',
  86. '2 columns with right bar' => '2columns-right',
  87. '3 columns' => '3columns',
  88. 'Empty' => 'empty',
  89. ]
  90. ];
  91. /**
  92. * Mapping values for "available_sort_by" field.
  93. *
  94. * @var array
  95. */
  96. protected $availableSortBy = [
  97. 'Position' => 'position',
  98. 'Name' => 'name',
  99. 'Price' => 'price',
  100. ];
  101. /**
  102. * @constructor
  103. * @param DataInterface $configuration
  104. * @param EventManagerInterface $eventManager
  105. * @param BackendDecorator $backendTransport
  106. */
  107. public function __construct(
  108. DataInterface $configuration,
  109. EventManagerInterface $eventManager,
  110. BackendDecorator $backendTransport
  111. ) {
  112. parent::__construct($configuration, $eventManager);
  113. $this->backendTransport = $backendTransport;
  114. }
  115. /**
  116. * Post request for creating Subcategory.
  117. *
  118. * @param FixtureInterface|null $fixture [optional]
  119. * @return array
  120. * @throws \Exception
  121. */
  122. public function persist(FixtureInterface $fixture = null)
  123. {
  124. $data = $this->prepareData($fixture);
  125. $url = $_ENV['app_backend_url'] . 'catalog/category/save/store/0/parent/' . $data['general']['parent_id'] . '/';
  126. $this->backendTransport->write($url, $data);
  127. $response = $this->backendTransport->read();
  128. $this->backendTransport->close();
  129. if (!strpos($response, 'data-ui-id="messages-message-success"')) {
  130. $this->_eventManager->dispatchEvent(['curl_failed'], [$response]);
  131. throw new \Exception('Category creation by curl handler was not successful!');
  132. }
  133. preg_match('#http://.+/id/(\d+).+store/#m', $response, $matches);
  134. $id = isset($matches[1]) ? (int)$matches[1] : null;
  135. return ['id' => $id];
  136. }
  137. /**
  138. * Prepare category data for curl.
  139. *
  140. * @param FixtureInterface $fixture
  141. * @return array
  142. */
  143. public function prepareData(FixtureInterface $fixture)
  144. {
  145. $this->fixture = $fixture;
  146. $this->fields = ['general' => $fixture->getData()];
  147. $this->prepareGeneralInformation();
  148. $this->prepareDisplaySetting();
  149. $this->prepareCategoryProducts();
  150. $this->fields['general'] = $this->replaceMappingData($this->fields['general']);
  151. return $this->fields;
  152. }
  153. /**
  154. * Prepare data for "General Information" tab.
  155. *
  156. * @return void
  157. */
  158. protected function prepareGeneralInformation()
  159. {
  160. $this->fields['general']['is_anchor'] = isset($this->fields['general']['is_anchor'])
  161. ? $this->fields['general']['is_anchor']
  162. : 'No';
  163. $this->fields['general']['include_in_menu'] = isset($this->fields['general']['include_in_menu'])
  164. ? $this->fields['general']['include_in_menu']
  165. : 'Yes';
  166. }
  167. /**
  168. * Prepare data for "Display Setting" tab.
  169. *
  170. * @return void
  171. */
  172. protected function prepareDisplaySetting()
  173. {
  174. if ($this->fixture->hasData('landing_page')) {
  175. $this->fields['general']['landing_page'] = $this->getBlockId($this->fixture->getLandingPage());
  176. }
  177. $this->prepareAvailableSortBy();
  178. $useConfig = array_diff($this->dataUseConfig, array_keys($this->fields['general']));
  179. if (!empty($useConfig)) {
  180. $this->fields['use_config'] = $useConfig;
  181. }
  182. unset($this->fields['general']['use_config']);
  183. }
  184. /**
  185. * Prepare data for "available_sort_by" field.
  186. *
  187. * @return void
  188. */
  189. protected function prepareAvailableSortBy()
  190. {
  191. if (isset($this->fields['general']['available_sort_by'])) {
  192. foreach ($this->fields['general']['available_sort_by'] as $key => $value) {
  193. $this->fields['general']['available_sort_by'][$key] = $this->availableSortBy[$value];
  194. }
  195. }
  196. }
  197. /**
  198. * Prepare category products data for curl.
  199. *
  200. * @return void
  201. */
  202. protected function prepareCategoryProducts()
  203. {
  204. $categoryProducts = [];
  205. $defaultPosition = 0;
  206. if ($this->fixture->hasData('category_products')) {
  207. $products = $this->fixture->getDataFieldConfig('category_products')['source']->getProducts();
  208. foreach ($products as $product) {
  209. $categoryProducts[$product->getId()] = $defaultPosition;
  210. }
  211. }
  212. $this->fields['category_products'] = json_encode($categoryProducts);
  213. unset($this->fields['general']['category_products']);
  214. }
  215. /**
  216. * Getting block id by name.
  217. *
  218. * @param string $landingName
  219. * @return int|null
  220. */
  221. protected function getBlockId($landingName)
  222. {
  223. $url = $_ENV['app_backend_url'] . 'catalog/category';
  224. $curl = new BackendDecorator(new CurlTransport(), $this->_configuration);
  225. $curl->write($url, [], CurlInterface::GET);
  226. $response = $curl->read();
  227. $curl->close();
  228. preg_match('~\{"value":"(\d+)","label":"' . preg_quote($landingName) . '"\}~', $response, $matches);
  229. $id = isset($matches[1]) ? (int)$matches[1] : null;
  230. return $id;
  231. }
  232. }