Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/MC-31925' into code-freeze
Browse files Browse the repository at this point in the history
  • Loading branch information
RuslanKostiv1 committed Mar 6, 2020
2 parents 71a4985 + 108ecfc commit 1d821cb
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,13 @@
use Magento\Catalog\Model\Indexer\Product\Category\Action\Rows;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\Store;
use Magento\CatalogSearch\Model\Indexer\Fulltext;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Framework\Indexer\IndexerRegistry;

/**
* Plugin for collect category data during saving process
*/
class CollectCategoriesDataForUpdate
{
/**
* @var IndexerRegistry
*/
private $indexerRegistry;

/**
* @var CollectionFactory
*/
Expand Down Expand Up @@ -62,8 +55,7 @@ public function __construct(
public function afterExecute(
Rows $subject,
Rows $result,
array $categoryIds = [],
$useTempTable = false
array $categoryIds = []
): Rows {
$categoryCollection = $this->collectionFactory->create();
$categoryCollection->addFieldToFilter('entity_id', $categoryIds);
Expand Down
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;
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/CatalogStorefrontConnector/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
<plugin name="collectCategoryDataForStorefrontApplicationOnSave" type="Magento\CatalogStorefrontConnector\Plugin\CollectCategoriesDataOnSave" />
<plugin name="collectCategoryDataForStorefrontApplicationOnDelete" type="Magento\CatalogStorefrontConnector\Plugin\CategoryOnDelete" />
</type>
<type name="Magento\Catalog\Model\Category">
<plugin name="collectCategoryDataForStorefrontApplicationOnMove" type="\Magento\CatalogStorefrontConnector\Plugin\CollectCategoriesDataOnMove" />
</type>
<type name="Magento\Catalog\Model\Indexer\Product\Category\Action\Rows">
<plugin name="collectCategoriesDataForStorefrontApplicationOnUpdate" type="Magento\CatalogStorefrontConnector\Plugin\CollectCategoriesDataForUpdate" />
</type>
Expand Down
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'
]
]
]
]
];
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ class CategoryOnUpdate extends CollectCategoriesDataForUpdate
public function afterExecute(
Rows $subject,
Rows $result,
array $entityIds = [],
$useTempTable = false
array $entityIds = []
): Rows {
$result = parent::afterExecute($subject, $result, $entityIds, $useTempTable);
$result = parent::afterExecute($subject, $result, $entityIds);

$objectManager = Bootstrap::getObjectManager();
/** @var \Magento\TestFramework\Workaround\ConsumerInvoker $consumerInvoker */
Expand Down
2 changes: 2 additions & 0 deletions dev/tests/integration/etc/di/preferences/graphql.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
=> \Magento\StorefrontTestFixer\CategoryAfterSave::class,
\Magento\CatalogStorefrontConnector\Plugin\CollectCategoriesDataForUpdate::class
=> \Magento\StorefrontTestFixer\CategoryOnUpdate::class,
\Magento\CatalogStorefrontConnector\Plugin\CollectCategoriesDataOnMove::class
=> \Magento\StorefrontTestFixer\CategoryOnMove::class,
\Magento\CatalogInventoryExtractor\Plugin\UpdateCategoriesOnConfigurationChange::class
=> \Magento\StorefrontTestFixer\CategoriesOnConfigurationChange::class,
\Magento\CatalogStorefrontConnector\Plugin\CollectProductsDataOnSave::class
Expand Down
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
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

/**
* Mock for preferences in integration/etc/di/preferences/graphql.php to be able run integration tests
*
* Due to changes in DI (added afterSave() plugins) for store front application
* we added empty plugin classes to keep plugin initialization chain
*/
class CategoryOnUpdate extends CollectCategoriesDataForUpdate
{
Expand Down

0 comments on commit 1d821cb

Please sign in to comment.