Skip to content

Commit 3905580

Browse files
committed
[BUGFIX] Forward-port of #14861 for Magento 2.3
1 parent 2a389b0 commit 3905580

File tree

2 files changed

+101
-9
lines changed

2 files changed

+101
-9
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/CategoryLink.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,14 @@ private function processCategoryLinks($newCategoryPositions, &$oldCategoryPositi
115115
{
116116
$result = ['changed' => [], 'updated' => []];
117117
foreach ($newCategoryPositions as $newCategoryPosition) {
118-
$key = array_search(
119-
$newCategoryPosition['category_id'],
120-
array_column($oldCategoryPositions, 'category_id')
121-
);
118+
$key = false;
119+
120+
foreach ($oldCategoryPositions as $oldKey => $oldCategoryPosition) {
121+
if ((int)$oldCategoryPosition['category_id'] === (int)$newCategoryPosition['category_id']) {
122+
$key = $oldKey;
123+
break;
124+
}
125+
}
122126

123127
if ($key === false) {
124128
$result['changed'][] = $newCategoryPosition;

app/code/Magento/Catalog/Test/Unit/Model/ResourceModel/Product/CategoryLinkTest.php

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Catalog\Test\Unit\Model\ResourceModel\Product;
78

89
use Magento\Catalog\Model\ResourceModel\Product\CategoryLink;
@@ -129,9 +130,65 @@ public function testSaveCategoryLinks($newCategoryLinks, $dbCategoryLinks, $affe
129130
);
130131
}
131132

133+
$expectedResult = [];
134+
135+
foreach ($affectedIds as $type => $ids) {
136+
$expectedResult = array_merge($expectedResult, $ids);
137+
138+
// Verify if the correct insert, update and/or delete actions are performed:
139+
switch ($type) {
140+
case 'insert':
141+
$this->connectionMock
142+
->expects($this->exactly(empty($ids) ? 0 : 1))
143+
->method('insertArray')
144+
->with(
145+
$this->anything(),
146+
$this->anything(),
147+
$this->callback(function ($data) use ($ids) {
148+
$foundIds = [];
149+
foreach ($data as $row) {
150+
$foundIds[] = $row['category_id'];
151+
}
152+
return $ids === $foundIds;
153+
})
154+
);
155+
break;
156+
case 'update':
157+
$this->connectionMock
158+
->expects($this->exactly(empty($ids) ? 0 : 1))
159+
->method('insertOnDuplicate')
160+
->with(
161+
$this->anything(),
162+
$this->callback(function ($data) use ($ids) {
163+
$foundIds = [];
164+
foreach ($data as $row) {
165+
$foundIds[] = $row['category_id'];
166+
}
167+
return $ids === $foundIds;
168+
})
169+
);
170+
break;
171+
case 'delete':
172+
$this->connectionMock
173+
->expects($this->exactly(empty($ids) ? 0 : 1))
174+
->method('delete')
175+
// Verify that the correct category ID's are touched:
176+
->with(
177+
$this->anything(),
178+
$this->callback(function ($data) use ($ids) {
179+
return array_values($data)[1] === $ids;
180+
})
181+
);
182+
break;
183+
}
184+
}
185+
132186
$actualResult = $this->model->saveCategoryLinks($product, $newCategoryLinks);
187+
133188
sort($actualResult);
134-
$this->assertEquals($affectedIds, $actualResult);
189+
sort($expectedResult);
190+
191+
$this->assertEquals($expectedResult, $actualResult);
135192
}
136193

137194
/**
@@ -151,7 +208,11 @@ public function getCategoryLinksDataProvider()
151208
['category_id' => 3, 'position' => 10],
152209
['category_id' => 4, 'position' => 20],
153210
],
154-
[], // Nothing to update - data not changed
211+
[
212+
'update' => [],
213+
'insert' => [],
214+
'delete' => [],
215+
],
155216
],
156217
[
157218
[
@@ -162,7 +223,11 @@ public function getCategoryLinksDataProvider()
162223
['category_id' => 3, 'position' => 10],
163224
['category_id' => 4, 'position' => 20],
164225
],
165-
[3, 4, 5], // 4 - updated position, 5 - added, 3 - deleted
226+
[
227+
'update' => [4],
228+
'insert' => [5],
229+
'delete' => [3],
230+
],
166231
],
167232
[
168233
[
@@ -173,16 +238,39 @@ public function getCategoryLinksDataProvider()
173238
['category_id' => 3, 'position' => 10],
174239
['category_id' => 4, 'position' => 20],
175240
],
176-
[3, 4], // 3 - updated position, 4 - deleted
241+
[
242+
'update' => [3],
243+
'insert' => [],
244+
'delete' => [4],
245+
],
177246
],
178247
[
179248
[],
180249
[
181250
['category_id' => 3, 'position' => 10],
182251
['category_id' => 4, 'position' => 20],
183252
],
184-
[3, 4], // 3, 4 - deleted
253+
[
254+
'update' => [],
255+
'insert' => [],
256+
'delete' => [3, 4],
257+
],
185258
],
259+
[
260+
[
261+
['category_id' => 3, 'position' => 10],
262+
['category_id' => 4, 'position' => 20],
263+
],
264+
[
265+
['category_id' => 3, 'position' => 20], // swapped positions
266+
['category_id' => 4, 'position' => 10], // swapped positions
267+
],
268+
[
269+
'update' => [3, 4],
270+
'insert' => [],
271+
'delete' => [],
272+
],
273+
]
186274
];
187275
}
188276
}

0 commit comments

Comments
 (0)