Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateTopCategoryEntityTest.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryEdit;
11 use Magento\Catalog\Test\Page\Adminhtml\CatalogCategoryIndex;
12 use Magento\Mtf\TestCase\Injectable;
13 use Magento\Mtf\Fixture\FixtureFactory;
14 
29 class UpdateTopCategoryEntityTest extends Injectable
30 {
31  /* tags */
32  const MVP = 'yes';
33  /* end tags */
34 
41 
48 
54  protected $fixtureFactory;
55 
64  public function __inject(
65  CatalogCategoryIndex $catalogCategoryIndex,
66  CatalogCategoryEdit $catalogCategoryEdit,
67  FixtureFactory $fixtureFactory
68  ) {
69  $this->fixtureFactory = $fixtureFactory;
70  $this->catalogCategoryIndex = $catalogCategoryIndex;
71  $this->catalogCategoryEdit = $catalogCategoryEdit;
72  }
73 
82  public function test(
83  Category $category,
84  Category $initialCategory,
85  $nestingLevel
86  ) {
87  $initialCategory->persist();
88  $topCategory = $this->getParentCategoryByNestingLevel($initialCategory, $nestingLevel);
89  $this->catalogCategoryIndex->open();
90  $this->catalogCategoryIndex->getTreeCategories()->selectCategory($topCategory);
91  $this->catalogCategoryEdit->getEditForm()->fill($category);
92  $this->catalogCategoryEdit->getFormPageActions()->save();
93 
94  $categories = [];
95  $categoriesBeforeSave = [];
96  $this->getCategoryFixture($categories, $initialCategory, $category->getData(), $nestingLevel);
97  $this->getCategory($initialCategory, $categoriesBeforeSave, $nestingLevel);
98 
99  return [
100  'categories' => $categories,
101  'categoriesBeforeSave' => $categoriesBeforeSave
102  ];
103  }
104 
114  private function getCategoryFixture(array &$categories, Category $currentCategory, array $data, int $nestingLevel)
115  {
116  if (--$nestingLevel) {
117  $parentCategory = $this->getCategoryFixture(
118  $categories,
119  $currentCategory->getDataFieldConfig('parent_id')['source']->getParentCategory(),
120  $data,
121  $nestingLevel
122  );
123  $category = $this->fixtureFactory->createByCode(
124  'category',
125  ['data' => array_merge($currentCategory->getData(), ['parent_id' => ['source' => $parentCategory]])]
126  );
127  } else {
128  $category = $this->fixtureFactory->createByCode(
129  'category',
130  ['data' => array_merge($currentCategory->getData(), $data)]
131  );
132  }
133  $categories[$nestingLevel + 1] = $category;
134  return $category;
135  }
136 
145  private function getCategory(Category $initialCategory, &$categoriesBeforeSave, $nestingLevel)
146  {
147  if (--$nestingLevel) {
148  $parentCategory = $this->getCategory(
149  $initialCategory->getDataFieldConfig('parent_id')['source']->getParentCategory(),
150  $categoriesBeforeSave,
151  $nestingLevel
152  );
153  $category = $this->fixtureFactory->createByCode(
154  'category',
155  ['data' => array_merge($initialCategory->getData(), ['parent_id' => ['source' => $parentCategory]])]
156  );
157  } else {
158  $category = $initialCategory;
159  }
160  $categoriesBeforeSave[$nestingLevel + 1] = $category;
161  return $category;
162  }
163 
171  private function getParentCategoryByNestingLevel(Category $category, $nestingLevel)
172  {
173  for ($nestingIterator = 1; $nestingIterator < $nestingLevel; $nestingIterator++) {
174  $category = $category->getDataFieldConfig('parent_id')['source']->getParentCategory();
175  }
176 
177  return $category;
178  }
179 }
test(Category $category, Category $initialCategory, $nestingLevel)
__inject(CatalogCategoryIndex $catalogCategoryIndex, CatalogCategoryEdit $catalogCategoryEdit, FixtureFactory $fixtureFactory)
$categories