diff --git a/features/order/customer_deletion.feature b/features/order/customer_deletion.feature new file mode 100644 index 0000000000..c40c9c30d0 --- /dev/null +++ b/features/order/customer_deletion.feature @@ -0,0 +1,27 @@ +@order @order_customer_deletion +Feature: Create a new order + + Background: + Given the site operates on a store in "Austria" + And the site operates on locale "en" + And the site has a tax rate "AT" with "20%" rate + And the site has a tax rule group "AT" + And the tax rule group has a tax rule for country "Austria" with tax rate "AT" + And the site has a product "T-Shirt" priced at 2000 + And the product has the tax rule group "AT" + And the site has a customer "some-customer@something.com" + And the customer "some-customer@something.com" has an address with country "Austria", "4600", "Wels", "Freiung", "9-11/N3" + + And the cart belongs to customer "some-customer@something.com" + + Scenario: Create a new order and delete the customer + Given I add the product "T-Shirt" to my cart + And the cart ships to customer "some-customer@something.com" address with postcode "4600" + And the cart invoices to customer "some-customer@something.com" address with postcode "4600" + And I create an order from my cart + Then It should throw an error deleting the customer "some-customer@something.com" + + Scenario: Just delete an Customer Account + Given the site has a customer "some-other@something.com" + And the customer "some-other@something.com" has an address with country "Austria", "4600", "Wels", "Freiung", "9-11/N3" + Then It should not throw an error deleting the customer "some-other@something.com" \ No newline at end of file diff --git a/src/CoreShop/Behat/Context/Domain/CustomerContext.php b/src/CoreShop/Behat/Context/Domain/CustomerContext.php index 64150d7956..1c095aaec3 100644 --- a/src/CoreShop/Behat/Context/Domain/CustomerContext.php +++ b/src/CoreShop/Behat/Context/Domain/CustomerContext.php @@ -57,4 +57,22 @@ public function iShouldBeLoggedInWithEmail(CustomerInterface $customer) ) ); } + + /** + * @Then /^It should throw an error deleting the (customer "[^"]+")$/ + */ + public function itShouldThrowAnErrorDeletingCustomer(CustomerInterface $customer) + { + Assert::throws(function() use ($customer) { + $customer->delete(); + }); + } + + /** + * @Then /^It should not throw an error deleting the (customer "[^"]+")$/ + */ + public function itShouldNotThrowAnErrorDeletingTheCustomer(CustomerInterface $customer) + { + $customer->delete(); + } } diff --git a/src/CoreShop/Behat/Resources/config/suites/domain/order.yml b/src/CoreShop/Behat/Resources/config/suites/domain/order.yml index 241885ee5f..8927ab5ddc 100644 --- a/src/CoreShop/Behat/Resources/config/suites/domain/order.yml +++ b/src/CoreShop/Behat/Resources/config/suites/domain/order.yml @@ -39,6 +39,7 @@ default: - coreshop.behat.context.setup.shipping - coreshop.behat.context.domain.order + - coreshop.behat.context.domain.customer - coreshop.behat.context.domain.notification_rule filters: tags: "@order" \ No newline at end of file diff --git a/src/CoreShop/Bundle/CoreBundle/EventListener/CustomerOrderDeletionListener.php b/src/CoreShop/Bundle/CoreBundle/EventListener/CustomerOrderDeletionListener.php new file mode 100644 index 0000000000..ca2f51a3b2 --- /dev/null +++ b/src/CoreShop/Bundle/CoreBundle/EventListener/CustomerOrderDeletionListener.php @@ -0,0 +1,72 @@ +orderRepository = $orderRepository; + } + + /** + * @param DataObjectDeleteInfoEvent $event + */ + public function checkCustomerDeletionAllowed(DataObjectDeleteInfoEvent $event) + { + $object = $event->getObject(); + + if (!$object instanceof CustomerInterface) { + return; + } + + $hasOrders = $this->orderRepository->hasCustomerOrders($object); + + if ($hasOrders) { + $event->setDeletionAllowed(false); + $event->setReason('Cannot delete a customer with orders'); + } + } + + /** + * @param DataObjectEvent $event + */ + public function checkCustomerOrdersBeforeDeletion(DataObjectEvent $event) + { + $object = $event->getObject(); + + if (!$object instanceof CustomerInterface) { + return; + } + + $hasOrders = $this->orderRepository->hasCustomerOrders($object); + + if ($hasOrders) { + throw new \InvalidArgumentException(sprintf('Cannot delete a customer with orders')); + } + } +} diff --git a/src/CoreShop/Bundle/CoreBundle/Resources/config/services/listeners.yml b/src/CoreShop/Bundle/CoreBundle/Resources/config/services/listeners.yml index 44f6389912..1b4ed57e66 100644 --- a/src/CoreShop/Bundle/CoreBundle/Resources/config/services/listeners.yml +++ b/src/CoreShop/Bundle/CoreBundle/Resources/config/services/listeners.yml @@ -69,4 +69,12 @@ services: - '@pimcore.http.request_helper' - '@session' tags: - - { name: kernel.event_listener, event: kernel.request, method: checkCartAvailability, priority: 7 } \ No newline at end of file + - { name: kernel.event_listener, event: kernel.request, method: checkCartAvailability, priority: 7 } + + coreshop.listener.customer_odrer_deletion: + class: CoreShop\Bundle\CoreBundle\EventListener\CustomerOrderDeletionListener + arguments: + - '@coreshop.repository.order' + tags: + - { name: kernel.event_listener, event: pimcore.dataobject.preDelete, method: checkCustomerOrdersBeforeDeletion } + - { name: kernel.event_listener, event: pimcore.dataobject.deleteInfo, method: checkCustomerDeletionAllowed } \ No newline at end of file diff --git a/src/CoreShop/Bundle/OrderBundle/Pimcore/Repository/OrderRepository.php b/src/CoreShop/Bundle/OrderBundle/Pimcore/Repository/OrderRepository.php index b95c4dea18..422ccfc140 100644 --- a/src/CoreShop/Bundle/OrderBundle/Pimcore/Repository/OrderRepository.php +++ b/src/CoreShop/Bundle/OrderBundle/Pimcore/Repository/OrderRepository.php @@ -35,6 +35,17 @@ public function findByCustomer(CustomerInterface $customer) return $list->getObjects(); } + /** + * {@inheritdoc} + */ + public function hasCustomerOrders(CustomerInterface $customer) + { + $list = $this->getList(); + $list->setCondition('customer__id = ?', [$customer->getId()]); + + return $list->getTotalCount() > 0; + } + /** * {@inheritdoc} */ diff --git a/src/CoreShop/Component/Order/Repository/OrderRepositoryInterface.php b/src/CoreShop/Component/Order/Repository/OrderRepositoryInterface.php index 316be1ae1d..d7638fb3b1 100644 --- a/src/CoreShop/Component/Order/Repository/OrderRepositoryInterface.php +++ b/src/CoreShop/Component/Order/Repository/OrderRepositoryInterface.php @@ -25,6 +25,13 @@ interface OrderRepositoryInterface extends PimcoreRepositoryInterface */ public function findByCustomer(CustomerInterface $customer); + /** + * @param CustomerInterface $customer + * + * @return boolean + */ + public function hasCustomerOrders(CustomerInterface $customer); + /** * @param int $days *