Skip to content

Commit fed9b76

Browse files
[Magento Community Engineering] Community Contributions - 2.4-develop
- merged latest code from mainline branch
2 parents 1bd7851 + 2863caa commit fed9b76

File tree

20 files changed

+1064
-109
lines changed

20 files changed

+1064
-109
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magento\Backend\Test\Unit\Helper\Dashboard;
10+
11+
use Magento\Backend\Helper\Dashboard\Data as HelperData;
12+
use Magento\Framework\App\DeploymentConfig;
13+
use Magento\Framework\Config\ConfigOptionsListConstants;
14+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Store\Model\ResourceModel\Store\Collection as StoreCollection;
16+
use Magento\Store\Model\Store;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
use PHPUnit\Framework\MockObject\MockObject;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class DataTest extends TestCase
22+
{
23+
/**
24+
* Stub path install
25+
*/
26+
private const STUB_PATH_INSTALL = 'Sat, 6 Sep 2014 16:46:11 UTC';
27+
28+
/**
29+
* Stub chart data hash
30+
*/
31+
private const STUB_CHART_DATA_HASH = '52870842b23068a78220e01eb9d4404d';
32+
33+
/**
34+
* @var \Magento\Backend\Helper\Dashboard\Data
35+
*/
36+
private $helper;
37+
38+
/**
39+
* @var StoreManagerInterface|MockObject
40+
*/
41+
private $storeManagerMock;
42+
43+
/**
44+
* @var DeploymentConfig|MockObject
45+
*/
46+
private $deploymentConfigMock;
47+
48+
/**
49+
* Prepare environment for test
50+
*/
51+
protected function setUp()
52+
{
53+
$this->storeManagerMock = $this->createMock(StoreManagerInterface::class);
54+
$this->deploymentConfigMock = $this->createMock(DeploymentConfig::class);
55+
$this->deploymentConfigMock->expects($this->once())->method('get')
56+
->with(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE)
57+
->will($this->returnValue(self::STUB_PATH_INSTALL));
58+
59+
$objectManager = new ObjectManager($this);
60+
$this->helper = $objectManager->getObject(
61+
HelperData::class,
62+
[
63+
'storeManager' => $this->storeManagerMock,
64+
'deploymentConfig' => $this->deploymentConfigMock
65+
]
66+
);
67+
}
68+
69+
/**
70+
* Test getStores() when $_stores attribute is null
71+
*/
72+
public function testGetStoresWhenStoreAttributeIsNull()
73+
{
74+
$storeMock = $this->createPartialMock(Store::class, ['getResourceCollection']);
75+
$storeCollectionMock = $this->createMock(StoreCollection::class);
76+
77+
$storeCollectionMock->expects($this->once())->method('load')->willReturnSelf();
78+
$storeMock->expects($this->once())->method('getResourceCollection')->willReturn($storeCollectionMock);
79+
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($storeMock);
80+
81+
$this->assertEquals($storeCollectionMock, $this->helper->getStores());
82+
}
83+
84+
/**
85+
* Test getDatePeriods() method
86+
*/
87+
public function testGetDatePeriods()
88+
{
89+
$this->assertEquals(
90+
[
91+
'24h' => (string)__('Last 24 Hours'),
92+
'7d' => (string)__('Last 7 Days'),
93+
'1m' => (string)__('Current Month'),
94+
'1y' => (string)__('YTD'),
95+
'2y' => (string)__('2YTD')
96+
],
97+
$this->helper->getDatePeriods()
98+
);
99+
}
100+
101+
/**
102+
* Test getChartDataHash() method
103+
*/
104+
public function testGetChartDataHash()
105+
{
106+
$this->assertEquals(
107+
self::STUB_CHART_DATA_HASH,
108+
$this->helper->getChartDataHash(self::STUB_PATH_INSTALL)
109+
);
110+
}
111+
}

app/code/Magento/Catalog/Test/Unit/ViewModel/Product/BreadcrumbsTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\Catalog\ViewModel\Product\Breadcrumbs;
1313
use Magento\Framework\App\Config\ScopeConfigInterface;
1414
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
15+
use Magento\Framework\Serialize\Serializer\JsonHexTag;
1516

