Skip to content

Commit c4071ee

Browse files
committed
MAGETWO-58479: [Github]Magento 2.1.0: db fields with mysql default value CURRENT_TIMESTAMP cannot be updated through the EntityManager #5385
1 parent 9fbaad7 commit c4071ee

File tree

3 files changed

+110
-29
lines changed

3 files changed

+110
-29
lines changed

dev/tests/integration/testsuite/Magento/Cms/Model/PageTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ public function testGenerateIdentifierFromTitle($data, $expectedIdentifier)
4444
$this->assertEquals($expectedIdentifier, $page->getIdentifier());
4545
}
4646

47+
/**
48+
* @magentoDbIsolation enabled
49+
*/
50+
public function testUpdateTime()
51+
{
52+
$updateTime = '2016-09-01 00:00:00';
53+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
54+
/** @var \Magento\Cms\Model\Page $page */
55+
$page = $objectManager->create(\Magento\Cms\Model\Page::class);
56+
$page->setData(['data' => ['title' => 'Test title', 'stores' => [1]], 'expectedIdentifier' => 'test-title']);
57+
$page->setUpdateTime($updateTime);
58+
$page->save();
59+
$page = $page->load($page->getId());
60+
$this->assertEquals($updateTime, $page->getUpdateTime());
61+
}
62+
4763
public function generateIdentifierFromTitleDataProvider()
4864
{
4965
return [

lib/internal/Magento/Framework/EntityManager/Db/UpdateRow.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ protected function prepareData(EntityMetadataInterface $metadata, AdapterInterfa
5050
{
5151
$output = [];
5252
foreach ($connection->describeTable($metadata->getEntityTable()) as $column) {
53-
if ($column['DEFAULT'] == 'CURRENT_TIMESTAMP' || $column['IDENTITY']) {
53+
$columnName = strtolower($column['COLUMN_NAME']);
54+
if ($this->canNotSetTimeStamp($columnName, $column, $data) || $column['IDENTITY']) {
5455
continue;
5556
}
5657

57-
if (isset($data[strtolower($column['COLUMN_NAME'])])) {
58+
if (isset($data[$columnName])) {
5859
$output[strtolower($column['COLUMN_NAME'])] = $data[strtolower($column['COLUMN_NAME'])];
5960
} elseif (!empty($column['NULLABLE'])) {
6061
$output[strtolower($column['COLUMN_NAME'])] = null;
@@ -83,4 +84,16 @@ public function execute($entityType, $data)
8384
);
8485
return $data;
8586
}
87+
88+
/**
89+
* @param string $columnName
90+
* @param string $column
91+
* @param array $data
92+
* @return bool
93+
*/
94+
protected function canNotSetTimeStamp($columnName, $column, array $data)
95+
{
96+
return $column['DEFAULT'] == 'CURRENT_TIMESTAMP' && !isset($data[$columnName])
97+
&& empty($column['NULLABLE']);
98+
}
8699
}

lib/internal/Magento/Framework/EntityManager/Test/Unit/Db/UpdateRowTest.php

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,14 @@ protected function setUp()
6363
]);
6464
}
6565

66-
public function testExecute()
66+
/**
67+
* @dataProvider columnsDataProvider
68+
* @param array $data
69+
* @param array $columns
70+
* @param array $preparedColumns
71+
*/
72+
public function testExecute(array $data, array $columns, array $preparedColumns)
6773
{
68-
$data = [
69-
'test_link_field' => 1,
70-
'identified_field' => 'test_identified_field',
71-
'test_simple' => 'test_value',
72-
];
73-
$columns = [
74-
'test_nullable' => [
75-
'NULLABLE' => true,
76-
'DEFAULT' => false,
77-
'IDENTITY' => false,
78-
'COLUMN_NAME' => 'test_nullable',
79-
],
80-
'test_simple' => [
81-
'NULLABLE' => true,
82-
'DEFAULT' => false,
83-
'IDENTITY' => false,
84-
'COLUMN_NAME' => 'test_simple',
85-
],
86-
];
87-
$preparedColumns = [
88-
'test_identified_field' => null,
89-
'test_nullable' => null,
90-
'test_simple' => 'test_value',
91-
];
92-
9374
$this->metadataPoolMock->expects($this->once())
9475
->method('getMetadata')
9576
->with('test')
@@ -115,7 +96,78 @@ public function testExecute()
11596
$this->metadataMock->expects($this->exactly(2))
11697
->method('getIdentifierField')
11798
->willReturn('test_identified_field');
118-
99+
if (empty($data['updated_at'])) {
100+
unset($data['updated_at']);
101+
}
119102
$this->assertSame($data, $this->model->execute('test', $data));
120103
}
104+
105+
/**
106+
* @return array
107+
*/
108+
public function columnsDataProvider()
109+
{
110+
$data = [
111+
'test_link_field' => 1,
112+
'identified_field' => 'test_identified_field',
113+
'test_simple' => 'test_value',
114+
];
115+
$columns = [
116+
'test_nullable' => [
117+
'NULLABLE' => true,
118+
'DEFAULT' => false,
119+
'IDENTITY' => false,
120+
'COLUMN_NAME' => 'test_nullable',
121+
],
122+
'test_simple' => [
123+
'NULLABLE' => true,
124+
'DEFAULT' => false,
125+
'IDENTITY' => false,
126+
'COLUMN_NAME' => 'test_simple',
127+
],
128+
];
129+
$preparedColumns = [
130+
'test_identified_field' => null,
131+
'test_nullable' => null,
132+
'test_simple' => 'test_value',
133+
];
134+
135+
return [
136+
'default' => [
137+
'data' => $data,
138+
'columns' => $columns,
139+
'preparedColumns' => $preparedColumns,
140+
],
141+
'empty timestamp field' => [
142+
'data' => array_merge($data, ['updated_at' => '']),
143+
'columns' => array_merge(
144+
$columns,
145+
[
146+
'updated_at' => [
147+
'NULLABLE' => false,
148+
'DEFAULT' => 'CURRENT_TIMESTAMP',
149+
'IDENTITY' => false,
150+
'COLUMN_NAME' => 'updated_at',
151+
],
152+
]
153+
),
154+
'preparedColumns' => $preparedColumns,
155+
],
156+
'filled timestamp field' => [
157+
'data' => array_merge($data, ['updated_at' => '2016-01-01 00:00:00']),
158+
'columns' => array_merge(
159+
$columns,
160+
[
161+
'updated_at' => [
162+
'NULLABLE' => false,
163+
'DEFAULT' => 'CURRENT_TIMESTAMP',
164+
'IDENTITY' => false,
165+
'COLUMN_NAME' => 'updated_at',
166+
],
167+
]
168+
),
169+
'preparedColumns' => array_merge($preparedColumns, ['updated_at' => '2016-01-01 00:00:00']),
170+
],
171+
];
172+
}
121173
}

0 commit comments

Comments
 (0)