Prevent adding a zone of type zone to itself

This commit is contained in:
Tobias Wojtylak 2017-06-30 16:01:34 +02:00
parent 4bb2b6602c
commit 2cd7b2d96f
4 changed files with 37 additions and 5 deletions

View file

@ -61,3 +61,9 @@ Feature: Editing a zone
Given the store has a zone "European Union" with code "EU"
When I want to modify the zone named "European Union"
Then the code field should be disabled
@ui
Scenario: Not seeing zone itself in member select when editing a zone of type zone
Given the store has a zone "European Union" with code "EU"
When I want to modify the zone named "European Union"
Then I can not add a zone "European Union"

View file

@ -12,6 +12,7 @@
namespace Sylius\Behat\Context\Ui\Admin;
use Behat\Behat\Context\Context;
use Behat\Mink\Exception\ElementNotFoundException;
use Sylius\Behat\NotificationType;
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
use Sylius\Behat\Page\Admin\Zone\CreatePageInterface;
@ -354,4 +355,16 @@ final class ManagingZonesContext implements Context
Assert::true($this->updatePage->hasMember($zoneMember));
}
/**
* @Then /^I can not add a(?: country| province| zone) "([^"]+)"$/
*/
public function iCanNotAddAZoneMember($name)
{
$member = null;
try {
$member = $this->createPage->chooseMember($name);
} catch (ElementNotFoundException $exception) {}
Assert::isEmpty($member);
}
}

View file

@ -53,8 +53,13 @@ final class ZoneCodeChoiceType extends AbstractType
{
$resolver
->setDefaults([
'choice_filter' => null,
'choices' => function (Options $options) {
return $this->zoneRepository->findAll();
$zones = $this->zoneRepository->findAll();
if ($options['choice_filter']) {
$zones = array_filter($zones, $options['choice_filter']);
}
return $zones;
},
'choice_value' => 'code',
'choice_label' => 'name',

View file

@ -75,12 +75,20 @@ final class ZoneType extends AbstractResourceType
/** @var ZoneInterface $zone */
$zone = $event->getData();
$entryOptions = [
'entry_type' => $this->getZoneMemberEntryType($zone->getType()),
'entry_options' => $this->getZoneMemberEntryOptions($zone->getType()),
];
if ($zone->getType() === ZoneInterface::TYPE_ZONE) {
$entryOptions['entry_options']['choice_filter'] = function(ZoneInterface $subZone) use ($zone){
return $zone->getId() !== $subZone->getId();
};
}
$event->getForm()->add('members', CollectionType::class, [
'entry_type' => ZoneMemberType::class,
'entry_options' => [
'entry_type' => $this->getZoneMemberEntryType($zone->getType()),
'entry_options' => $this->getZoneMemberEntryOptions($zone->getType()),
],
'entry_options' => $entryOptions,
'button_add_label' => 'sylius.form.zone.add_member',
'allow_add' => true,
'allow_delete' => true,