Skip to content

Translate order getCreatedAtFormatted() to store locale #10491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions app/code/Magento/Sales/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use Magento\Directory\Model\Currency;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
Expand Down Expand Up @@ -211,6 +213,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
*/
protected $_currencyFactory;

/**
* @var \Magento\Eav\Model\Config
*/
private $_eavConfig;

/**
* @var \Magento\Sales\Model\Order\Status\HistoryFactory
*/
Expand Down Expand Up @@ -266,6 +273,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
*/
protected $timezone;

/**
* @var ResolverInterface
*/
private $localeResolver;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand Down Expand Up @@ -294,6 +306,7 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param ResolverInterface $localeResolver
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand Down Expand Up @@ -323,7 +336,8 @@ public function __construct(
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productListFactory,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = []
array $data = [],
ResolverInterface $localeResolver = null
) {
$this->_storeManager = $storeManager;
$this->_orderConfig = $orderConfig;
Expand All @@ -345,6 +359,8 @@ public function __construct(
$this->_trackCollectionFactory = $trackCollectionFactory;
$this->salesOrderCollectionFactory = $salesOrderCollectionFactory;
$this->priceCurrency = $priceCurrency;
$this->localeResolver = $localeResolver ?: ObjectManager::getInstance()->get(ResolverInterface::class);

parent::__construct(
$context,
$registry,
Expand Down Expand Up @@ -1829,7 +1845,7 @@ public function getCreatedAtFormatted($format)
new \DateTime($this->getCreatedAt()),
$format,
$format,
null,
$this->localeResolver->getDefaultLocale(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this public method can be easily covered with unit test, however, as there is no logic and we just call method of another class with a bunch of arguments I do not insist.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It indeed calls public methods, making a test would be duplicate I guess?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, mock this dependency, the only thing to be asserted that we are passing concrete locale and not null.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do that later today and notify you when ready!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for keeping me updated, waiting for a ping.

$this->timezone->getConfigTimezone('store', $this->getStore())
);
}
Expand Down
34 changes: 33 additions & 1 deletion app/code/Magento/Sales/Test/Unit/Model/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\ResourceModel\Order\Status\History\CollectionFactory as HistoryCollectionFactory;
Expand Down Expand Up @@ -73,6 +75,16 @@ class OrderTest extends \PHPUnit\Framework\TestCase
*/
protected $productCollectionFactoryMock;

/**
* @var ResolverInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $localeResolver;

/**
* @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $timezone;

protected function setUp()
{
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand Down Expand Up @@ -124,6 +136,8 @@ protected function setUp()
true,
['round']
);
$this->localeResolver = $this->createMock(ResolverInterface::class);
$this->timezone = $this->createMock(TimezoneInterface::class);
$this->incrementId = '#00000001';
$this->eventManager = $this->createMock(\Magento\Framework\Event\Manager::class);
$context = $this->createPartialMock(\Magento\Framework\Model\Context::class, ['getEventDispatcher']);
Expand All @@ -138,7 +152,9 @@ protected function setUp()
'historyCollectionFactory' => $this->historyCollectionFactoryMock,
'salesOrderCollectionFactory' => $this->salesOrderCollectionFactoryMock,
'priceCurrency' => $this->priceCurrency,
'productListFactory' => $this->productCollectionFactoryMock
'productListFactory' => $this->productCollectionFactoryMock,
'localeResolver' => $this->localeResolver,
'timezone' => $this->timezone,
]
);
}
Expand Down Expand Up @@ -1044,6 +1060,22 @@ public function testResetOrderWillResetPayment()
);
}

public function testGetCreatedAtFormattedUsesCorrectLocale()
{
$localeCode = 'nl_NL';

$this->localeResolver->expects($this->once())->method('getDefaultLocale')->willReturn($localeCode);
$this->timezone->expects($this->once())->method('formatDateTime')
->with(
$this->anything(),
$this->anything(),
$this->anything(),
$localeCode
);

$this->order->getCreatedAtFormatted(\IntlDateFormatter::SHORT);
}

public function notInvoicingStatesProvider()
{
return [
Expand Down