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