|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Catalog\Test\TestCase\Category; |
| 8 | + |
| 9 | +use Magento\Catalog\Test\Fixture\Category; |
| 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 | + |
| 15 | +/** |
| 16 | + * Precondition: |
| 17 | + * 1. Categories are created |
| 18 | + * |
| 19 | + * Test Flow: |
| 20 | + * 1. Log in to Backend |
| 21 | + * 2. Navigate to the Products>Categories |
| 22 | + * 3. Select SubCategory |
| 23 | + * 4. Drag'n'Drop SubCategory |
| 24 | + * 5. Dismiss alert |
| 25 | + * 6. Drag'n'Drop SubCategory |
| 26 | + * 7. Accept alert |
| 27 | + * 8. Save category |
| 28 | + * 9. Verify category |
| 29 | + * |
| 30 | + * @group Category_Management |
| 31 | + * @ZephyrId MAGETWO-27319 |
| 32 | + */ |
| 33 | +class AdvancedMoveCategoryEntityTest extends Injectable |
| 34 | +{ |
| 35 | + /* tags */ |
| 36 | + const MVP = 'no'; |
| 37 | + /* end tags */ |
| 38 | + |
| 39 | + /** |
| 40 | + * CatalogCategoryIndex page. |
| 41 | + * |
| 42 | + * @var CatalogCategoryIndex |
| 43 | + */ |
| 44 | + private $catalogCategoryIndex; |
| 45 | + |
| 46 | + /** |
| 47 | + * CatalogCategoryEdit page. |
| 48 | + * |
| 49 | + * @var CatalogCategoryEdit |
| 50 | + */ |
| 51 | + private $catalogCategoryEdit; |
| 52 | + |
| 53 | + /** |
| 54 | + * Factory for fixtures. |
| 55 | + * |
| 56 | + * @var FixtureFactory |
| 57 | + */ |
| 58 | + private $fixtureFactory; |
| 59 | + |
| 60 | + /** |
| 61 | + * Inject page end prepare default category. |
| 62 | + * |
| 63 | + * @param CatalogCategoryIndex $catalogCategoryIndex |
| 64 | + * @param CatalogCategoryEdit $catalogCategoryEdit |
| 65 | + * @param FixtureFactory $fixtureFactory |
| 66 | + * @return void |
| 67 | + */ |
| 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 | + |
| 78 | + /** |
| 79 | + * Runs test. |
| 80 | + * |
| 81 | + * @param Category $childCategory |
| 82 | + * @param Category $parentCategory |
| 83 | + * @param int|null $moveLevel |
| 84 | + * @return array |
| 85 | + */ |
| 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 | + |
| 127 | + /** |
| 128 | + * Get moved category tree. |
| 129 | + * |
| 130 | + * @param Category $movedCategory |
| 131 | + * @param Category $parentCategory |
| 132 | + * @param Category $childCategory |
| 133 | + * @return Category |
| 134 | + */ |
| 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 | +} |
0 commit comments