Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AdvancedMoveCategoryEntityTest.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\Fixture\FixtureFactory;
13 use Magento\Mtf\TestCase\Injectable;
14 
33 class AdvancedMoveCategoryEntityTest extends Injectable
34 {
35  /* tags */
36  const MVP = 'no';
37  /* end tags */
38 
44  private $catalogCategoryIndex;
45 
51  private $catalogCategoryEdit;
52 
58  private $fixtureFactory;
59 
68  public function __inject(
69  CatalogCategoryIndex $catalogCategoryIndex,
70  CatalogCategoryEdit $catalogCategoryEdit,
71  FixtureFactory $fixtureFactory
72  ) {
73  $this->catalogCategoryIndex = $catalogCategoryIndex;
74  $this->catalogCategoryEdit = $catalogCategoryEdit;
75  $this->fixtureFactory = $fixtureFactory;
76  }
77 
86  public function test(
87  Category $childCategory,
88  Category $parentCategory,
89  $moveLevel = null
90  ) {
91  // Preconditions:
92  $parentCategory->persist();
93  $childCategory->persist();
94  $resultCategory = $childCategory;
95 
96  if (!empty($moveLevel)) {
97  for ($nestingIterator = 1; $nestingIterator < $moveLevel; $nestingIterator++) {
98  $childCategory = $childCategory->getDataFieldConfig('parent_id')['source']->getParentCategory();
99  }
100  $resultCategory = $this->getMovedCategoryTree($resultCategory, $parentCategory, $childCategory);
101  }
102 
103  // Steps:
104  $this->catalogCategoryIndex->open();
105  $this->catalogCategoryIndex->getTreeCategories()->expandAllCategories();
106  $this->catalogCategoryIndex->getTreeCategories()->selectCategory($childCategory);
107  $this->catalogCategoryIndex->getTreeCategories()->assignCategory(
108  $parentCategory->getName(),
109  $childCategory->getName()
110  );
111  $this->catalogCategoryEdit->getModalBlock()->dismissWarning();
112 
113  $this->catalogCategoryIndex->getTreeCategories()->assignCategory(
114  $parentCategory->getName(),
115  $childCategory->getName()
116  );
117  $this->catalogCategoryEdit->getModalBlock()->acceptWarning();
118  $this->catalogCategoryEdit->getFormPageActions()->save();
119 
120  return [
121  'category' => $resultCategory,
122  'parentCategory' => $parentCategory,
123  'childCategory' => $childCategory,
124  ];
125  }
126 
135  public function getMovedCategoryTree(Category $movedCategory, Category $parentCategory, Category $childCategory)
136  {
137  $bottomChildCategory = [];
138  while ($movedCategory->getName() != $childCategory->getName()) {
139  $bottomChildCategory[] = $movedCategory->getData();
140  $movedCategory = $movedCategory->getDataFieldConfig('parent_id')['source']->getParentCategory();
141  }
142  $bottomChildCategory[] = $movedCategory->getData();
143 
144  $newCategory = $parentCategory;
145  for ($i = count($bottomChildCategory) - 1; $i >= 0; $i--) {
146  unset($bottomChildCategory[$i]['parent_id']);
147  $bottomChildCategory[$i]['parent_id']['source'] = $newCategory;
148  $newCategory = $this->fixtureFactory->createByCode(
149  'category',
150  ['data' => $bottomChildCategory[$i]]
151  );
152  }
153 
154  return $newCategory;
155  }
156 }
__inject(CatalogCategoryIndex $catalogCategoryIndex, CatalogCategoryEdit $catalogCategoryEdit, FixtureFactory $fixtureFactory)
getMovedCategoryTree(Category $movedCategory, Category $parentCategory, Category $childCategory)
test(Category $childCategory, Category $parentCategory, $moveLevel=null)
$i
Definition: gallery.phtml:31