Skip to content

Commit 3a43a25

Browse files
authored
Merge pull request #468 from magento-okapis/MAGETWO-59105-SortOrder
Fixed Issue: - MAGETWO-59105 Errors on storefront after install for MPAF builds.
2 parents 6674762 + 83eb455 commit 3a43a25

File tree

3 files changed

+101
-18
lines changed

3 files changed

+101
-18
lines changed

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/Initialization/HelperTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,15 +278,19 @@ public function testInitialize()
278278
->method('getOptionsReadOnly')
279279
->willReturn(false);
280280

281+
$firstExpectedCustomOption = clone $this->customOptionMock;
282+
$firstExpectedCustomOption->setData($optionsData['option2']);
283+
$secondExpectedCustomOption = clone $this->customOptionMock;
284+
$secondExpectedCustomOption->setData($optionsData['option3']);
281285
$this->customOptionFactoryMock->expects($this->any())
282286
->method('create')
283287
->willReturnMap([
284288
[
285289
['data' => $optionsData['option2']],
286-
(clone $this->customOptionMock)->setData($optionsData['option2'])
290+
$firstExpectedCustomOption
287291
], [
288292
['data' => $optionsData['option3']],
289-
(clone $this->customOptionMock)->setData($optionsData['option3'])
293+
$secondExpectedCustomOption
290294
]
291295
]);
292296

lib/internal/Magento/Framework/Data/Argument/Interpreter/ArrayType.php

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,70 @@ public function evaluate(array $data)
5454
*/
5555
private function sortItems($items)
5656
{
57-
uasort(
58-
$items,
59-
function ($firstItem, $secondItem) {
60-
$firstValue = 0;
61-
$secondValue = 0;
62-
if (isset($firstItem['sortOrder'])) {
63-
$firstValue = intval($firstItem['sortOrder']);
57+
$sortOrderDefined = $this->isSortOrderDefined($items);
58+
if ($sortOrderDefined) {
59+
$indexedItems = [];
60+
foreach ($items as $key => $item) {
61+
$indexedItems[] = ['key' => $key, 'item' => $item];
62+
}
63+
uksort(
64+
$indexedItems,
65+
function ($firstItemKey, $secondItemKey) use ($indexedItems) {
66+
return $this->compareItems($firstItemKey, $secondItemKey, $indexedItems);
6467
}
68+
);
69+
// Convert array of sorted items back to initial format
70+
$items = [];
71+
foreach ($indexedItems as $indexedItem) {
72+
$items[$indexedItem['key']] = $indexedItem['item'];
73+
}
74+
}
75+
return $items;
76+
}
6577

66-
if (isset($secondItem['sortOrder'])) {
67-
$secondValue = intval($secondItem['sortOrder']);
68-
}
78+
/**
79+
* Compare sortOrder of item
80+
*
81+
* @param mixed $firstItemKey
82+
* @param mixed $secondItemKey
83+
* @param array $indexedItems
84+
* @return int
85+
*/
86+
private function compareItems($firstItemKey, $secondItemKey, $indexedItems)
87+
{
88+
$firstItem = $indexedItems[$firstItemKey]['item'];
89+
$secondItem = $indexedItems[$secondItemKey]['item'];
90+
$firstValue = 0;
91+
$secondValue = 0;
92+
if (isset($firstItem['sortOrder'])) {
93+
$firstValue = intval($firstItem['sortOrder']);
94+
}
6995

70-
if ($firstValue == $secondValue) {
71-
return 0;
72-
}
73-
return $firstValue < $secondValue ? -1 : 1;
96+
if (isset($secondItem['sortOrder'])) {
97+
$secondValue = intval($secondItem['sortOrder']);
98+
}
99+
100+
if ($firstValue == $secondValue) {
101+
// These keys reflect initial relative position of items.
102+
// Allows stable sort for items with equal 'sortOrder'
103+
return $firstItemKey < $secondItemKey ? -1 : 1;
104+
}
105+
return $firstValue < $secondValue ? -1 : 1;
106+
}
107+
108+
/**
109+
* Determine if a sort order exists for any of the items.
110+
*
111+
* @param array $items
112+
* @return bool
113+
*/
114+
private function isSortOrderDefined($items)
115+
{
116+
foreach ($items as $itemData) {
117+
if (isset($itemData['sortOrder'])) {
118+
return true;
74119
}
75-
);
76-
return $items;
120+
}
121+
return false;
77122
}
78123
}

lib/internal/Magento/Framework/Data/Test/Unit/Argument/Interpreter/ArrayTypeTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,40 @@ public function evaluateDataProvider()
103103
'key1' => '-value 1-',
104104
],
105105
],
106+
'pre-sorted array items' => [
107+
[
108+
'item' => [
109+
'key1' => ['value' => 'value 1'],
110+
'key4' => ['value' => 'value 4'],
111+
'key2' => ['value' => 'value 2', 'sortOrder' => 10],
112+
'key3' => ['value' => 'value 3'],
113+
],
114+
],
115+
[
116+
'key1' => '-value 1-',
117+
'key4' => '-value 4-',
118+
'key3' => '-value 3-',
119+
'key2' => '-value 2-',
120+
],
121+
],
122+
'sort order edge case values' => [
123+
[
124+
'item' => [
125+
'key1' => ['value' => 'value 1', 'sortOrder' => 101],
126+
'key4' => ['value' => 'value 4'],
127+
'key2' => ['value' => 'value 2', 'sortOrder' => -10],
128+
'key3' => ['value' => 'value 3'],
129+
'key5' => ['value' => 'value 5', 'sortOrder' => 20],
130+
],
131+
],
132+
[
133+
'key2' => '-value 2-',
134+
'key4' => '-value 4-',
135+
'key3' => '-value 3-',
136+
'key5' => '-value 5-',
137+
'key1' => '-value 1-',
138+
],
139+
],
106140
];
107141
}
108142
}

0 commit comments

Comments
 (0)