-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/MC-31925' into code-freeze
- Loading branch information
Showing
9 changed files
with
295 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/code/Magento/CatalogStorefrontConnector/Plugin/CollectCategoriesDataOnMove.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\CatalogStorefrontConnector\Plugin; | ||
|
||
use Magento\Catalog\Model\Category; | ||
use Magento\Store\Model\Store; | ||
|
||
/** | ||
* Plugin for update categories on move event | ||
*/ | ||
class CollectCategoriesDataOnMove | ||
{ | ||
/** | ||
* @var CategoryUpdatesPublisher | ||
*/ | ||
private $categoryPublisher; | ||
|
||
/** | ||
* @param CategoryUpdatesPublisher $categoryPublisher | ||
*/ | ||
public function __construct( | ||
CategoryUpdatesPublisher $categoryPublisher | ||
) { | ||
$this->categoryPublisher = $categoryPublisher; | ||
} | ||
|
||
/** | ||
* Reindex category permissions on category move event | ||
* | ||
* @param Category $category | ||
* @param Category $result | ||
* @return Category | ||
*/ | ||
public function afterMove( | ||
Category $category, | ||
Category $result | ||
): Category { | ||
$categoryId = (string)$category->getId(); | ||
$categoryIdsByStore = []; | ||
foreach ($category->getStoreIds() as $storeId) { | ||
$storeId = (int)$storeId; | ||
if ($storeId === Store::DEFAULT_STORE_ID) { | ||
continue ; | ||
} | ||
$categoryIdsByStore[$storeId][] = [$categoryId]; | ||
if ($category->getParentIds()) { | ||
$categoryIdsByStore[$storeId][] = $category->getParentIds(); | ||
} | ||
} | ||
foreach ($categoryIdsByStore as $storeId => $storeCategoryIds) { | ||
// phpcs:ignore Magento2.Performance.ForeachArrayMerge | ||
$this->categoryPublisher->publish(array_unique(array_merge(...$storeCategoryIds)), $storeId); | ||
} | ||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/CategoryMoveTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Catalog; | ||
|
||
use Magento\Catalog\Api\CategoryRepositoryInterface; | ||
use Magento\Catalog\Model\Category; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\ObjectManager; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
/** | ||
* Test for move category scenario | ||
* | ||
* Preconditions: | ||
* Fixture with some categories in different levels and assigned products created | ||
* Steps: | ||
* Send Request: | ||
* { | ||
* categoryList(filters: {ids: {in: ["$parentCategoryId"]}}) { | ||
* name | ||
* path | ||
* product_count | ||
* children { | ||
* name | ||
* path | ||
* product_count | ||
* } | ||
* } | ||
* } | ||
* | ||
* Expected response: | ||
* { | ||
* "data": { | ||
* "categoryList": [ | ||
* { | ||
* "name": "Category 1", | ||
* "path": "1/2/3", | ||
* "product_count": 3, | ||
* "children": [ | ||
* { | ||
* "name": "Category 1.1", | ||
* "path": "1/2/3/4", | ||
* "product_count": 2 | ||
* }, | ||
* { | ||
* "name": "Category 12", | ||
* "path": "1/2/3/12", | ||
* "product_count": 1 | ||
* }, | ||
* { | ||
* "name": "Category 1.2", | ||
* "path": "1/2/3/13", | ||
* "product_count": 2 | ||
* } | ||
* ] | ||
* } | ||
* ] | ||
* } | ||
* } | ||
*/ | ||
class CategoryMoveTest extends GraphQlAbstract | ||
{ | ||
/** | ||
* @var ObjectManager | ||
*/ | ||
private $objectManager; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->objectManager = Bootstrap::getObjectManager(); | ||
} | ||
|
||
/** | ||
* Verify that category move will affect on GraphQL response | ||
* | ||
* @param string $parentCategoryId | ||
* @param string $movingCategory | ||
* @param string $childCategoryId | ||
* @param array $expectedData | ||
* @return void | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
* @throws \Magento\Framework\Exception\NoSuchEntityException | ||
* @dataProvider categoryDataProvider | ||
* @magentoApiDataFixture Magento/Catalog/_files/categories.php | ||
*/ | ||
public function testCategoryMove( | ||
string $parentCategoryId, | ||
string $movingCategory, | ||
string $childCategoryId, | ||
array $expectedData | ||
): void { | ||
/** @var CategoryRepositoryInterface $categoryRepository */ | ||
$categoryRepository = $this->objectManager->get(CategoryRepositoryInterface::class); | ||
/** @var Category $category */ | ||
$category = $categoryRepository->get($movingCategory); | ||
$category->move($parentCategoryId, $childCategoryId); | ||
$query = <<<QUERY | ||
{ | ||
categoryList(filters: {ids: {in: ["$parentCategoryId"]}}) { | ||
name | ||
path, | ||
product_count | ||
children { | ||
name | ||
path | ||
product_count | ||
} | ||
} | ||
} | ||
QUERY; | ||
$response = $this->graphQlQuery($query, [], ''); | ||
|
||
// check are there any items in the return data | ||
self::assertNotNull($response['categoryList'], 'category must not be null'); | ||
|
||
// check entire response | ||
$this->assertResponseFields($response['categoryList'][0], $expectedData); | ||
} | ||
|
||
/** | ||
* Data provider for category move | ||
* | ||
* @return array | ||
*/ | ||
public function categoryDataProvider(): array | ||
{ | ||
return [ | ||
[ | ||
'parent_category_id' => '3', | ||
'moving_category_id' => '12', | ||
'child_category_id' => '12', | ||
'expected_data' => [ | ||
'name' => 'Category 1', | ||
'path' => '1/2/3', | ||
'product_count' => '3', | ||
'children' => [ | ||
[ | ||
'name' => 'Category 1.1', | ||
'path' => '1/2/3/4', | ||
'product_count' => '2' | ||
], | ||
[ | ||
'name' => 'Category 12', | ||
'path' => '1/2/3/12', | ||
'product_count' => '1' | ||
], | ||
[ | ||
'name' => 'Category 1.2', | ||
'path' => '1/2/3/13', | ||
'product_count' => '2' | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
dev/tests/api-functional/testsuite/Magento/StorefrontTestFixer/CategoryOnMove.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\StorefrontTestFixer; | ||
|
||
use Magento\Catalog\Model\Category; | ||
use Magento\CatalogStorefrontConnector\Plugin\CollectCategoriesDataOnMove; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
|
||
/** | ||
* Plugin for collect category data during saving process | ||
*/ | ||
class CategoryOnMove extends CollectCategoriesDataOnMove | ||
{ | ||
/** | ||
* @inheritdoc | ||
* | ||
* Ad-hoc solution. Force run consumers after category save inside test-case | ||
* | ||
* @throws \Magento\Framework\Exception\LocalizedException | ||
*/ | ||
public function afterMove( | ||
Category $category, | ||
Category $result | ||
): Category { | ||
$result = parent::afterMove($category, $result); | ||
|
||
$objectManager = Bootstrap::getObjectManager(); | ||
/** @var ConsumerInvoker $consumerInvoker */ | ||
$consumerInvoker = $objectManager->get(ConsumerInvoker::class); | ||
$consumerInvoker->invoke(true); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
dev/tests/integration/testsuite/Magento/StorefrontTestFixer/CategoryOnMove.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\StorefrontTestFixer; | ||
|
||
use Magento\CatalogStorefrontConnector\Plugin\CollectCategoriesDataOnMove; | ||
|
||
/** | ||
* Plugin for collect product data during moving process | ||
* | ||
* Due to changes in DI (added afterSave() plugins) for store front application | ||
* we added empty plugin classes to keep plugin initialization chain | ||
*/ | ||
class CategoryOnMove extends CollectCategoriesDataOnMove | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters