Skip to content

Commit f0accb0

Browse files
ENGCOM-1007: [Forwardport] Add missing implementation for applySortOrder() #14231
- Merge Pull Request #14231 from rostyslav-hymon/magento2:2.3-develop-PR-port-13641 - Merged commits: 1. a7ff305
2 parents 4046856 + a7ff305 commit f0accb0

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

app/code/Magento/Sales/Block/Order/Totals.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,12 @@ public function removeTotal($code)
293293
*/
294294
public function applySortOrder($order)
295295
{
296+
\uksort(
297+
$this->_totals,
298+
function ($code1, $code2) use ($order) {
299+
return ($order[$code1] ?? 0) <=> ($order[$code2] ?? 0);
300+
}
301+
);
296302
return $this;
297303
}
298304

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Sales\Test\Unit\Block\Order;
7+
8+
use Magento\Framework\Registry;
9+
use Magento\Sales\Block\Order\Totals;
10+
use Magento\Sales\Model\Order;
11+
use Magento\Sales\Model\Order\Total;
12+
13+
class TotalsTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var \Magento\Sales\Block\Order\Totals
17+
*/
18+
protected $block;
19+
20+
/**
21+
* @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
protected $context;
24+
25+
protected function setUp()
26+
{
27+
$this->context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
28+
$this->block = new Totals($this->context, new Registry);
29+
$this->block->setOrder($this->createMock(Order::class));
30+
}
31+
32+
public function testApplySortOrder()
33+
{
34+
$this->block->addTotal(new Total(['code' => 'one']), 'last');
35+
$this->block->addTotal(new Total(['code' => 'two']), 'last');
36+
$this->block->addTotal(new Total(['code' => 'three']), 'last');
37+
$this->block->applySortOrder(
38+
[
39+
'one' => 10,
40+
'two' => 30,
41+
'three' => 20,
42+
]
43+
);
44+
$this->assertEqualsSorted(
45+
[
46+
'one' => new Total(['code' => 'one']),
47+
'three' => new Total(['code' => 'three']),
48+
'two' => new Total(['code' => 'two']),
49+
],
50+
$this->block->getTotals()
51+
);
52+
}
53+
54+
private function assertEqualsSorted(array $expected, array $actual)
55+
{
56+
$this->assertEquals($expected, $actual, 'Array contents should be equal.');
57+
$this->assertEquals(array_keys($expected), array_keys($actual), 'Array sort order should be equal.');
58+
}
59+
}

0 commit comments

Comments
 (0)