Skip to content

Commit 70d7ca4

Browse files
Merge pull request #2085 from magento-tango/2.1.13-PR-0.4
Bugs - MAGETWO-71669 [Magento Cloud] Incorrect date format with Arabic language locale - MAGETWO-80426 Calling the getCustomAttributes() method on a product instance before it is saved, internal _data property structure is changed and extracted incorrectly before saved - MAGETWO-80502 Product set to one website resets incorrectly for all websites after special price scheduled update ends
2 parents 7b9aeef + fd0e15b commit 70d7ca4

File tree

19 files changed

+293
-57
lines changed

19 files changed

+293
-57
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/Model/ProductRepository.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,27 +292,34 @@ protected function initializeProductData(array $productData, $createNew)
292292
foreach ($productData as $key => $value) {
293293
$product->setData($key, $value);
294294
}
295-
$this->assignProductToWebsites($product);
295+
$this->assignProductToWebsites($product, $createNew);
296296

297297
return $product;
298298
}
299299

300300
/**
301301
* @param \Magento\Catalog\Model\Product $product
302+
* @param bool $createNew
302303
* @return void
303304
*/
304-
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product)
305+
private function assignProductToWebsites(\Magento\Catalog\Model\Product $product, $createNew)
305306
{
306-
if (!$this->storeManager->hasSingleStore()) {
307-
308-
if ($this->storeManager->getStore()->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
309-
$websiteIds = array_keys($this->storeManager->getWebsites());
310-
} else {
311-
$websiteIds = [$this->storeManager->getStore()->getWebsiteId()];
312-
}
307+
$websiteIds = $product->getWebsiteIds();
308+
309+
if ($createNew && !$this->storeManager->hasSingleStore()) {
310+
$websiteIds = array_unique(
311+
array_merge(
312+
$websiteIds,
313+
[$this->storeManager->getStore()->getWebsiteId()]
314+
)
315+
);
316+
}
313317

314-
$product->setWebsiteIds(array_unique(array_merge($product->getWebsiteIds(), $websiteIds)));
318+
if ($createNew && $this->storeManager->getStore(true)->getCode() == \Magento\Store\Model\Store::ADMIN_CODE) {
319+
$websiteIds = array_keys($this->storeManager->getWebsites());
315320
}
321+
322+
$product->setWebsiteIds($websiteIds);
316323
}
317324

318325
/**

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/Catalog/Test/Unit/Model/CategoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ public function testGetCustomAttributes()
550550
$this->assertEquals("description", $this->category->getCustomAttribute($descriptionAttributeCode)->getValue());
551551

552552
//Change the attribute value, should reflect in getCustomAttribute
553-
$this->category->setData($descriptionAttributeCode, "new description");
553+
$this->category->setCustomAttribute($descriptionAttributeCode, "new description");
554554
$this->assertEquals(1, count($this->category->getCustomAttributes()));
555555
$this->assertNotNull($this->category->getCustomAttribute($descriptionAttributeCode));
556556
$this->assertEquals(

app/code/Magento/Catalog/Test/Unit/Model/ProductRepositoryTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface;
1717
use Magento\Framework\Api\SortOrder;
1818
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
19+
use Magento\Store\Api\Data\StoreInterface;
1920

2021
/**
2122
* Tests \Magento\Catalog\Model\ProductRepositoryTest
@@ -1290,6 +1291,45 @@ public function testSaveExistingWithNewMediaGalleryEntries()
12901291
$this->model->save($this->productMock);
12911292
}
12921293

1294+
public function testSaveWithDifferentWebsites()
1295+
{
1296+
$getWebsitesResultData = [
1297+
1 => ['first'],
1298+
2 => ['second'],
1299+
3 => ['third']
1300+
];
1301+
$getWebsiteIdsResultData = [1,2,3];
1302+
$setWebsiteIdsResultData = [2,3];
1303+
$getIdBySkuResultData = 100;
1304+
$storeMock = $this->getMock(StoreInterface::class);
1305+
$this->resourceModelMock->expects($this->at(0))->method('getIdBySku')->will($this->returnValue(null));
1306+
$this->resourceModelMock
1307+
->expects($this->at(3))
1308+
->method('getIdBySku')
1309+
->will($this->returnValue($getIdBySkuResultData));
1310+
$this->productFactoryMock->expects($this->any())
1311+
->method('create')
1312+
->will($this->returnValue($this->productMock));
1313+
$this->initializationHelperMock->expects($this->never())->method('initialize');
1314+
$this->resourceModelMock->expects($this->once())->method('validate')->with($this->productMock)
1315+
->willReturn(true);
1316+
$this->resourceModelMock->expects($this->once())->method('save')->with($this->productMock)->willReturn(true);
1317+
$this->extensibleDataObjectConverterMock
1318+
->expects($this->once())
1319+
->method('toNestedArray')
1320+
->will($this->returnValue($this->productData));
1321+
$this->storeManagerMock->expects($this->any())
1322+
->method('getStore')
1323+
->willReturn($storeMock);
1324+
$this->storeManagerMock->expects($this->once())
1325+
->method('getWebsites')
1326+
->willReturn($getWebsitesResultData);
1327+
$this->productMock->expects($this->once())->method('getWebsiteIds')->willReturn($getWebsiteIdsResultData);
1328+
$this->productMock->expects($this->once())->method('setWebsiteIds')->willReturn($setWebsiteIdsResultData);
1329+
1330+
$this->assertEquals($this->productMock, $this->model->save($this->productMock));
1331+
}
1332+
12931333
public function testSaveExistingWithMediaGalleryEntries()
12941334
{
12951335
//update one entry, delete one entry

app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,7 +1247,7 @@ public function testGetCustomAttributes()
12471247
$this->assertEquals("red", $this->model->getCustomAttribute($colorAttributeCode)->getValue());
12481248

12491249
//Change the attribute value, should reflect in getCustomAttribute
1250-
$this->model->setData($colorAttributeCode, "blue");
1250+
$this->model->setCustomAttribute($colorAttributeCode, "blue");
12511251
$this->assertEquals(1, count($this->model->getCustomAttributes()));
12521252
$this->assertNotNull($this->model->getCustomAttribute($colorAttributeCode));
12531253
$this->assertEquals("blue", $this->model->getCustomAttribute($colorAttributeCode)->getValue());

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>

0 commit comments

Comments
 (0)