Skip to content

Commit de672eb

Browse files
author
Denys Rudchenko
committed
MAGETWO-34194: [GitHub] Required date validation on magento2 Backend. #1040
1 parent 3c66c19 commit de672eb

File tree

5 files changed

+82
-15
lines changed

5 files changed

+82
-15
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,15 @@ public function execute()
142142
$category->setData('use_post_data_config', $this->getRequest()->getPost('use_config'));
143143

144144
try {
145+
$categoryResource = $category->getResource();
146+
if ($category->hasCustomDesignTo()) {
147+
$categoryResource->getAttribute('custom_design_from')->setMaxValue($category->getCustomDesignTo());
148+
}
145149
$validate = $category->validate();
146150
if ($validate !== true) {
147151
foreach ($validate as $code => $error) {
148152
if ($error === true) {
149-
$attribute = $category->getResource()->getAttribute($code)->getFrontend()->getLabel();
153+
$attribute = $categoryResource->getAttribute($code)->getFrontend()->getLabel();
150154
throw new \Magento\Framework\Exception\LocalizedException(
151155
__('Attribute "%1" is required.', $attribute)
152156
);

app/code/Magento/Catalog/Model/Product/Attribute/Backend/Startdate.php renamed to app/code/Magento/Catalog/Model/Attribute/Backend/Startdate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Copyright © 2015 Magento. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6-
namespace Magento\Catalog\Model\Product\Attribute\Backend;
6+
namespace Magento\Catalog\Model\Attribute\Backend;
77

88
/**
99
*

app/code/Magento/Catalog/Setup/CategorySetup.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function getDefaultEntities()
217217
'type' => 'datetime',
218218
'label' => 'Active From',
219219
'input' => 'date',
220-
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\Datetime',
220+
'backend' => 'Magento\Catalog\Model\Attribute\Backend\Startdate',
221221
'required' => false,
222222
'sort_order' => 30,
223223
'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_STORE,
@@ -420,7 +420,7 @@ public function getDefaultEntities()
420420
'type' => 'datetime',
421421
'label' => 'Special Price From Date',
422422
'input' => 'date',
423-
'backend' => 'Magento\Catalog\Model\Product\Attribute\Backend\Startdate',
423+
'backend' => 'Magento\Catalog\Model\Attribute\Backend\Startdate',
424424
'required' => false,
425425
'sort_order' => 4,
426426
'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_WEBSITE,
@@ -584,7 +584,7 @@ public function getDefaultEntities()
584584
'type' => 'datetime',
585585
'label' => 'Set Product as New from Date',
586586
'input' => 'date',
587-
'backend' => 'Magento\Catalog\Model\Product\Attribute\Backend\Startdate',
587+
'backend' => 'Magento\Catalog\Model\Attribute\Backend\Startdate',
588588
'required' => false,
589589
'sort_order' => 7,
590590
'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_WEBSITE,
@@ -652,7 +652,7 @@ public function getDefaultEntities()
652652
'type' => 'datetime',
653653
'label' => 'Active From',
654654
'input' => 'date',
655-
'backend' => 'Magento\Catalog\Model\Product\Attribute\Backend\Startdate',
655+
'backend' => 'Magento\Catalog\Model\Attribute\Backend\Startdate',
656656
'required' => false,
657657
'sort_order' => 2,
658658
'global' => \Magento\Catalog\Model\Resource\Eav\Attribute::SCOPE_STORE,

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Category/SaveTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ public function testExecute($categoryId, $storeId, $activeTabId, $parentId)
239239
'setStoreId',
240240
'load',
241241
'getPath',
242+
'getResource',
242243
'setPath',
243244
'setParentId',
244245
'setData',
@@ -491,6 +492,17 @@ public function testExecute($categoryId, $storeId, $activeTabId, $parentId)
491492
'catalog_category_prepare_save',
492493
['category' => $categoryMock, 'request' => $this->requestMock]
493494
);
495+
496+
$categoryResource = $this->getMock(
497+
'Magento\Catalog\Model\Resource\Category',
498+
[],
499+
[],
500+
'',
501+
false
502+
);
503+
$categoryMock->expects($this->once())
504+
->method('getResource')
505+
->will($this->returnValue($categoryResource));
494506
$categoryMock->expects($this->once())
495507
->method('validate')
496508
->will($this->returnValue(true));

dev/tests/integration/testsuite/Magento/Catalog/Controller/Adminhtml/CategoryTest.php

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ class CategoryTest extends \Magento\Backend\Utility\Controller
1818
* @param array $inputData
1919
* @param array $defaultAttributes
2020
* @param array $attributesSaved
21+
* @param bool $isSuccess
2122
*/
22-
public function testSaveAction($inputData, $defaultAttributes, $attributesSaved = [])
23+
public function testSaveAction($inputData, $defaultAttributes, $attributesSaved = [], $isSuccess = true)
2324
{
2425
/** @var $store \Magento\Store\Model\Store */
2526
$store = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create('Magento\Store\Model\Store');
@@ -31,10 +32,12 @@ public function testSaveAction($inputData, $defaultAttributes, $attributesSaved
3132
$this->getRequest()->setParam('id', 2);
3233
$this->dispatch('backend/catalog/category/save');
3334

34-
$this->assertSessionMessages(
35-
$this->equalTo(['You saved the category.']),
36-
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
37-
);
35+
if ($isSuccess) {
36+
$this->assertSessionMessages(
37+
$this->equalTo(['You saved the category.']),
38+
\Magento\Framework\Message\MessageInterface::TYPE_SUCCESS
39+
);
40+
}
3841

3942
/** @var $category \Magento\Catalog\Model\Category */
4043
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
@@ -214,8 +217,8 @@ public function saveActionDataProvider()
214217
'is_anchor' => '1',
215218
'custom_apply_to_products' => '0',
216219
'custom_design' => 'Magento/blank',
217-
'custom_design_from' => '',
218-
'custom_design_to' => '',
220+
'custom_design_from' => '5/21/2015',
221+
'custom_design_to' => '5/29/2015',
219222
'page_layout' => '',
220223
'custom_layout_update' => '',
221224
],
@@ -257,10 +260,58 @@ public function saveActionDataProvider()
257260
'meta_keywords' => 'Custom keywords',
258261
'meta_description' => 'Custom meta description',
259262
'custom_layout_update' => null,
260-
'custom_design_from' => null,
261-
'custom_design_to' => null,
263+
'custom_design_from' => '2015-05-21 00:00:00',
264+
'custom_design_to' => '2015-05-29 00:00:00',
262265
'filter_price_range' => null
263266
],
267+
],
268+
'incorrect datefrom' => [
269+
[
270+
'general' => [
271+
'id' => '2',
272+
'path' => '1/2',
273+
'name' => 'Custom Name',
274+
'is_active' => '0',
275+
'description' => 'Custom Description',
276+
'meta_title' => 'Custom Title',
277+
'meta_keywords' => 'Custom keywords',
278+
'meta_description' => 'Custom meta description',
279+
'include_in_menu' => '0',
280+
'url_key' => 'default-category',
281+
'display_mode' => 'PRODUCTS',
282+
'landing_page' => '1',
283+
'is_anchor' => '1',
284+
'custom_apply_to_products' => '0',
285+
'custom_design' => 'Magento/blank',
286+
'custom_design_from' => '5/29/2015',
287+
'custom_design_to' => '5/21/2015',
288+
'page_layout' => '',
289+
'custom_layout_update' => '',
290+
],
291+
'use_config' => [0 => 'available_sort_by', 1 => 'default_sort_by', 2 => 'filter_price_range'],
292+
],
293+
[
294+
'name' => false,
295+
'default_sort_by' => false,
296+
'display_mode' => false,
297+
'meta_title' => false,
298+
'custom_design' => false,
299+
'page_layout' => false,
300+
'is_active' => false,
301+
'include_in_menu' => false,
302+
'landing_page' => false,
303+
'custom_apply_to_products' => false,
304+
'available_sort_by' => false,
305+
'description' => false,
306+
'meta_keywords' => false,
307+
'meta_description' => false,
308+
'custom_layout_update' => false,
309+
'custom_design_from' => false,
310+
'custom_design_to' => false,
311+
'filter_price_range' => false
312+
],
313+
[],
314+
false
264315
]
265316
];
266317
}

0 commit comments

Comments
 (0)