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

Checked if quote object contains id before looking for quote items #26489

5 changes: 2 additions & 3 deletions app/code/Magento/Customer/Block/Adminhtml/Edit/Tab/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,13 @@ protected function _prepareCollection()
{
$quote = $this->getQuote();

if ($quote) {
if ($quote && $quote->getId()) {
$collection = $quote->getItemsCollection(false);
$collection->addFieldToFilter('parent_item_id', ['null' => true]);
} else {
$collection = $this->_dataCollectionFactory->create();
}

$collection->addFieldToFilter('parent_item_id', ['null' => true]);

$this->setCollection($collection);

return parent::_prepareCollection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,52 @@
*/
namespace Magento\Customer\Block\Adminhtml\Edit\Tab;

use Magento\Backend\Block\Template\Context;
use Magento\Backend\Model\Session\Quote as SessionQuote;
use Magento\Customer\Controller\RegistryConstants;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Quote\Model\Quote;
use Magento\Store\Model\StoreManagerInterface;

/**
* Magento\Customer\Block\Adminhtml\Edit\Tab\Cart
*
* @magentoAppArea adminhtml
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CartTest extends \PHPUnit\Framework\TestCase
{
const CUSTOMER_ID_VALUE = 1234;
slavvka marked this conversation as resolved.
Show resolved Hide resolved

/** @var \Magento\Backend\Block\Template\Context */
/**
* @var Context
*/
private $_context;

/** @var \Magento\Framework\Registry */
/**
* @var Registry
*/
private $_coreRegistry;

/** @var \Magento\Store\Model\StoreManagerInterface */
/**
* @var StoreManagerInterface
*/
private $_storeManager;

/** @var Cart */
/**
* @var Cart
*/
private $_block;

/** @var \Magento\Framework\ObjectManagerInterface */
/**
* @var ObjectManagerInterface
*/
private $_objectManager;

/**
* @inheritdoc
*/
public function setUp()
{
$this->_objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
Expand All @@ -53,22 +73,96 @@ public function setUp()
);
}

/**
* @inheritdoc
*/
public function tearDown()
{
$this->_coreRegistry->unregister(RegistryConstants::CURRENT_CUSTOMER_ID);
}

public function testGetCustomerId()
/**
* Verify Grid with quote items
*
* @magentoDataFixture Magento/Sales/_files/quote_with_two_products_and_customer.php
* @magentoDataFixture Magento/Customer/_files/customer.php
* @dataProvider getQuoteDataProvider
*
* @param int $customerId
* @param bool $guest
* @param bool $contains
* @return void
*/
public function testVerifyCollectionWithQuote(int $customerId, bool $guest, bool $contains): void
{
$session = $this->_objectManager->create(SessionQuote::class);
$session->setCustomerId($customerId);
$quoteFixture = $this->_objectManager->create(Quote::class);
$quoteFixture->load('test01', 'reserved_order_id');
$quoteFixture->setCustomerIsGuest($guest)
->setCustomerId($customerId)
->save();
$this->_block->toHtml();
if ($contains) {
$this->assertContains(
"We couldn't find any records",
$this->_block->getGridParentHtml()
);
} else {
$this->assertNotContains(
"We couldn't find any records",
$this->_block->getGridParentHtml()
);
}
}

/**
* Data provider for withQuoteTest
*
* @return array
*/
public function getQuoteDataProvider(): array
{
return [
[
6,
false,
true
],
[
self::CUSTOMER_ID_VALUE,
true,
false
],
];
}

/**
* Verify Customer id
*
* @return void
*/
public function testGetCustomerId(): void
{
$this->assertEquals(self::CUSTOMER_ID_VALUE, $this->_block->getCustomerId());
}

public function testGetGridUrl()
/**
* Verify get grid url
*
* @return void
*/
public function testGetGridUrl(): void
{
$this->assertContains('/backend/customer/index/cart', $this->_block->getGridUrl());
}

public function testGetGridParentHtml()
/**
* Verify grid parent html
*
* @return void
*/
public function testGetGridParentHtml(): void
{
$this->_block = $this->_objectManager->get(
\Magento\Framework\View\LayoutInterface::class
Expand All @@ -87,14 +181,24 @@ public function testGetGridParentHtml()
);
}

public function testGetRowUrl()
/**
* Verify row url
*
* @return void
*/
public function testGetRowUrl(): void
{
$row = new \Magento\Framework\DataObject();
$row->setProductId(1);
$this->assertContains('/backend/catalog/product/edit/id/1', $this->_block->getRowUrl($row));
}

public function testGetHtml()
/**
* Verify get html
*
* @return void
*/
public function testGetHtml(): void
{
$html = $this->_block->toHtml();
$this->assertContains("<div id=\"customer_cart_grid\"", $html);
Expand Down