Skip to content

Commit

Permalink
Merge branch 'develop' of github.corp.magento.com:magento2/magento2ce…
Browse files Browse the repository at this point in the history
… into MPI-bugfixes
  • Loading branch information
Yushkin, Dmytro committed Apr 11, 2016
2 parents 75b318b + 603fcb4 commit aea6eee
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Helper

/**
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
*
* @deprecated
*/
protected $dateFilter;

Expand Down Expand Up @@ -68,6 +70,11 @@ class Helper
*/
private $linkResolver;

/**
* @var \Magento\Framework\Stdlib\DateTime\Filter\DateTime
*/
private $dateTimeFilter;

/**
* Helper constructor.
* @param \Magento\Framework\App\RequestInterface $request
Expand Down Expand Up @@ -142,7 +149,7 @@ public function initializeFromData(\Magento\Catalog\Model\Product $product, arra
foreach ($attributes as $attrKey => $attribute) {
if ($attribute->getBackend()->getType() == 'datetime') {
if (array_key_exists($attrKey, $productData) && $productData[$attrKey] != '') {
$dateFieldFilters[$attrKey] = $this->dateFilter;
$dateFieldFilters[$attrKey] = $this->getDateTimeFilter();
}
}
}
Expand Down Expand Up @@ -363,4 +370,18 @@ private function getLinkResolver()
}
return $this->linkResolver;
}

/**
* @return \Magento\Framework\Stdlib\DateTime\Filter\DateTime
*
* @deprecated
*/
private function getDateTimeFilter()
{
if ($this->dateTimeFilter === null) {
$this->dateTimeFilter = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Stdlib\DateTime\Filter\DateTime::class);
}
return $this->dateTimeFilter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Validate extends \Magento\Catalog\Controller\Adminhtml\Product
{
/**
* @var \Magento\Framework\Stdlib\DateTime\Filter\Date
*
* @deprecated
*/
protected $_dateFilter;
Expand Down
40 changes: 23 additions & 17 deletions app/code/Magento/Reports/Model/ResourceModel/Report/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ protected function _getDayInterval(\DateTime $dateStart)
\IntlDateFormatter::SHORT,
\IntlDateFormatter::NONE
),
'start' => $dateStart->format('Y-m-d 00:00:00'),
'end' => $dateStart->format('Y-m-d 23:59:59'),
'start' => $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 00:00:00')),
'end' => $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 23:59:59')),
];
return $interval;
}
Expand All @@ -195,21 +195,25 @@ protected function _getMonthInterval(\DateTime $dateStart, \DateTime $dateEnd, $
$interval = [];
$interval['period'] = $dateStart->format('m/Y');
if ($firstInterval) {
$interval['start'] = $dateStart->format('Y-m-d 00:00:00');
$interval['start'] = $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 00:00:00'));
} else {
$interval['start'] = $dateStart->format('Y-m-01 00:00:00');
$interval['start'] = $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-01 00:00:00'));
}

if ($dateStart->diff($dateEnd)->m == 0) {
$interval['end'] = $dateStart->setDate(
$dateStart->format('Y'),
$dateStart->format('m'),
$dateEnd->format('d')
)->format(
'Y-m-d 23:59:59'
$interval['end'] = $this->_localeDate->convertConfigTimeToUtc(
$dateStart->setDate(
$dateStart->format('Y'),
$dateStart->format('m'),
$dateEnd->format('d')
)->format(
'Y-m-d 23:59:59'
)
);
} else {
$interval['end'] = $dateStart->format('Y-m-' . date('t', $dateStart->getTimestamp()) . ' 23:59:59');
$interval['end'] = $this->_localeDate->convertConfigTimeToUtc(
$dateStart->format('Y-m-' . date('t', $dateStart->getTimestamp()) . ' 23:59:59')
);
}

$dateStart->modify('+1 month');
Expand All @@ -234,13 +238,15 @@ protected function _getYearInterval(\DateTime $dateStart, \DateTime $dateEnd, $f
$interval = [];
$interval['period'] = $dateStart->format('Y');
$interval['start'] = $firstInterval
? $dateStart->format('Y-m-d 00:00:00')
: $dateStart->format('Y-01-01 00:00:00');
? $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-m-d 00:00:00'))
: $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-01-01 00:00:00'));

$interval['end'] = $dateStart->diff($dateEnd)->y == 0
? $dateStart->setDate($dateStart->format('Y'), $dateEnd->format('m'), $dateEnd->format('d'))
->format('Y-m-d 23:59:59')
: $dateStart->format('Y-12-31 23:59:59');
? $this->_localeDate->convertConfigTimeToUtc(
$dateStart->setDate($dateStart->format('Y'), $dateEnd->format('m'), $dateEnd->format('d'))
->format('Y-m-d 23:59:59')
)
: $this->_localeDate->convertConfigTimeToUtc($dateStart->format('Y-12-31 23:59:59'));
$dateStart->modify('+1 year');

if ($dateStart->diff($dateEnd)->y == 0) {
Expand Down Expand Up @@ -355,7 +361,7 @@ public function getReports()
}
return $this->_reports;
}

/**
* Load data
*
Expand Down
25 changes: 18 additions & 7 deletions lib/internal/Magento/Framework/Stdlib/DateTime/Filter/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,40 @@
namespace Magento\Framework\Stdlib\DateTime\Filter;

use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;

class Date implements \Zend_Filter_Interface
{
/**
* Filter that converts localized input into normalized format
*
* @var \Zend_Filter_LocalizedToNormalized
*
* @deprecated
*/
protected $_localToNormalFilter;

/**
* Filter that converts normalized input into internal format
*
* @var \Zend_Filter_NormalizedToLocalized
*
* @deprecated
*/
protected $_normalToLocalFilter;

/**
* @var TimezoneInterface
*
* @deprecated
*/
protected $_localeDate;

/**
* @param TimezoneInterface $localeDate
*
* @deprecated
*/
public function __construct(TimezoneInterface $localeDate)
{
Expand All @@ -46,17 +56,18 @@ public function __construct(TimezoneInterface $localeDate)

/**
* Convert date from localized to internal format
*
*
* @param string $value
* @return string
* @throws LocalizedException
*/
public function filter($value)
{
$value = $this->_normalToLocalFilter->filter($this->_localToNormalFilter->filter($value));

/**
* @todo MAGETWO-51391
*/
return str_replace("\xc2\xa0", '', $value);
try {
$value = new \DateTime($value);
return $value->format('Y-m-d');
} catch (\Exception $e) {
throw new \Exception('Invalid input date format');
}
}
}
22 changes: 22 additions & 0 deletions lib/internal/Magento/Framework/Stdlib/DateTime/Filter/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
*/
namespace Magento\Framework\Stdlib\DateTime\Filter;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Phrase;

class DateTime extends Date
{
/**
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
*
* @deprecated
*/
public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
{
Expand All @@ -26,4 +31,21 @@ public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface
['date_format' => \Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT]
);
}

/**
* Convert date from localized to internal format
*
* @param string $value
* @return string
* @throws LocalizedException
*/
public function filter($value)
{
try {
$value = new \DateTime($value);
return $value->format('Y-m-d H:i:s');
} catch (\Exception $e) {
throw new \Exception('Invalid input datetime format');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

class DateTest extends \PHPUnit_Framework_TestCase
{
public function testFilter()
/**
* @param string $inputData
* @param string $expectedDate
*
* @dataProvider dateFilterDataProvider
*/
public function testFilter($inputData, $expectedDate)
{
$localeMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
$localeMock->expects(
Expand All @@ -22,7 +28,52 @@ public function testFilter()
$this->returnValue('MM-dd-yyyy')
);
$model = new Date($localeMock);
// Check that date is converted to 'yyyy-MM-dd' format
$this->assertEquals('2241-12-31', $model->filter('12-31-2241'));

$this->assertEquals($expectedDate, $model->filter($inputData));
}

/**
* @return array
*/
public function dateFilterDataProvider()
{
return [
['2000-01-01', '2000-01-01'],
['2014-03-30T02:30:00', '2014-03-30'],
['12/31/2000', '2000-12-31']
];
}

/**
* @dataProvider dateFilterWithExceptionDataProvider
*/
public function testFilterWithException($inputData)
{
$this->setExpectedException('\Exception');

$localeMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
$localeMock->expects(
$this->once()
)->method(
'getDateFormat'
)->with(
\IntlDateFormatter::SHORT
)->will(
$this->returnValue('MM-dd-yyyy')
);
$model = new Date($localeMock);

$model->filter($inputData);
}

/**
* @return array
*/
public function dateFilterWithExceptionDataProvider()
{
return [
['12-31-2000'],
['22/2000-01'],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

class DateTimeTest extends \PHPUnit_Framework_TestCase
{
public function testFilter()
/**
* @param string $inputData
* @param string $expectedDate
*
* @dataProvider dateTimeFilterDataProvider
*/
public function testFilter($inputData, $expectedDate)
{
$localeMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
$localeMock->expects(
Expand All @@ -22,7 +28,53 @@ public function testFilter()
$this->returnValue('HH:mm:ss MM-dd-yyyy')
);
$model = new DateTime($localeMock);
// Check that datetime is converted to 'yyyy-MM-dd HH:mm:ss' format
$this->assertEquals('2241-12-31 23:59:53', $model->filter('23:59:53 12-31-2241'));

$this->assertEquals($expectedDate, $model->filter($inputData));
}

/**
* @return array
*/
public function dateTimeFilterDataProvider()
{
return [
['2000-01-01 02:30:00', '2000-01-01 02:30:00'],
['2014-03-30T02:30:00', '2014-03-30 02:30:00'],
['12/31/2000 02:30:00', '2000-12-31 02:30:00'],
['02:30:00 12/31/2000', '2000-12-31 02:30:00'],
];
}

/**
* @dataProvider dateTimeFilterWithExceptionDataProvider
*/
public function testFilterWithException($inputData)
{
$this->setExpectedException('\Exception');

$localeMock = $this->getMock('\Magento\Framework\Stdlib\DateTime\TimezoneInterface');
$localeMock->expects(
$this->once()
)->method(
'getDateFormat'
)->with(
\IntlDateFormatter::SHORT
)->will(
$this->returnValue('MM-dd-yyyy')
);
$model = new DateTime($localeMock);

$model->filter($inputData);
}

/**
* @return array
*/
public function dateTimeFilterWithExceptionDataProvider()
{
return [
['12-31-2000 22:22:22'],
['22/2000-01 22:22:22'],
];
}
}

0 comments on commit aea6eee

Please sign in to comment.