diff --git a/app/code/Magento/Review/Block/Adminhtml/Main.php b/app/code/Magento/Review/Block/Adminhtml/Main.php index 45d14a1c60e69..5fae6acbce48a 100644 --- a/app/code/Magento/Review/Block/Adminhtml/Main.php +++ b/app/code/Magento/Review/Block/Adminhtml/Main.php @@ -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 { /** @@ -37,6 +44,13 @@ 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 @@ -44,6 +58,7 @@ class Main extends \Magento\Backend\Block\Widget\Grid\Container * @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, @@ -51,12 +66,15 @@ public function __construct( \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); } @@ -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 = ''; diff --git a/app/code/Magento/Review/Test/Unit/Block/Adminhtml/MainTest.php b/app/code/Magento/Review/Test/Unit/Block/Adminhtml/MainTest.php index c7ff721d4ba22..41cabf68262d1 100644 --- a/app/code/Magento/Review/Test/Unit/Block/Adminhtml/MainTest.php +++ b/app/code/Magento/Review/Test/Unit/Block/Adminhtml/MainTest.php @@ -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') @@ -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) @@ -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 ] ); }