Skip to content

Commit 81fd7ee

Browse files
authored
Merge branch 'magento-commerce:2.4-develop' into comm_78764_33411
2 parents 919b404 + e0d650c commit 81fd7ee

File tree

79 files changed

+2430
-295
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+2430
-295
lines changed

app/code/Magento/Backend/Model/Dashboard/Chart/Date.php

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

88
namespace Magento\Backend\Model\Dashboard\Chart;
99

10-
use DateTimeZone;
1110
use Magento\Backend\Model\Dashboard\Period;
1211
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1312
use Magento\Reports\Model\ResourceModel\Order\CollectionFactory;
@@ -57,36 +56,38 @@ public function getByPeriod(string $period): array
5756
);
5857

5958
$timezoneLocal = $this->localeDate->getConfigTimezone();
60-
61-
$dateStart->setTimezone(new DateTimeZone($timezoneLocal));
62-
$dateEnd->setTimezone(new DateTimeZone($timezoneLocal));
59+
$localStartDate = new \DateTime($dateStart->format('Y-m-d H:i:s'), new \DateTimeZone($timezoneLocal));
60+
$localEndDate = new \DateTime($dateEnd->format('Y-m-d H:i:s'), new \DateTimeZone($timezoneLocal));
6361

6462
if ($period === Period::PERIOD_24_HOURS) {
65-
$dateEnd->modify('-1 hour');
63+
$localEndDate = new \DateTime('now', new \DateTimeZone($timezoneLocal));
64+
$localStartDate = clone $localEndDate;
65+
$localStartDate->modify('-1 day');
66+
$localStartDate->modify('+1 hour');
6667
} elseif ($period === Period::PERIOD_TODAY) {
67-
$dateEnd->modify('now');
68+
$localEndDate->modify('now');
6869
} else {
69-
$dateEnd->setTime(23, 59, 59);
70-
$dateStart->setTime(0, 0, 0);
70+
$localEndDate->setTime(23, 59, 59);
71+
$localStartDate->setTime(0, 0, 0);
7172
}
7273

7374
$dates = [];
7475

