Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alias in grid massaction for eav collections #23452

Merged
merged 4 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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