|
10 | 10 | use Magento\Mtf\Client\Element\SimpleElement;
|
11 | 11 | use Magento\Mtf\Client\Locator;
|
12 | 12 | use Magento\Mtf\Fixture\FixtureInterface;
|
| 13 | +use Magento\Mtf\Client\ElementInterface; |
13 | 14 |
|
14 | 15 | /**
|
15 | 16 | * Select version block.
|
@@ -71,4 +72,161 @@ public function chooseUpgradeOtherComponents()
|
71 | 72 | $this->waitForElementVisible($this->loader);
|
72 | 73 | $this->waitForElementNotVisible($this->loader);
|
73 | 74 | }
|
| 75 | + |
| 76 | + /** |
| 77 | + * Set maximum compatible sample data for each row |
| 78 | + * Iterates through each page of the grid and sets the compatible version from fixture |
| 79 | + * |
| 80 | + * @param string $sampleDataVersion |
| 81 | + * @return void |
| 82 | + */ |
| 83 | + public function chooseVersionUpgradeOtherComponents($sampleDataVersion) |
| 84 | + { |
| 85 | + do { |
| 86 | + $this->iterateAndSetComponentsRows($this->convertVersionFixtureToRegexp($sampleDataVersion)); |
| 87 | + } while ($this->canClickOnNextPage()); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Gets components rows as ElementInterface |
| 92 | + * |
| 93 | + * @return ElementInterface[] |
| 94 | + */ |
| 95 | + private function getComponentsTableRows() |
| 96 | + { |
| 97 | + return $this->_rootElement->getElements("table.data-grid tbody tr"); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Iterate through components in table and set compatible version for selected magento version |
| 102 | + * |
| 103 | + * @param $sampleDataVersion |
| 104 | + * @return void |
| 105 | + */ |
| 106 | + private function iterateAndSetComponentsRows($sampleDataVersion) |
| 107 | + { |
| 108 | + $rows = $this->getComponentsTableRows(); |
| 109 | + for ($rowIndex = 1; $rowIndex <= count($rows); $rowIndex++) { |
| 110 | + $textElement = $this->getRowComponentTitle($rowIndex); |
| 111 | + if ($this->titleContainsSampleData($textElement)) { |
| 112 | + $this->setSampleDataVersionToRowSelect($rowIndex, $sampleDataVersion); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Clicks on Next Page of the grid |
| 119 | + * |
| 120 | + * @return void |
| 121 | + */ |
| 122 | + private function clickOnNextPage() |
| 123 | + { |
| 124 | + $this->_rootElement->find(".admin__data-grid-pager .action-next", Locator::SELECTOR_CSS)->click(); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Can click on next page |
| 129 | + * |
| 130 | + * @return bool |
| 131 | + */ |
| 132 | + private function canClickOnNextPage() |
| 133 | + { |
| 134 | + $element = $this->_rootElement->find(".admin__data-grid-pager .action-next"); |
| 135 | + if ($element->isVisible()) { |
| 136 | + $result = !$element->isDisabled(); |
| 137 | + $this->clickOnNextPage(); |
| 138 | + return $result; |
| 139 | + } |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Gets rows component title |
| 145 | + * |
| 146 | + * @param int $rowIndex |
| 147 | + * @return ElementInterface |
| 148 | + */ |
| 149 | + private function getRowComponentTitle($rowIndex) |
| 150 | + { |
| 151 | + return $this->_rootElement->find( |
| 152 | + "//table//tbody//tr[" . $rowIndex . "]//td//*[contains(text(),'sample')]", |
| 153 | + Locator::SELECTOR_XPATH |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Gets the select element from row |
| 159 | + * |
| 160 | + * @param int $rowIndex |
| 161 | + * @return ElementInterface |
| 162 | + */ |
| 163 | + private function getSelectFromRow($rowIndex) |
| 164 | + { |
| 165 | + return $this->_rootElement->find( |
| 166 | + '//table//tbody//tr[' . $rowIndex . ']//td//select', |
| 167 | + Locator::SELECTOR_XPATH, |
| 168 | + 'select' |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Convert sample data version fixture to regexp format |
| 174 | + * Example 100.1.* to 100\.1\.[0-9]+ |
| 175 | + * |
| 176 | + * @param string $sampleDataVersion |
| 177 | + * @return string |
| 178 | + * @throws \Exception |
| 179 | + */ |
| 180 | + private function convertVersionFixtureToRegexp($sampleDataVersion) |
| 181 | + { |
| 182 | + if (!preg_match('/\d+(?:\.*\d*)*/', $sampleDataVersion)) { |
| 183 | + throw new \Exception('Wrong format for sample data version fixture. Example: 100.1.* needed.'); |
| 184 | + } |
| 185 | + return str_replace('*', '[0-9]+', $sampleDataVersion); |
| 186 | + } |
| 187 | + |
| 188 | + /** |
| 189 | + * Asserts if element's text contains sample data |
| 190 | + * |
| 191 | + * @param ElementInterface $element |
| 192 | + * @return bool |
| 193 | + */ |
| 194 | + private function titleContainsSampleData($element) |
| 195 | + { |
| 196 | + return preg_match('/magento\/.*sample-data/', $element->getText()); |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Sets sample data version matching the maximum compatible version from fixture |
| 201 | + * |
| 202 | + * @param int $rowIndex |
| 203 | + * @param string $sampleDataVersionForRegex |
| 204 | + * @return void |
| 205 | + */ |
| 206 | + private function setSampleDataVersionToRowSelect($rowIndex, $sampleDataVersionForRegex) |
| 207 | + { |
| 208 | + $selectElement = $this->getSelectFromRow($rowIndex); |
| 209 | + $optionTextArray = []; |
| 210 | + foreach ($selectElement->getElements('option') as $option) { |
| 211 | + $optionText = $option->getText(); |
| 212 | + if (preg_match('/' . $sampleDataVersionForRegex . '/', $optionText)) { |
| 213 | + preg_match('/([0-9\.\-a-zA-Z]+)/', $optionText, $match); |
| 214 | + $optionTextArray[$optionText] = current($match); |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + if (!empty($optionTextArray)) { |
| 219 | + uasort( |
| 220 | + $optionTextArray, |
| 221 | + function ($versionOne, $versionTwo) { |
| 222 | + return version_compare($versionOne, $versionTwo) * -1; |
| 223 | + } |
| 224 | + ); |
| 225 | + |
| 226 | + $toSelectVersion = key($optionTextArray); |
| 227 | + if ($toSelectVersion !== $selectElement->getText()) { |
| 228 | + $selectElement->setValue($toSelectVersion); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
74 | 232 | }
|
0 commit comments