Skip to content

Commit

Permalink
ENGCOM-7110: Fix Report date doesn't matching in configuration setting
Browse files Browse the repository at this point in the history
  • Loading branch information
slavvka authored Mar 26, 2020
2 parents 71ba40e + 36c4167 commit 979b221
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
*/
namespace Magento\Reports\Block\Adminhtml\Config\Form\Field;

use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;

/**
* Dashboard Year-To-Date Month and Day starts Field Renderer
*
* @author Magento Core Team <core@magentocommerce.com>
*/
class YtdStart extends \Magento\Config\Block\System\Config\Form\Field
class YtdStart extends Field
{
/**
* Get Month and Day Element
*
* @param AbstractElement $element
* @return string
* @SuppressWarnings(PHPMD.NPathComplexity)
Expand All @@ -23,36 +24,29 @@ protected function _getElementHtml(AbstractElement $element)
{
$_months = [];
for ($i = 1; $i <= 12; $i++) {
$_months[$i] = $this->_localeDate->date(mktime(null, null, null, $i, 1))->format('m');
$month = $this->_localeDate->date(mktime(null, null, null, $i, 1))
->format('m');
$_months[$month] = $month;
}
ksort($_months);

$_days = [];
for ($i = 1; $i <= 31; $i++) {
$_days[$i] = $i < 10 ? '0' . $i : $i;
}

if ($element->getValue()) {
$values = explode(',', $element->getValue());
} else {
$values = [];
}

$values = $element->getValue() ? explode(',', $element->getValue()) : [];
$element->setName($element->getName() . '[]');

$_monthsHtml = $element->setStyle(
'width:100px;'
)->setValues(
$_months
)->setValue(
isset($values[0]) ? $values[0] : null
)->getElementHtml();
$_monthsHtml = $element->setStyle('width:100px;')
->setValues($_months)
->setValue(isset($values[0]) ? $values[0] : null)
->getElementHtml();

$_daysHtml = $element->setStyle(
'width:50px;'
)->setValues(
$_days
)->setValue(
isset($values[1]) ? $values[1] : null
)->getElementHtml();
$_daysHtml = $element->setStyle('width:50px;')
->setValues($_days)
->setValue(isset($values[1]) ? $values[1] : null)
->getElementHtml();

return sprintf('%s %s', $_monthsHtml, $_daysHtml);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Reports\Block\Adminhtml\Config\Form\Field;

use Magento\TestFramework\TestCase\AbstractBackendController;

/**
* Test for \Magento\Reports\Block\Adminhtml\Config\Form\Field\YtdStart.
*
* @magentoAppArea adminhtml
*/
class YtdStartTest extends AbstractBackendController
{
/**
* @var array
*/
private $monthNumbers = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];

/**
* Test Get Month and Day Element renderer
*
* @return void
* @magentoAppIsolation enabled
* @magentoDbIsolation enabled
*/
public function testGetElementHtml(): void
{
$this->dispatch('backend/admin/system_config/edit/section/reports/');
$body = $this->getResponse()->getBody();

$this->assertContains($this->getOptionsHtml('01'), $body);
}

/**
* Options html
*
* @param string $selected
* @return string
*/
private function getOptionsHtml(string $selected): string
{
$html = '';
foreach ($this->monthNumbers as $number) {
$html .= $number === $selected
? '<option value="' . $selected . '" selected="selected">' . $selected . '</option>'
: '<option value="' . $number . '">' . $number . '</option>';

$html .= PHP_EOL;
}

return $html;
}
}

0 comments on commit 979b221

Please sign in to comment.