Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateConfigurableProductStep.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Catalog\Test\Fixture\CatalogProductAttribute;
10 use Magento\Catalog\Test\Page\Adminhtml\CatalogProductEdit;
13 use Magento\Catalog\Test\Page\Adminhtml\CatalogProductIndex;
14 use Magento\Mtf\Fixture\FixtureFactory;
15 use Magento\Mtf\TestStep\TestStepInterface;
16 
20 class UpdateConfigurableProductStep implements TestStepInterface
21 {
28 
34  protected $fixtureFactory;
35 
41  protected $deletedAttributes = [];
42 
48  protected $initialProduct;
49 
55  protected $product;
56 
62  protected $attributeTypeAction = '';
63 
67  private $productGrid;
68 
78  public function __construct(
79  FixtureFactory $fixtureFactory,
80  CatalogProductIndex $productGrid,
81  CatalogProductEdit $catalogProductEdit,
83  ConfigurableProduct $updatedProduct,
85  ) {
86  $this->fixtureFactory = $fixtureFactory;
87  $this->catalogProductEdit = $catalogProductEdit;
88  $this->initialProduct = $product;
89  $this->product = $updatedProduct;
90  $this->attributeTypeAction = $attributeTypeAction;
91  $this->productGrid = $productGrid;
92  }
93 
99  public function run()
100  {
101  $product = $this->prepareProduct($this->initialProduct, $this->product, $this->attributeTypeAction);
102  $this->updateProduct($product);
103 
104  return ['product' => $product, 'deletedProductAttributes' => $this->deletedAttributes];
105  }
106 
115  protected function prepareProduct(
119  ) {
120  if ($attributeTypeAction == 'deleteAll') {
121  $this->deletedAttributes = $initialProduct->getDataFieldConfig('configurable_attributes_data')['source']
122  ->getAttributes();
123  return $product;
124  }
125 
126  $dataProduct = $product->getData();
127  $dataInitialProduct = $initialProduct->getData();
128 
129  if ($attributeTypeAction == 'deleteLast') {
130  array_pop($dataInitialProduct['configurable_attributes_data']['attributes_data']);
131  $attributes = $initialProduct->getDataFieldConfig('configurable_attributes_data')['source']
132  ->getAttributes();
133  $this->deletedAttributes[] = array_pop($attributes);
134  }
135 
136  $attributesData = $dataInitialProduct['configurable_attributes_data']['attributes_data'];
137  if ($attributeTypeAction == 'addOptions') {
138  $this->addOptions($attributesData, $dataProduct['configurable_attributes_data']['attributes_data']);
139  } else {
140  $this->addAttributes($attributesData, $dataProduct['configurable_attributes_data']['attributes_data']);
141  }
142 
143  $dataProduct['configurable_attributes_data'] = [
144  'attributes_data' => $attributesData,
145  ];
146 
147  if ($product->hasData('category_ids')) {
148  $dataProduct['category_ids']['category'] = $product->getDataFieldConfig('category_ids')['source']
149  ->getCategories()[0];
150  }
151 
152  return $this->fixtureFactory->createByCode('configurableProduct', ['data' => $dataProduct]);
153  }
154 
162  protected function addOptions(array &$attributes, array $data)
163  {
164  foreach ($attributes as $key => $attribute) {
165  if (isset($data[$key])) {
166  $index = count($attribute['options']);
167  foreach ($data[$key]['options'] as $newOption) {
168  $attributes[$key]['options']['option_key_' . $index] = $newOption;
169  $index++;
170  }
171  }
172  }
173  }
174 
182  protected function addAttributes(array &$attributes, array $data)
183  {
184  $index = count($attributes);
185  foreach ($data as $attribute) {
186  $attributes['attribute_key_' . $index] = $attribute;
187  $index++;
188  }
189  }
190 
197  protected function updateProduct(ConfigurableProduct $product)
198  {
199  //open product
200  $filter = ['sku' => $this->initialProduct->getSku()];
201  $this->productGrid->open();
202  $this->productGrid->getProductGrid()->searchAndOpen($filter);
203 
204  //update
205  $productForm = $this->catalogProductEdit->getProductForm();
206  $productForm->openSection('variations');
208  $variationsSection = $productForm->getSection('variations');
209  $variationsSection->deleteVariations();
210  $this->catalogProductEdit->getProductForm()->fill($product);
211  }
212 }
prepareProduct(ConfigurableProduct $initialProduct, ConfigurableProduct $product, $attributeTypeAction)
$attributesData
__construct(FixtureFactory $fixtureFactory, CatalogProductIndex $productGrid, CatalogProductEdit $catalogProductEdit, ConfigurableProduct $product, ConfigurableProduct $updatedProduct, $attributeTypeAction='')
$attributes
Definition: matrix.phtml:13
$index
Definition: list.phtml:44