75-
while ($dateStart <= $dateEnd) {
76+
while ($localStartDate <= $localEndDate) {
7677
switch ($period) {
7778
case Period::PERIOD_7_DAYS:
7879
case Period::PERIOD_1_MONTH:
79-
$d = $dateStart->format('Y-m-d');
80-
$dateStart->modify('+1 day');
80+
$d = $localStartDate->format('Y-m-d');
81+
$localStartDate->modify('+1 day');
8182
break;
8283
case Period::PERIOD_1_YEAR:
8384
case Period::PERIOD_2_YEARS:
84-
$d = $dateStart->format('Y-m');
85-
$dateStart->modify('first day of next month');
85+
$d = $localStartDate->format('Y-m');
86+
$localStartDate->modify('first day of next month');
8687
break;
8788
default:
88-
$d = $dateStart->format('Y-m-d H:00');
89-
$dateStart->modify('+1 hour');
89+
$d = $localStartDate->format('Y-m-d H:00');
90+
$localStartDate->modify('+1 hour');
9091
}
9192

9293
$dates[] = $d;
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
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\Backend\Test\Unit\Model\Dashboard\Chart;
9+
10+
use Magento\Backend\Model\Dashboard\Chart\Date;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
13+
use Magento\Framework\Data\Collection\EntityFactory;
14+
use Magento\Framework\DB\Adapter\AdapterInterface;
15+
use Magento\Framework\DB\Adapter\Pdo\Mysql;
16+
use Magento\Framework\DB\Helper;
17+
use Magento\Framework\DB\Select;
18+
use Magento\Framework\Event\ManagerInterface;
19+
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
20+
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
21+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
22+
use Magento\Reports\Model\ResourceModel\Order\CollectionFactory;
23+
use Magento\Backend\Model\Dashboard\Period;
24+
use Magento\Reports\Model\ResourceModel\Order\Collection;
25+
use Magento\Sales\Model\Order\Config;
26+
use Magento\Sales\Model\ResourceModel\Report\OrderFactory;
27+
use Magento\Store\Model\ScopeInterface;
28+
use Magento\Store\Model\StoreManagerInterface;
29+
use PHPUnit\Framework\MockObject\MockObject;
30+
use PHPUnit\Framework\TestCase;
31+
use Psr\Log\LoggerInterface;
32+
33+
/**
34+
* @SuppressWarnings(PHPMD.TooManyFields)
35+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36+
*/
37+
class DateTest extends TestCase
38+
{
39+
/**
40+
* @var Date
41+
*/
42+
private $model;
43+
44+
/**
45+
* @var Collection
46+
*/
47+
private $collection;
48+
49+
/**
50+
* @var EntityFactory|MockObject
51+
*/
52+
private $entityFactoryMock;
53+
54+
/**
55+
* @var LoggerInterface|MockObject
56+
*/
57+
private $loggerMock;
58+
59+
/**
60+
* @var FetchStrategyInterface|MockObject
61+
*/
62+
private $fetchStrategyMock;
63+
64+
/**
65+
* @var ManagerInterface|MockObject
66+
*/
67+
private $managerMock;
68+
69+
/**
70+
* @var \Magento\Sales\Model\ResourceModel\EntitySnapshot|MockObject
71+
*/
72+
private $entitySnapshotMock;
73+
74+
/**
75+
* @var Helper|MockObject
76+
*/
77+
private $helperMock;
78+
79+
/**
80+
* @var ScopeConfigInterface|MockObject
81+
*/
82+
private $scopeConfigMock;
83+
84+
/**
85+
* @var StoreManagerInterface|MockObject
86+
*/
87+
private $storeManagerMock;
88+
89+
/**
90+
* @var TimezoneInterface|MockObject
91+
*/
92+
private $timezoneMock;
93+
94+
/**
95+
* @var Config|MockObject
96+
*/
97+
private $configMock;
98+
99+
/**
100+
* @var OrderFactory|MockObject
101+
*/
102+
private $orderFactoryMock;
103+
104+
/**
105+
* @var AdapterInterface|MockObject
106+
*/
107+
private $connectionMock;
108+
109+
/**
110+
* @var Select|MockObject
111+
*/
112+
private $selectMock;
113+
114+
/**
115+
* @var AbstractDb|MockObject
116+
*/
117+
private $resourceMock;
118+
119+
/**
120+
* @var CollectionFactory
121+
*/
122+
private $collectionFactoryMock;
123+
124+
/**
125+
* @inheritDoc
126+
*/
127+
protected function setUp(): void
128+
{
129+
$this->collection = $this->getCollectionObject();
130+
$this->collectionFactoryMock = $this->getMockBuilder(CollectionFactory::class)
131+
->disableOriginalConstructor()
132+
->getMock();
133+
$this->collectionFactoryMock
134+
->expects($this->any())
135+
->method('create')
136+
->willReturn($this->collection);
137+
$this->model = new Date($this->collectionFactoryMock, $this->timezoneMock);
138+
}
139+
140+
/**
141+
* @param string $period
142+
* @param string $config
143+
* @param int $expectedYear
144+
*
145+
* @return void
146+
* @dataProvider getByPeriodDataProvider
147+
*/
148+
public function testGetByPeriod($period, $config, $expectedYear): void
149+
{
150+
$this->scopeConfigMock
151+
->expects($this->once())
152+
->method('getValue')
153+
->with(
154+
$config,
155+
ScopeInterface::SCOPE_STORE
156+
)
157+
->willReturn(1);
158+
$dates = $this->model->getByPeriod($period);
159+
$this->assertEquals($expectedYear, substr($dates[0], 0, 4));
160+
}
161+
162+
/**
163+
* @return array
164+
*/
165+
public function getByPeriodDataProvider(): array
166+
{
167+
$dateStart = new \DateTime();
168+
$expectedYear = $dateStart->format('Y');
169+
$expected2YTDYear = $expectedYear - 1;
170+
171+
return [
172+
[Period::PERIOD_1_YEAR, 'reports/dashboard/ytd_start', $expectedYear],
173+
[Period::PERIOD_2_YEARS, 'reports/dashboard/ytd_start', $expected2YTDYear]
174+
];
175+
}
176+
177+
/**
178+
* @return Collection
179+
*/
180+
private function getCollectionObject()
181+
{
182+
$this->entityFactoryMock = $this->getMockBuilder(EntityFactory::class)
183+
->disableOriginalConstructor()
184+
->getMock();
185+
$this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
186+
->getMock();
187+
$this->fetchStrategyMock = $this->getMockBuilder(
188+
FetchStrategyInterface::class
189+
)->getMock();
190+
$this->managerMock = $this->getMockBuilder(ManagerInterface::class)
191+
->getMock();
192+
$snapshotClassName = Snapshot::class;
193+
$this->entitySnapshotMock = $this->getMockBuilder($snapshotClassName)
194+
->disableOriginalConstructor()
195+
->getMock();
196+
$this->helperMock = $this->getMockBuilder(Helper::class)
197+
->disableOriginalConstructor()
198+
->getMock();
199+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
200+
->getMock();
201+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
202+
->getMock();
203+
$this->timezoneMock = $this->getMockBuilder(TimezoneInterface::class)
204+
->getMock();
205+
$this->timezoneMock
206+
->expects($this->any())
207+
->method('getConfigTimezone')
208+
->willReturn('America/Chicago');
209+
$this->configMock = $this->getMockBuilder(Config::class)
210+
->disableOriginalConstructor()
211+
->getMock();
212+
$this->orderFactoryMock = $this->getMockBuilder(OrderFactory::class)
213+
->onlyMethods(['create'])
214+
->disableOriginalConstructor()
215+
->getMock();
216+
$this->selectMock = $this->getMockBuilder(Select::class)
217+
->disableOriginalConstructor()
218+
->getMock();
219+
$this->selectMock
220+
->expects($this->any())
221+
->method('columns')
222+
->willReturnSelf();
223+
$this->selectMock
224+
->expects($this->any())
225+
->method('where')
226+
->willReturnSelf();
227+
$this->selectMock
228+
->expects($this->any())
229+
->method('order')
230+
->willReturnSelf();
231+
$this->selectMock
232+
->expects($this->any())
233+
->method('group')
234+
->willReturnSelf();
235+
$this->selectMock
236+
->expects($this->any())
237+
->method('getPart')
238+
->willReturn([]);
239+
$this->connectionMock = $this->getMockBuilder(Mysql::class)
240+
->onlyMethods(['select', 'getIfNullSql', 'getDateFormatSql', 'prepareSqlCondition', 'getCheckSql'])
241+
->disableOriginalConstructor()
242+
->getMock();
243+
$this->connectionMock
244+
->expects($this->any())
245+
->method('select')
246+
->willReturn($this->selectMock);
247+
$this->resourceMock = $this->getMockBuilder(AbstractDb::class)
248+
->disableOriginalConstructor()
249+
->getMock();
250+
$this->resourceMock
251+
->expects($this->once())
252+
->method('getConnection')
253+
->willReturn($this->connectionMock);
254+
return new Collection(
255+
$this->entityFactoryMock,
256+
$this->loggerMock,
257+
$this->fetchStrategyMock,
258+
$this->managerMock,
259+
$this->entitySnapshotMock,
260+
$this->helperMock,
261+
$this->scopeConfigMock,
262+
$this->storeManagerMock,
263+
$this->timezoneMock,
264+
$this->configMock,
265+
$this->orderFactoryMock,
266+
null,
267+
$this->resourceMock
268+
);
269+
}
270+
}

app/code/Magento/Catalog/CustomerData/CompareProducts.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Magento\Framework\App\Config\ScopeConfigInterface;
1010
use Magento\Framework\App\ObjectManager;
1111
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Store\Model\StoreManagerInterface;
1213

1314
/**
1415
* Catalog Product Compare Widget
@@ -37,22 +38,30 @@ class CompareProducts implements SectionSourceInterface
3738
*/
3839
private $scopeConfig;
3940

41+
/**
42+
* @var StoreManagerInterface
43+
*/
44+
private $storeManager;
45+
4046
/**
4147
* @param \Magento\Catalog\Helper\Product\Compare $helper
4248
* @param \Magento\Catalog\Model\Product\Url $productUrl
4349
* @param \Magento\Catalog\Helper\Output $outputHelper
4450
* @param ScopeConfigInterface|null $scopeConfig
51+
* @param StoreManagerInterface|null $storeManager
4552
*/
4653
public function __construct(
4754
\Magento\Catalog\Helper\Product\Compare $helper,
4855
\Magento\Catalog\Model\Product\Url $productUrl,
4956
\Magento\Catalog\Helper\Output $outputHelper,
50-
?ScopeConfigInterface $scopeConfig = null
57+
?ScopeConfigInterface $scopeConfig = null,
58+
?StoreManagerInterface $storeManager = null
5159
) {
5260
$this->helper = $helper;
5361
$this->productUrl = $productUrl;
5462
$this->outputHelper = $outputHelper;
5563
$this->scopeConfig = $scopeConfig ?? ObjectManager::getInstance()->get(ScopeConfigInterface::class);
64+
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
5665
}
5766

5867
/**
@@ -66,6 +75,7 @@ public function getSectionData()
6675
'countCaption' => $count == 1 ? __('1 item') : __('%1 items', $count),
6776
'listUrl' => $this->helper->getListUrl(),
6877
'items' => $count ? $this->getItems() : [],
78+
'websiteId' => $this->storeManager->getWebsite()->getId()
6979
];
7080
}
7181

0 commit comments

Comments
 (0)