Skip to content

Commit 1cdb6b4

Browse files
ENGCOM-6039: #23627 improved getCustomerName method for Order #24746
2 parents 6b82de1 + bf7c4f8 commit 1cdb6b4

File tree

2 files changed

+141
-15
lines changed

2 files changed

+141
-15
lines changed

app/code/Magento/Sales/Model/Order.php

Lines changed: 75 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@
55
*/
66
namespace Magento\Sales\Model;
77

8+
use Magento\Config\Model\Config\Source\Nooptreq;
89
use Magento\Directory\Model\Currency;
910
use Magento\Framework\Api\AttributeValueFactory;
11+
use Magento\Framework\Api\SearchCriteriaBuilder;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
1013
use Magento\Framework\App\ObjectManager;
1114
use Magento\Framework\Exception\LocalizedException;
1215
use Magento\Framework\Locale\ResolverInterface;
1316
use Magento\Framework\Pricing\PriceCurrencyInterface;
1417
use Magento\Sales\Api\Data\OrderInterface;
1518
use Magento\Sales\Api\Data\OrderItemInterface;
1619
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
20+
use Magento\Sales\Api\OrderItemRepositoryInterface;
1721
use Magento\Sales\Model\Order\Payment;
1822
use Magento\Sales\Model\Order\ProductOption;
1923
use Magento\Sales\Model\ResourceModel\Order\Address\Collection;
@@ -24,8 +28,7 @@
2428
use Magento\Sales\Model\ResourceModel\Order\Shipment\Collection as ShipmentCollection;
2529
use Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection as TrackCollection;
2630
use Magento\Sales\Model\ResourceModel\Order\Status\History\Collection as HistoryCollection;
27-
use Magento\Sales\Api\OrderItemRepositoryInterface;
28-
use Magento\Framework\Api\SearchCriteriaBuilder;
31+
use Magento\Store\Model\ScopeInterface;
2932

3033
/**
3134
* Order model
@@ -299,6 +302,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
299302
*/
300303
private $searchCriteriaBuilder;
301304

