Skip to content

Commit

Permalink
ENGCOM-5367: Fix alias in grid massaction for eav collections #23452
Browse files Browse the repository at this point in the history
 - Merge Pull Request #23452 from thomas-kl1/magento2:patch-fix-grid-massaction-eav-collection
 - Merged commits:
   1. 81a542b
   2. f15b76f
   3. a6cec0a
   4. ab04d7e
  • Loading branch information
magento-engcom-team committed Oct 4, 2019
2 parents 71c781d + ab04d7e commit c2af7f2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,35 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Backend\Block\Widget\Grid\Massaction;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Block\Widget;
use Magento\Backend\Block\Widget\Grid\Column;
use Magento\Backend\Block\Widget\Grid\ColumnSet;
use Magento\Backend\Block\Widget\Grid\Massaction\VisibilityCheckerInterface as VisibilityChecker;
use Magento\Framework\Data\Collection\AbstractDb;
use Magento\Framework\DataObject;
use Magento\Framework\DB\Select;
use Magento\Framework\Json\EncoderInterface;
use Magento\Quote\Model\Quote;

/**
* Grid widget massaction block
*
* phpcs:disable Magento2.Classes.AbstractApi
* @api
* @method \Magento\Quote\Model\Quote setHideFormElement(boolean $value) Hide Form element to prevent IE errors
* @method Quote setHideFormElement(boolean $value) Hide Form element to prevent IE errors
* @method boolean getHideFormElement()
* @deprecated 100.2.0 in favour of UI component implementation
* @since 100.0.2
*/
abstract class AbstractMassaction extends \Magento\Backend\Block\Widget
abstract class AbstractMassaction extends Widget
{
/**
* @var \Magento\Framework\Json\EncoderInterface
* @var EncoderInterface
*/
protected $_jsonEncoder;

Expand All @@ -39,13 +48,13 @@ abstract class AbstractMassaction extends \Magento\Backend\Block\Widget
protected $_template = 'Magento_Backend::widget/grid/massaction.phtml';

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param Context $context
* @param EncoderInterface $jsonEncoder
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
Context $context,
EncoderInterface $jsonEncoder,
array $data = []
) {
$this->_jsonEncoder = $jsonEncoder;
Expand Down Expand Up @@ -118,15 +127,11 @@ private function isVisible(DataObject $item)
* Retrieve massaction item with id $itemId
*
* @param string $itemId
* @return \Magento\Backend\Block\Widget\Grid\Massaction\Item|null
* @return Item|null
*/
public function getItem($itemId)
{
if (isset($this->_items[$itemId])) {
return $this->_items[$itemId];
}

return null;
return $this->_items[$itemId] ?? null;
}

/**
Expand Down Expand Up @@ -161,7 +166,7 @@ public function getItemsJson()
*/
public function getCount()
{
return sizeof($this->_items);
return count($this->_items);
}

/**
Expand Down Expand Up @@ -288,11 +293,11 @@ public function getGridIdsJson()

if ($collection instanceof AbstractDb) {
$idsSelect = clone $collection->getSelect();
$idsSelect->reset(\Magento\Framework\DB\Select::ORDER);
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_COUNT);
$idsSelect->reset(\Magento\Framework\DB\Select::LIMIT_OFFSET);
$idsSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
$idsSelect->columns($this->getMassactionIdField(), 'main_table');
$idsSelect->reset(Select::ORDER);
$idsSelect->reset(Select::LIMIT_COUNT);
$idsSelect->reset(Select::LIMIT_OFFSET);
$idsSelect->reset(Select::COLUMNS);
$idsSelect->columns($this->getMassactionIdField());
$idList = $collection->getConnection()->fetchCol($idsSelect);
} else {
$idList = $collection->setPageSize(0)->getColumnValues($this->getMassactionIdField());
Expand Down Expand Up @@ -358,7 +363,7 @@ public function prepareMassactionColumn()
{
$columnId = 'massaction';
$massactionColumn = $this->getLayout()->createBlock(
\Magento\Backend\Block\Widget\Grid\Column::class
Column::class
)->setData(
[
'index' => $this->getMassactionIdField(),
Expand All @@ -378,7 +383,7 @@ public function prepareMassactionColumn()
$gridBlock = $this->getParentBlock();
$massactionColumn->setSelected($this->getSelected())->setGrid($gridBlock)->setId($columnId);

/** @var $columnSetBlock \Magento\Backend\Block\Widget\Grid\ColumnSet */
/** @var $columnSetBlock ColumnSet */
$columnSetBlock = $gridBlock->getColumnSet();
$childNames = $columnSetBlock->getChildNames();
$siblingElement = count($childNames) ? current($childNames) : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
* See COPYING.txt for license details.
*/

/**
* Test class for \Magento\Backend\Block\Widget\Grid\Massaction
*/
namespace Magento\Backend\Test\Unit\Block\Widget\Grid;

use Magento\Backend\Block\Widget\Grid\Massaction\VisibilityCheckerInterface as VisibilityChecker;
use Magento\Framework\Authorization;
use Magento\Framework\Data\Collection\AbstractDb as Collection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\DB\Select;

/**
* Test class for \Magento\Backend\Block\Widget\Grid\Massaction
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class MassactionTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -54,6 +59,21 @@ class MassactionTest extends \PHPUnit\Framework\TestCase
*/
private $visibilityCheckerMock;

/**
* @var Collection|\PHPUnit\Framework\MockObject\MockObject
*/
private $gridCollectionMock;

/**
* @var Select|\PHPUnit\Framework\MockObject\MockObject
*/
private $gridCollectionSelectMock;

/**
* @var AdapterInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $connectionMock;

protected function setUp()
{
$this->_gridMock = $this->getMockBuilder(\Magento\Backend\Block\Widget\Grid::class)
Expand Down Expand Up @@ -97,6 +117,18 @@ protected function setUp()
->setMethods(['isAllowed'])
->getMock();

$this->gridCollectionMock = $this->createMock(Collection::class);
$this->gridCollectionSelectMock = $this->createMock(Select::class);
$this->connectionMock = $this->createMock(AdapterInterface::class);

$this->gridCollectionMock->expects($this->any())
->method('getSelect')
->willReturn($this->gridCollectionSelectMock);

$this->gridCollectionMock->expects($this->any())
->method('getConnection')
->willReturn($this->connectionMock);

$arguments = [
'layout' => $this->_layoutMock,
'request' => $this->_requestMock,
Expand Down Expand Up @@ -269,6 +301,41 @@ public function testGetGridIdsJsonWithoutUseSelectAll()
$this->assertEmpty($this->_block->getGridIdsJson());
}

/**
* Test for getGridIdsJson when select all functionality flag set to true.
*/
public function testGetGridIdsJsonWithUseSelectAll()
{
$this->_block->setUseSelectAll(true);

$this->_gridMock->expects($this->once())
->method('getCollection')
->willReturn($this->gridCollectionMock);

$this->gridCollectionSelectMock->expects($this->exactly(4))
->method('reset')
->withConsecutive(
[Select::ORDER],
[Select::LIMIT_COUNT],
[Select::LIMIT_OFFSET],
[Select::COLUMNS]
);

$this->gridCollectionSelectMock->expects($this->once())
->method('columns')
->with('test_id');

$this->connectionMock->expects($this->once())
->method('fetchCol')
->with($this->gridCollectionSelectMock)
->willReturn([1, 2, 3]);

$this->assertEquals(
'1,2,3',
$this->_block->getGridIdsJson()
);
}

/**
* @param string $itemId
* @param array|\Magento\Framework\DataObject $item
Expand Down

0 comments on commit c2af7f2

Please sign in to comment.