Skip to content

Remove "Add Review" if System has no Product issue24310 #24399

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

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
24 changes: 23 additions & 1 deletion app/code/Magento/Review/Block/Adminhtml/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

/**
* Adminhtml review main block
*/
namespace Magento\Review\Block\Adminhtml;

use Magento\Framework\App\ObjectManager;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;

/**
* Class \Magento\Review\Block\Adminhtml\Main
*/
class Main extends \Magento\Backend\Block\Widget\Grid\Container
{
/**
Expand Down Expand Up @@ -37,26 +44,37 @@ class Main extends \Magento\Backend\Block\Widget\Grid\Container
*/
protected $_customerViewHelper;

/**
* Product Collection
*
* @var ProductCollectionFactory
*/
private $productCollectionFactory;

/**
* @param \Magento\Backend\Block\Widget\Context $context
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
* @param \Magento\Catalog\Model\ProductFactory $productFactory
* @param \Magento\Framework\Registry $registry
* @param \Magento\Customer\Helper\View $customerViewHelper
* @param array $data
* @param ProductCollectionFactory $productCollectionFactory
*/
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
\Magento\Catalog\Model\ProductFactory $productFactory,
\Magento\Framework\Registry $registry,
\Magento\Customer\Helper\View $customerViewHelper,
array $data = []
array $data = [],
ProductCollectionFactory $productCollectionFactory = null
) {
$this->_coreRegistry = $registry;
$this->customerRepository = $customerRepository;
$this->_productFactory = $productFactory;
$this->_customerViewHelper = $customerViewHelper;
$this->productCollectionFactory = $productCollectionFactory ?: ObjectManager::getInstance()
->get(ProductCollectionFactory::class);
parent::__construct($context, $data);
}

Expand All @@ -73,6 +91,10 @@ protected function _construct()
$this->_blockGroup = 'Magento_Review';
$this->_controller = 'adminhtml';

if (!$this->productCollectionFactory->create()->getSize()) {
$this->removeButton('add');
}

// lookup customer, if id is specified
$customerId = $this->getRequest()->getParam('customerId', false);
$customerName = '';
Expand Down
49 changes: 38 additions & 11 deletions app/code/Magento/Review/Test/Unit/Block/Adminhtml/MainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,59 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Review\Test\Unit\Block\Adminhtml;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Helper\View as ViewHelper;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Review\Block\Adminhtml\Main as MainBlock;
use Magento\Framework\DataObject;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

/**
* Unit Test For Main Block
*
* Class \Magento\Review\Test\Unit\Block\Adminhtml\MainTest
*/
class MainTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Review\Block\Adminhtml\Main
* @var MainBlock
*/
protected $model;

/**
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
* @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $request;

/**
* @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
* @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $customerRepository;

/**
* @var \Magento\Customer\Helper\View|\PHPUnit_Framework_MockObject_MockObject
* @var ViewHelper|\PHPUnit_Framework_MockObject_MockObject
*/
protected $customerViewHelper;

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

public function testConstruct()
{
$this->customerRepository = $this
->getMockForAbstractClass(\Magento\Customer\Api\CustomerRepositoryInterface::class);
$this->customerViewHelper = $this->createMock(\Magento\Customer\Helper\View::class);
$dummyCustomer = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\CustomerInterface::class);
->getMockForAbstractClass(CustomerRepositoryInterface::class);
$this->customerViewHelper = $this->createMock(ViewHelper::class);
$this->collectionFactory = $this->createMock(CollectionFactory::class);
$dummyCustomer = $this->getMockForAbstractClass(CustomerInterface::class);

$this->customerRepository->expects($this->once())
->method('getById')
Expand All @@ -44,8 +64,8 @@ public function testConstruct()
$this->customerViewHelper->expects($this->once())
->method('getCustomerName')
->with($dummyCustomer)
->will($this->returnValue(new \Magento\Framework\DataObject()));
$this->request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class);
->will($this->returnValue(new DataObject()));
$this->request = $this->getMockForAbstractClass(RequestInterface::class);
$this->request->expects($this->at(0))
->method('getParam')
->with('customerId', false)
Expand All @@ -54,14 +74,21 @@ public function testConstruct()
->method('getParam')
->with('productId', false)
->will($this->returnValue(false));
$productCollection = $this->getMockBuilder(Collection::class)
->disableOriginalConstructor()
->getMock();
$this->collectionFactory->expects($this->once())
->method('create')
->will($this->returnValue($productCollection));

$objectManagerHelper = new ObjectManagerHelper($this);
$this->model = $objectManagerHelper->getObject(
\Magento\Review\Block\Adminhtml\Main::class,
MainBlock::class,
[
'request' => $this->request,
'customerRepository' => $this->customerRepository,
'customerViewHelper' => $this->customerViewHelper
'customerViewHelper' => $this->customerViewHelper,
'productCollectionFactory' => $this->collectionFactory
]
);
}
Expand Down