1617
/**
1718
* Unit test for Magento\Catalog\ViewModel\Product\Breadcrumbs.
@@ -38,6 +39,11 @@ class BreadcrumbsTest extends \PHPUnit\Framework\TestCase
3839
*/
3940
private $objectManager;
4041

42+
/**
43+
* @var JsonHexTag|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $serializer;
46+
4147
/**
4248
* @inheritdoc
4349
*/
@@ -55,12 +61,15 @@ protected function setUp() : void
5561

5662
$escaper = $this->getObjectManager()->getObject(\Magento\Framework\Escaper::class);
5763

64+
$this->serializer = $this->createMock(JsonHexTag::class);
65+
5866
$this->viewModel = $this->getObjectManager()->getObject(
5967
Breadcrumbs::class,
6068
[
6169
'catalogData' => $this->catalogHelper,
6270
'scopeConfig' => $this->scopeConfig,
63-
'escaper' => $escaper
71+
'escaper' => $escaper,
72+
'jsonSerializer' => $this->serializer
6473
]
6574
);
6675
}
@@ -141,6 +150,8 @@ public function testGetJsonConfiguration($product, string $expectedJson) : void
141150
->with('catalog/seo/category_url_suffix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
142151
->willReturn('."html');
143152

153+
$this->serializer->expects($this->once())->method('serialize')->willReturn($expectedJson);
154+
144155
$this->assertEquals($expectedJson, $this->viewModel->getJsonConfiguration());
145156
}
146157

app/code/Magento/Catalog/ViewModel/Product/Breadcrumbs.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Framework\App\ObjectManager;
1212
use Magento\Framework\DataObject;
1313
use Magento\Framework\Serialize\Serializer\Json;
14+
use Magento\Framework\Serialize\Serializer\JsonHexTag;
1415
use Magento\Framework\View\Element\Block\ArgumentInterface;
1516
use Magento\Framework\Escaper;
1617

@@ -36,24 +37,32 @@ class Breadcrumbs extends DataObject implements ArgumentInterface
3637
*/
3738
private $escaper;
3839

40+
/**
41+
* @var JsonHexTag
42+
*/
43+
private $jsonSerializer;
44+
3945
/**
4046
* @param Data $catalogData
4147
* @param ScopeConfigInterface $scopeConfig
4248
* @param Json|null $json
4349
* @param Escaper|null $escaper
50+
* @param JsonHexTag|null $jsonSerializer
4451
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4552
*/
4653
public function __construct(
4754
Data $catalogData,
4855
ScopeConfigInterface $scopeConfig,
4956
Json $json = null,
50-
Escaper $escaper = null
57+
Escaper $escaper = null,
58+
JsonHexTag $jsonSerializer = null
5159
) {
5260
parent::__construct();
5361

5462
$this->catalogData = $catalogData;
5563
$this->scopeConfig = $scopeConfig;
5664
$this->escaper = $escaper ?: ObjectManager::getInstance()->get(Escaper::class);
65+
$this->jsonSerializer = $jsonSerializer ?: ObjectManager::getInstance()->get(JsonHexTag::class);
5766
}
5867

5968
/**
@@ -101,15 +110,14 @@ public function getProductName(): string
101110
*/
102111
public function getJsonConfigurationHtmlEscaped() : string
103112
{
104-
return json_encode(
113+
return $this->jsonSerializer->serialize(
105114
[
106115
'breadcrumbs' => [
107116
'categoryUrlSuffix' => $this->escaper->escapeHtml($this->getCategoryUrlSuffix()),
108117
'useCategoryPathInUrl' => (int)$this->isCategoryUsedInProductUrl(),
109118
'product' => $this->escaper->escapeHtml($this->getProductName())
110119
]
111-
],
112-
JSON_HEX_TAG
120+
]
113121
);
114122
}
115123

0 commit comments

Comments
 (0)