Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ImagesAndVideos.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Mtf\Client\Element\SimpleElement;
11 
15 class ImagesAndVideos extends Tab
16 {
22  private $firstVideoId = 0;
23 
29  protected $addVideoButton = '[data-role="add-video-button"]';
30 
36  protected $newVideoDialog = '.mage-new-video-dialog';
37 
43  protected $imageItem = '.image.item';
44 
50  public function getVideoDialog()
51  {
52  $this->waitForElementVisible($this->newVideoDialog);
53  return $this->blockFactory->create(
54  \Magento\ProductVideo\Test\Block\Adminhtml\Product\Edit\Tab\Images\VideoDialog::class,
55  ['element' => $this->browser->find($this->newVideoDialog)]
56  );
57  }
58 
64  protected function clickAddVideo()
65  {
66  $this->_rootElement->find($this->addVideoButton)->click();
67  }
68 
77  public function setFieldsData(array $fields, SimpleElement $element = null)
78  {
79  if (!array_key_exists('images', $fields['media_gallery']['value'])) {
80  return $this;
81  }
82  if ($fields['media_gallery']['value']['images'] == '') {
83  $fields['media_gallery']['value']['images'] = [];
84  }
85  $currentImages = $this->getImageIds();
86  $newImages = array_keys($fields['media_gallery']['value']['images']);
87  $updateIds = array_intersect($currentImages, $newImages);
88  $addIds = array_diff($newImages, $currentImages);
89  $deleteIds = array_diff($currentImages, $newImages);
90 
91  foreach ($updateIds as $id) {
92  $this->updateVideo($id, $fields['media_gallery']['value']['images'][$id]);
93  }
94 
95  foreach ($deleteIds as $id) {
96  $this->deleteVideo($id);
97  }
98 
99  foreach ($addIds as $id) {
100  $this->addVideo($fields['media_gallery']['value']['images'][$id]);
101  }
102 
103  return $this;
104  }
105 
112  protected function addVideo($data)
113  {
114  $this->clickAddVideo();
115  $this->getVideoDialog()->fillForm($data)->clickSaveButton();
116  }
117 
124  protected function deleteVideo($id)
125  {
126  $this->clickVideo($id);
127  $this->getVideoDialog()->clickDeleteButton();
128  }
129 
137  protected function updateVideo($id, $data)
138  {
139  $this->clickVideo($id);
140  $this->getVideoDialog()->fillForm($data)->clickEditButton();
141  }
142 
151  public function getFieldsData($tabFields = null, SimpleElement $element = null)
152  {
153  $fields = reset($tabFields);
154  $name = key($tabFields);
155  $formData = [];
156  if (empty($fields['value']) || !array_key_exists('images', $fields['value'])) {
157  return '';
158  }
159  if ($fields['value']['images'] == '') {
160  $fields['value']['images'] = [];
161  }
162 
163  $formData[$name]['images'] = [];
164 
165  $imageArray = $fields['value']['images'];
166  $resetImages = array_flip(array_keys($imageArray));
167 
168  foreach ($imageArray as $keyRoot => $fieldSet) {
169  $image = $this->_rootElement->find($this->getImageSelector($resetImages[$keyRoot]));
170  if ($image) {
171  $image->click();
172  $videoDialog = $this->getVideoDialog();
173  $data = $videoDialog->getVideoInfo();
174  foreach (array_keys($fieldSet) as $field) {
175  if (isset($data[$field])) {
176  $formData[$name]['images'][$keyRoot][$field] = $data[$field];
177  }
178  }
179  $videoDialog->clickCloseButton();
180  }
181  }
182 
183  if (count($formData[$name]['images']) == 0) {
184  $formData[$name]['images'] = '';
185  }
186 
187  return $formData;
188  }
189 
196  protected function getImageSelector($id)
197  {
198  ++$id;
199  return $this->imageItem . ':nth-child(' . $id . ') .product-image-wrapper';
200  }
201 
207  protected function getImageIds()
208  {
209  $images = $this->_rootElement->getElements($this->imageItem);
210  return array_keys($images);
211  }
212 
219  protected function clickVideo($id)
220  {
221  $this->_rootElement->find($this->getImageSelector($id))->click();
222  }
223 
229  public function clickFirstVideo()
230  {
231  $this->_rootElement->find($this->getImageSelector($this->firstVideoId))->click();
232  return $this;
233  }
234 
240  public function deleteFirstVideo()
241  {
242  $this->deleteVideo($this->firstVideoId);
243  }
244 }
$id
Definition: fieldset.phtml:14
$fields
Definition: details.phtml:14
if(!isset($_GET['name'])) $name
Definition: log.php:14
$element
Definition: element.phtml:12