Skip to content

Commit 5966d3a

Browse files
MAGETWO-82577: [Backport 2.2] Translate order getCreatedAtFormatted() to store locale #11422
2 parents 44c9096 + 8e8b0b3 commit 5966d3a

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

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

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

88
use Magento\Directory\Model\Currency;
99
use Magento\Framework\Api\AttributeValueFactory;
10+
use Magento\Framework\App\ObjectManager;
11+
use Magento\Framework\Locale\ResolverInterface;
1012
use Magento\Framework\Pricing\PriceCurrencyInterface;
1113
use Magento\Sales\Api\Data\OrderInterface;
1214
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
@@ -267,6 +269,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
267269
*/
268270
protected $timezone;
269271

272+
/**
273+
* @var ResolverInterface
274+
*/
275+
private $localeResolver;
276+
270277
/**
271278
* @param \Magento\Framework\Model\Context $context
272279
* @param \Magento\Framework\Registry $registry
@@ -295,7 +302,9 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
295302
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
296303
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
297304
* @param array $data
305+
* @param ResolverInterface $localeResolver
298306
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
307+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
299308
*/
300309
public function __construct(
301310
\Magento\Framework\Model\Context $context,
@@ -324,7 +333,8 @@ public function __construct(
324333
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productListFactory,
325334
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
326335
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
327-
array $data = []
336+
array $data = [],
337+
ResolverInterface $localeResolver = null
328338
) {
329339
$this->_storeManager = $storeManager;
330340
$this->_orderConfig = $orderConfig;
@@ -335,7 +345,6 @@ public function __construct(
335345
$this->_productVisibility = $productVisibility;
336346
$this->invoiceManagement = $invoiceManagement;
337347
$this->_currencyFactory = $currencyFactory;
338-
$this->_eavConfig = $eavConfig;
339348
$this->_orderHistoryFactory = $orderHistoryFactory;
340349
$this->_addressCollectionFactory = $addressCollectionFactory;
341350
$this->_paymentCollectionFactory = $paymentCollectionFactory;
@@ -346,6 +355,8 @@ public function __construct(
346355
$this->_trackCollectionFactory = $trackCollectionFactory;
347356
$this->salesOrderCollectionFactory = $salesOrderCollectionFactory;
348357
$this->priceCurrency = $priceCurrency;
358+
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(ResolverInterface::class);
359+
349360
parent::__construct(
350361
$context,
351362
$registry,
@@ -1830,7 +1841,7 @@ public function getCreatedAtFormatted($format)
18301841
new \DateTime($this->getCreatedAt()),
18311842
$format,
18321843
$format,
1833-
null,
1844+
$this->localeResolver->getDefaultLocale(),
18341845
$this->timezone->getConfigTimezone('store', $this->getStore())
18351846
);
18361847
}

app/code/Magento/Sales/Test/Unit/Model/OrderTest.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
use Magento\Catalog\Api\Data\ProductInterface;
99
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
10+
use Magento\Framework\Locale\ResolverInterface;
11+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1012
use Magento\Sales\Api\Data\OrderInterface;
1113
use Magento\Sales\Model\Order;
1214
use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory as HistoryCollectionFactory;
@@ -73,6 +75,16 @@ class OrderTest extends \PHPUnit\Framework\TestCase
7375
*/
7476
protected $productCollectionFactoryMock;
7577

78+
/**
79+
* @var ResolverInterface|\PHPUnit_Framework_MockObject_MockObject
80+
*/
81+
private $localeResolver;
82+
83+
/**
84+
* @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
85+
*/
86+
private $timezone;
87+
7688
protected function setUp()
7789
{
7890
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -124,6 +136,8 @@ protected function setUp()
124136
true,
125137
['round']
126138
);
139+
$this->localeResolver = $this->createMock(ResolverInterface::class);
140+
$this->timezone = $this->createMock(TimezoneInterface::class);
127141
$this->incrementId = '#00000001';
128142
$this->eventManager = $this->createMock(\Magento\Framework\Event\Manager::class);
129143
$context = $this->createPartialMock(\Magento\Framework\Model\Context::class, ['getEventDispatcher']);
@@ -138,7 +152,9 @@ protected function setUp()
138152
'historyCollectionFactory' => $this->historyCollectionFactoryMock,
139153
'salesOrderCollectionFactory' => $this->salesOrderCollectionFactoryMock,
140154
'priceCurrency' => $this->priceCurrency,
141-
'productListFactory' => $this->productCollectionFactoryMock
155+
'productListFactory' => $this->productCollectionFactoryMock,
156+
'localeResolver' => $this->localeResolver,
157+
'timezone' => $this->timezone,
142158
]
143159
);
144160
}
@@ -1044,6 +1060,22 @@ public function testResetOrderWillResetPayment()
10441060
);
10451061
}
10461062

1063+
public function testGetCreatedAtFormattedUsesCorrectLocale()
1064+
{
1065+
$localeCode = 'nl_NL';
1066+
1067+
$this->localeResolver->expects($this->once())->method('getDefaultLocale')->willReturn($localeCode);
1068+
$this->timezone->expects($this->once())->method('formatDateTime')
1069+
->with(
1070+
$this->anything(),
1071+
$this->anything(),
1072+
$this->anything(),
1073+
$localeCode
1074+
);
1075+
1076+
$this->order->getCreatedAtFormatted(\IntlDateFormatter::SHORT);
1077+
}
1078+
10471079
public function notInvoicingStatesProvider()
10481080
{
10491081
return [

0 commit comments

Comments
 (0)