Skip to content

Commit 3d0a08d

Browse files
committed
Merge branch 'ACP2E-3361' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-14-10-2024
2 parents 15a78a0 + 94714d3 commit 3d0a08d

File tree

4 files changed

+105
-47
lines changed

4 files changed

+105
-47
lines changed

app/code/Magento/Customer/Test/Unit/Model/ResourceModel/Db/VersionControl/AddressSnapshotTest.php

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2016 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

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

1010
use Magento\Customer\Model\ResourceModel\Db\VersionControl\AddressSnapshot;
1111
use Magento\Framework\DataObject;
12+
use Magento\Framework\Exception\LocalizedException;
1213
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Metadata;
14+
use Magento\Framework\Serialize\SerializerInterface;
1315
use PHPUnit\Framework\MockObject\MockObject;
1416
use PHPUnit\Framework\TestCase;
1517

@@ -18,22 +20,30 @@ class AddressSnapshotTest extends TestCase
1820
/**
1921
* @var AddressSnapshot
2022
*/
21-
private $model;
23+
private AddressSnapshot $model;
2224

2325
/**
2426
* @var Metadata|MockObject
2527
*/
26-
private $metadataMock;
28+
private Metadata $metadataMock;
2729

30+
/**
31+
* @var SerializerInterface|MockObject
32+
*/
33+
private SerializerInterface $serializer;
34+
35+
/**
36+
* @return void
37+
* @throws \PHPUnit\Framework\MockObject\Exception
38+
*/
2839
protected function setUp(): void
2940
{
30-
$this->metadataMock = $this->getMockBuilder(
31-
Metadata::class
32-
)->disableOriginalConstructor()
33-
->getMock();
41+
$this->metadataMock = $this->createMock(Metadata::class);
42+
$this->serializer = $this->createMock(SerializerInterface::class);
3443

3544
$this->model = new AddressSnapshot(
36-
$this->metadataMock
45+
$this->metadataMock,
46+
$this->serializer
3747
);
3848
}
3949

@@ -43,13 +53,10 @@ protected function setUp(): void
4353
* @param int $isDefaultShipping
4454
* @param bool $expected
4555
* @dataProvider dataProviderIsModified
56+
* @throws LocalizedException
4657
*/
47-
public function testIsModified(
48-
$isCustomerSaveTransaction,
49-
$isDefaultBilling,
50-
$isDefaultShipping,
51-
$expected
52-
) {
58+
public function testIsModified($isCustomerSaveTransaction, $isDefaultBilling, $isDefaultShipping, $expected): void
59+
{
5360
$entityId = 1;
5461

5562
$dataObjectMock = $this->getMockBuilder(DataObject::class)
@@ -96,7 +103,7 @@ public function testIsModified(
96103
/**
97104
* @return array
98105
*/
99-
public static function dataProviderIsModified()
106+
public static function dataProviderIsModified(): array
100107
{
101108
return [
102109
[false, 1, 1, true],
@@ -106,7 +113,11 @@ public static function dataProviderIsModified()
106113
];
107114
}
108115

109-
public function testIsModifiedBypass()
116+
/**
117+
* @return void
118+
* @throws LocalizedException
119+
*/
120+
public function testIsModifiedBypass(): void
110121
{
111122
$dataObjectMock = $this->getMockBuilder(DataObject::class)
112123
->disableOriginalConstructor()

dev/tests/integration/testsuite/Magento/Downloadable/Model/Observer/SaveDownloadableOrderItemObserverTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2023 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -70,6 +70,9 @@ public function testOrderStateIsProcessingAndInvoicedOrderItemLinkIsDownloadable
7070

7171
/** Save order item to trigger observers */
7272
$orderItemRepository = $this->objectManager->get(ItemRepository::class);
73+
$updateDate = new \DateTime();
74+
$updateDate->modify('+5 minute');
75+
$orderItem->setUpdatedAt($updateDate->format('Y-m-d H:i:s'));
7376
$orderItemRepository->save($orderItem);
7477

7578
$this->assertOrderItemLinkStatus((int)$orderItem->getId(), Item::LINK_STATUS_AVAILABLE);

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
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,21 +36,24 @@ 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
*
42-
* @param \Magento\Framework\DataObject $entity
52+
* @param DataObject $entity
4353
* @return void
44-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
54+
* @throws LocalizedException
4555
*/
46-
public function registerSnapshot(\Magento\Framework\DataObject $entity)
56+
public function registerSnapshot(DataObject $entity)
4757
{
4858
$metaData = $this->metadata->getFields($entity);
4959
$filteredData = array_intersect_key($entity->getData(), $metaData);
@@ -72,10 +82,10 @@ public function getSnapshotData(DataObject $entity): array
7282
/**
7383
* Check is current entity has changes, by comparing current object state with stored snapshot
7484
*
75-
* @param \Magento\Framework\DataObject $entity
85+
* @param DataObject $entity
7686
* @return bool
7787
*/
78-
public function isModified(\Magento\Framework\DataObject $entity)
88+
public function isModified(DataObject $entity)
7989
{
8090
if (!$entity->getId()) {
8191
return true;
@@ -86,7 +96,15 @@ public function isModified(\Magento\Framework\DataObject $entity)
8696
return true;
8797
}
8898
foreach ($this->snapshotData[$entityClass][$entity->getId()] as $field => $value) {
89-
if ($entity->getDataByKey($field) != $value) {
99+
$entityValue = $entity->getDataByKey($field);
100+
if (is_array($entityValue) && is_string($value)) {
101+
try {
102+
$value = $this->serializer->unserialize($value);
103+
} catch (\InvalidArgumentException) {
104+
return true;
105+
}
106+
}
107+
if ($entityValue != $value) {
90108
return true;
91109
}
92110
}
@@ -97,9 +115,9 @@ public function isModified(\Magento\Framework\DataObject $entity)
97115
/**
98116
* Clear snapshot data
99117
*
100-
* @param \Magento\Framework\DataObject|null $entity
118+
* @param DataObject|null $entity
101119
*/
102-
public function clear(\Magento\Framework\DataObject $entity = null)
120+
public function clear(DataObject $entity = null)
103121
{
104122
if ($entity !== null) {
105123
$this->snapshotData[get_class($entity)] = [];

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

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
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)