diff --git a/app/code/Magento/Reports/Block/Adminhtml/Config/Form/Field/YtdStart.php b/app/code/Magento/Reports/Block/Adminhtml/Config/Form/Field/YtdStart.php index 4ac12501aa90f..b564e582943ac 100644 --- a/app/code/Magento/Reports/Block/Adminhtml/Config/Form/Field/YtdStart.php +++ b/app/code/Magento/Reports/Block/Adminhtml/Config/Form/Field/YtdStart.php @@ -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 */ -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) @@ -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); } diff --git a/dev/tests/integration/testsuite/Magento/Reports/Block/Adminhtml/Config/Form/Field/YtdStartTest.php b/dev/tests/integration/testsuite/Magento/Reports/Block/Adminhtml/Config/Form/Field/YtdStartTest.php new file mode 100644 index 0000000000000..d378b3e7344e1 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Reports/Block/Adminhtml/Config/Form/Field/YtdStartTest.php @@ -0,0 +1,58 @@ +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 + ? '' + : ''; + + $html .= PHP_EOL; + } + + return $html; + } +}