Skip to content
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

Remove dependency of date renderer on global state locale #1469

Merged
merged 1 commit into from
Jul 29, 2015
Merged

Remove dependency of date renderer on global state locale #1469

merged 1 commit into from
Jul 29, 2015

Conversation

Vinai
Copy link
Contributor

@Vinai Vinai commented Jul 11, 2015

This does two things

1) Introduce new test to reproduce bug from Issue #1468
The new test is called

\Magento\Reports\Test\Unit\Block\Adminhtml\Sales\Grid\Column\Renderer\DateTest::testDateIsRenderedIndependentOfSystemDefaultLocale()

The existing test testRender was refactored a little to avoid code duplication with the new test.

2) Introduce fix for Issue #1468
The fix is to simply pass the locale that was used to create the date format string as a third argument to \IntlDateFormatter::formatObject(), so it will be used to fetch the matching datetime constants also.

\IntlDateFormatter::formatObject($date, $format, $this->_localeResolver->getLocale());

Summary
==
This patch introduces a test to reproduce the bug described in issue #1468.
`\Magento\Reports\Test\Unit\Block\Adminhtml\Sales\Grid\Column\Renderer\DateTest::testDateIsRenderedIndependentOfSystemDefaultLocale`

The existing test `testRender` was refactored a little to avoid code duplication with the new test.

The patch also includes a fix for the issue, which simply is to pass the locale that was used to create the date format as a third argument to `\IntlDateFormatter::formatObject()`.

The following contents of the commit message are a slightly abbreviated copy of the issue #1468.

-----

The method `\Magento\Reports\Block\Adminhtml\Sales\Grid\Column\Renderer\Date::render()` creates it's return value by calling `\IntlDateFormatter::formatObject($date, $format);`.
Please note that `formatObject()` is called without the optional third parameter specifying the locale, which means that the default ICU locale will be used.
Here is the method signature from the [PHP manual](http://php.net/manual/en/intldateformatter.formatobject.php):

```
public static string IntlDateFormatter::formatObject ( object $object [, mixed $format = NULL [, string $locale = NULL ]] )
```

If the third locale is not specified as an argument, the default ICU locale is used.
If no default locale is set, this value will default to the system default.

This causes the test `\Magento\Reports\Test\Unit\Block\Adminhtml\Sales\Grid\Column\Renderer\DateTest::testRender()` to fail sometimes, because it depends on the system locale.
If the system locale is set to a **en_US** locale the test is green. If it is a non-english locale, the test fails, because the expected output does not match the expected format.

``
1) Magento\Reports\Test\Unit\Block\Adminhtml\Sales\Grid\Column\Renderer\DateTest::testRender with data set #4 ('2014-06-25', 'en_US', 'period', 'day', 'Jun 25, 2014')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Jun 25, 2014'
+'Juni 25, 2014'
```

The above output is from a dev machine where the system locale is set to **de_DE.UTF-8**.

To summarize:
The date format is correctly constructed for a given locale.
However, the date constants rendered do not necessarily match that locale.
@kokoc
Copy link
Member

kokoc commented Jul 22, 2015

@Vinai thank you for the contribution! Internal reference: MAGETWO-40651

@magento-team magento-team merged commit a408079 into magento:develop Jul 29, 2015
magento-team pushed a commit that referenced this pull request Sep 8, 2017
[EngCom] Public Pull Requests
 - MAGETWO-72390: Remove the usage of the DataObject for response management #10808
 - MAGETWO-71545: Added 'application/json' Content-Type to Ajax responses in the Magento_UI module. #10521
 - MAGETWO-72388: Fix spelling mistake in AddressTest.php #10806
 - MAGETWO-72283: Code generate: support variadic parameter #10771
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants