Add new method to TaxonDeletionListener

This commit is contained in:
TheMilek 2022-11-22 09:22:52 +01:00
parent 3de0a9a812
commit c52baf29da
No known key found for this signature in database
GPG key ID: 2E44205E7374692F
5 changed files with 38 additions and 1 deletions

View file

@ -6,6 +6,7 @@ Feature: Deleting a taxon
Background: Background:
Given I am logged in as an administrator Given I am logged in as an administrator
And the store operates on a channel named "Web Store"
@ui @javascript @ui @javascript
Scenario: Deleted taxon should disappear from the registry Scenario: Deleted taxon should disappear from the registry
@ -27,7 +28,16 @@ Feature: Deleting a taxon
@ui @javascript @ui @javascript
Scenario: Being unable to delete a menu taxon of a channel Scenario: Being unable to delete a menu taxon of a channel
Given the store classifies its products as "T-Shirts" and "Caps" 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" And channel "Web Store" has menu taxon "Caps"
When I try to delete taxon named "Caps" When I try to delete taxon named "Caps"
Then I should be notified that I cannot delete a menu taxon of any channel 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

View file

@ -267,6 +267,10 @@ final class ManagingTaxonsContext implements Context
*/ */
public function taxonNamedShouldNotBeAdded($name) public function taxonNamedShouldNotBeAdded($name)
{ {
if (!$this->createPage->isOpen()) {
$this->createPage->open();
}
Assert::same($this->createPage->countTaxonsByName($name), 0); Assert::same($this->createPage->countTaxonsByName($name), 0);
} }

View file

@ -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);
}
}
} }

View file

@ -93,6 +93,7 @@
<argument type="service" id="sylius.promotion_rule_updater.has_taxon" /> <argument type="service" id="sylius.promotion_rule_updater.has_taxon" />
<tag name="kernel.event_listener" event="sylius.taxon.pre_delete" method="protectFromRemovingMenuTaxon" /> <tag name="kernel.event_listener" event="sylius.taxon.pre_delete" method="protectFromRemovingMenuTaxon" />
<tag name="kernel.event_listener" event="sylius.taxon.post_delete" method="removeTaxonFromPromotionRules" /> <tag name="kernel.event_listener" event="sylius.taxon.post_delete" method="removeTaxonFromPromotionRules" />
<tag name="kernel.event_listener" event="sylius.taxon.pre_delete" method="handleRemovingRootTaxonAtPositionZero" />
</service> </service>
</services> </services>
</container> </container>

View file

@ -123,4 +123,15 @@ final class TaxonDeletionListenerSpec extends ObjectBehavior
$this->removeTaxonFromPromotionRules($event); $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);
}
} }