Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SubcategoryNotIncludeInNavigationMenuTest.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 
30 {
31  /* tags */
32  const MVP = 'yes';
33  /* end tags */
34 
40  private $catalogCategoryIndex;
41 
47  private $catalogCategoryEdit;
48 
54  private $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  $this->getCategoryFixture($categories, $initialCategory, $category->getData(), $nestingLevel);
96  return [
97  'category' => $categories[1],
98  'subcategory' => $categories[2],
99  ];
100  }
101 
111  private function getCategoryFixture(array &$categories, Category $currentCategory, array $data, int $nestingLevel)
112  {
113  if (--$nestingLevel) {
114  $parentCategory = $this->getCategoryFixture(
115  $categories,
116  $currentCategory->getDataFieldConfig('parent_id')['source']->getParentCategory(),
117  $data,
118  $nestingLevel
119  );
120  $category = $this->fixtureFactory->createByCode(
121  'category',
122  ['data' => array_merge($currentCategory->getData(), ['parent_id' => ['source' => $parentCategory]])]
123  );
124  } else {
125  $category = $this->fixtureFactory->createByCode(
126  'category',
127  ['data' => array_merge($currentCategory->getData(), $data)]
128  );
129  }
130  $categories[$nestingLevel + 1] = $category;
131  return $category;
132  }
133 
141  private function getParentCategoryByNestingLevel(Category $category, $nestingLevel)
142  {
143  for ($nestingIterator = 1; $nestingIterator < $nestingLevel; $nestingIterator++) {
144  $category = $category->getDataFieldConfig('parent_id')['source']->getParentCategory();
145  }
146 
147  return $category;
148  }
149 }
__inject(CatalogCategoryIndex $catalogCategoryIndex, CatalogCategoryEdit $catalogCategoryEdit, FixtureFactory $fixtureFactory)
$categories