Skip to content

Commit d061352

Browse files
committed
Merge branch 'MAGETWO-71669' into 2.1.13-PR-0.4
2 parents 75f3c91 + 2fa3f2e commit d061352

File tree

14 files changed

+228
-44
lines changed

14 files changed

+228
-44
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ public function execute()
129129
$isNewCategory = !isset($categoryPostData['entity_id']);
130130
$categoryPostData = $this->stringToBoolConverting($categoryPostData);
131131
$categoryPostData = $this->imagePreprocessing($categoryPostData);
132-
$categoryPostData = $this->dateTimePreprocessing($category, $categoryPostData);
133132
$storeId = isset($categoryPostData['store_id']) ? $categoryPostData['store_id'] : null;
134133
$store = $this->storeManager->getStore($storeId);
135134
$this->storeManager->setCurrentStore($store->getCode());

app/code/Magento/Catalog/Model/Product/Option/Type/Date.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function prepareForCart()
142142

143143
if ($this->_dateExists()) {
144144
if ($this->useCalendar()) {
145-
$timestamp += $this->_localeDate->date($value['date'], null, true, false)->getTimestamp();
145+
$timestamp += $this->_localeDate->date($value['date'], null, true)->getTimestamp();
146146
} else {
147147
$timestamp += mktime(0, 0, 0, $value['month'], $value['day'], $value['year']);
148148
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ public function testExecute($categoryId, $storeId, $parentId)
279279
'setParentId',
280280
'setData',
281281
'addData',
282-
'getAttributes',
283282
'setAttributeSetId',
284283
'getDefaultAttributeSetId',
285284
'getProductsReadonly',
@@ -470,9 +469,6 @@ public function testExecute($categoryId, $storeId, $parentId)
470469
$categoryMock->expects($this->any())
471470
->method('getId')
472471
->will($this->returnValue($categoryId));
473-
$categoryMock->expects($this->once())
474-
->method('getAttributes')
475-
->willReturn([]);
476472
if (!$parentId) {
477473
if ($storeId) {
478474
$storeManagerMock->expects($this->once())

app/code/Magento/Customer/Block/Widget/Dob.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,11 @@ public function getHtmlId()
208208
*/
209209
public function getDateFormat()
210210
{
211-
return $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
211+
/** Escape invisible characters which are present in some locales and may corrupt formatting */
212+
$dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
213+
$escapedDateFormat = preg_replace('/[^MmDdYy\/\.\-]/', '', $dateFormat);
214+
215+
return $escapedDateFormat;
212216
}
213217

214218
/**

app/code/Magento/Eav/Model/Entity/Attribute/Backend/Datetime.php

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,50 @@ public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface
3737
public function beforeSave($object)
3838
{
3939
$attributeName = $this->getAttribute()->getName();
40-
$_formated = $object->getData($attributeName . '_is_formated');
41-
if (!$_formated && $object->hasData($attributeName)) {
42-
try {
43-
$value = $this->formatDate($object->getData($attributeName));
44-
} catch (\Exception $e) {
45-
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid date'));
46-
}
40+
$attributeValue = $object->getData($attributeName);
41+
if ($object->hasData($attributeName)) {
42+
// format only date that is not formatted yet
43+
$dateFormatted = $this->dateIsFormatted($attributeValue);
44+
if (!$dateFormatted) {
45+
try {
46+
$value = $this->formatDate($attributeValue);
47+
} catch (\Exception $e) {
48+
throw new \Magento\Framework\Exception\LocalizedException(__('Invalid date'));
49+
}
4750

48-
if (is_null($value)) {
49-
$value = $object->getData($attributeName);
50-
}
51+
if (is_null($value)) {
52+
$value = $attributeValue;
53+
}
5154

52-
$object->setData($attributeName, $value);
53-
$object->setData($attributeName . '_is_formated', true);
55+
$object->setData($attributeName, $value);
56+
}
5457
}
5558

5659
return $this;
5760
}
5861

62+
/**
63+
* Check if date is formatted
64+
*
65+
* @param string|\DateTime $attributeValue
66+
* @return bool
67+
*/
68+
private function dateIsFormatted($attributeValue)
69+
{
70+
$pattern = '/(\d{4})-(\d{2})-(\d{2})(\s(\d{2}):(\d{2}):(\d{2}))?/';
71+
if ($attributeValue instanceof \DateTime) {
72+
return false;
73+
} elseif (preg_match($pattern, $attributeValue)) {
74+
return true;
75+
}
76+
return false;
77+
}
78+
5979
/**
6080
* Prepare date for save in DB
6181
*
6282
* string format used from input fields (all date input fields need apply locale settings)
63-
* int value can be declared in code (this meen whot we use valid date)
83+
* int value can be declared in code (this means that we use valid date)
6484
*
6585
* @param string|int|\DateTime $date
6686
* @return string
@@ -74,9 +94,9 @@ public function formatDate($date)
7494
if (is_scalar($date) && preg_match('/^[0-9]+$/', $date)) {
7595
$date = (new \DateTime())->setTimestamp($date);
7696
} elseif (!($date instanceof \DateTime)) {
77-
// normalized format expecting Y-m-d[ H:i:s] - time is optional
78-
$date = new \DateTime($date);
97+
$date = $this->_localeDate->date($date, null, false);
7998
}
99+
// normalized format expecting Y-m-d [H:i:s] - time is optional
80100
return $date->format('Y-m-d H:i:s');
81101
}
82102
}

app/code/Magento/Sales/view/adminhtml/templates/order/create/data.phtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,9 @@
108108
</div>
109109
</div>
110110
<?php endif; ?>
111-
111+
<script>
112+
require(['mage/apply/main'], function(mage){
113+
mage.apply();
114+
});
115+
</script>
112116
</div>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Controller\Account;
7+
8+
use Magento\TestFramework\Helper\Bootstrap;
9+
10+
/**
11+
* @magentoAppArea adminhtml
12+
*/
13+
class CreatePostTest extends \Magento\TestFramework\TestCase\AbstractController
14+
{
15+
const EXPECTED_DOB = '1991-12-31';
16+
const EXPECTED_DATE = '2017-12-25 00:00:00';
17+
18+
/**
19+
* @var CreatePost
20+
*/
21+
private $model;
22+
23+
protected function setUp()
24+
{
25+
$this->model = Bootstrap::getObjectManager()->create(CreatePost::class);
26+
}
27+
28+
/**
29+
* @magentoDbIsolation enabled
30+
* @magentoDataFixture Magento/Customer/_files/date_attribute.php
31+
* @magentoDataFixture Magento/Customer/_files/customer_date_attribute.php
32+
*/
33+
public function testCustomerSaveWithDateAttributes()
34+
{
35+
$objectManager = Bootstrap::getObjectManager();
36+
37+
/** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */
38+
$repository = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface');
39+
$customer1 = $repository->get('john.doe1@ex.com', 1);
40+
$customerDob = $customer1->getDob();
41+
$customerDate = $customer1->getCustomAttribute('date')->getValue();
42+
$this->assertEquals(self::EXPECTED_DOB, $customerDob);
43+
$this->assertEquals(self::EXPECTED_DATE, $customerDate);
44+
45+
$customer2 = $repository->get('john.doe2@ex.com', 1);
46+
$customerDob = $customer2->getDob();
47+
$customerDate = $customer2->getCustomAttribute('date')->getValue();
48+
$this->assertEquals(self::EXPECTED_DOB, $customerDob);
49+
$this->assertEquals(self::EXPECTED_DATE, $customerDate);
50+
51+
$customer3 = $repository->get('john.doe3@ex.com', 1);
52+
$customerDob = $customer3->getDob();
53+
$customerDate = $customer3->getCustomAttribute('date')->getValue();
54+
$this->assertEquals(self::EXPECTED_DOB, $customerDob);
55+
$this->assertEquals(self::EXPECTED_DATE, $customerDate);
56+
}
57+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
/** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */
9+
$repository = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface');
10+
11+
$objectManager->get('Magento\Framework\Locale\ResolverInterface')->setLocale('en_US');
12+
$customer1 = $objectManager->create('Magento\Customer\Api\Data\CustomerInterface');
13+
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
14+
$customer1->setWebsiteId(1)
15+
->setEmail('john.doe1@ex.com')
16+
->setGroupId(1)
17+
->setStoreId(1)
18+
->setFirstname('John')
19+
->setLastname('Smith')
20+
->setDefaultBilling(1)
21+
->setDefaultShipping(1)
22+
->setTaxvat('12')
23+
->setGender(0)
24+
->setDob('1991-12-31')
25+
->setCustomAttribute('date', '12/25/2017');
26+
$repository->save($customer1, 'password');
27+
28+
$objectManager->get('Magento\Framework\Locale\ResolverInterface')->setLocale('fr_FR');
29+
$customer2 = $objectManager->create('Magento\Customer\Api\Data\CustomerInterface');
30+
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
31+
$customer2->setWebsiteId(1)
32+
->setEmail('john.doe2@ex.com')
33+
->setGroupId(1)
34+
->setStoreId(1)
35+
->setFirstname('John')
36+
->setLastname('Smith')
37+
->setDefaultBilling(1)
38+
->setDefaultShipping(1)
39+
->setTaxvat('12')
40+
->setGender(0)
41+
->setDob('1991-12-31')
42+
->setCustomAttribute('date', '25/12/2017');
43+
$repository->save($customer2, 'password');
44+
45+
$objectManager->get('Magento\Framework\Locale\ResolverInterface')->setLocale('ar_KW');
46+
$customer3 = $objectManager->create('Magento\Customer\Api\Data\CustomerInterface');
47+
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
48+
$customer3->setWebsiteId(1)
49+
->setEmail('john.doe3@ex.com')
50+
->setGroupId(1)
51+
->setStoreId(1)
52+
->setFirstname('John')
53+
->setLastname('Smith')
54+
->setDefaultBilling(1)
55+
->setDefaultShipping(1)
56+
->setTaxvat('12')
57+
->setGender(0)
58+
->setDob('1991-12-31')
59+
->setCustomAttribute('date', '25/12/2017');
60+
$repository->save($customer3, 'password');
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
/** @var $repository \Magento\Customer\Api\CustomerRepositoryInterface */
9+
$repository = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface');
10+
11+
$customer = $objectManager->create('Magento\Customer\Model\Customer');
12+
$customer1 = $repository->get('john.doe1@ex.com', 1);
13+
$repository->delete($customer1);
14+
15+
$customer2 = $repository->get('john.doe2@ex.com', 1);
16+
$repository->delete($customer2);
17+
18+
$customer3 = $repository->get('john.doe3@ex.com', 1);
19+
$repository->delete($customer3);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
/** @var Magento\Customer\Model\Attribute $dateAttribute */
9+
$dateAttribute = $objectManager->create(\Magento\Customer\Model\Attribute::class);
10+
$dateAttribute->setName('date')
11+
->setEntityTypeId(1)
12+
->setIsUserDefined(1)
13+
->setAttributeSetId(1)
14+
->setAttributeGroupId(1)
15+
->setAttributeCode('date')
16+
->setFrontendInput('date')
17+
->setFrontendLabel('date_attribute_frontend_label')
18+
->setFrontendModel('Magento\Eav\Model\Entity\Attribute\Frontend\Datetime')
19+
->setBackendType('datetime')
20+
->setBackendModel('Magento\Eav\Model\Entity\Attribute\Backend\Datetime')
21+
->setSortOrder(42)
22+
->save();

0 commit comments

Comments
 (0)