Skip to content

Commit 1add0f6

Browse files
committed
ACP2E-3361: Some of the relations records are saved to DB when order record is saved
- improved solution - added unit test
1 parent a58e3c0 commit 1add0f6

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

lib/internal/Magento/Framework/Model/ResourceModel/Db/VersionControl/Snapshot.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
namespace Magento\Framework\Model\ResourceModel\Db\VersionControl;
77

88
use Magento\Framework\DataObject;
9+
use Magento\Framework\Exception\LocalizedException;
910
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
11+
use Magento\Framework\Serialize\SerializerInterface;
1012

1113
/**
1214
* Class Snapshot register snapshot of entity data, for tracking changes
1315
*/
1416
class Snapshot implements ResetAfterRequestInterface
1517
{
18+
/**
19+
* @var SerializerInterface
20+
*/
21+
private SerializerInterface $serializer;
22+
1623
/**
1724
* Array of snapshots of entities data
1825
*
@@ -29,19 +36,22 @@ class Snapshot implements ResetAfterRequestInterface
2936
* Initialization
3037
*
3138
* @param Metadata $metadata
39+
* @param SerializerInterface $serializer
3240
*/
3341
public function __construct(
34-
Metadata $metadata
42+
Metadata $metadata,
43+
SerializerInterface $serializer
3544
) {
3645
$this->metadata = $metadata;
46+
$this->serializer = $serializer;
3747
}
3848

3949
/**
4050
* Register snapshot of entity data, for tracking changes
4151
*
4252
* @param DataObject $entity
4353
* @return void
44-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
54+
* @throws LocalizedException
4555
*/
4656
public function registerSnapshot(DataObject $entity)
4757
{
@@ -88,9 +98,10 @@ public function isModified(DataObject $entity)
8898
foreach ($this->snapshotData[$entityClass][$entity->getId()] as $field => $value) {
8999
$entityValue = $entity->getDataByKey($field);
90100
if (is_array($entityValue) && is_string($value)) {
91-
$decodedValue = json_decode($value, true);
92-
if (json_last_error() === JSON_ERROR_NONE) {
93-
$value = $decodedValue;
101+
try {
102+
$value = $this->serializer->unserialize($value);
103+
} catch (\InvalidArgumentException) {
104+
return true;
94105
}
95106
}
96107
if ($entityValue != $value) {

lib/internal/Magento/Framework/Model/Test/Unit/ResourceModel/Db/VersionControl/SnapshotTest.php

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
namespace Magento\Framework\Model\Test\Unit\ResourceModel\Db\VersionControl;
99

10+
use Magento\Framework\Exception\LocalizedException;
1011
use Magento\Framework\Model\AbstractModel;
1112
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Metadata;
1213
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
13-
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
14+
use Magento\Framework\Serialize\SerializerInterface;
15+
use PHPUnit\Framework\MockObject\Exception;
1416
use PHPUnit\Framework\MockObject\MockObject;
1517
use PHPUnit\Framework\TestCase;
1618

@@ -19,38 +21,47 @@ class SnapshotTest extends TestCase
1921
/**
2022
* @var Snapshot
2123
*/
22-
protected $entitySnapshot;
24+
protected Snapshot $entitySnapshot;
2325

2426
/**
2527
* @var MockObject|Metadata
2628
*/
27-
protected $entityMetadata;
29+
private Metadata $entityMetadata;
2830

2931
/**
3032
* @var MockObject|AbstractModel
3133
*/
32-
protected $model;
34+
private AbstractModel $model;
3335

3436
/**
35-
* Initialization
37+
* @var SerializerInterface|MockObject
38+
*/
39+
private SerializerInterface $serializer;
40+
41+
/**
42+
* @return void
43+
* @throws Exception
3644
*/
3745
protected function setUp(): void
3846
{
39-
$objectManager = new ObjectManager($this);
4047
$this->model = $this->createPartialMock(AbstractModel::class, ['getId']);
4148

4249
$this->entityMetadata = $this->createPartialMock(
4350
Metadata::class,
4451
['getFields']
4552
);
53+
$this->serializer = $this->createMock(SerializerInterface::class);
4654

47-
$this->entitySnapshot = $objectManager->getObject(
48-
Snapshot::class,
49-
['metadata' => $this->entityMetadata]
50-
);
55+
$this->entitySnapshot = new Snapshot($this->entityMetadata, $this->serializer);
56+
57+
parent::setUp();
5158
}
5259

53-
public function testRegisterSnapshot()
60+
/**
61+
* @return void
62+
* @throws LocalizedException
63+
*/
64+
public function testRegisterSnapshot(): void
5465
{
5566
$entityId = 1;
5667
$data = [
@@ -72,19 +83,26 @@ public function testRegisterSnapshot()
7283
$this->assertFalse($this->entitySnapshot->isModified($this->model));
7384
}
7485

75-
public function testIsModified()
86+
/**
87+
* @return void
88+
* @throws LocalizedException
89+
*/
90+
public function testIsModified(): void
7691
{
7792
$entityId = 1;
93+
$options = ['option1' => 'value1', 'option2' => 'value2'];
7894
$data = [
7995
'id' => $entityId,
8096
'name' => 'test',
8197
'description' => '',
82-
'custom_not_present_attribute' => ''
98+
'custom_not_present_attribute' => '',
99+
'options' => json_encode($options)
83100
];
84101
$fields = [
85102
'id' => [],
86103
'name' => [],
87-
'description' => []
104+
'description' => [],
105+
'options' => []
88106
];
89107
$modifiedData = array_merge($data, ['name' => 'newName']);
90108
$this->model->expects($this->any())->method('getId')->willReturn($entityId);
@@ -93,11 +111,19 @@ public function testIsModified()
93111
$this->entitySnapshot->registerSnapshot($this->model);
94112
$this->model->setData($modifiedData);
95113
$this->assertTrue($this->entitySnapshot->isModified($this->model));
114+
115+
$this->model->setData('options', $options);
116+
$this->assertTrue($this->entitySnapshot->isModified($this->model));
117+
96118
$this->entitySnapshot->registerSnapshot($this->model);
97119
$this->assertFalse($this->entitySnapshot->isModified($this->model));
98120
}
99121

100-
public function testClear()
122+
/**
123+
* @return void
124+
* @throws LocalizedException
125+
*/
126+
public function testClear(): void
101127
{
102128
$entityId = 1;
103129
$data = [

0 commit comments

Comments
 (0)