Skip to content

Commit 4d987d2

Browse files
Indrani SonawaneIndrani Sonawane
Indrani Sonawane
authored and
Indrani Sonawane
committed
Merge remote-tracking branch '37932/patch-16' into community_prs_march
2 parents 01cf443 + d8fc6d9 commit 4d987d2

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

app/code/Magento/Sales/Model/Order/Address/Renderer.php

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

78
namespace Magento\Sales\Model\Order\Address;
89

@@ -71,16 +72,22 @@ public function __construct(
7172
public function format(Address $address, $type)
7273
{
7374
$orderStore = $address->getOrder()->getStore();
75+
$originalStore = $this->storeManager->getStore();
7476
$this->storeManager->setCurrentStore($orderStore);
77+
$this->addressConfig->setStore($orderStore);
7578
$formatType = $this->addressConfig->getFormatByCode($type);
7679
if (!$formatType || !$formatType->getRenderer()) {
7780
return null;
7881
}
7982
$this->eventManager->dispatch('customer_address_format', ['type' => $formatType, 'address' => $address]);
8083
$addressData = $address->getData();
8184
$addressData['locale'] = $this->getLocaleByStoreId((int) $orderStore->getId());
85+
$rendered = $formatType->getRenderer()->renderArray($addressData);
8286

83-
return $formatType->getRenderer()->renderArray($addressData);
87+
$this->addressConfig->setStore($originalStore);
88+
$this->storeManager->setCurrentStore($originalStore);
89+
90+
return $rendered;
8491
}
8592

8693
/**

app/code/Magento/Sales/Test/Unit/Model/Order/Address/RendererTest.php

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99

1010
use Magento\Customer\Block\Address\Renderer\RendererInterface as CustomerAddressBlockRenderer;
1111
use Magento\Customer\Model\Address\Config as CustomerAddressConfig;
12+
use Magento\Directory\Helper\Data;
1213
use Magento\Framework\App\Config\ScopeConfigInterface;
1314
use Magento\Framework\DataObject;
1415
use Magento\Framework\Event\ManagerInterface as EventManager;
1516
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1617
use Magento\Sales\Model\Order;
1718
use Magento\Sales\Model\Order\Address as OrderAddress;
1819
use Magento\Sales\Model\Order\Address\Renderer as OrderAddressRenderer;
19-
use Magento\Store\Api\Data\StoreInterface;
2020
use Magento\Store\Model\StoreManagerInterface;
21+
use Magento\Store\Model\Store;
2122
use PHPUnit\Framework\MockObject\MockObject;
2223
use PHPUnit\Framework\TestCase;
2324

@@ -96,7 +97,7 @@ protected function setUp(): void
9697

9798
$this->storeConfigMock = $this->createMock(ScopeConfigInterface::class);
9899
$this->storeManagerMck = $this->getMockBuilder(StoreManagerInterface::class)
99-
->onlyMethods(['setCurrentStore'])
100+
->onlyMethods(['setCurrentStore', 'getStore'])
100101
->disableOriginalConstructor()
101102
->getMockForAbstractClass();
102103
$this->objectManagerHelper = new ObjectManagerHelper($this);
@@ -106,7 +107,7 @@ protected function setUp(): void
106107
'addressConfig' => $this->customerAddressConfigMock,
107108
'eventManager' => $this->eventManagerMock,
108109
'scopeConfig' => $this->storeConfigMock,
109-
'storeManager' => $this->storeManagerMck,
110+
'storeManager' => $this->storeManagerMck
110111
]
111112
);
112113
}
@@ -131,7 +132,7 @@ public function testFormat(): void
131132
->willReturn($addressData);
132133
$this->storeConfigMock->expects($this->once())
133134
->method('getValue')
134-
->willReturn(1);
135+
->willReturn('1');
135136
$this->customerAddressBlockRendererMock->expects(static::once())
136137
->method('renderArray')
137138
->with($addressData, null)
@@ -162,11 +163,34 @@ public function testFormatNoRenderer(): void
162163
*/
163164
private function setStoreExpectations(): void
164165
{
165-
$storeMock = $this->getMockBuilder(StoreInterface::class)
166+
$originalStoreMock = $this->getMockBuilder(Store::class)
166167
->disableOriginalConstructor()
167168
->onlyMethods(['getId'])
168169
->getMockForAbstractClass();
170+
$storeMock = $this->getMockBuilder(Store::class)
171+
->disableOriginalConstructor()
172+
->onlyMethods(['getId'])
173+
->getMockForAbstractClass();
174+
169175
$this->orderMock->expects(self::once())->method('getStore')->willReturn($storeMock);
170-
$this->storeManagerMck->expects(self::once())->method('setCurrentStore')->with($storeMock);
176+
177+
// One call to setup the store from the order, and an other one to rollback to the original store value
178+
$expected = [$storeMock, $originalStoreMock];
179+
$matcher = $this->exactly(count($expected));
180+
$this->storeManagerMck->expects(self::once())->method('getStore')->willReturn($originalStoreMock);
181+
$this->storeManagerMck->expects(self::any())->method('setCurrentStore')->with(
182+
$this->callback(function ($store) use ($matcher, $expected) {
183+
$this->assertEquals($store, $expected[$matcher->getInvocationCount()]);
184+
return true;
185+
})
186+
);
187+
188+
// One call to setup the store from the order, and an other one to rollback to the original store value
189+
$this->customerAddressConfigMock->expects(self::any())->method('setStore')->with(
190+
$this->callback(function ($store) use ($matcher, $expected) {
191+
$this->assertEquals($store, $expected[$matcher->getInvocationCount()]);
192+
return true;
193+
})
194+
);
171195
}
172196
}

dev/tests/integration/testsuite/Magento/Sales/Model/Order/Address/RendererTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,10 @@ public function testFormat()
124124
*/
125125
public function testFormatNonDisplayedCompanyField()
126126
{
127-
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
128-
$website = $storeManager->getWebsites(false, true)['test'];
127+
$orderFixtureStore = $this->objectManager->create(Order::class)->loadByIncrementId('100000001');
129128
$configData = [
130129
'section' => 'customer',
131-
'website' => $website->getId(),
130+
'website' => $orderFixtureStore->getStore()->getWebsite()->getId(),
132131
'store' => null,
133132
'groups' => [
134133
'address' => [
@@ -141,7 +140,6 @@ public function testFormatNonDisplayedCompanyField()
141140
$configFactory = $this->objectManager->get(Factory::class);
142141
$config = $configFactory->create(['data' => $configData]);
143142
$config->save();
144-
$orderFixtureStore = $this->objectManager->create(Order::class)->loadByIncrementId('100000001');
145143
$address = $orderFixtureStore->getBillingAddress();
146144
self::assertStringNotContainsString('Test Company', $this->orderAddressRenderer->format($address, 'html'));
147145
}

0 commit comments

Comments
 (0)