Skip to content

Commit 9d0befb

Browse files
author
Jeroen van Leusden
committed
Translate order getCreatedAtFormatted() to store locale
1 parent d2cbddd commit 9d0befb

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

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

Lines changed: 18 additions & 2 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;
@@ -212,6 +214,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
212214
*/
213215
protected $_currencyFactory;
214216

217+
/**
218+
* @var \Magento\Eav\Model\Config
219+
*/
220+
private $_eavConfig;
221+
215222
/**
216223
* @var \Magento\Sales\Model\Order\Status\HistoryFactory
217224
*/
@@ -267,6 +274,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
267274
*/
268275
protected $timezone;
269276

277+
/**
278+
* @var ResolverInterface
279+
*/
280+
private $localeResolver;
281+
270282
/**
271283
* @param \Magento\Framework\Model\Context $context
272284
* @param \Magento\Framework\Registry $registry
@@ -295,6 +307,7 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
295307
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
296308
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
297309
* @param array $data
310+
* @param ResolverInterface $localeResolver
298311
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
299312
*/
300313
public function __construct(
@@ -324,7 +337,8 @@ public function __construct(
324337
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productListFactory,
325338
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
326339
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
327-
array $data = []
340+
array $data = [],
341+
ResolverInterface $localeResolver = null
328342
) {
329343
$this->_storeManager = $storeManager;
330344
$this->_orderConfig = $orderConfig;
@@ -346,6 +360,8 @@ public function __construct(
346360
$this->_trackCollectionFactory = $trackCollectionFactory;
347361
$this->salesOrderCollectionFactory = $salesOrderCollectionFactory;
348362
$this->priceCurrency = $priceCurrency;
363+
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(ResolverInterface::class);
364+
349365
parent::__construct(
350366
$context,
351367
$registry,
@@ -1830,7 +1846,7 @@ public function getCreatedAtFormatted($format)
18301846
new \DateTime($this->getCreatedAt()),
18311847
$format,
18321848
$format,
1833-
null,
1849+
$this->localeResolver->getDefaultLocale(),
18341850
$this->timezone->getConfigTimezone('store', $this->getStore())
18351851
);
18361852
}

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)