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

ChangeQuoteControl minor refactor #29669

Merged
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
21 changes: 6 additions & 15 deletions app/code/Magento/Quote/Model/ChangeQuoteControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\Quote\Model;
Expand All @@ -12,13 +11,10 @@
use Magento\Quote\Api\ChangeQuoteControlInterface;
use Magento\Quote\Api\Data\CartInterface;

/**
* {@inheritdoc}
*/
class ChangeQuoteControl implements ChangeQuoteControlInterface
{
/**
* @var UserContextInterface $userContext
* @var UserContextInterface
*/
private $userContext;

Expand All @@ -31,25 +27,20 @@ public function __construct(UserContextInterface $userContext)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function isAllowed(CartInterface $quote): bool
{
switch ($this->userContext->getUserType()) {
case UserContextInterface::USER_TYPE_CUSTOMER:
$isAllowed = ($quote->getCustomerId() == $this->userContext->getUserId());
break;
return ($quote->getCustomerId() == $this->userContext->getUserId());
case UserContextInterface::USER_TYPE_GUEST:
$isAllowed = ($quote->getCustomerId() === null);
break;
return ($quote->getCustomerId() === null);
case UserContextInterface::USER_TYPE_ADMIN:
case UserContextInterface::USER_TYPE_INTEGRATION:
$isAllowed = true;
break;
default:
$isAllowed = false;
return true;
}

return $isAllowed;
return false;
}
}
76 changes: 20 additions & 56 deletions app/code/Magento/Quote/Test/Unit/Model/ChangeQuoteControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,130 +16,94 @@

/**
* Unit test for \Magento\Quote\Model\ChangeQuoteControl
*
* Class \Magento\Quote\Test\Unit\Model\ChangeQuoteControlTest
*/
class ChangeQuoteControlTest extends TestCase
{
/**
* @var ObjectManager
*/
protected $objectManager;

/**
* @var ChangeQuoteControl
*/
protected $model;

/**
* @var MockObject
* @var MockObject|UserContextInterface
*/
protected $userContextMock;

/**
* @var MockObject
* @var MockObject|CartInterface
*/
protected $quoteMock;

protected function setUp(): void
{
$this->objectManager = new ObjectManager($this);
$this->userContextMock = $this->getMockForAbstractClass(UserContextInterface::class);

$this->model = $this->objectManager->getObject(
ChangeQuoteControl::class,
[
'userContext' => $this->userContextMock
]
);

$this->quoteMock = $this->getMockForAbstractClass(
CartInterface::class,
[],
'',
false,
true,
true,
['getCustomerId']
);
$this->model = new ChangeQuoteControl($this->userContextMock);

$this->quoteMock = $this->getMockBuilder(CartInterface::class)
->disableOriginalConstructor()
->addMethods(['getCustomerId'])
->getMockForAbstractClass();
}

/**
* Test if the quote is belonged to customer
*/
public function testIsAllowedIfTheQuoteIsBelongedToCustomer()
{
$quoteCustomerId = 1;
$this->quoteMock->expects($this->any())->method('getCustomerId')
$this->quoteMock->method('getCustomerId')
->willReturn($quoteCustomerId);
$this->userContextMock->expects($this->any())->method('getUserType')
$this->userContextMock->method('getUserType')
->willReturn(UserContextInterface::USER_TYPE_CUSTOMER);
$this->userContextMock->expects($this->any())->method('getUserId')
$this->userContextMock->method('getUserId')
->willReturn($quoteCustomerId);

$this->assertTrue($this->model->isAllowed($this->quoteMock));
}

/**
* Test if the quote is not belonged to customer
*/
public function testIsAllowedIfTheQuoteIsNotBelongedToCustomer()
{
$currentCustomerId = 1;
$quoteCustomerId = 2;

$this->quoteMock->expects($this->any())->method('getCustomerId')
$this->quoteMock->method('getCustomerId')
->willReturn($quoteCustomerId);
$this->userContextMock->expects($this->any())->method('getUserType')
$this->userContextMock->method('getUserType')
->willReturn(UserContextInterface::USER_TYPE_CUSTOMER);
$this->userContextMock->expects($this->any())->method('getUserId')
$this->userContextMock->method('getUserId')
->willReturn($currentCustomerId);

$this->assertFalse($this->model->isAllowed($this->quoteMock));
}

/**
* Test if the quote is belonged to guest and the context is guest
*/
public function testIsAllowedIfQuoteIsBelongedToGuestAndContextIsGuest()
{
$quoteCustomerId = null;
$this->quoteMock->expects($this->any())->method('getCustomerId')
$this->quoteMock->method('getCustomerId')
->willReturn($quoteCustomerId);
$this->userContextMock->expects($this->any())->method('getUserType')
$this->userContextMock->method('getUserType')
->willReturn(UserContextInterface::USER_TYPE_GUEST);
$this->assertTrue($this->model->isAllowed($this->quoteMock));
}

/**
* Test if the quote is belonged to customer and the context is guest
*/
public function testIsAllowedIfQuoteIsBelongedToCustomerAndContextIsGuest()
{
$quoteCustomerId = 1;
$this->quoteMock->expects($this->any())->method('getCustomerId')
$this->quoteMock->method('getCustomerId')
->willReturn($quoteCustomerId);
$this->userContextMock->expects($this->any())->method('getUserType')
$this->userContextMock->method('getUserType')
->willReturn(UserContextInterface::USER_TYPE_GUEST);
$this->assertFalse($this->model->isAllowed($this->quoteMock));
}

/**
* Test if the context is admin
*/
public function testIsAllowedIfContextIsAdmin()
{
$this->userContextMock->expects($this->any())->method('getUserType')
$this->userContextMock->method('getUserType')
->willReturn(UserContextInterface::USER_TYPE_ADMIN);
$this->assertTrue($this->model->isAllowed($this->quoteMock));
}

/**
* Test if the context is integration
*/
public function testIsAllowedIfContextIsIntegration()
{
$this->userContextMock->expects($this->any())->method('getUserType')
$this->userContextMock->method('getUserType')
->willReturn(UserContextInterface::USER_TYPE_INTEGRATION);
$this->assertTrue($this->model->isAllowed($this->quoteMock));
}
Expand Down