Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Curl.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Mtf\Config\DataInterface;
11 use Magento\Mtf\Fixture\FixtureInterface;
12 use Magento\Mtf\Handler\Curl as AbstractCurl;
13 use Magento\Mtf\System\Event\EventManagerInterface;
14 use Magento\Mtf\Util\Protocol\CurlInterface;
17 
21 class Curl extends AbstractCurl implements CategoryInterface
22 {
28  protected $backendTransport;
29 
35  protected $fixture;
36 
42  protected $fields;
43 
49  protected $dataUseConfig = [
50  'available_sort_by',
51  'default_sort_by',
52  'filter_price_range',
53  ];
54 
60  protected $mappingData = [
61  'is_active' => [
62  'Yes' => 1,
63  'No' => 0,
64  ],
65  'include_in_menu' => [
66  'Yes' => 1,
67  'No' => 0,
68  ],
69  'display_mode' => [
70  'Static block and products' => 'PRODUCTS_AND_PAGE',
71  'Static block only' => 'PAGE',
72  'Products only' => 'PRODUCTS',
73  ],
74  'is_anchor' => [
75  'Yes' => 1,
76  'No' => 0,
77  ],
78  'available_product_listing_config' => [
79  'Yes' => 1,
80  'No' => 0,
81  ],
82  'custom_use_parent_settings' => [
83  'Yes' => 1,
84  'No' => 0,
85  ],
86  'custom_apply_to_products' => [
87  'Yes' => 1,
88  'No' => 0,
89  ],
90  'page_layout' => [
91  '1 column' => '1column',
92  '2 columns with left bar' => '2columns-left',
93  '2 columns with right bar' => '2columns-right',
94  '3 columns' => '3columns',
95  'Empty' => 'empty',
96  ]
97  ];
98 
104  protected $availableSortBy = [
105  'Position' => 'position',
106  'Name' => 'name',
107  'Price' => 'price',
108  ];
109 
116  public function __construct(
117  DataInterface $configuration,
118  EventManagerInterface $eventManager,
120  ) {
121  parent::__construct($configuration, $eventManager);
122  $this->backendTransport = $backendTransport;
123  }
124 
132  public function persist(FixtureInterface $fixture = null)
133  {
134  $data = $this->prepareData($fixture);
135  $url = $_ENV['app_backend_url'] . 'catalog/category/save/store/0/parent/' . $data['general']['parent_id'] . '/';
136 
137  $this->backendTransport->write($url, $data);
138  $response = $this->backendTransport->read();
139  $this->backendTransport->close();
140 
141  if (strpos($response, 'data-ui-id="messages-message-success"') === false) {
142  $this->_eventManager->dispatchEvent(['curl_failed'], [$response]);
143  throw new \Exception('Category creation by curl handler was not successful!');
144  }
145 
146  preg_match('#http://.+/id/(\d+).+store/#m', $response, $matches);
147  $id = isset($matches[1]) ? (int)$matches[1] : null;
148  return ['id' => $id];
149  }
150 
157  public function prepareData(FixtureInterface $fixture)
158  {
159  $this->fixture = $fixture;
160  $this->fields = ['general' => $fixture->getData()];
161 
162  $this->prepareGeneralInformation();
163  $this->prepareDisplaySetting();
164  $this->prepareCategoryProducts();
165 
166  $this->fields['general'] = $this->replaceMappingData($this->fields['general']);
167  return $this->fields;
168  }
169 
175  protected function prepareGeneralInformation()
176  {
177  $this->fields['general']['is_anchor'] = isset($this->fields['general']['is_anchor'])
178  ? $this->fields['general']['is_anchor']
179  : 'No';
180 
181  $this->fields['general']['include_in_menu'] = isset($this->fields['general']['include_in_menu'])
182  ? $this->fields['general']['include_in_menu']
183  : 'Yes';
184  }
185 
191  protected function prepareDisplaySetting()
192  {
193  if ($this->fixture->hasData('landing_page')) {
194  $this->fields['general']['landing_page'] = $this->getBlockId($this->fixture->getLandingPage());
195  }
196 
197  $this->prepareAvailableSortBy();
198 
199  $useConfig = array_diff($this->dataUseConfig, array_keys($this->fields['general']));
200  if (!empty($useConfig)) {
201  $this->fields['use_config'] = $useConfig;
202  }
203  unset($this->fields['general']['use_config']);
204  }
205 
211  protected function prepareAvailableSortBy()
212  {
213  if (isset($this->fields['general']['available_sort_by'])) {
214  foreach ($this->fields['general']['available_sort_by'] as $key => $value) {
215  $this->fields['general']['available_sort_by'][$key] = $this->availableSortBy[$value];
216  }
217  }
218  }
219 
225  protected function prepareCategoryProducts()
226  {
227  $categoryProducts = [];
228  $defaultPosition = 0;
229 
230  if ($this->fixture->hasData('category_products')) {
231  $products = $this->fixture->getDataFieldConfig('category_products')['source']->getProducts();
232  foreach ($products as $product) {
233  $categoryProducts[$product->getId()] = $defaultPosition;
234  }
235  }
236 
237  $this->fields['category_products'] = json_encode($categoryProducts);
238  unset($this->fields['general']['category_products']);
239  }
240 
247  protected function getBlockId($landingName)
248  {
249  $url = $_ENV['app_backend_url'] . 'catalog/category';
250  $curl = new BackendDecorator(new CurlTransport(), $this->_configuration);
251  $curl->write($url, [], CurlInterface::GET);
252  $response = $curl->read();
253  $curl->close();
254  preg_match('~\{"value":"(\d+)","label":"' . preg_quote($landingName) . '"\}~', $response, $matches);
255  $id = isset($matches[1]) ? (int)$matches[1] : null;
256 
257  return $id;
258  }
259 }
$response
Definition: 404.php:11
$configuration
Definition: index.php:33
$id
Definition: fieldset.phtml:14
$value
Definition: gender.phtml:16
prepareData(FixtureInterface $fixture)
Definition: Curl.php:157
persist(FixtureInterface $fixture=null)
Definition: Curl.php:132
__construct(DataInterface $configuration, EventManagerInterface $eventManager, BackendDecorator $backendTransport)
Definition: Curl.php:116