From c52baf29dacfc1c3a93c46b0173a97ef66d167f3 Mon Sep 17 00:00:00 2001 From: TheMilek Date: Tue, 22 Nov 2022 09:22:52 +0100 Subject: [PATCH] Add new method to TaxonDeletionListener --- .../taxonomy/managing_taxons/deleting_taxon.feature | 12 +++++++++++- .../Behat/Context/Ui/Admin/ManagingTaxonsContext.php | 4 ++++ .../EventListener/TaxonDeletionListener.php | 11 +++++++++++ .../Resources/config/services/listeners.xml | 1 + .../spec/EventListener/TaxonDeletionListenerSpec.php | 11 +++++++++++ 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/features/taxonomy/managing_taxons/deleting_taxon.feature b/features/taxonomy/managing_taxons/deleting_taxon.feature index e797b434a5..99b89bdd96 100644 --- a/features/taxonomy/managing_taxons/deleting_taxon.feature +++ b/features/taxonomy/managing_taxons/deleting_taxon.feature @@ -6,6 +6,7 @@ Feature: Deleting a taxon Background: Given I am logged in as an administrator + And the store operates on a channel named "Web Store" @ui @javascript Scenario: Deleted taxon should disappear from the registry @@ -27,7 +28,16 @@ Feature: Deleting a taxon @ui @javascript Scenario: Being unable to delete a menu taxon of a channel Given the store classifies its products as "T-Shirts" and "Caps" - And the store operates on a channel named "Web Store" And channel "Web Store" has menu taxon "Caps" When I try to delete taxon named "Caps" Then I should be notified that I cannot delete a menu taxon of any channel + + @ui @javascript + Scenario: Deleting root taxon above menu taxon + Given the store has "Main Category" taxonomy + And the store has "Clothes Category" taxonomy + And channel "Web Store" has menu taxon "Main Category" + When I want to see all taxons in store + And I move down "Main Category" taxon + And I delete taxon named "Clothes Category" + Then the taxon named "Clothes Category" should no longer exist in the registry diff --git a/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php b/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php index f0338a48f2..f457a317b4 100644 --- a/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php +++ b/src/Sylius/Behat/Context/Ui/Admin/ManagingTaxonsContext.php @@ -267,6 +267,10 @@ final class ManagingTaxonsContext implements Context */ public function taxonNamedShouldNotBeAdded($name) { + if (!$this->createPage->isOpen()) { + $this->createPage->open(); + } + Assert::same($this->createPage->countTaxonsByName($name), 0); } diff --git a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php index 35693ed71e..af3f94d8d2 100644 --- a/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php +++ b/src/Sylius/Bundle/CoreBundle/EventListener/TaxonDeletionListener.php @@ -68,4 +68,15 @@ final class TaxonDeletionListener ]); } } + + public function handleRemovingRootTaxonAtPositionZero(GenericEvent $event): void + { + /** @var TaxonInterface $taxon */ + $taxon = $event->getSubject(); + Assert::isInstanceOf($taxon, TaxonInterface::class); + + if ($taxon->getPosition() === 0) { + $taxon->setPosition(-1); + } + } } diff --git a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml index f28993ae14..91f5d9269d 100644 --- a/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml +++ b/src/Sylius/Bundle/CoreBundle/Resources/config/services/listeners.xml @@ -93,6 +93,7 @@ + diff --git a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php index 240852f231..d1f00b723c 100644 --- a/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php +++ b/src/Sylius/Bundle/CoreBundle/spec/EventListener/TaxonDeletionListenerSpec.php @@ -123,4 +123,15 @@ final class TaxonDeletionListenerSpec extends ObjectBehavior $this->removeTaxonFromPromotionRules($event); } + + function it_changes_taxon_position_to_minus_one_if_base_position_is_zero( + GenericEvent $event, + TaxonInterface $taxon, + ): void { + $event->getSubject()->willReturn($taxon); + $taxon->getPosition()->willReturn(0); + $taxon->setPosition(-1)->shouldBeCalled(); + + $this->handleRemovingRootTaxonAtPositionZero($event); + } }