Skip to content

Commit

Permalink
Merge pull request #1269 from magento-tsg/2.1.8-develop-pr21
Browse files Browse the repository at this point in the history
[TSG] Backporting for 2.1 (pr21) (2.1.8)
  • Loading branch information
duhon authored Jul 6, 2017
2 parents ca53c80 + 47796ad commit 658e754
Show file tree
Hide file tree
Showing 84 changed files with 7,187 additions and 851 deletions.
28 changes: 20 additions & 8 deletions app/code/Magento/Catalog/Controller/Adminhtml/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ abstract class Category extends \Magento\Backend\App\Action
*/
protected function _initCategory($getRootInstead = false)
{
$categoryId = (int)$this->getRequest()->getParam('id', false);
$categoryId = $this->resolveCategoryId();
$storeId = (int)$this->getRequest()->getParam('store');
$category = $this->_objectManager->create('Magento\Catalog\Model\Category');
$category = $this->_objectManager->create(\Magento\Catalog\Model\Category::class);
$category->setStoreId($storeId);

if ($categoryId) {
$category->load($categoryId);
if ($storeId) {
$rootId = $this->_objectManager->get(
'Magento\Store\Model\StoreManagerInterface'
\Magento\Store\Model\StoreManagerInterface::class
)->getStore(
$storeId
)->getRootCategoryId();
Expand All @@ -55,13 +55,25 @@ protected function _initCategory($getRootInstead = false)
}
}

$this->_objectManager->get('Magento\Framework\Registry')->register('category', $category);
$this->_objectManager->get('Magento\Framework\Registry')->register('current_category', $category);
$this->_objectManager->get('Magento\Cms\Model\Wysiwyg\Config')
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('category', $category);
$this->_objectManager->get(\Magento\Framework\Registry::class)->register('current_category', $category);
$this->_objectManager->get(\Magento\Cms\Model\Wysiwyg\Config::class)
->setStoreId($this->getRequest()->getParam('store'));
return $category;
}

/**
* Resolve Category Id (from get or from post).
*
* @return int
*/
private function resolveCategoryId()
{
$categoryId = (int)$this->getRequest()->getParam('id', false);

return $categoryId ?: (int)$this->getRequest()->getParam('entity_id', false);
}

/**
* Build response for ajax request
*
Expand All @@ -79,7 +91,7 @@ protected function ajaxRequestResponse($category, $resultPage)
if (empty($breadcrumbsPath)) {
// but if no category, and it is deleted - prepare breadcrumbs from path, saved in session
$breadcrumbsPath = $this->_objectManager->get(
'Magento\Backend\Model\Auth\Session'
\Magento\Backend\Model\Auth\Session::class
)->getDeletedPath(
true
);
Expand Down Expand Up @@ -107,7 +119,7 @@ protected function ajaxRequestResponse($category, $resultPage)
['response' => $eventResponse, 'controller' => $this]
);
/** @var \Magento\Framework\Controller\Result\Json $resultJson */
$resultJson = $this->_objectManager->get('Magento\Framework\Controller\Result\Json');
$resultJson = $this->_objectManager->get(\Magento\Framework\Controller\Result\Json::class);
$resultJson->setHeader('Content-type', 'application/json', true);
$resultJson->setData($eventResponse->getData());
return $resultJson;
Expand Down
50 changes: 32 additions & 18 deletions app/code/Magento/Catalog/Model/ResourceModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class Category extends AbstractResource
*/
protected $_categoryProductTable;

/**
* Entities where attribute is filled.
*
* @var array[]
*/
private $entitiesWhereAttributesIs;

