From 1689d05d58f8650266cdeccafbc1cc14b1569096 Mon Sep 17 00:00:00 2001 From: everwhatever Date: Wed, 4 Jan 2023 08:41:22 +0100 Subject: [PATCH] Extract authorized user assignment to separate method --- .../clearing_cart_after_logging_out.feature | 17 +++- ...t_as_guest_with_a_registered_email.feature | 2 +- .../Behat/Context/Api/Shop/CartContext.php | 2 +- .../CommandHandler/Cart/BlameCartHandler.php | 2 +- .../CommandHandler/Cart/PickupCartHandler.php | 4 +- .../OrderGetMethodItemExtension.php | 6 +- .../OrderMethodsItemExtension.php | 9 +- .../EventListener/ApiCartBlamerListener.php | 2 +- .../Cart/BlameCartHandlerSpec.php | 2 +- .../Cart/PickupCartHandlerSpec.php | 3 +- .../OrderGetMethodItemExtensionSpec.php | 21 +++-- .../OrderMethodsItemExtensionSpec.php | 90 +++++++++++++++---- .../ApiCartBlamerListenerSpec.php | 4 +- .../EventListener/ShopCartBlamerListener.php | 3 +- .../ShopCartBlamerListenerSpec.php | 12 +-- .../Cart/Context/ShopBasedCartContext.php | 17 ++-- src/Sylius/Component/Core/Model/Order.php | 13 ++- .../Component/Core/Model/OrderInterface.php | 5 ++ .../Cart/Context/ShopBasedCartContextSpec.php | 39 +------- .../Component/Core/spec/Model/OrderSpec.php | 20 ++++- 20 files changed, 177 insertions(+), 96 deletions(-) diff --git a/features/cart/shopping_cart/clearing_cart_after_logging_out.feature b/features/cart/shopping_cart/clearing_cart_after_logging_out.feature index bd227d604a..edecbc2e3e 100644 --- a/features/cart/shopping_cart/clearing_cart_after_logging_out.feature +++ b/features/cart/shopping_cart/clearing_cart_after_logging_out.feature @@ -7,16 +7,27 @@ Feature: Clearing cart after logging out Background: Given the store operates on a single channel in "United States" And the store has a product "Stark T-shirt" priced at "$12.00" - And I am a logged in customer - And I have product "Stark T-Shirt" in the cart @ui Scenario: Clearing cart after logging out + Given I am a logged in customer + And I have product "Stark T-Shirt" in the cart When I log out And I see the summary of my cart Then my cart should be empty @api Scenario: Clearing cart after logging out + Given I am a logged in customer + And I have product "Stark T-Shirt" in the cart When I log out - Then I don't have access to see the summary of my previous cart + Then I should not have access to the summary of my previous cart + + @api + Scenario: Blocking access to cart if logged user did any action over it (what can be treated as signing it) + Given there is a user "john@snow.com" + When I add this product to the cart + And I log in as "john@snow.com" with "sylius" password + And I add this product to the cart + And I log out + Then I should not have access to the summary of my previous cart diff --git a/features/checkout/ability_to_checkout_as_guest_with_a_registered_email.feature b/features/checkout/ability_to_checkout_as_guest_with_a_registered_email.feature index 1705f3b3a1..d73b9fb0ad 100644 --- a/features/checkout/ability_to_checkout_as_guest_with_a_registered_email.feature +++ b/features/checkout/ability_to_checkout_as_guest_with_a_registered_email.feature @@ -11,7 +11,7 @@ Feature: Checking out as guest with a registered email And the store allows paying offline And there is a customer account "john@example.com" - @ui + @ui @api Scenario: Successfully placing an order Given I have product "PHP T-Shirt" in the cart When I complete addressing step with email "john@example.com" and "United States" based billing address diff --git a/src/Sylius/Behat/Context/Api/Shop/CartContext.php b/src/Sylius/Behat/Context/Api/Shop/CartContext.php index 4303e2f54a..a34bb45020 100644 --- a/src/Sylius/Behat/Context/Api/Shop/CartContext.php +++ b/src/Sylius/Behat/Context/Api/Shop/CartContext.php @@ -269,7 +269,7 @@ final class CartContext implements Context } /** - * @Then /^I don't have access to see the summary of my (previous cart)$/ + * @Then /^I should not have access to the summary of my (previous cart)$/ */ public function iDoNotHaveAccessToSeeTheSummaryOfMyCart(string $tokenValue): void { diff --git a/src/Sylius/Bundle/ApiBundle/CommandHandler/Cart/BlameCartHandler.php b/src/Sylius/Bundle/ApiBundle/CommandHandler/Cart/BlameCartHandler.php index c7be66eed6..56d1f2b553 100644 --- a/src/Sylius/Bundle/ApiBundle/CommandHandler/Cart/BlameCartHandler.php +++ b/src/Sylius/Bundle/ApiBundle/CommandHandler/Cart/BlameCartHandler.php @@ -50,7 +50,7 @@ final class BlameCartHandler implements MessageHandlerInterface throw new \InvalidArgumentException('There is an assigned customer to this cart'); } - $cart->setCustomer($user->getCustomer()); + $cart->setCustomerWithAuthorization($user->getCustomer()); $this->orderProcessor->process($cart); } diff --git a/src/Sylius/Bundle/ApiBundle/CommandHandler/Cart/PickupCartHandler.php b/src/Sylius/Bundle/ApiBundle/CommandHandler/Cart/PickupCartHandler.php index 8bae04f53d..a0057fa939 100644 --- a/src/Sylius/Bundle/ApiBundle/CommandHandler/Cart/PickupCartHandler.php +++ b/src/Sylius/Bundle/ApiBundle/CommandHandler/Cart/PickupCartHandler.php @@ -70,9 +70,9 @@ final class PickupCartHandler implements MessageHandlerInterface $cart->setLocaleCode($this->getLocaleCode($pickupCart->localeCode, $channel)); $cart->setCurrencyCode($currency->getCode()); $cart->setTokenValue($pickupCart->tokenValue ?? $this->generator->generateUriSafeString(10)); + if ($customer !== null) { - $cart->setCustomer($customer); - $cart->setCreatedByGuest(false); + $cart->setCustomerWithAuthorization($customer); } $this->orderManager->persist($cart); diff --git a/src/Sylius/Bundle/ApiBundle/Doctrine/QueryItemExtension/OrderGetMethodItemExtension.php b/src/Sylius/Bundle/ApiBundle/Doctrine/QueryItemExtension/OrderGetMethodItemExtension.php index 74e8966eb4..404e783b80 100644 --- a/src/Sylius/Bundle/ApiBundle/Doctrine/QueryItemExtension/OrderGetMethodItemExtension.php +++ b/src/Sylius/Bundle/ApiBundle/Doctrine/QueryItemExtension/OrderGetMethodItemExtension.php @@ -69,9 +69,9 @@ final class OrderGetMethodItemExtension implements QueryItemExtensionInterface sprintf('%s.customer IS NULL', $rootAlias), $queryBuilder->expr()->andX( sprintf('%s.customer IS NOT NULL', $rootAlias), - sprintf('%s.createdByGuest = true', $rootAlias), - ), - )) + sprintf('%s.createdByGuest = :createdByGuest', $rootAlias) + ) + ))->setParameter('createdByGuest', true) ; return; diff --git a/src/Sylius/Bundle/ApiBundle/Doctrine/QueryItemExtension/OrderMethodsItemExtension.php b/src/Sylius/Bundle/ApiBundle/Doctrine/QueryItemExtension/OrderMethodsItemExtension.php index 71aefa37eb..b9c1e0b4fa 100644 --- a/src/Sylius/Bundle/ApiBundle/Doctrine/QueryItemExtension/OrderMethodsItemExtension.php +++ b/src/Sylius/Bundle/ApiBundle/Doctrine/QueryItemExtension/OrderMethodsItemExtension.php @@ -79,7 +79,14 @@ final class OrderMethodsItemExtension implements QueryItemExtensionInterface $queryBuilder ->leftJoin(sprintf('%s.customer', $rootAlias), 'customer') ->leftJoin('customer.user', 'user') - ->andWhere($queryBuilder->expr()->orX('user IS NULL', sprintf('%s.customer IS NULL', $rootAlias))) + ->andWhere($queryBuilder->expr()->orX( + 'user IS NULL', + sprintf('%s.customer IS NULL', $rootAlias), + $queryBuilder->expr()->andX( + sprintf('%s.customer IS NOT NULL', $rootAlias), + sprintf('%s.createdByGuest = :createdByGuest', $rootAlias) + ) + ))->setParameter('createdByGuest', true) ; if ($operationName !== 'shop_select_payment_method') { diff --git a/src/Sylius/Bundle/ApiBundle/EventListener/ApiCartBlamerListener.php b/src/Sylius/Bundle/ApiBundle/EventListener/ApiCartBlamerListener.php index 8b4272ecad..56bf4a5e0d 100644 --- a/src/Sylius/Bundle/ApiBundle/EventListener/ApiCartBlamerListener.php +++ b/src/Sylius/Bundle/ApiBundle/EventListener/ApiCartBlamerListener.php @@ -45,7 +45,7 @@ final class ApiCartBlamerListener } $cart = $this->getCart(); - if (null === $cart || null !== $cart->getCustomer()) { + if (null === $cart || !$cart->isCreatedByGuest()) { return; } diff --git a/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Cart/BlameCartHandlerSpec.php b/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Cart/BlameCartHandlerSpec.php index f9809b847b..0a31b41878 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Cart/BlameCartHandlerSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Cart/BlameCartHandlerSpec.php @@ -57,7 +57,7 @@ final class BlameCartHandlerSpec extends ObjectBehavior $user->getCustomer()->willReturn($customer); - $cart->setCustomer($customer)->shouldBeCalled(); + $cart->setCustomerWithAuthorization($customer)->shouldBeCalled(); $orderProcessor->process($cart)->shouldBeCalled(); diff --git a/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Cart/PickupCartHandlerSpec.php b/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Cart/PickupCartHandlerSpec.php index 60edeb8488..41bd606709 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Cart/PickupCartHandlerSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/CommandHandler/Cart/PickupCartHandlerSpec.php @@ -87,8 +87,7 @@ final class PickupCartHandlerSpec extends ObjectBehavior $channel->getLocales()->willReturn(new ArrayCollection([$locale->getWrappedObject()])); $cartFactory->createNew()->willReturn($cart); - $cart->setCustomer($customer)->shouldBeCalled(); - $cart->setCreatedByGuest(false)->shouldBeCalled(); + $cart->setCustomerWithAuthorization($customer)->shouldBeCalled(); $cart->setChannel($channel)->shouldBeCalled(); $cart->setCurrencyCode('USD')->shouldBeCalled(); $cart->setLocaleCode('en_US')->shouldBeCalled(); diff --git a/src/Sylius/Bundle/ApiBundle/spec/Doctrine/QueryItemExtension/OrderGetMethodItemExtensionSpec.php b/src/Sylius/Bundle/ApiBundle/spec/Doctrine/QueryItemExtension/OrderGetMethodItemExtensionSpec.php index 9b63ce1ac2..b5759a0f00 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/Doctrine/QueryItemExtension/OrderGetMethodItemExtensionSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/Doctrine/QueryItemExtension/OrderGetMethodItemExtensionSpec.php @@ -60,18 +60,27 @@ final class OrderGetMethodItemExtensionSpec extends ObjectBehavior ->willReturn($expr) ; - $expr - ->andX('o.customer IS NOT NULL', 'o.createdByGuest = true') - ->willReturn('o.customer IS NOT NULL AND o.createdByGuest = true') + $queryBuilder + ->setParameter('createdByGuest', true) + ->shouldBeCalled() + ->willReturn($expr) ; $expr - ->orX('user IS NULL', 'o.customer IS NULL', 'o.customer IS NOT NULL AND o.createdByGuest = true') - ->willReturn('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = true)') + ->andX('o.customer IS NOT NULL', 'o.createdByGuest = :createdByGuest') + ->shouldBeCalled() + ->willReturn('o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest') + ; + + $expr + ->orX('user IS NULL', 'o.customer IS NULL', 'o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest') + ->shouldBeCalled() + ->willReturn('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest)') ; $queryBuilder - ->andWhere('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = true)') + ->andWhere('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest)') + ->shouldBeCalled() ->willReturn($queryBuilder) ; diff --git a/src/Sylius/Bundle/ApiBundle/spec/Doctrine/QueryItemExtension/OrderMethodsItemExtensionSpec.php b/src/Sylius/Bundle/ApiBundle/spec/Doctrine/QueryItemExtension/OrderMethodsItemExtensionSpec.php index 6b4fd4b0c9..9d1c62bb83 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/Doctrine/QueryItemExtension/OrderMethodsItemExtensionSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/Doctrine/QueryItemExtension/OrderMethodsItemExtensionSpec.php @@ -62,14 +62,26 @@ final class OrderMethodsItemExtensionSpec extends ObjectBehavior ->willReturn($expr) ; - $expr - ->orX('user IS NULL', sprintf('%s.customer IS NULL', 'o')) + $queryBuilder + ->setParameter('createdByGuest', true) ->shouldBeCalled() - ->willReturn(sprintf('user IS NULL OR %s.customer IS NULL', 'o')) + ->willReturn($expr) + ; + + $expr + ->andX('o.customer IS NOT NULL', 'o.createdByGuest = :createdByGuest') + ->shouldBeCalled() + ->willReturn('o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest') + ; + + $expr + ->orX('user IS NULL', 'o.customer IS NULL', 'o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest') + ->shouldBeCalled() + ->willReturn('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest)') ; $queryBuilder - ->andWhere(sprintf('user IS NULL OR %s.customer IS NULL', 'o')) + ->andWhere('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest)') ->shouldBeCalled() ->willReturn($queryBuilder) ; @@ -108,7 +120,7 @@ final class OrderMethodsItemExtensionSpec extends ObjectBehavior $userContext->getUser()->willReturn(null); $queryBuilder - ->leftJoin(sprintf('%s.customer', 'o'), 'customer') + ->leftJoin('o.customer', 'customer') ->shouldBeCalled() ->willReturn($queryBuilder) ; @@ -119,20 +131,33 @@ final class OrderMethodsItemExtensionSpec extends ObjectBehavior ->willReturn($queryBuilder) ; + $queryBuilder ->expr() ->shouldBeCalled() ->willReturn($expr) ; - $expr - ->orX('user IS NULL', sprintf('%s.customer IS NULL', 'o')) + $queryBuilder + ->setParameter('createdByGuest', true) ->shouldBeCalled() - ->willReturn(sprintf('user IS NULL OR %s.customer IS NULL', 'o')) + ->willReturn($expr) + ; + + $expr + ->andX('o.customer IS NOT NULL', 'o.createdByGuest = :createdByGuest') + ->shouldBeCalled() + ->willReturn('o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest') + ; + + $expr + ->orX('user IS NULL', 'o.customer IS NULL', 'o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest') + ->shouldBeCalled() + ->willReturn('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest)') ; $queryBuilder - ->andWhere(sprintf('user IS NULL OR %s.customer IS NULL', 'o')) + ->andWhere('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest)') ->shouldBeCalled() ->willReturn($queryBuilder) ; @@ -188,14 +213,26 @@ final class OrderMethodsItemExtensionSpec extends ObjectBehavior ->willReturn($expr) ; - $expr - ->orX('user IS NULL', sprintf('%s.customer IS NULL', 'o')) + $queryBuilder + ->setParameter('createdByGuest', true) ->shouldBeCalled() - ->willReturn(sprintf('user IS NULL OR %s.customer IS NULL', 'o')) + ->willReturn($expr) + ; + + $expr + ->andX('o.customer IS NOT NULL', 'o.createdByGuest = :createdByGuest') + ->shouldBeCalled() + ->willReturn('o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest') + ; + + $expr + ->orX('user IS NULL', 'o.customer IS NULL', 'o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest') + ->shouldBeCalled() + ->willReturn('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest)') ; $queryBuilder - ->andWhere(sprintf('user IS NULL OR %s.customer IS NULL', 'o')) + ->andWhere('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest)') ->shouldBeCalled() ->willReturn($queryBuilder) ; @@ -371,15 +408,32 @@ final class OrderMethodsItemExtensionSpec extends ObjectBehavior ->willReturn($queryBuilder) ; - $queryBuilder->expr()->shouldBeCalled()->willReturn($expr); - $expr - ->orX('user IS NULL', sprintf('%s.customer IS NULL', 'o')) + $queryBuilder + ->expr() ->shouldBeCalled() - ->willReturn(sprintf('user IS NULL OR %s.customer IS NULL', 'o')) + ->willReturn($expr) ; $queryBuilder - ->andWhere(sprintf('user IS NULL OR %s.customer IS NULL', 'o')) + ->setParameter('createdByGuest', true) + ->shouldBeCalled() + ->willReturn($expr) + ; + + $expr + ->andX('o.customer IS NOT NULL', 'o.createdByGuest = :createdByGuest') + ->shouldBeCalled() + ->willReturn('o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest') + ; + + $expr + ->orX('user IS NULL', 'o.customer IS NULL', 'o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest') + ->shouldBeCalled() + ->willReturn('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest)') + ; + + $queryBuilder + ->andWhere('user IS NULL OR o.customer IS NULL OR (o.customer IS NOT NULL AND o.createdByGuest = :createdByGuest)') ->shouldBeCalled() ->willReturn($queryBuilder) ; diff --git a/src/Sylius/Bundle/ApiBundle/spec/EventListener/ApiCartBlamerListenerSpec.php b/src/Sylius/Bundle/ApiBundle/spec/EventListener/ApiCartBlamerListenerSpec.php index 37b04506ca..e3e986ec58 100644 --- a/src/Sylius/Bundle/ApiBundle/spec/EventListener/ApiCartBlamerListenerSpec.php +++ b/src/Sylius/Bundle/ApiBundle/spec/EventListener/ApiCartBlamerListenerSpec.php @@ -75,7 +75,7 @@ final class ApiCartBlamerListenerSpec extends ObjectBehavior ): void { $sectionResolver->getSection()->willReturn($shopApiOrdersSubSectionSection); $cartContext->getCart()->willReturn($cart); - $cart->getCustomer()->willReturn(null); + $cart->isCreatedByGuest()->willReturn(true); $token->getUser()->willReturn($user); $user->getCustomer()->willReturn($customer); @@ -104,7 +104,7 @@ final class ApiCartBlamerListenerSpec extends ObjectBehavior ): void { $sectionResolver->getSection()->willReturn($shopApiOrdersSubSectionSection); $cartContext->getCart()->willReturn($cart); - $cart->getCustomer()->willReturn($customer); + $cart->isCreatedByGuest()->willReturn(false); $cart->setCustomer(Argument::any())->shouldNotBeCalled(); diff --git a/src/Sylius/Bundle/ShopBundle/EventListener/ShopCartBlamerListener.php b/src/Sylius/Bundle/ShopBundle/EventListener/ShopCartBlamerListener.php index 78b6c0740c..f4d51ae7f8 100644 --- a/src/Sylius/Bundle/ShopBundle/EventListener/ShopCartBlamerListener.php +++ b/src/Sylius/Bundle/ShopBundle/EventListener/ShopCartBlamerListener.php @@ -65,8 +65,7 @@ final class ShopCartBlamerListener return; } - $cart->setCustomer($user->getCustomer()); - $cart->setCreatedByGuest(false); + $cart->setCustomerWithAuthorization($user->getCustomer()); } /** diff --git a/src/Sylius/Bundle/ShopBundle/spec/EventListener/ShopCartBlamerListenerSpec.php b/src/Sylius/Bundle/ShopBundle/spec/EventListener/ShopCartBlamerListenerSpec.php index ac7b93bad7..d30a22c307 100644 --- a/src/Sylius/Bundle/ShopBundle/spec/EventListener/ShopCartBlamerListenerSpec.php +++ b/src/Sylius/Bundle/ShopBundle/spec/EventListener/ShopCartBlamerListenerSpec.php @@ -88,8 +88,7 @@ final class ShopCartBlamerListenerSpec extends ObjectBehavior $userEvent->getUser()->willReturn($user); $user->getCustomer()->willReturn($customer); - $cart->setCustomer($customer)->shouldBeCalled(); - $cart->setCreatedByGuest(false)->shouldBeCalled(); + $cart->setCustomerWithAuthorization($customer)->shouldBeCalled(); $this->onImplicitLogin($userEvent); } @@ -110,8 +109,7 @@ final class ShopCartBlamerListenerSpec extends ObjectBehavior $token->getUser()->willReturn($user); $user->getCustomer()->willReturn($customer); - $cart->setCustomer($customer)->shouldBeCalled(); - $cart->setCreatedByGuest(false)->shouldBeCalled(); + $cart->setCustomerWithAuthorization($customer)->shouldBeCalled(); $this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject())); } @@ -129,8 +127,7 @@ final class ShopCartBlamerListenerSpec extends ObjectBehavior $cartContext->getCart()->willReturn($cart); $cart->getCustomer()->willReturn($customer); - $cart->setCustomer(Argument::any())->shouldNotBeCalled(); - $cart->setCreatedByGuest(false)->shouldNotBeCalled(); + $cart->setCustomerWithAuthorization(Argument::any())->shouldNotBeCalled(); $this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject())); } @@ -147,8 +144,7 @@ final class ShopCartBlamerListenerSpec extends ObjectBehavior $cartContext->getCart()->willReturn($cart); $token->getUser()->willReturn('anon.'); - $cart->setCustomer(Argument::any())->shouldNotBeCalled(); - $cart->setCreatedByGuest(false)->shouldNotBeCalled(); + $cart->setCustomerWithAuthorization(Argument::any())->shouldNotBeCalled(); $this->onInteractiveLogin(new InteractiveLoginEvent($request->getWrappedObject(), $token->getWrappedObject())); } diff --git a/src/Sylius/Component/Core/Cart/Context/ShopBasedCartContext.php b/src/Sylius/Component/Core/Cart/Context/ShopBasedCartContext.php index 055d39e16d..fbd6d5d0f1 100644 --- a/src/Sylius/Component/Core/Cart/Context/ShopBasedCartContext.php +++ b/src/Sylius/Component/Core/Cart/Context/ShopBasedCartContext.php @@ -90,11 +90,7 @@ final class ShopBasedCartContext implements CartContextInterface private function setCustomerAndAddressOnCart(OrderInterface $cart, CustomerInterface $customer): void { - $cart->setCustomer($customer); - - if ($this->createdByGuestFlagResolver !== null) { - $cart->setCreatedByGuest($this->createdByGuestFlagResolver->resolveFlag()); - } + $this->setCustomer($cart, $customer); $defaultAddress = $customer->getDefaultAddress(); if (null !== $defaultAddress) { @@ -103,4 +99,15 @@ final class ShopBasedCartContext implements CartContextInterface $cart->setBillingAddress($clonedAddress); } } + + private function setCustomer(OrderInterface $cart, CustomerInterface $customer): void + { + if ($this->createdByGuestFlagResolver !== null && !$this->createdByGuestFlagResolver->resolveFlag()) { + $cart->setCustomerWithAuthorization($customer); + + return; + } + + $cart->setCustomer($customer); + } } diff --git a/src/Sylius/Component/Core/Model/Order.php b/src/Sylius/Component/Core/Model/Order.php index aef899e837..6b6f31a685 100644 --- a/src/Sylius/Component/Core/Model/Order.php +++ b/src/Sylius/Component/Core/Model/Order.php @@ -114,6 +114,12 @@ class Order extends BaseOrder implements OrderInterface $this->customer = $customer; } + public function setCustomerWithAuthorization(?BaseCustomerInterface $customer): void + { + $this->setCustomer($customer); + $this->createdByGuest = false; + } + public function getChannel(): ?BaseChannelInterface { return $this->channel; @@ -460,11 +466,16 @@ class Order extends BaseOrder implements OrderInterface return $total; } - public function getCreatedByGuest(): bool + public function isCreatedByGuest(): bool { return $this->createdByGuest; } + public function getCreatedByGuest(): bool + { + return $this->isCreatedByGuest(); + } + public function setCreatedByGuest(bool $createdByGuest): void { $this->createdByGuest = $createdByGuest; diff --git a/src/Sylius/Component/Core/Model/OrderInterface.php b/src/Sylius/Component/Core/Model/OrderInterface.php index 84ceaac21c..3302d12e38 100644 --- a/src/Sylius/Component/Core/Model/OrderInterface.php +++ b/src/Sylius/Component/Core/Model/OrderInterface.php @@ -17,6 +17,7 @@ use Doctrine\Common\Collections\Collection; use Sylius\Component\Channel\Model\ChannelAwareInterface; use Sylius\Component\Channel\Model\ChannelInterface as BaseChannelInterface; use Sylius\Component\Customer\Model\CustomerAwareInterface; +use Sylius\Component\Customer\Model\CustomerInterface as BaseCustomerInterface; use Sylius\Component\Order\Model\OrderInterface as BaseOrderInterface; use Sylius\Component\Payment\Model\PaymentsSubjectInterface; use Sylius\Component\Promotion\Model\CountablePromotionSubjectInterface; @@ -113,6 +114,10 @@ interface OrderInterface extends public function setCustomerIp(?string $customerIp): void; + public function setCustomerWithAuthorization(?BaseCustomerInterface $customer): void; + + public function isCreatedByGuest(): bool; + public function getCreatedByGuest(): bool; public function setCreatedByGuest(bool $createdByGuest): void; diff --git a/src/Sylius/Component/Core/spec/Cart/Context/ShopBasedCartContextSpec.php b/src/Sylius/Component/Core/spec/Cart/Context/ShopBasedCartContextSpec.php index 5aae388f31..a5daebc44d 100644 --- a/src/Sylius/Component/Core/spec/Cart/Context/ShopBasedCartContextSpec.php +++ b/src/Sylius/Component/Core/spec/Cart/Context/ShopBasedCartContextSpec.php @@ -189,7 +189,7 @@ final class ShopBasedCartContextSpec extends ObjectBehavior $this->getCart()->shouldReturn($secondCart); } - function it_sets_created_by_guest_flag_as_false_if_order_is_created_by_logged_in_customer( + function it_creates_order_for_authorized_user( CartContextInterface $cartContext, ShopperContextInterface $shopperContext, CreatedByGuestFlagResolverInterface $createdByGuestFlagResolver, @@ -202,8 +202,6 @@ final class ShopBasedCartContextSpec extends ObjectBehavior $createdByGuestFlagResolver->resolveFlag()->willReturn(false); - $cart->setCreatedByGuest(false)->shouldBeCalled(); - $cartContext->getCart()->shouldBeCalledTimes(1)->willReturn($cart); $shopperContext->getChannel()->willReturn($channel); @@ -217,40 +215,7 @@ final class ShopBasedCartContextSpec extends ObjectBehavior $cart->setChannel($channel)->shouldBeCalled(); $cart->setCurrencyCode('PLN')->shouldBeCalled(); $cart->setLocaleCode('pl')->shouldBeCalled(); - $cart->setCustomer($customer)->shouldBeCalled(); - - $this->getCart()->shouldReturn($cart); - } - - function it_sets_created_by_guest_flag_as_true_if_order_is_created_by_anonymous_user( - CartContextInterface $cartContext, - ShopperContextInterface $shopperContext, - CreatedByGuestFlagResolverInterface $createdByGuestFlagResolver, - OrderInterface $cart, - ChannelInterface $channel, - CurrencyInterface $currency, - CustomerInterface $customer, - ): void { - $this->beConstructedWith($cartContext, $shopperContext, $createdByGuestFlagResolver); - - $createdByGuestFlagResolver->resolveFlag()->willReturn(true); - - $cart->setCreatedByGuest(true)->shouldBeCalled(); - - $cartContext->getCart()->shouldBeCalledTimes(1)->willReturn($cart); - - $shopperContext->getChannel()->willReturn($channel); - $shopperContext->getLocaleCode()->willReturn('pl'); - $shopperContext->getCustomer()->willReturn($customer); - $customer->getDefaultAddress()->willReturn(null); - - $channel->getBaseCurrency()->willReturn($currency); - $currency->getCode()->willReturn('PLN'); - - $cart->setChannel($channel)->shouldBeCalled(); - $cart->setCurrencyCode('PLN')->shouldBeCalled(); - $cart->setLocaleCode('pl')->shouldBeCalled(); - $cart->setCustomer($customer)->shouldBeCalled(); + $cart->setCustomerWithAuthorization($customer)->shouldBeCalled(); $this->getCart()->shouldReturn($cart); } diff --git a/src/Sylius/Component/Core/spec/Model/OrderSpec.php b/src/Sylius/Component/Core/spec/Model/OrderSpec.php index 7eb941df35..f77c89beab 100644 --- a/src/Sylius/Component/Core/spec/Model/OrderSpec.php +++ b/src/Sylius/Component/Core/spec/Model/OrderSpec.php @@ -51,12 +51,30 @@ final class OrderSpec extends ObjectBehavior $this->getCustomer()->shouldReturn(null); } - function its_allows_defining_customer(CustomerInterface $customer): void + function it_allows_defining_customer(CustomerInterface $customer): void { $this->setCustomer($customer); $this->getCustomer()->shouldReturn($customer); } + function it_allows_defining_authorized_customer(CustomerInterface $customer): void + { + $this->setCustomerWithAuthorization($customer); + $this->getCustomer()->shouldReturn($customer); + $this->isCreatedByGuest()->shouldReturn(false); + } + + function its_created_by_guest_customer_by_default(): void + { + $this->isCreatedByGuest()->shouldReturn(true); + } + + function it_allows_to_mutate_create_by_guest_field(): void + { + $this->setCreatedByGuest(false); + $this->isCreatedByGuest()->shouldReturn(false); + } + function its_customer_can_be_nullable(): void { $this->setCustomer(null);