Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#23627 improved getCustomerName method for Order #24746

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 75 additions & 9 deletions app/code/Magento/Sales/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
*/
namespace Magento\Sales\Model;

use Magento\Config\Model\Config\Source\Nooptreq;
use Magento\Directory\Model\Currency;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Api\Data\OrderStatusHistoryInterface;
use Magento\Sales\Api\OrderItemRepositoryInterface;
use Magento\Sales\Model\Order\Payment;
use Magento\Sales\Model\Order\ProductOption;
use Magento\Sales\Model\ResourceModel\Order\Address\Collection;
Expand All @@ -24,8 +28,7 @@
use Magento\Sales\Model\ResourceModel\Order\Shipment\Collection as ShipmentCollection;
use Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection as TrackCollection;
use Magento\Sales\Model\ResourceModel\Order\Status\History\Collection as HistoryCollection;
use Magento\Sales\Api\OrderItemRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Store\Model\ScopeInterface;

/**
* Order model
Expand Down Expand Up @@ -299,6 +302,11 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
*/
private $searchCriteriaBuilder;

/**
* @var ScopeConfigInterface;
*/
private $scopeConfig;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand Down Expand Up @@ -331,6 +339,7 @@ class Order extends AbstractModel implements EntityInterface, OrderInterface
* @param ProductOption|null $productOption
* @param OrderItemRepositoryInterface $itemRepository
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param ScopeConfigInterface $scopeConfig
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand Down Expand Up @@ -364,7 +373,8 @@ public function __construct(
ResolverInterface $localeResolver = null,
ProductOption $productOption = null,
OrderItemRepositoryInterface $itemRepository = null,
SearchCriteriaBuilder $searchCriteriaBuilder = null
SearchCriteriaBuilder $searchCriteriaBuilder = null,
ScopeConfigInterface $scopeConfig = null
) {
$this->_storeManager = $storeManager;
$this->_orderConfig = $orderConfig;
Expand Down Expand Up @@ -392,6 +402,7 @@ public function __construct(
->get(OrderItemRepositoryInterface::class);
$this->searchCriteriaBuilder = $searchCriteriaBuilder ?: ObjectManager::getInstance()
->get(SearchCriteriaBuilder::class);
$this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class);

parent::__construct(
$context,
Expand Down Expand Up @@ -1111,7 +1122,7 @@ public function addStatusHistoryComment($comment, $status = false)
{
return $this->addCommentToStatusHistory($comment, $status, false);
}

/**
* Add a comment to order status history.
*
Expand Down Expand Up @@ -1503,7 +1514,7 @@ public function getItemById($itemId)
* Get item by quote item id
*
* @param mixed $quoteItemId
* @return \Magento\Framework\DataObject|null
* @return \Magento\Framework\DataObject|null
*/
public function getItemByQuoteItemId($quoteItemId)
{
Expand Down Expand Up @@ -1967,11 +1978,23 @@ public function getRelatedObjects()
*/
public function getCustomerName()
{
if ($this->getCustomerFirstname()) {
$customerName = $this->getCustomerFirstname() . ' ' . $this->getCustomerLastname();
} else {
$customerName = (string)__('Guest');
if (null === $this->getCustomerFirstname()) {
return (string)__('Guest');
}

$customerName = '';
if ($this->isVisibleCustomerPrefix() && strlen($this->getCustomerPrefix())) {
$customerName .= $this->getCustomerPrefix() . ' ';
}
$customerName .= $this->getCustomerFirstname();
if ($this->isVisibleCustomerMiddlename() && strlen($this->getCustomerMiddlename())) {
$customerName .= ' ' . $this->getCustomerMiddlename();
}
$customerName .= ' ' . $this->getCustomerLastname();
if ($this->isVisibleCustomerSuffix() && strlen($this->getCustomerSuffix())) {
$customerName .= ' ' . $this->getCustomerSuffix();
}

return $customerName;
}

Expand Down Expand Up @@ -4534,5 +4557,48 @@ public function setShippingMethod($shippingMethod)
return $this->setData('shipping_method', $shippingMethod);
}

/**
* Is visible customer middlename
*
* @return bool
*/
private function isVisibleCustomerMiddlename(): bool
{
return $this->scopeConfig->isSetFlag(
'customer/address/middlename_show',
ScopeInterface::SCOPE_STORE
);
}

/**
* Is visible customer prefix
*
* @return bool
*/
private function isVisibleCustomerPrefix(): bool
{
$prefixShowValue = $this->scopeConfig->getValue(
'customer/address/prefix_show',
ScopeInterface::SCOPE_STORE
);

return $prefixShowValue !== Nooptreq::VALUE_NO;
}

/**
* Is visible customer suffix
*
* @return bool
*/
private function isVisibleCustomerSuffix(): bool
{
$prefixShowValue = $this->scopeConfig->getValue(
'customer/address/suffix_show',
ScopeInterface::SCOPE_STORE
);

return $prefixShowValue !== Nooptreq::VALUE_NO;
}

//@codeCoverageIgnoreEnd
}
72 changes: 66 additions & 6 deletions app/code/Magento/Sales/Test/Unit/Model/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Sales\Test\Unit\Model;

use Magento\Catalog\Api\Data\ProductInterface;
Expand All @@ -17,13 +18,16 @@
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteria;
use Magento\Sales\Api\Data\OrderItemSearchResultInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;
use PHPUnit\Framework\MockObject\MockObject;

/**
* Test class for \Magento\Sales\Model\Order
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyPublicMethods)
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
* @SuppressWarnings(PHPMD.TooManyFields)
*/
class OrderTest extends \PHPUnit\Framework\TestCase
{
Expand Down Expand Up @@ -102,6 +106,11 @@ class OrderTest extends \PHPUnit\Framework\TestCase
*/
private $searchCriteriaBuilder;

/**
* @var MockObject|ScopeConfigInterface $scopeConfigMock
*/
private $scopeConfigMock;

protected function setUp()
{
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand All @@ -125,14 +134,17 @@ protected function setUp()
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory::class,
['create']
);
$this->item = $this->createPartialMock(\Magento\Sales\Model\ResourceModel\Order\Item::class, [
$this->item = $this->createPartialMock(
\Magento\Sales\Model\ResourceModel\Order\Item::class,
[
'isDeleted',
'getQtyToInvoice',
'getParentItemId',
'getQuoteItemId',
'getLockedDoInvoice',
'getProductId',
]);
]
);
$this->salesOrderCollectionMock = $this->getMockBuilder(
\Magento\Sales\Model\ResourceModel\Order\Collection::class
)->disableOriginalConstructor()
Expand Down Expand Up @@ -168,6 +180,7 @@ protected function setUp()
->setMethods(['addFilter', 'create'])
->disableOriginalConstructor()->getMockForAbstractClass();

$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
$this->order = $helper->getObject(
\Magento\Sales\Model\Order::class,
[
Expand All @@ -182,7 +195,8 @@ protected function setUp()
'localeResolver' => $this->localeResolver,
'timezone' => $this->timezone,
'itemRepository' => $this->itemRepository,
'searchCriteriaBuilder' => $this->searchCriteriaBuilder
'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
'scopeConfig' => $this->scopeConfigMock
]
);
}
Expand Down Expand Up @@ -354,6 +368,51 @@ public function testCanInvoice()
$this->assertTrue($this->order->canInvoice());
}

/**
* Ensure customer name returned correctly.
*
* @dataProvider customerNameProvider
* @param array $expectedData
*/
public function testGetCustomerName(array $expectedData)
{
$this->order->setCustomerFirstname($expectedData['first_name']);
$this->order->setCustomerSuffix($expectedData['customer_suffix']);
$this->order->setCustomerPrefix($expectedData['customer_prefix']);
$this->scopeConfigMock->expects($this->exactly($expectedData['invocation']))
->method('isSetFlag')
->willReturn(true);
$this->assertEquals($expectedData['expected_name'], $this->order->getCustomerName());
}

/**
* Customer name data provider
*/
public function customerNameProvider()
{
return
[
[
[
'first_name' => null,
'invocation' => 0,
'expected_name' => 'Guest',
'customer_suffix' => 'smith',
'customer_prefix' => 'mr.'
]
],
[
[
'first_name' => 'Smith',
'invocation' => 1,
'expected_name' => 'mr. Smith Carl',
'customer_suffix' => 'Carl',
'customer_prefix' => 'mr.'
]
]
];
}

/**
* @param string $status
*
Expand Down Expand Up @@ -819,9 +878,10 @@ public function testCanVoidPayment($actionFlags, $orderState)
if ($orderState == \Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW) {
$canVoidOrder = false;
}
if ($orderState == \Magento\Sales\Model\Order::STATE_HOLDED && (!isset(
$actionFlags[\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD]
) || $actionFlags[\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD] !== false)
if ($orderState == \Magento\Sales\Model\Order::STATE_HOLDED &&
(!isset($actionFlags[\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD]) ||
$actionFlags[\Magento\Sales\Model\Order::ACTION_FLAG_UNHOLD] !== false
)
) {
$canVoidOrder = false;
}
Expand Down