Skip to content

Commit 65564bf

Browse files
MAGETWO-71603: Translate order getCreatedAtFormatted() to store locale #10491
2 parents 4124474 + 61f9848 commit 65564bf

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;
@@ -211,6 +213,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
211213
*/
212214
protected $_currencyFactory;
213215

216+
/**
217+
* @var \Magento\Eav\Model\Config
218+
*/
219+
private $_eavConfig;
220+
214221
/**
215222
* @var \Magento\Sales\Model\Order\Status\HistoryFactory
216223
*/
@@ -266,6 +273,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
266273
*/
267274
protected $timezone;
268275

276+
/**
277+
* @var ResolverInterface
278+
*/
279+
private $localeResolver;
280+
269281
/**
270282
* @param \Magento\Framework\Model\Context $context
271283
* @param \Magento\Framework\Registry $registry
@@ -294,6 +306,7 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
294306
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
295307
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
296308
* @param array $data
309+
* @param ResolverInterface $localeResolver
297310
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
298311
*/
299312
public function __construct(
@@ -323,7 +336,8 @@ public function __construct(
323336
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productListFactory,
324337
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
325338
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
326-
array $data = []
339+
array $data = [],
340+
ResolverInterface $localeResolver = null
327341
) {
328342
$this->_storeManager = $storeManager;
329343
$this->_orderConfig = $orderConfig;
@@ -345,6 +359,8 @@ public function __construct(
345359
$this->_trackCollectionFactory = $trackCollectionFactory;
346360
$this->salesOrderCollectionFactory = $salesOrderCollectionFactory;
347361
$this->priceCurrency = $priceCurrency;
362+
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(ResolverInterface::class);
363+
348364
parent::__construct(
349365
$context,
350366
$registry,
@@ -1829,7 +1845,7 @@ public function getCreatedAtFormatted($format)
18291845
new \DateTime($this->getCreatedAt()),
18301846
$format,
18311847
$format,
1832-
null,
1848+
$this->localeResolver->getDefaultLocale(),
18331849
$this->timezone->getConfigTimezone('store', $this->getStore())
18341850
);
18351851
}

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)