diff --git a/app/code/Magento/Customer/Model/Customer.php b/app/code/Magento/Customer/Model/Customer.php index 81ea34e6f9430..8c7f3e1661fcd 100644 --- a/app/code/Magento/Customer/Model/Customer.php +++ b/app/code/Magento/Customer/Model/Customer.php @@ -357,7 +357,7 @@ public function getDataModel() \Magento\Customer\Api\Data\CustomerInterface::class ); $customerDataObject->setAddresses($addressesData) - ->setId($this->getId() ? (int) $this->getId() : null); + ->setId($this->getId()); return $customerDataObject; } diff --git a/app/code/Magento/Persistent/Model/Plugin/ConvertCustomerCartToGuest.php b/app/code/Magento/Persistent/Model/Plugin/ConvertCustomerCartToGuest.php index 3e4b005db8930..862414cacded8 100644 --- a/app/code/Magento/Persistent/Model/Plugin/ConvertCustomerCartToGuest.php +++ b/app/code/Magento/Persistent/Model/Plugin/ConvertCustomerCartToGuest.php @@ -24,6 +24,9 @@ use Magento\Quote\Model\Quote; use Magento\Quote\Model\QuoteManagement; +/** + * @SuppressWarnings(PHPMD.CookieAndSessionMisuse) + */ class ConvertCustomerCartToGuest { /** diff --git a/app/code/Magento/Persistent/Model/QuoteManager.php b/app/code/Magento/Persistent/Model/QuoteManager.php index 7c7433aa2263e..4485240650cd7 100644 --- a/app/code/Magento/Persistent/Model/QuoteManager.php +++ b/app/code/Magento/Persistent/Model/QuoteManager.php @@ -23,22 +23,16 @@ class QuoteManager { /** - * Persistent session - * * @var \Magento\Persistent\Helper\Session */ protected $persistentSession; /** - * Checkout session - * * @var \Magento\Checkout\Model\Session */ protected $checkoutSession; /** - * Persistent data - * * @var Data */ protected $persistentData; diff --git a/app/code/Magento/Persistent/Test/Unit/Model/QuoteManagerTest.php b/app/code/Magento/Persistent/Test/Unit/Model/QuoteManagerTest.php index a30f0755fa951..d239c9096f050 100644 --- a/app/code/Magento/Persistent/Test/Unit/Model/QuoteManagerTest.php +++ b/app/code/Magento/Persistent/Test/Unit/Model/QuoteManagerTest.php @@ -343,30 +343,38 @@ public function testExpire(): void */ public function testConvertCustomerCartToGuest(): void { - $quoteId = 1; $addressArgs = ['customerAddressId' => null]; $customerIdArgs = ['customerId' => null]; - $emailArgs = ['email' => null]; - - $this->checkoutSessionMock->expects($this->once()) - ->method('getQuoteId')->willReturn($quoteId); - $this->quoteMock->expects($this->once())->method('getId')->willReturn($quoteId); - $this->quoteRepositoryMock->expects($this->once())->method('get')->with($quoteId)->willReturn($this->quoteMock); + $email = 'test@example.com'; + $firstname = 'Firstname'; + $lastname = 'Lastname'; + + $billingAddressMock = $this->createMock(Address::class); + $billingAddressMock->method('getEmail')->willReturn($email); + $billingAddressMock->method('getFirstname')->willReturn($firstname); + $billingAddressMock->method('getLastname')->willReturn($lastname); + $this->quoteMock->method('getBillingAddress')->willReturn($billingAddressMock); $this->quoteMock->expects($this->once()) - ->method('setIsActive')->with(true)->willReturn($this->quoteMock); + ->method('setCustomerId') + ->with(null) + ->willReturn($this->quoteMock); $this->quoteMock->expects($this->once()) - ->method('setCustomerId')->with(null)->willReturn($this->quoteMock); + ->method('setCustomerEmail')->with($email) + ->willReturn($this->quoteMock); $this->quoteMock->expects($this->once()) - ->method('setCustomerEmail')->with(null)->willReturn($this->quoteMock); + ->method('setCustomerFirstname') + ->with($firstname) + ->willReturn($this->quoteMock); $this->quoteMock->expects($this->once()) - ->method('setCustomerFirstname')->with(null)->willReturn($this->quoteMock); + ->method('setCustomerLastname')->with($lastname) + ->willReturn($this->quoteMock); $this->quoteMock->expects($this->once()) - ->method('setCustomerLastname')->with(null)->willReturn($this->quoteMock); - $this->quoteMock->expects($this->never())->method('setCustomerGroupId') + ->method('setCustomerGroupId') + ->with(0) ->willReturn($this->quoteMock); $this->quoteMock->expects($this->once()) ->method('setIsPersistent')->with(false)->willReturn($this->quoteMock); - $this->quoteMock->expects($this->exactly(3)) + $this->quoteMock->expects($this->exactly(2)) ->method('getAddressesCollection')->willReturn($this->abstractCollectionMock); $customerMock = $this->createMock(CustomerInterface::class); $customerMock->expects($this->once()) @@ -375,49 +383,18 @@ public function testConvertCustomerCartToGuest(): void ->willReturnSelf(); $this->quoteMock->expects($this->once()) ->method('getCustomer')->willReturn($customerMock); - $this->abstractCollectionMock->expects($this->exactly(3))->method('walk')->with( + $this->abstractCollectionMock->expects($this->exactly(2))->method('walk')->with( $this->logicalOr( $this->equalTo('setCustomerAddressId'), $this->equalTo($addressArgs), $this->equalTo('setCustomerId'), - $this->equalTo($customerIdArgs), - $this->equalTo('setEmail'), - $this->equalTo($emailArgs) + $this->equalTo($customerIdArgs) ) ); $this->quoteMock->expects($this->once())->method('collectTotals')->willReturn($this->quoteMock); - $this->persistentSessionMock->expects($this->once()) - ->method('getSession')->willReturn($this->sessionMock); - $this->sessionMock->expects($this->once()) - ->method('removePersistentCookie')->willReturn($this->sessionMock); $this->quoteRepositoryMock->expects($this->once())->method('save')->with($this->quoteMock); - $this->model->convertCustomerCartToGuest(); - } - - /** - * @return void - */ - public function testConvertCustomerCartToGuestWithEmptyQuote(): void - { - $this->checkoutSessionMock->expects($this->once()) - ->method('getQuoteId')->willReturn(null); - $this->quoteRepositoryMock->expects($this->once())->method('get')->with(null)->willReturn(null); - $this->model->convertCustomerCartToGuest(); - } - - /** - * @return void - */ - public function testConvertCustomerCartToGuestWithEmptyQuoteId(): void - { - $this->checkoutSessionMock->expects($this->once()) - ->method('getQuoteId')->willReturn(1); - $quoteWithNoId = $this->quoteMock = $this->createMock(Quote::class); - $quoteWithNoId->expects($this->once())->method('getId')->willReturn(null); - $this->quoteRepositoryMock->expects($this->once())->method('get')->with(1)->willReturn($quoteWithNoId); - $this->quoteMock->expects($this->once())->method('getId')->willReturn(1); - $this->model->convertCustomerCartToGuest(); + $this->model->convertCustomerCartToGuest($this->quoteMock); } /** @@ -429,7 +406,7 @@ private function getExtensionAttributesMock(): MockObject { $extensionMockBuilder = $this->getMockBuilder(CartExtensionInterface::class); try { - $extensionMockBuilder->addMethods(['setShippingAssignments']); + $extensionMockBuilder->onlyMethods(['setShippingAssignments']); } catch (RuntimeException $e) { // do nothing as CartExtensionInterface already generated and has 'setShippingAssignments' method. } diff --git a/app/code/Magento/Quote/Model/QuoteAddressValidator.php b/app/code/Magento/Quote/Model/QuoteAddressValidator.php index 6d9a82e2d4e4b..5ac830d1b78c6 100644 --- a/app/code/Magento/Quote/Model/QuoteAddressValidator.php +++ b/app/code/Magento/Quote/Model/QuoteAddressValidator.php @@ -156,7 +156,7 @@ public function validateForCart(CartInterface $cart, AddressInterface $address): if ($cart->getCustomerIsGuest()) { $this->doValidateForGuestQuoteAddress($address, $cart); } - $this->doValidate($address, $cart->getCustomer()->getId()); + $this->doValidate($address, !$cart->getCustomer()->getId() ? null : (int) $cart->getCustomer()->getId()); } /**