Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AssertProductForm.php
Go to the documentation of this file.
1 <?php
8 
9 use Magento\Catalog\Test\Page\Adminhtml\CatalogProductEdit;
10 use Magento\Catalog\Test\Page\Adminhtml\CatalogProductIndex;
12 use Magento\Mtf\Fixture\FixtureInterface;
13 
18 {
24  protected $skippedFixtureFields = [
25  'id',
26  'checkout_data',
27  ];
28 
34  protected $sortFields = [
35  'cross_sell_products::id',
36  'up_sell_products::id',
37  'related_products::id',
38  ];
39 
45  protected $specialArray = [];
46 
55  public function processAssert(
56  FixtureInterface $product,
57  CatalogProductIndex $productGrid,
58  CatalogProductEdit $productPage
59  ) {
60  $filter = ['sku' => $product->getSku()];
61  $productGrid->open();
62  $productGrid->getProductGrid()->searchAndOpen($filter);
63 
64  $productData = $product->getData();
65  if ($product->hasData('custom_options')) {
66  $customOptionsSource = $product->getDataFieldConfig('custom_options')['source'];
67  $productData['custom_options'] = $customOptionsSource->getCustomOptions();
68  }
69  $fixtureData = $this->prepareFixtureData($productData, $this->sortFields);
70  $formData = $this->prepareFormData($productPage->getProductForm()->getData($product), $this->sortFields);
71  $error = $this->verifyData($fixtureData, $formData);
72  \PHPUnit\Framework\Assert::assertTrue(empty($error), $error);
73  }
74 
82  protected function prepareFixtureData(array $data, array $sortFields = [])
83  {
84  $data = array_diff_key($data, array_flip($this->skippedFixtureFields));
85 
86  if (isset($data['website_ids']) && !is_array($data['website_ids'])) {
87  $data['website_ids'] = [$data['website_ids']];
88  }
89 
90  if (!empty($this->specialArray)) {
91  $data = $this->prepareSpecialPriceArray($data);
92  }
93 
94  if (isset($data['price'])) {
95  $data['price'] = number_format($data['price'], 2);
96  }
97 
98  foreach ($sortFields as $path) {
99  $data = $this->sortDataByPath($data, $path);
100  }
101 
102  return $data;
103  }
104 
111  protected function prepareSpecialPriceArray(array $fields)
112  {
113  foreach ($this->specialArray as $key => $value) {
114  if (array_key_exists($key, $fields)) {
115  if (isset($value['type']) && $value['type'] == 'date') {
116  $fields[$key] = vsprintf('%d/%d/%d', explode('/', $fields[$key]));
117  }
118  }
119  }
120  return $fields;
121  }
122 
130  protected function prepareFormData(array $data, array $sortFields = [])
131  {
132  foreach ($sortFields as $path) {
133  $data = $this->sortDataByPath($data, $path);
134  }
135 
136  return $data;
137  }
138 
144  public function toString()
145  {
146  return 'Form data equal the fixture data.';
147  }
148 }
prepareFormData(array $data, array $sortFields=[])
processAssert(FixtureInterface $product, CatalogProductIndex $productGrid, CatalogProductEdit $productPage)
$fields
Definition: details.phtml:14
verifyData(array $fixtureData, array $formData, $isStrict=false, $isPrepareError=true)
$value
Definition: gender.phtml:16
prepareFixtureData(array $data, array $sortFields=[])
$productData