PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tests/functional/tests/app/Mage/Adminhtml/Test/Block/Widget/FormTabs.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 302 lines | 173 code | 21 blank | 108 comment | 10 complexity | 30d9897db21038bbe2c3cc9bcc8cc0f4 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Tests
  22. * @package Tests_Functional
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. namespace Mage\Adminhtml\Test\Block\Widget;
  27. use Magento\Mtf\Client\Element\SimpleElement as Element;
  28. use Magento\Mtf\Client\Locator;
  29. use Magento\Mtf\Fixture\FixtureInterface;
  30. use Magento\Mtf\Block\Form;
  31. /**
  32. * Is used to represent any form with tabs on the page.
  33. *
  34. * @SuppressWarnings(PHPMD.NumberOfChildren)
  35. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  36. */
  37. class FormTabs extends Form
  38. {
  39. /**
  40. * Tabs array.
  41. *
  42. * @var array
  43. */
  44. protected $tabs = [];
  45. /**
  46. * Fields which aren't assigned to any tab.
  47. *
  48. * @var array
  49. */
  50. protected $unassignedFields = [];
  51. /**
  52. * Initialize block.
  53. *
  54. * @return void
  55. */
  56. protected function init()
  57. {
  58. $this->tabs = $this->getFormMapping();
  59. }
  60. /**
  61. * Get path for form *.xml file with mapping
  62. *
  63. * @return string
  64. */
  65. protected function getFormMapping()
  66. {
  67. $result = [];
  68. $paths = $this->getPaths();
  69. foreach ($paths as $path) {
  70. $content = $this->mapper->read($path);
  71. if (is_array($content)) {
  72. $result = array_replace_recursive($result, $content);
  73. }
  74. }
  75. return $result;
  76. }
  77. /**
  78. * Get xml files paths for merge.
  79. *
  80. * @return array
  81. */
  82. protected function getPaths()
  83. {
  84. $realPath = str_replace('\\', '/', get_class($this)) . '.xml';
  85. $paths = glob(MTF_TESTS_PATH . preg_replace('/Mage\/\w+/', '*/*', $realPath));
  86. if (strpos($realPath, 'Adminhtml') !== false) {
  87. $paths = array_merge(
  88. $paths,
  89. glob(MTF_TESTS_PATH . preg_replace('/Mage\/(\w+)(\/.*Block\/)/', '*/*$2$1/', $realPath)),
  90. glob(MTF_TESTS_PATH . preg_replace('@.*Adminhtml/(.*)@', '*/Adminhtml/Test/Block/$1', $realPath)),
  91. glob(MTF_TESTS_PATH . preg_replace('/.*Adminhtml\/(.*)/', '*/*/Test/Block/*/$1', $realPath))
  92. );
  93. }
  94. return array_reverse($paths);
  95. }
  96. /**
  97. * Fill form with tabs.
  98. *
  99. * @param FixtureInterface $fixture
  100. * @param Element|null $element
  101. * @return FormTabs
  102. */
  103. public function fill(FixtureInterface $fixture, Element $element = null)
  104. {
  105. $tabs = $this->getFieldsByTabs($fixture);
  106. return $this->fillTabs($tabs, $element);
  107. }
  108. /**
  109. * Fill specified form with tabs.
  110. *
  111. * @param array $tabs
  112. * @param Element|null $element
  113. * @return FormTabs
  114. */
  115. protected function fillTabs(array $tabs, Element $element = null)
  116. {
  117. $context = ($element === null) ? $this->_rootElement : $element;
  118. foreach ($tabs as $tabName => $tabFields) {
  119. $tabElement = $this->getTabElement($tabName);
  120. $this->openTab($tabName);
  121. $tabElement->fillFormTab(array_merge($tabFields, $this->unassignedFields), $context);
  122. $this->updateUnassignedFields($tabElement);
  123. }
  124. if (!empty($this->unassignedFields)) {
  125. $this->fillMissedFields($tabs);
  126. }
  127. return $this;
  128. }
  129. /**
  130. * Update array with fields which aren't assigned to any tab.
  131. *
  132. * @param Tab $tabElement
  133. * @return void
  134. */
  135. protected function updateUnassignedFields(Tab $tabElement)
  136. {
  137. $this->unassignedFields = array_diff_key(
  138. $this->unassignedFields,
  139. array_intersect_key($this->unassignedFields, $tabElement->setFields)
  140. );
  141. }
  142. /**
  143. * Fill fields which weren't found on filled tabs.
  144. *
  145. * @param array $tabs
  146. * @throws \Exception
  147. *
  148. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  149. */
  150. protected function fillMissedFields(array $tabs)
  151. {
  152. foreach (array_diff_key($this->tabs, $tabs) as $tabName => $tabData) {
  153. $tabElement = $this->getTabElement($tabName);
  154. if ($this->openTab($tabName)) {
  155. $tabElement->fillFormTab($this->unassignedFields, $this->_rootElement);
  156. $this->updateUnassignedFields($tabElement);
  157. if (empty($this->unassignedFields)) {
  158. break;
  159. }
  160. }
  161. }
  162. if (!empty($this->unassignedFields)) {
  163. throw new \Exception(
  164. 'Could not find all elements on the tabs: ' . implode(', ', array_keys($this->unassignedFields))
  165. );
  166. }
  167. }
  168. /**
  169. * Get data of the tabs.
  170. *
  171. * @param FixtureInterface|null $fixture
  172. * @param Element|null $element
  173. * @return array
  174. *
  175. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  176. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  177. */
  178. public function getData(FixtureInterface $fixture = null, Element $element = null)
  179. {
  180. $data = [];
  181. if (null === $fixture) {
  182. foreach ($this->tabs as $tabName => $tab) {
  183. $this->openTab($tabName);
  184. $tabData = $this->getTabElement($tabName)->getDataFormTab();
  185. $data = array_merge($data, $tabData);
  186. }
  187. } else {
  188. $tabsFields = $fixture->hasData() ? $this->getFieldsByTabs($fixture) : [];
  189. foreach ($tabsFields as $tabName => $fields) {
  190. $this->openTab($tabName);
  191. $tabData = $this->getTabElement($tabName)->getDataFormTab($fields, $this->_rootElement);
  192. $data = array_merge($data, $tabData);
  193. }
  194. }
  195. return $data;
  196. }
  197. /**
  198. * Update form with tabs.
  199. *
  200. * @param FixtureInterface $fixture
  201. * @return FormTabs
  202. */
  203. public function update(FixtureInterface $fixture)
  204. {
  205. $tabs = $this->getFieldsByTabs($fixture);
  206. foreach ($tabs as $tab => $tabFields) {
  207. $this->openTab($tab)->updateFormTab($tabFields, $this->_rootElement);
  208. }
  209. return $this;
  210. }
  211. /**
  212. * Create data array for filling tabs.
  213. *
  214. * @param FixtureInterface $fixture
  215. * @return array
  216. */
  217. protected function getFieldsByTabs(FixtureInterface $fixture)
  218. {
  219. return $this->getFixtureFieldsByTabs($fixture);
  220. }
  221. /**
  222. * Create data array for filling tabs (new fixture specification).
  223. *
  224. * @param FixtureInterface $fixture
  225. * @return array
  226. */
  227. private function getFixtureFieldsByTabs(FixtureInterface $fixture)
  228. {
  229. $tabs = [];
  230. $data = $fixture->getData();
  231. foreach ($data as $field => $value) {
  232. $attributes = $fixture->getDataFieldConfig($field);
  233. $attributes['value'] = $value;
  234. if (array_key_exists('group', $attributes) && $attributes['group'] != 'null') {
  235. $tabs[$attributes['group']][$field] = $attributes;
  236. } elseif (!array_key_exists('group', $attributes)) {
  237. $this->unassignedFields[$field] = $attributes;
  238. }
  239. }
  240. return $tabs;
  241. }
  242. /**
  243. * Get tab element.
  244. *
  245. * @param string $tabName
  246. * @return Tab
  247. * @throws \Exception
  248. */
  249. public function getTabElement($tabName)
  250. {
  251. $tabClass = $this->tabs[$tabName]['class'];
  252. /** @var Tab $tabElement */
  253. $tabElement = $this->blockFactory->create($tabClass, ['element' => $this->_rootElement]);
  254. if (!$tabElement instanceof Tab) {
  255. throw new \Exception('Wrong Tab Class.');
  256. }
  257. $tabElement->setWrapper(isset($this->tabs[$tabName]['wrapper']) ? $this->tabs[$tabName]['wrapper'] : '');
  258. $tabElement->setMapping(isset($this->tabs[$tabName]['fields']) ? (array)$this->tabs[$tabName]['fields'] : []);
  259. return $tabElement;
  260. }
  261. /**
  262. * Open tab.
  263. *
  264. * @param string $tabName
  265. * @return Tab
  266. */
  267. public function openTab($tabName)
  268. {
  269. $selector = $this->tabs[$tabName]['selector'];
  270. $strategy = isset($this->tabs[$tabName]['strategy'])
  271. ? $this->tabs[$tabName]['strategy']
  272. : Locator::SELECTOR_CSS;
  273. $tab = $this->_rootElement->find($selector, $strategy);
  274. $tab->click();
  275. return $this;
  276. }
  277. }