Skip to content

Commit b1651ae

Browse files
committed
Merge branch 'ACP2E-3486' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-VK-2024-12-17-CE
2 parents f3e6674 + 8b91644 commit b1651ae

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

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

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All Rights Reserved.
55
*/
6-
76
namespace Magento\Eav\Model\Entity\Attribute\Backend;
87

98
/**
@@ -57,6 +56,14 @@ public function beforeSave($object)
5756
$object->setData($attributeName . '_is_formated', true);
5857
}
5958

59+
$defaultValue = $this->getDefaultValue();
60+
if ($object->getData($attributeName) === null
61+
&& $defaultValue !== null
62+
&& !$object->hasData($attributeName)) {
63+
$object->setData($attributeName, $defaultValue);
64+
$object->setData($attributeName . '_is_formated', true);
65+
}
66+
6067
return $this;
6168
}
6269

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Eav\Test\Unit\Model\Entity\Attribute\Backend;
9+
10+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
11+
use Magento\Eav\Model\Entity\Attribute\Backend\Datetime;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
14+
use Magento\Framework\DataObject;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class DatetimeTest extends TestCase
19+
{
20+
/**
21+
* @var TimezoneInterface|MockObject
22+
*/
23+
private $timezone;
24+
25+
/**
26+
* @var AbstractAttribute|MockObject
27+
*/
28+
private $attribute;
29+
30+
/**
31+
* @var Datetime
32+
*/
33+
private $model;
34+
35+
protected function setUp(): void
36+
{
37+
$this->attribute = $this->getMockBuilder(AbstractAttribute::class)
38+
->disableOriginalConstructor()
39+
->getMock();
40+
$this->timezone = $this->createMock(TimezoneInterface::class);
41+
$this->model = new Datetime($this->timezone);
42+
$this->model->setAttribute($this->attribute);
43+
}
44+
45+
/**
46+
* @throws LocalizedException
47+
*/
48+
public function testGetDefaultValue()
49+
{
50+
$attributeName = 'attribute';
51+
$defaultValue = '2024-01-01 00:00:00';
52+
$this->attribute->expects($this->once())->method('getName')->willReturn($attributeName);
53+
$this->attribute->expects($this->exactly(2))->method('getDefaultValue')->willReturn($defaultValue);
54+
$object = new DataObject();
55+
$this->model->beforeSave($object);
56+
$this->assertEquals($defaultValue, $object->getData($attributeName));
57+
}
58+
}

0 commit comments

Comments
 (0)