Skip to content

Commit

Permalink
Merge forwardport of #13408 to 2.3-develop branch
Browse files Browse the repository at this point in the history
  • Loading branch information
magento-engcom-team authored Feb 15, 2018
2 parents 61d0d96 + dbdb772 commit 85e0956
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,45 @@
*/
namespace Magento\Analytics\Block\Adminhtml\System\Config;

use Magento\Framework\App\ObjectManager;

/**
* Provides label with default Time Zone
*/
class CollectionTimeLabel extends \Magento\Config\Block\System\Config\Form\Field
{
/**
* Add default time zone to comment
* @var \Magento\Framework\Locale\ResolverInterface
*/
private $localeResolver;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param array $data
* @param \Magento\Framework\Locale\ResolverInterface|null $localeResolver
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
array $data = [],
\Magento\Framework\Locale\ResolverInterface $localeResolver = null
) {
$this->localeResolver = $localeResolver ?:
ObjectManager::getInstance()->get(\Magento\Framework\Locale\ResolverInterface::class);
parent::__construct($context, $data);
}

/**
* Add current time zone to comment, properly translated according to locale
*
* @param \Magento\Framework\Data\Form\Element\AbstractElement $element
* @return string
*/
public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$timeZoneCode = $this->_localeDate->getConfigTimezone();
$getLongTimeZoneName = \IntlTimeZone::createTimeZone($timeZoneCode)->getDisplayName();
$locale = $this->localeResolver->getLocale();
$getLongTimeZoneName = \IntlTimeZone::createTimeZone($timeZoneCode)
->getDisplayName(false, \IntlTimeZone::DISPLAY_LONG, $locale);
$element->setData(
'comment',
sprintf("%s (%s)", $getLongTimeZoneName, $timeZoneCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Data\Form;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

Expand All @@ -34,6 +35,11 @@ class CollectionTimeLabelTest extends \PHPUnit\Framework\TestCase
*/
private $abstractElementMock;

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

protected function setUp()
{
$this->abstractElementMock = $this->getMockBuilder(AbstractElement::class)
Expand All @@ -53,12 +59,17 @@ protected function setUp()
$this->contextMock->expects($this->any())
->method('getLocaleDate')
->willReturn($this->timeZoneMock);
$this->localeResolver = $this->getMockBuilder(ResolverInterface::class)
->disableOriginalConstructor()
->setMethods(['getLocale'])
->getMockForAbstractClass();

$objectManager = new ObjectManager($this);
$this->collectionTimeLabel = $objectManager->getObject(
CollectionTimeLabel::class,
[
'context' => $this->contextMock
'context' => $this->contextMock,
'localeResolver' => $this->localeResolver
]
);
}
Expand All @@ -73,6 +84,9 @@ public function testRender()
$this->abstractElementMock->expects($this->any())
->method('getComment')
->willReturn('Eastern Standard Time (America/New_York)');
$this->localeResolver->expects($this->once())
->method('getLocale')
->willReturn('en_US');
$this->assertRegexp(
"/Eastern Standard Time \(America\/New_York\)/",
$this->collectionTimeLabel->render($this->abstractElementMock)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Analytics\Block\Adminhtml\System\Config;

use Magento\Framework\Data\FormFactory;
use Magento\Framework\Data\Form\Element\TimeFactory;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Provide tests for CollectionTimeLabel block.
*/
class CollectionTimeLabelTest extends TestCase
{
/**
* Test render will add comment considering config locale(default en_US).
*
* @magentoAppIsolation enabled
*/
public function testRenderWithDefaultLocale()
{
$result = $this->render();
$this->assertRegExp('/<span>Pacific Standard Time/', $result);
}

/**
* Test render will add comment considering config locale(non-default de_DE).
*
* @magentoConfigFixture default_store general/locale/code de_DE
* @magentoAppIsolation enabled
*/
public function testRenderWithNonDefaultLocale()
{
$result = $this->render();
$this->assertRegExp('/<span>Nordamerikanische Westküsten-Normalzeit/', $result);
}

/**
* Render 'time' element.
*
* @return string
*/
private function render()
{
$collectionTimeLabel = Bootstrap::getObjectManager()->get(CollectionTimeLabelFactory::class)->create();
$form = Bootstrap::getObjectManager()->get(FormFactory::class)->create();
$element = Bootstrap::getObjectManager()->get(TimeFactory::class)->create();
$element->setForm($form);

return $collectionTimeLabel->render($element);
}
}

0 comments on commit 85e0956

Please sign in to comment.