Skip to content

Commit 5233b27

Browse files
author
Joan He
committed
MAGETWO-91528: Customizable options truncated when displaying ordered product in admin
1 parent dbe8c7b commit 5233b27

File tree

7 files changed

+304
-4
lines changed

7 files changed

+304
-4
lines changed

app/code/Magento/Sales/Block/Adminhtml/Items/Column/Name.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Sales\Block\Adminhtml\Items\Column;
77

8+
use Magento\Framework\Filter\TruncateFilter\Result;
9+
810
/**
911
* Sales Order items name column renderer
1012
*
@@ -13,6 +15,11 @@
1315
*/
1416
class Name extends \Magento\Sales\Block\Adminhtml\Items\Column\DefaultColumn
1517
{
18+
/**
19+
* @var Result
20+
*/
21+
private $truncateResult = null;
22+
1623
/**
1724
* Truncate string
1825
*
@@ -25,10 +32,11 @@ class Name extends \Magento\Sales\Block\Adminhtml\Items\Column\DefaultColumn
2532
*/
2633
public function truncateString($value, $length = 80, $etc = '...', &$remainder = '', $breakWords = true)
2734
{
28-
return $this->filterManager->truncate(
35+
$this->truncateResult = $this->filterManager->truncateFilter(
2936
$value,
30-
['length' => $length, 'etc' => $etc, 'remainder' => $remainder, 'breakWords' => $breakWords]
37+
['length' => $length, 'etc' => $etc, 'breakWords' => $breakWords]
3138
);
39+
return $this->truncateResult->getValue();
3240
}
3341

3442
/**
@@ -40,8 +48,11 @@ public function truncateString($value, $length = 80, $etc = '...', &$remainder =
4048
public function getFormattedOption($value)
4149
{
4250
$remainder = '';
43-
$value = $this->truncateString($value, 55, '', $remainder);
44-
$result = ['value' => nl2br($value), 'remainder' => nl2br($remainder)];
51+
$this->truncateString($value, 55, '', $remainder);
52+
$result = [
53+
'value' => nl2br($this->truncateResult->getValue()),
54+
'remainder' => nl2br($this->truncateResult->getRemainder())
55+
];
4556

4657
return $result;
4758
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Filter;
9+
10+
class TruncateFilterTest extends \PHPUnit\Framework\TestCase
11+
{
12+
/**
13+
* @param string $expectedValue
14+
* @param string $expectedRemainder
15+
* @param string $string
16+
* @param int $length
17+
* @param string $etc
18+
* @param bool $breakWords
19+
* @dataProvider truncateDataProvider
20+
*/
21+
public function testFilter(
22+
$expectedValue, $expectedRemainder,
23+
$string,
24+
$length = 5,
25+
$etc = '...',
26+
$breakWords = true
27+
) {
28+
/** @var TruncateFilter $truncateFilter */
29+
$truncateFilter = \Magento\TestFramework\ObjectManager::getInstance()->create(
30+
TruncateFilter::class,
31+
[
32+
'length' => $length,
33+
'etc' => $etc,
34+
'breakWords' => $breakWords,
35+
]
36+
);
37+
$result = $truncateFilter->filter($string);
38+
$this->assertEquals($expectedValue, $result->getValue());
39+
$this->assertEquals($expectedRemainder, $result->getRemainder());
40+
}
41+
42+
public function truncateDataProvider() : array
43+
{
44+
return [
45+
'1' => [
46+
'12...',
47+
'34567890',
48+
'1234567890',
49+
],
50+
'2' => [
51+
'123..',
52+
' 456 789',
53+
'123 456 789',
54+
8,
55+
'..',
56+
false
57+
]
58+
];
59+
}
60+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Sales\Block\Adminhtml\Items\Column;
9+
10+
/**
11+
* @magentoAppArea adminhtml
12+
*/
13+
class NameTest extends \PHPUnit\Framework\TestCase
14+
{
15+
/**
16+
* @var Name
17+
*/
18+
private $block;
19+
20+
protected function setUp()
21+
{
22+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
23+
/** @var $layout \Magento\Framework\View\Layout */
24+
$layout = $objectManager->create(\Magento\Framework\View\LayoutInterface::class);
25+
/** @var $block \Magento\Sales\Block\Adminhtml\Items\AbstractItems */
26+
$this->block = $layout->createBlock(Name::class, 'block');
27+
}
28+
29+
public function testTruncateString() : void
30+
{
31+
$remainder = '';
32+
$this->assertEquals(
33+
'12345',
34+
$this->block->truncateString('1234567890', 5, '', $remainder)
35+
);
36+
}
37+
38+
public function testGetFormattedOptiong() : void
39+
{
40+
$this->assertEquals(
41+
[
42+
'value' => '1234567890123456789012345678901234567890123456789012345',
43+
'remainder' => '67890',
44+
],
45+
$this->block->getFormattedOption(
46+
'123456789012345678901234567890123456789012345678901234567890'
47+
)
48+
);
49+
}
50+
}

lib/internal/Magento/Framework/Filter/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Factory extends AbstractFactory
3232
'decrypt' => \Magento\Framework\Filter\Decrypt::class,
3333
'translit' => \Magento\Framework\Filter\Translit::class,
3434
'translitUrl' => \Magento\Framework\Filter\TranslitUrl::class,
35+
'truncateFilter' => \Magento\Framework\Filter\TruncateFilter::class,
3536
];
3637

3738
/**

lib/internal/Magento/Framework/Filter/FilterManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* @method string removeTags(string $value, $params = array())
2222
* @method string stripTags(string $value, $params = array())
2323
* @method string truncate(string $value, $params = array())
24+
* @method string truncateFilter(string $value, $params = array())
2425
* @method string encrypt(string $value, $params = array())
2526
* @method string decrypt(string $value, $params = array())
2627
* @method string translit(string $value)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Filter;
9+
10+
use Magento\Framework\Filter\TruncateFilter\Result;
11+
use Magento\Framework\Filter\TruncateFilter\ResultFactory;
12+
13+
/**
14+
* Truncate filter
15+
*
16+
* Truncate a string to a certain length if necessary, appending the $etc string.
17+
* $remainder will contain the string that has been replaced with $etc.
18+
*/
19+
class TruncateFilter implements \Zend_Filter_Interface
20+
{
21+
/**
22+
* @var int
23+
*/
24+
private $length;
25+
26+
/**
27+
* @var string
28+
*/
29+
private $etc;
30+
31+
/**
32+
* @var bool
33+
*/
34+
private $breakWords;
35+
36+
/**
37+
* @var \Magento\Framework\Stdlib\StringUtils
38+
*/
39+
private $stringUtils;
40+
41+
/**
42+
* @var ResultFactory
43+
*/
44+
private $resultFactory;
45+
46+
/**
47+
* @param \Magento\Framework\Stdlib\StringUtils $stringUtils
48+
* @param ResultFactory $resultFactory
49+
* @param int $length
50+
* @param string $etc
51+
* @param bool $breakWords
52+
*/
53+
public function __construct(
54+
\Magento\Framework\Stdlib\StringUtils $stringUtils,
55+
ResultFactory $resultFactory,
56+
$length = 80,
57+
$etc = '...',
58+
$breakWords = true
59+
) {
60+
$this->stringUtils = $stringUtils;
61+
$this->resultFactory = $resultFactory;
62+
$this->length = $length;
63+
$this->etc = $etc;
64+
$this->breakWords = $breakWords;
65+
}
66+
67+
/**
68+
* Filter value
69+
*
70+
* @param string $string
71+
* @return Result
72+
*/
73+
public function filter($string) : Result
74+
{
75+
/** @var Result $result */
76+
$result = $this->resultFactory->create(['value' => $string, 'remainder' => '']);
77+
$length = $this->length;
78+
if (0 == $length) {
79+
$result->setValue('');
80+
return $result;
81+
}
82+
83+
$originalLength = $this->stringUtils->strlen($string);
84+
if ($originalLength > $length) {
85+
$length -= $this->stringUtils->strlen($this->etc);
86+
if ($length <= 0) {
87+
$result->setValue('');
88+
return $result;
89+
}
90+
$preparedString = $string;
91+
$preparedLength = $length;
92+
if (!$this->breakWords) {
93+
$preparedString = preg_replace('/\s+?(\S+)?$/u', '', $this->stringUtils->substr($string, 0, $length + 1));
94+
$preparedLength = $this->stringUtils->strlen($preparedString);
95+
}
96+
$result->setRemainder($this->stringUtils->substr($string, $preparedLength, $originalLength));
97+
$result->setValue($this->stringUtils->substr($preparedString, 0, $length) . $this->etc);
98+
return $result;
99+
}
100+
101+
return $result;
102+
}
103+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Filter\TruncateFilter;
9+
10+
class Result
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $value;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $remainder;
21+
22+
/**
23+
* Result constructor.
24+
* @param string $value
25+
* @param string $remainder
26+
*/
27+
public function __construct(string $value, string $remainder)
28+
{
29+
$this->value = $value;
30+
$this->remainder = $remainder;
31+
}
32+
33+
/**
34+
* Set result value
35+
*
36+
* @param string $value
37+
* @return void
38+
*/
39+
public function setValue(string $value) : void
40+
{
41+
$this->value = $value;
42+
}
43+
44+
/**
45+
* Get value
46+
*
47+
* @return string
48+
*/
49+
public function getValue() : string
50+
{
51+
return $this->value;
52+
}
53+
54+
/**
55+
* Set remainder
56+
*
57+
* @param string $remainder
58+
* @return void
59+
*/
60+
public function setRemainder(string $remainder) : void
61+
{
62+
$this->remainder = $remainder;
63+
}
64+
65+
/**
66+
* Get remainder
67+
*
68+
* @return string
69+
*/
70+
public function getRemainder() : string
71+
{
72+
return $this->remainder;
73+
}
74+
}

0 commit comments

Comments
 (0)