305+
/**
306+
* @var ScopeConfigInterface;
307+
*/
308+
private $scopeConfig;
309+
302310
/**
303311
* @param \Magento\Framework\Model\Context $context
304312
* @param \Magento\Framework\Registry $registry
@@ -331,6 +339,7 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
331339
* @param ProductOption|null $productOption
332340
* @param OrderItemRepositoryInterface $itemRepository
333341
* @param SearchCriteriaBuilder $searchCriteriaBuilder
342+
* @param ScopeConfigInterface $scopeConfig
334343
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
335344
*/
336345
public function __construct(
@@ -364,7 +373,8 @@ public function __construct(
364373
ResolverInterface $localeResolver = null,
365374
ProductOption $productOption = null,
366375
OrderItemRepositoryInterface $itemRepository = null,
367-
SearchCriteriaBuilder $searchCriteriaBuilder = null
376+
SearchCriteriaBuilder $searchCriteriaBuilder = null,
377+
ScopeConfigInterface $scopeConfig = null
368378
) {
369379
$this->_storeManager = $storeManager;
370380
$this->_orderConfig = $orderConfig;
@@ -392,6 +402,7 @@ public function __construct(
392402
->get(OrderItemRepositoryInterface::class);
393403
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()
394404
->get(SearchCriteriaBuilder::class);
405+
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);
395406

396407
parent::__construct(
397408
$context,
@@ -1111,7 +1122,7 @@ public function addStatusHistoryComment($comment, $status = false)
11111122
{
11121123
return $this->addCommentToStatusHistory($comment, $status, false);
11131124
}
1114-
1125+
11151126
/**
11161127
* Add a comment to order status history.
11171128
*
@@ -1503,7 +1514,7 @@ public function getItemById($itemId)
15031514
* Get item by quote item id
15041515
*
15051516
* @param mixed $quoteItemId
1506-
* @return \Magento\Framework\DataObject|null
1517+
* @return \Magento\Framework\DataObject|null
15071518
*/
15081519
public function getItemByQuoteItemId($quoteItemId)
15091520
{
@@ -1967,11 +1978,23 @@ public function getRelatedObjects()
19671978
*/
19681979
public function getCustomerName()
19691980
{
1970-
if ($this->getCustomerFirstname()) {
1971-
$customerName = $this->getCustomerFirstname() . ' ' . $this->getCustomerLastname();
1972-
} else {
1973-
$customerName = (string)__('Guest');
1981+
if (null === $this->getCustomerFirstname()) {
1982+
return (string)__('Guest');
19741983
}
1984+
1985+
$customerName = '';
1986+
if ($this->isVisibleCustomerPrefix() && strlen($this->getCustomerPrefix())) {
1987+
$customerName .= $this->getCustomerPrefix() . ' ';
1988+
}
1989+
$customerName .= $this->getCustomerFirstname();
1990+
if ($this->isVisibleCustomerMiddlename() && strlen($this->getCustomerMiddlename())) {
1991+
$customerName .= ' ' . $this->getCustomerMiddlename();
1992+
}
1993+
$customerName .= ' ' . $this->getCustomerLastname();
1994+
if ($this->isVisibleCustomerSuffix() && strlen($this->getCustomerSuffix())) {
1995+
$customerName .= ' ' . $this->getCustomerSuffix();
1996+
}
1997+
19751998
return $customerName;
19761999
}
19772000

@@ -4534,5 +4557,48 @@ public function setShippingMethod($shippingMethod)
45344557
return $this->setData('shipping_method', $shippingMethod);
45354558
}
45364559

4560+
/**
4561+
* Is visible customer middlename
4562+
*
4563+
* @return bool
4564+
*/
4565+
private function isVisibleCustomerMiddlename(): bool
4566+
{
4567+
return $this->scopeConfig->isSetFlag(
4568+
'customer/address/middlename_show',
4569+
ScopeInterface::SCOPE_STORE
4570+
);
4571+
}
4572+
4573+
/**
4574+
* Is visible customer prefix
4575+
*
4576+
* @return bool
4577+
*/
4578+
private function isVisibleCustomerPrefix(): bool
4579+
{
4580+
$prefixShowValue = $this->scopeConfig->getValue(
4581+
'customer/address/prefix_show',
4582+
ScopeInterface::SCOPE_STORE
4583+
);
4584+
4585+
return $prefixShowValue !== Nooptreq::VALUE_NO;
4586+
}
4587+
4588+
/**
4589+
* Is visible customer suffix
4590+
*
4591+
* @return bool
4592+
*/
4593+
private function isVisibleCustomerSuffix(): bool
4594+
{
4595+
$prefixShowValue = $this->scopeConfig->getValue(
4596+
'customer/address/suffix_show',
4597+
ScopeInterface::SCOPE_STORE
4598+
);
4599+
4600+
return $prefixShowValue !== Nooptreq::VALUE_NO;
4601+
}
4602+
45374603
//@codeCoverageIgnoreEnd
45384604
}

app/code/Magento/Sales/Test/Unit/Model/OrderTest.php

Lines changed: 66 additions & 6 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\Sales\Test\Unit\Model;
78

89
use Magento\Catalog\Api\Data\ProductInterface;
@@ -17,13 +18,16 @@
1718
use Magento\Framework\Api\SearchCriteriaBuilder;
1819
use Magento\Framework\Api\SearchCriteria;
1920
use Magento\Sales\Api\Data\OrderItemSearchResultInterface;
21+
use Magento\Framework\App\Config\ScopeConfigInterface;
22+
use PHPUnit\Framework\MockObject\MockObject;
2023

2124
/**
2225
* Test class for \Magento\Sales\Model\Order
2326
*
2427
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2528
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
2629
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
30+
* @SuppressWarnings(PHPMD.TooManyFields)
2731
*/
2832
class OrderTest extends \PHPUnit\Framework\TestCase
2933
{
@@ -102,6 +106,11 @@ class OrderTest extends \PHPUnit\Framework\TestCase
102106
*/
103107
private $searchCriteriaBuilder;
104108

109+
/**
110+
* @var MockObject|ScopeConfigInterface $scopeConfigMock
111+
*/
112+
private $scopeConfigMock;
113+
105114
protected function setUp()
106115
{
107116
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -125,14 +134,17 @@ protected function setUp()
125134
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class,
126135
['create']
127136
);
128-
$this->item = $this->createPartialMock(\Magento\Sales\Model\ResourceModel\Order\Item::class, [
137+
$this->item = $this->createPartialMock(
138+
\Magento\Sales\Model\ResourceModel\Order\Item::class,
139+
[
129140
'isDeleted',
130141
'getQtyToInvoice',
131142
'getParentItemId',
132143
'getQuoteItemId',
133144
'getLockedDoInvoice',
134145
'getProductId',
135-
]);
146+
]
147+
);
136148
$this->salesOrderCollectionMock = $this->getMockBuilder(
137149
\Magento\Sales\Model\ResourceModel\Order\Collection::class
138150
)->disableOriginalConstructor()
@@ -168,6 +180,7 @@ protected function setUp()
168180
->setMethods(['addFilter', 'create'])
169181
->disableOriginalConstructor()->getMockForAbstractClass();
170182

183+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
171184
$this->order = $helper->getObject(
172185
\Magento\Sales\Model\Order::class,
173186
[
@@ -182,7 +195,8 @@ protected function setUp()
182195
'localeResolver' => $this->localeResolver,
183196
'timezone' => $this->timezone,
184197
'itemRepository' => $this->itemRepository,
185-
'searchCriteriaBuilder' => $this->searchCriteriaBuilder
198+
'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
199+
'scopeConfig' => $this->scopeConfigMock
186200
]
187201
);
188202
}
@@ -354,6 +368,51 @@ public function testCanInvoice()
354368
$this->assertTrue($this->order->canInvoice());
355369
}
356370

371+
/**
372+
* Ensure customer name returned correctly.
373+
*
374+
* @dataProvider customerNameProvider
375+
* @param array $expectedData
376+
*/
377+
public function testGetCustomerName(array $expectedData)
378+
{
379+
$this->order->setCustomerFirstname($expectedData['first_name']);
380+
$this->order->setCustomerSuffix($expectedData['customer_suffix']);
381+
$this->order->setCustomerPrefix($expectedData['customer_prefix']);
382+
$this->scopeConfigMock->expects($this->exactly($expectedData['invocation']))
383+
->method('isSetFlag')
384+
->willReturn(true);
385+
$this->assertEquals($expectedData['expected_name'], $this->order->getCustomerName());
386+
}
387+
388+
/**
389+
* Customer name data provider
390+
*/
391+
public function customerNameProvider()
392+
{
393+
return
394+
[
395+
[
396+
[
397+
'first_name' => null,
398+
'invocation' => 0,
399+
'expected_name' => 'Guest',
400+
'customer_suffix' => 'smith',
401+
'customer_prefix' => 'mr.'
402+
]
403+
],
404+
[
405+
[
406+
'first_name' => 'Smith',
407+
'invocation' => 1,
408+
'expected_name' => 'mr. Smith Carl',
409+
'customer_suffix' => 'Carl',
410+
'customer_prefix' => 'mr.'
411+
]
412+
]
413+
];
414+
}
415+
357416
/**
358417
* @param string $status
359418
*
@@ -819,9 +878,10 @@ public function testCanVoidPayment($actionFlags, $orderState)
819878
if ($orderState == \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW) {
820879
$canVoidOrder = false;
821880
}
822-
if ($orderState == \Magento\Sales\Model\Order::STATE_HOLDED && (!isset(
823-
$actionFlags[\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD]
824-
) || $actionFlags[\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD] !== false)
881+
if ($orderState == \Magento\Sales\Model\Order::STATE_HOLDED &&
882+
(!isset($actionFlags[\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD]) ||
883+
$actionFlags[\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD] !== false
884+
)
825885
) {
826886
$canVoidOrder = false;
827887
}

0 commit comments

Comments
 (0)