Skip to content

Commit 121caa9

Browse files
authored
ENGCOM-5774: Remove "Add Review" if System has no Product issue24310 #24399
2 parents 16681cf + 5f01dd6 commit 121caa9

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

app/code/Magento/Review/Block/Adminhtml/Main.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,19 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
/**
89
* Adminhtml review main block
910
*/
1011
namespace Magento\Review\Block\Adminhtml;
1112

13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
15+
16+
/**
17+
* Class \Magento\Review\Block\Adminhtml\Main
18+
*/
1219
class Main extends \Magento\Backend\Block\Widget\Grid\Container
1320
{
1421
/**
@@ -37,26 +44,37 @@ class Main extends \Magento\Backend\Block\Widget\Grid\Container
3744
*/
3845
protected $_customerViewHelper;
3946

47+
/**
48+
* Product Collection
49+
*
50+
* @var ProductCollectionFactory
51+
*/
52+
private $productCollectionFactory;
53+
4054
/**
4155
* @param \Magento\Backend\Block\Widget\Context $context
4256
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
4357
* @param \Magento\Catalog\Model\ProductFactory $productFactory
4458
* @param \Magento\Framework\Registry $registry
4559
* @param \Magento\Customer\Helper\View $customerViewHelper
4660
* @param array $data
61+
* @param ProductCollectionFactory $productCollectionFactory
4762
*/
4863
public function __construct(
4964
\Magento\Backend\Block\Widget\Context $context,
5065
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
5166
\Magento\Catalog\Model\ProductFactory $productFactory,
5267
\Magento\Framework\Registry $registry,
5368
\Magento\Customer\Helper\View $customerViewHelper,
54-
array $data = []
69+
array $data = [],
70+
ProductCollectionFactory $productCollectionFactory = null
5571
) {
5672
$this->_coreRegistry = $registry;
5773
$this->customerRepository = $customerRepository;
5874
$this->_productFactory = $productFactory;
5975
$this->_customerViewHelper = $customerViewHelper;
76+
$this->productCollectionFactory = $productCollectionFactory ?: ObjectManager::getInstance()
77+
->get(ProductCollectionFactory::class);
6078
parent::__construct($context, $data);
6179
}
6280

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

94+
if (!$this->productCollectionFactory->create()->getSize()) {
95+
$this->removeButton('add');
96+
}
97+
7698
// lookup customer, if id is specified
7799
$customerId = $this->getRequest()->getParam('customerId', false);
78100
$customerName = '';

app/code/Magento/Review/Test/Unit/Block/Adminhtml/MainTest.php

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,59 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

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

910
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\Customer\Helper\View as ViewHelper;
13+
use Magento\Customer\Api\Data\CustomerInterface;
14+
use Magento\Framework\App\RequestInterface;
15+
use Magento\Review\Block\Adminhtml\Main as MainBlock;
16+
use Magento\Framework\DataObject;
17+
use Magento\Catalog\Model\ResourceModel\Product\Collection;
18+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
1019

20+
/**
21+
* Unit Test For Main Block
22+
*
23+
* Class \Magento\Review\Test\Unit\Block\Adminhtml\MainTest
24+
*/
1125
class MainTest extends \PHPUnit\Framework\TestCase
1226
{
1327
/**
14-
* @var \Magento\Review\Block\Adminhtml\Main
28+
* @var MainBlock
1529
*/
1630
protected $model;
1731

1832
/**
19-
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
33+
* @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
2034
*/
2135
protected $request;
2236

2337
/**
24-
* @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
38+
* @var CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
2539
*/
2640
protected $customerRepository;
2741

2842
/**
29-
* @var \Magento\Customer\Helper\View|\PHPUnit_Framework_MockObject_MockObject
43+
* @var ViewHelper|\PHPUnit_Framework_MockObject_MockObject
3044
*/
3145
protected $customerViewHelper;
3246

47+
/**
48+
* @var CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
protected $collectionFactory;
51+
3352
public function testConstruct()
3453
{
3554
$this->customerRepository = $this
36-
->getMockForAbstractClass(\Magento\Customer\Api\CustomerRepositoryInterface::class);
37-
$this->customerViewHelper = $this->createMock(\Magento\Customer\Helper\View::class);
38-
$dummyCustomer = $this->getMockForAbstractClass(\Magento\Customer\Api\Data\CustomerInterface::class);
55+
->getMockForAbstractClass(CustomerRepositoryInterface::class);
56+
$this->customerViewHelper = $this->createMock(ViewHelper::class);
57+
$this->collectionFactory = $this->createMock(CollectionFactory::class);
58+
$dummyCustomer = $this->getMockForAbstractClass(CustomerInterface::class);
3959

4060
$this->customerRepository->expects($this->once())
4161
->method('getById')
@@ -44,8 +64,8 @@ public function testConstruct()
4464
$this->customerViewHelper->expects($this->once())
4565
->method('getCustomerName')
4666
->with($dummyCustomer)
47-
->will($this->returnValue(new \Magento\Framework\DataObject()));
48-
$this->request = $this->getMockForAbstractClass(\Magento\Framework\App\RequestInterface::class);
67+
->will($this->returnValue(new DataObject()));
68+
$this->request = $this->getMockForAbstractClass(RequestInterface::class);
4969
$this->request->expects($this->at(0))
5070
->method('getParam')
5171
->with('customerId', false)
@@ -54,14 +74,21 @@ public function testConstruct()
5474
->method('getParam')
5575
->with('productId', false)
5676
->will($this->returnValue(false));
77+
$productCollection = $this->getMockBuilder(Collection::class)
78+
->disableOriginalConstructor()
79+
->getMock();
80+
$this->collectionFactory->expects($this->once())
81+
->method('create')
82+
->will($this->returnValue($productCollection));
5783

5884
$objectManagerHelper = new ObjectManagerHelper($this);
5985
$this->model = $objectManagerHelper->getObject(
60-
\Magento\Review\Block\Adminhtml\Main::class,
86+
MainBlock::class,
6187
[
6288
'request' => $this->request,
6389
'customerRepository' => $this->customerRepository,
64-
'customerViewHelper' => $this->customerViewHelper
90+
'customerViewHelper' => $this->customerViewHelper,
91+
'productCollectionFactory' => $this->collectionFactory
6592
]
6693
);
6794
}

0 commit comments

Comments
 (0)