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

Minor refactoring to AccessChangeQuoteControl and it's Unit Tests #29670

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
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.
*/
declare(strict_types=1);

namespace Magento\Quote\Model\QuoteRepository\Plugin;

Expand Down Expand Up @@ -32,16 +33,17 @@ public function __construct(ChangeQuoteControlInterface $changeQuoteControl)
/**
* Checks if change quote's customer id is allowed for current user.
*
* A StateException is thrown if Guest's or Customer's customer_id not match user_id or unknown user type
*
* @param CartRepositoryInterface $subject
* @param CartInterface $quote
* @throws StateException if Guest has customer_id or Customer's customer_id not much with user_id
* or unknown user's type
* @return void
* @throws StateException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeSave(CartRepositoryInterface $subject, CartInterface $quote)
public function beforeSave(CartRepositoryInterface $subject, CartInterface $quote): void
{
if (! $this->changeQuoteControl->isAllowed($quote)) {
if (!$this->changeQuoteControl->isAllowed($quote)) {
throw new StateException(__("Invalid state change requested"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Magento\Quote\Test\Unit\Model\QuoteRepository\Plugin;

use Magento\Authorization\Model\UserContextInterface;
use Magento\Framework\Exception\StateException;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Quote\Model\ChangeQuoteControl;
use Magento\Quote\Model\Quote;
Expand Down Expand Up @@ -52,7 +53,7 @@ protected function setUp(): void

$this->quoteMock = $this->getMockBuilder(Quote::class)
->disableOriginalConstructor()
->setMethods(['getCustomerId'])
->addMethods(['getCustomerId'])
->getMock();

$this->quoteRepositoryMock = $this->getMockBuilder(QuoteRepository::class)
Expand All @@ -63,17 +64,10 @@ protected function setUp(): void
->disableOriginalConstructor()
->getMock();

$objectManagerHelper = new ObjectManager($this);
$this->accessChangeQuoteControl = $objectManagerHelper->getObject(
AccessChangeQuoteControl::class,
['changeQuoteControl' => $this->changeQuoteControlMock]
);
$this->accessChangeQuoteControl = new AccessChangeQuoteControl($this->changeQuoteControlMock);
}

/**
* User with role Customer and customer_id matches context user_id.
*/
public function testBeforeSaveForCustomer()
public function testBeforeSaveForCustomerWithCustomerIdMatchinQuoteUserIdIsAllowed()
{
$this->quoteMock->method('getCustomerId')
->willReturn(1);
Expand All @@ -84,17 +78,12 @@ public function testBeforeSaveForCustomer()
$this->changeQuoteControlMock->method('isAllowed')
->willReturn(true);

$result = $this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);

$this->assertNull($result);
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
}

/**
* The user_id and customer_id from the quote are different.
*/
public function testBeforeSaveException()
public function testBeforeSaveThrowsExceptionForCustomerWithCustomerIdNotMatchingQuoteUserId()
{
$this->expectException('Magento\Framework\Exception\StateException');
$this->expectException(StateException::class);
$this->expectExceptionMessage('Invalid state change requested');
$this->quoteMock->method('getCustomerId')
->willReturn(2);
Expand All @@ -108,10 +97,7 @@ public function testBeforeSaveException()
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
}

/**
* User with role Admin and customer_id not much with user_id.
*/
public function testBeforeSaveForAdmin()
public function testBeforeSaveForAdminUserRoleIsAllowed()
{
$this->quoteMock->method('getCustomerId')
->willReturn(2);
Expand All @@ -122,15 +108,10 @@ public function testBeforeSaveForAdmin()
$this->changeQuoteControlMock->method('isAllowed')
->willReturn(true);

$result = $this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);

$this->assertNull($result);
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
}

/**
* User with role Guest and customer_id === null.
*/
public function testBeforeSaveForGuest()
public function testBeforeSaveForGuestIsAllowed()
{
$this->quoteMock->method('getCustomerId')
->willReturn(null);
Expand All @@ -141,17 +122,12 @@ public function testBeforeSaveForGuest()
$this->changeQuoteControlMock->method('isAllowed')
->willReturn(true);

$result = $this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);

$this->assertNull($result);
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
}

/**
* User with role Guest and customer_id !== null.
*/
public function testBeforeSaveForGuestException()
public function testBeforeSaveThrowsExceptionForGuestDoesNotEquals()
{
$this->expectException('Magento\Framework\Exception\StateException');
$this->expectException(StateException::class);
$this->expectExceptionMessage('Invalid state change requested');
$this->quoteMock->method('getCustomerId')
->willReturn(1);
Expand All @@ -165,12 +141,9 @@ public function testBeforeSaveForGuestException()
$this->accessChangeQuoteControl->beforeSave($this->quoteRepositoryMock, $this->quoteMock);
}

/**
* User with unknown role.
*/
public function testBeforeSaveForUnknownUserTypeException()
public function testBeforeSaveThrowsExceptionForUnknownUserType()
{
$this->expectException('Magento\Framework\Exception\StateException');
$this->expectException(StateException::class);
$this->expectExceptionMessage('Invalid state change requested');
$this->quoteMock->method('getCustomerId')
->willReturn(2);
Expand Down