Skip to content

Commit

Permalink
ENGCOM-8028: ChangeQuoteControl minor refactor #29669
Browse files Browse the repository at this point in the history
  • Loading branch information
sidolov authored Aug 20, 2020
2 parents 8488a5a + e31086a commit bc95fa4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 71 deletions.
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

0 comments on commit bc95fa4

Please sign in to comment.