Skip to content

Commit fdd02f7

Browse files
committed
Merge remote-tracking branch 'origin/MC-38074' into 2.4-develop-pr44
2 parents db20a40 + 837175d commit fdd02f7

File tree

6 files changed

+310
-27
lines changed

6 files changed

+310
-27
lines changed

app/code/Magento/Reports/Block/Adminhtml/Grid/Shopcart.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Shopcart extends \Magento\Backend\Block\Widget\Grid\Extended
2828

2929
/**
3030
* StoreIds setter
31+
*
3132
* @codeCoverageIgnore
3233
*
3334
* @param array $storeIds
@@ -46,6 +47,10 @@ public function setStoreIds($storeIds)
4647
*/
4748
public function getCurrentCurrencyCode()
4849
{
50+
if (empty($this->_storeIds)) {
51+
$this->setStoreIds(array_keys($this->_storeManager->getStores()));
52+
}
53+
4954
if ($this->_currentCurrencyCode === null) {
5055
reset($this->_storeIds);
5156
$this->_currentCurrencyCode = count(
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Reports\Model\Product;
9+
10+
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
11+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Retrieve products data for reports by entity id's
16+
*/
17+
class DataRetriever
18+
{
19+
/**
20+
* @var ProductCollectionFactory
21+
*/
22+
private $productCollectionFactory;
23+
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* DataRetriever constructor.
31+
*
32+
* @param ProductCollectionFactory $productCollectionFactory
33+
* @param StoreManagerInterface $storeManager
34+
*/
35+
public function __construct(
36+
ProductCollectionFactory $productCollectionFactory,
37+
StoreManagerInterface $storeManager
38+
) {
39+
$this->productCollectionFactory = $productCollectionFactory;
40+
$this->storeManager = $storeManager;
41+
}
42+
43+
/**
44+
* Retrieve products data by entity id's
45+
*
46+
* @param array $entityIds
47+
* @return array
48+
*/
49+
public function execute(array $entityIds = []): array
50+
{
51+
$productCollection = $this->getProductCollection($entityIds);
52+
53+
return $this->prepareDataByCollection($productCollection);
54+
}
55+
56+
/**
57+
* Get product collection filtered by entity id's
58+
*
59+
* @param array $entityIds
60+
* @return ProductCollection
61+
*/
62+
private function getProductCollection(array $entityIds = []): ProductCollection
63+
{
64+
$productCollection = $this->productCollectionFactory->create();
65+
$productCollection->addAttributeToSelect('name');
66+
$productCollection->addIdFilter($entityIds);
67+
$productCollection->addPriceData(null, $this->getWebsiteIdForFilter());
68+
69+
return $productCollection;
70+
}
71+
72+
/**
73+
* Retrieve website id for filter collection
74+
*
75+
* @return int
76+
*/
77+
private function getWebsiteIdForFilter(): int
78+
{
79+
$defaultStoreView = $this->storeManager->getDefaultStoreView();
80+
if ($defaultStoreView) {
81+
$websiteId = (int)$defaultStoreView->getWebsiteId();
82+
} else {
83+
$websites = $this->storeManager->getWebsites();
84+
$website = reset($websites);
85+
$websiteId = (int)$website->getId();
86+
}
87+
88+
return $websiteId;
89+
}
90+
91+
/**
92+
* Prepare data by collection
93+
*
94+
* @param ProductCollection $productCollection
95+
* @return array
96+
*/
97+
private function prepareDataByCollection(ProductCollection $productCollection): array
98+
{
99+
$productsData = [];
100+
foreach ($productCollection as $product) {
101+
$productsData[$product->getId()] = $product->getData();
102+
}
103+
104+
return $productsData;
105+
}
106+
}

app/code/Magento/Reports/Model/ResourceModel/Quote/Item/Collection.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
namespace Magento\Reports\Model\ResourceModel\Quote\Item;
99

10-
use Magento\Framework\App\ResourceConnection;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Reports\Model\Product\DataRetriever as ProductDataRetriever;
1112

1213
/**
1314
* Collection of Magento\Quote\Model\Quote\Item
@@ -49,6 +50,11 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
4950
*/
5051
protected $orderResource;
5152

53+
/**
54+
* @var ProductDataRetriever
55+
*/
56+
private $productDataRetriever;
57+
5258
/**
5359
* @param \Magento\Framework\Data\Collection\EntityFactory $entityFactory
5460
* @param \Psr\Log\LoggerInterface $logger
@@ -59,6 +65,9 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
5965
* @param \Magento\Sales\Model\ResourceModel\Order\Collection $orderResource
6066
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
6167
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
68+
* @param ProductDataRetriever|null $productDataRetriever
69+
*
70+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
6271
*/
6372
public function __construct(
6473
\Magento\Framework\Data\Collection\EntityFactory $entityFactory,
@@ -69,7 +78,8 @@ public function __construct(
6978
\Magento\Customer\Model\ResourceModel\Customer $customerResource,
7079
\Magento\Sales\Model\ResourceModel\Order\Collection $orderResource,
7180
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
72-
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
81+
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
82+
?ProductDataRetriever $productDataRetriever = null
7383
) {
7484
parent::__construct(
7585
$entityFactory,
@@ -82,6 +92,8 @@ public function __construct(
8292
$this->productResource = $productResource;
8393
$this->customerResource = $customerResource;
8494
$this->orderResource = $orderResource;
95+
$this->productDataRetriever = $productDataRetriever
96+
?? ObjectManager::getInstance()->get(ProductDataRetriever::class);
8597
}
8698

8799
/**
@@ -225,7 +237,7 @@ protected function _afterLoad()
225237
foreach ($items as $item) {
226238
$productIds[] = $item->getProductId();
227239
}
228-
$productData = $this->getProductData($productIds);
240+
$productData = $this->productDataRetriever->execute($productIds);
229241
$orderData = $this->getOrdersData($productIds);
230242
foreach ($items as $item) {
231243
$item->setId($item->getProductId());
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Reports\Test\Unit\Block\Adminhtml\Grid;
9+
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Reports\Block\Adminhtml\Grid\Shopcart;
12+
use Magento\Store\Api\Data\StoreInterface;
13+
use Magento\Store\Model\StoreManagerInterface;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Test for class \Magento\Reports\Block\Adminhtml\Grid\Shopcart.
19+
*/
20+
class ShopcartTest extends TestCase
21+
{
22+
/**
23+
* @var Shopcart|MockObject
24+
*/
25+
private $model;
26+
27+
/**
28+
* @var StoreManagerInterface|MockObject
29+
*/
30+
private $storeManagerMock;
31+
32+
protected function setUp(): void
33+
{
34+
$objectManager = new ObjectManager($this);
35+
36+
$this->storeManagerMock = $this->getMockForAbstractClass(
37+
StoreManagerInterface::class,
38+
[],
39+
'',
40+
true,
41+
true,
42+
true,
43+
['getStore']
44+
);
45+
46+
$this->model = $objectManager->getObject(
47+
Shopcart::class,
48+
['_storeManager' => $this->storeManagerMock]
49+
);
50+
}
51+
52+
/**
53+
* @param $storeIds
54+
*
55+
* @dataProvider getCurrentCurrencyCodeDataProvider
56+
*/
57+
public function testGetCurrentCurrencyCode($storeIds)
58+
{
59+
$storeMock = $this->getMockForAbstractClass(
60+
StoreInterface::class,
61+
[],
62+
'',
63+
true,
64+
true,
65+
true,
66+
['getBaseCurrencyCode']
67+
);
68+
69+
$this->model->setStoreIds($storeIds);
70+
71+
if ($storeIds) {
72+
$expectedCurrencyCode = 'EUR';
73+
$this->storeManagerMock->expects($this->once())
74+
->method('getStore')
75+
->with($storeIds[0])
76+
->willReturn($storeMock);
77+
$storeMock->expects($this->once())
78+
->method('getBaseCurrencyCode')
79+
->willReturn($expectedCurrencyCode);
80+
} else {
81+
$expectedCurrencyCode = 'USD';
82+
$this->storeManagerMock->expects($this->once())
83+
->method('getStore')
84+
->with(1)
85+
->willReturn($storeMock);
86+
$this->storeManagerMock->expects($this->once())
87+
->method('getStores')
88+
->willReturn([1 => $storeMock]);
89+
$storeMock->expects($this->once())
90+
->method('getBaseCurrencyCode')
91+
->willReturn($expectedCurrencyCode);
92+
}
93+
94+
$currencyCode = $this->model->getCurrentCurrencyCode();
95+
$this->assertEquals($expectedCurrencyCode, $currencyCode);
96+
}
97+
98+
/**
99+
* DataProvider for testGetCurrentCurrencyCode.
100+
*
101+
* @return array
102+
*/
103+
public function getCurrentCurrencyCodeDataProvider()
104+
{
105+
return [
106+
[[]],
107+
[[2]],
108+
];
109+
}
110+
}

0 commit comments

Comments
 (0)