/**
* Id of 'is_active' category attribute
*
Expand Down Expand Up @@ -575,22 +582,29 @@ public function getIsActiveAttributeId()
*/
public function findWhereAttributeIs($entityIdsFilter, $attribute, $expectedValue)
{
$linkField = $this->getLinkField();
$bind = ['attribute_id' => $attribute->getId(), 'value' => $expectedValue];
$selectEntities = $this->getConnection()->select()->from(
['ce' => $this->getTable('catalog_category_entity')],
['entity_id']
)->joinLeft(
['ci' => $attribute->getBackend()->getTable()],
"ci.{$linkField} = ce.{$linkField} AND attribute_id = :attribute_id",
['value']
)->where(
'ci.value = :value'
)->where(
'ce.entity_id IN (?)',
$entityIdsFilter
);
return $this->getConnection()->fetchCol($selectEntities, $bind);
$entityIdsFilterHash = md5(serialize($entityIdsFilter));

if (!isset($this->entitiesWhereAttributesIs[$entityIdsFilterHash][$attribute->getId()][$expectedValue])) {
$linkField = $this->getLinkField();
$bind = ['attribute_id' => $attribute->getId(), 'value' => $expectedValue];
$selectEntities = $this->getConnection()->select()->from(
['ce' => $this->getTable('catalog_category_entity')],
['entity_id']
)->joinLeft(
['ci' => $attribute->getBackend()->getTable()],
"ci.{$linkField} = ce.{$linkField} AND attribute_id = :attribute_id",
['value']
)->where(
'ci.value = :value'
)->where(
'ce.entity_id IN (?)',
$entityIdsFilter
);
$this->entitiesWhereAttributesIs[$entityIdsFilterHash][$attribute->getId()][$expectedValue] =
$this->getConnection()->fetchCol($selectEntities, $bind);
}

return $this->entitiesWhereAttributesIs[$entityIdsFilterHash][$attribute->getId()][$expectedValue];
}

/**
Expand Down Expand Up @@ -1035,7 +1049,7 @@ private function getEntityManager()
{
if (null === $this->entityManager) {
$this->entityManager = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Framework\EntityManager\EntityManager');
->get(\Magento\Framework\EntityManager\EntityManager::class);
}
return $this->entityManager;
}
Expand All @@ -1047,7 +1061,7 @@ private function getAggregateCount()
{
if (null === $this->aggregateCount) {
$this->aggregateCount = \Magento\Framework\App\ObjectManager::getInstance()
->get('Magento\Catalog\Model\ResourceModel\Category\AggregateCount');
->get(\Magento\Catalog\Model\ResourceModel\Category\AggregateCount::class);
}
return $this->aggregateCount;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Catalog\Test\Unit\Model\ResourceModel;

use Magento\Catalog\Model\Factory;
use Magento\Catalog\Model\ResourceModel\Category;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;
use Magento\Eav\Model\Config;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend;
use Magento\Eav\Model\Entity\Context;
use Magento\Eav\Model\Entity\Type;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface as Adapter;
use Magento\Framework\DB\Select;
use Magento\Framework\Event\ManagerInterface;
use Magento\Store\Model\StoreManagerInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CategoryTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Category
*/
protected $category;

/**
* @var Context|\PHPUnit_Framework_MockObject_MockObject
*/
protected $contextMock;

/**
* @var Select|\PHPUnit_Framework_MockObject_MockObject
*/
private $selectMock;

/**
* @var Adapter|\PHPUnit_Framework_MockObject_MockObject
*/
private $connectionMock;

/**
* @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
*/
private $resourceMock;

/**
* @var Config|\PHPUnit_Framework_MockObject_MockObject
*/
private $eavConfigMock;

/**
* @var Type|\PHPUnit_Framework_MockObject_MockObject
*/
private $entityType;

/**
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $storeManagerMock;

/**
* @var Factory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $factoryMock;

/**
* @var ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $managerMock;

/**
* @var Category\TreeFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $treeFactoryMock;

/**
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $collectionFactoryMock;

/**
* {@inheritDoc}
*/
protected function setUp()
{
$this->selectMock = $this->getMockBuilder(Select::class)->disableOriginalConstructor()->getMock();
$this->selectMock->expects($this->at(2))->method('where')->willReturnSelf();
$this->selectMock->expects($this->once())->method('from')->willReturnSelf();
$this->selectMock->expects($this->once())->method('joinLeft')->willReturnSelf();
$this->connectionMock = $this->getMockBuilder(Adapter::class)->getMockForAbstractClass();
$this->connectionMock->expects($this->once())->method('select')->willReturn($this->selectMock);
$this->resourceMock = $this->getMockBuilder(ResourceConnection::class)->disableOriginalConstructor()->getMock();
$this->resourceMock->expects($this->any())->method('getConnection')->willReturn($this->connectionMock);
$this->connectionMock->expects($this->any())->method('getTableName')->willReturn('TableName');
$this->resourceMock->expects($this->any())->method('getTableName')->willReturn('TableName');
$this->contextMock = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock();
$this->eavConfigMock = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->getMock();
$this->entityType = $this->getMockBuilder(Type::class)->disableOriginalConstructor()->getMock();
$this->eavConfigMock->expects($this->any())->method('getEntityType')->willReturn($this->entityType);
$this->contextMock->expects($this->any())->method('getEavConfig')->willReturn($this->eavConfigMock);
$this->contextMock->expects($this->any())->method('getResource')->willReturn($this->resourceMock);
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)->getMock();
$this->factoryMock = $this->getMockBuilder(Factory::class)->disableOriginalConstructor()->getMock();
$this->managerMock = $this->getMockBuilder(ManagerInterface::class)->getMock();
$this->treeFactoryMock = $this->getMockBuilder(Category\TreeFactory::class)
->disableOriginalConstructor()
->getMock();
$this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
->disableOriginalConstructor()
->getMock();

