Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
MoveCategoryEntityTest.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 
30 class MoveCategoryEntityTest extends Injectable
31 {
32  /* tags */
33  const MVP = 'yes';
34  /* end tags */
35 
41  private $catalogCategoryIndex;
42 
48  private $catalogCategoryEdit;
49 
55  private $fixtureFactory;
56 
65  public function __inject(
66  CatalogCategoryIndex $catalogCategoryIndex,
67  CatalogCategoryEdit $catalogCategoryEdit,
68  FixtureFactory $fixtureFactory
69  ) {
70  $this->catalogCategoryIndex = $catalogCategoryIndex;
71  $this->catalogCategoryEdit = $catalogCategoryEdit;
72  $this->fixtureFactory = $fixtureFactory;
73  }
74 
83  public function test(
84  Category $childCategory,
85  Category $parentCategory,
86  $moveLevel = null
87  ) {
88  // Preconditions:
89  $parentCategory->persist();
90  $childCategory->persist();
91  $resultCategory = $childCategory;
92 
93  if (!empty($moveLevel)) {
94  for ($nestingIterator = 1; $nestingIterator < $moveLevel; $nestingIterator++) {
95  $childCategory = $childCategory->getDataFieldConfig('parent_id')['source']->getParentCategory();
96  }
97  $resultCategory = $this->getMovedCategoryTree($resultCategory, $parentCategory, $childCategory);
98  }
99 
100  // Steps:
101  $this->catalogCategoryIndex->open();
102  $this->catalogCategoryIndex->getTreeCategories()->expandAllCategories();
103  $this->catalogCategoryIndex->getTreeCategories()->assignCategory(
104  $parentCategory->getName(),
105  $childCategory->getName()
106  );
107  $this->catalogCategoryEdit->getModalBlock()->acceptWarning();
108 
109  return [
110  'category' => $resultCategory,
111  'parentCategory' => $parentCategory,
112  'childCategory' => $childCategory,
113  ];
114  }
115 
124  public function getMovedCategoryTree(Category $movedCategory, Category $parentCategory, Category $childCategory)
125  {
126  $bottomChildCategory = [];
127  while ($movedCategory->getName() != $childCategory->getName()) {
128  $bottomChildCategory[] = $movedCategory->getData();
129  $movedCategory = $movedCategory->getDataFieldConfig('parent_id')['source']->getParentCategory();
130  }
131  $bottomChildCategory[] = $movedCategory->getData();
132 
133  $newCategory = $parentCategory;
134  for ($i = count($bottomChildCategory) - 1; $i >= 0; $i--) {
135  unset($bottomChildCategory[$i]['parent_id']);
136  $bottomChildCategory[$i]['parent_id']['source'] = $newCategory;
137  $newCategory = $this->fixtureFactory->createByCode(
138  'category',
139  ['data' => $bottomChildCategory[$i]]
140  );
141  }
142 
143  return $newCategory;
144  }
145 }
__inject(CatalogCategoryIndex $catalogCategoryIndex, CatalogCategoryEdit $catalogCategoryEdit, FixtureFactory $fixtureFactory)
test(Category $childCategory, Category $parentCategory, $moveLevel=null)
getMovedCategoryTree(Category $movedCategory, Category $parentCategory, Category $childCategory)
$i
Definition: gallery.phtml:31