-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1581 from magento-tango/MAGETWO-71554
Bug - MAGETWO-71554 Visual Merchandiser category edit performance issue - for 2.2
- Loading branch information
Showing
12 changed files
with
441 additions
and
61 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/Catalog/Model/Category/Product/PositionResolver.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\Catalog\Model\Category\Product; | ||
|
||
/** | ||
* Resolver to get product positions by ids assigned to specific category | ||
*/ | ||
class PositionResolver extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb | ||
{ | ||
/** | ||
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context | ||
* @param string $connectionName | ||
*/ | ||
public function __construct( | ||
\Magento\Framework\Model\ResourceModel\Db\Context $context, | ||
$connectionName = null | ||
) { | ||
parent::__construct($context, $connectionName); | ||
} | ||
|
||
/** | ||
* Initialize resource model | ||
* | ||
* @return void | ||
*/ | ||
protected function _construct() | ||
{ | ||
$this->_init('catalog_product_entity', 'entity_id'); | ||
} | ||
|
||
/** | ||
* Get category product positions | ||
* | ||
* @param int $categoryId | ||
* @return array | ||
*/ | ||
public function getPositions(int $categoryId) | ||
{ | ||
$connection = $this->getConnection(); | ||
|
||
$select = $connection->select()->from( | ||
['cpe' => $this->getTable('catalog_product_entity')], | ||
'entity_id' | ||
)->joinLeft( | ||
['ccp' => $this->getTable('catalog_category_product')], | ||
'ccp.product_id=cpe.entity_id' | ||
)->where( | ||
'ccp.category_id = ?', | ||
$categoryId | ||
)->order( | ||
'ccp.position ' . \Magento\Framework\DB\Select::SQL_ASC | ||
); | ||
|
||
return array_flip($connection->fetchCol($select)); | ||
} | ||
} |
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
120 changes: 120 additions & 0 deletions
120
app/code/Magento/Catalog/Test/Unit/Model/Category/Product/PositionResolverTest.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,120 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\Catalog\Test\Unit\Model\Category\Product; | ||
|
||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
use Magento\Catalog\Model\Category\Product\PositionResolver; | ||
use Magento\Framework\Model\ResourceModel\Db\Context; | ||
use Magento\Framework\App\ResourceConnection; | ||
use Magento\Framework\DB\Adapter\AdapterInterface; | ||
use Magento\Framework\DB\Select; | ||
|
||
class PositionResolverTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** | ||
* @var Context|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $resources; | ||
|
||
/** | ||
* @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* @var Select|\PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $select; | ||
|
||
/** | ||
* @var PositionResolver | ||
*/ | ||
private $model; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $positions = [ | ||
'3' => 100, | ||
'2' => 101, | ||
'1' => 102 | ||
]; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $flippedPositions = [ | ||
'100' => 3, | ||
'101' => 2, | ||
'102' => 1 | ||
]; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $categoryId = 1; | ||
|
||
protected function setUp() | ||
{ | ||
$this->context = $this->getMockBuilder(Context::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->resources = $this->getMockBuilder(ResourceConnection::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->connection = $this->getMockBuilder(AdapterInterface::class) | ||
->disableOriginalConstructor() | ||
->getMockForAbstractClass(); | ||
|
||
$this->select = $this->getMockBuilder(Select::class) | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
|
||
$this->model = (new ObjectManager($this))->getObject( | ||
PositionResolver::class, | ||
[ | ||
'context' => $this->context, | ||
null, | ||
'_resources' => $this->resources | ||
] | ||
); | ||
} | ||
|
||
public function testGetPositions() | ||
{ | ||
$this->resources->expects($this->once()) | ||
->method('getConnection') | ||
->willReturn($this->connection); | ||
|
||
$this->connection->expects($this->once()) | ||
->method('select') | ||
->willReturn($this->select); | ||
$this->select->expects($this->once()) | ||
->method('from') | ||
->willReturnSelf(); | ||
$this->select->expects($this->once()) | ||
->method('where') | ||
->willReturnSelf(); | ||
$this->select->expects($this->once()) | ||
->method('order') | ||
->willReturnSelf(); | ||
$this->select->expects($this->once()) | ||
->method('joinLeft') | ||
->willReturnSelf(); | ||
$this->connection->expects($this->once()) | ||
->method('fetchCol') | ||
->willReturn($this->positions); | ||
|
||
$this->assertEquals($this->flippedPositions, $this->model->getPositions($this->categoryId)); | ||
} | ||
} |
Oops, something went wrong.