$this->category = new Category(
$this->contextMock,
$this->storeManagerMock,
$this->factoryMock,
$this->managerMock,
$this->treeFactoryMock,
$this->collectionFactoryMock,
[]
);
}

/**
* @return void
*/
public function testFindWhereAttributeIs()
{
$entityIdsFilter = [1, 2];
$expectedValue = 123;
$attribute = $this->getMockBuilder(Attribute::class)->disableOriginalConstructor()->getMock();
$backendModel = $this->getMockBuilder(AbstractBackend::class)->disableOriginalConstructor()->getMock();

$attribute->expects($this->any())->method('getBackend')->willReturn($backendModel);
$this->connectionMock->expects($this->once())->method('fetchCol')->willReturn(['result']);

$result = $this->category->findWhereAttributeIs($entityIdsFilter, $attribute, $expectedValue);
$this->assertEquals(['result'], $result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

use Magento\Catalog\Model\Category;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory;
use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
use Magento\UrlRewrite\Model\MergeDataProviderFactory;
use Magento\Framework\App\ObjectManager;

class ChildrenUrlRewriteGenerator
{
Expand All @@ -16,36 +19,51 @@ class ChildrenUrlRewriteGenerator
/** @var \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory */
protected $categoryUrlRewriteGeneratorFactory;

/**
* Container for new generated url rewrites.
*
* @var \Magento\UrlRewrite\Model\MergeDataProvider
*/
private $mergeDataProviderPrototype;

/**
* @param \Magento\CatalogUrlRewrite\Model\Category\ChildrenCategoriesProvider $childrenCategoriesProvider
* @param \Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory
* @param \Magento\UrlRewrite\Model\MergeDataProviderFactory|null $mergeDataProviderFactory
*/
public function __construct(
ChildrenCategoriesProvider $childrenCategoriesProvider,
CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory
CategoryUrlRewriteGeneratorFactory $categoryUrlRewriteGeneratorFactory,
MergeDataProviderFactory $mergeDataProviderFactory = null
) {
$this->childrenCategoriesProvider = $childrenCategoriesProvider;
$this->categoryUrlRewriteGeneratorFactory = $categoryUrlRewriteGeneratorFactory;
if (!isset($mergeDataProviderFactory)) {
$mergeDataProviderFactory = ObjectManager::getInstance()->get(MergeDataProviderFactory::class);
}
$this->mergeDataProviderPrototype = $mergeDataProviderFactory->create();
}

/**
* Generate list of children urls
*
* @param int $storeId
* @param \Magento\Catalog\Model\Category $category
* @param int|null $rootCategoryId
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
*/
public function generate($storeId, Category $category)
public function generate($storeId, Category $category, $rootCategoryId = null)
{
$urls = [];
foreach ($this->childrenCategoriesProvider->getChildren($category) as $childCategory) {
$mergeDataProvider = clone $this->mergeDataProviderPrototype;
foreach ($this->childrenCategoriesProvider->getChildren($category, true) as $childCategory) {
$childCategory->setStoreId($storeId);
$childCategory->setData('save_rewrites_history', $category->getData('save_rewrites_history'));
$urls = array_merge(
$urls,
$this->categoryUrlRewriteGeneratorFactory->create()->generate($childCategory)
/** @var CategoryUrlRewriteGenerator $categoryUrlRewriteGenerator */
$categoryUrlRewriteGenerator = $this->categoryUrlRewriteGeneratorFactory->create();
$mergeDataProvider->merge(
$categoryUrlRewriteGenerator->generate($childCategory, false, $rootCategoryId)
);
}
return $urls;
return $mergeDataProvider->getData();
}
}
Loading

0 comments on commit 658e754

Please sign in to comment.