Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Attribute] Configuration validation of text and select attributes #8755

Merged
merged 3 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Feature: Adding a new select product attribute
And I name it "Mug material" in "English (United States)"
And I add value "Banana Skin"
And I also add value "Plastic"
And I check multiple option
And I add it
Then I should be notified that it has been successfully created
And the select attribute "Mug material" should appear in the store
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@managing_product_attributes
Feature: Select product attribute validation
In order to avoid making mistakes when managing select product attributes
As an Administrator
I want to be prevented from adding it with wrong configuration

Background:
Given the store is available in "English (United States)"
And I am logged in as an administrator

@ui @javascript
Scenario: Trying to add a new select product attribute with wrong max entries value
When I want to create a new select product attribute
And I name it "Mug material" in "English (United States)"
And I specify its code as "mug_material"
And I add value "Banana Skin"
And I also add value "Orange Skin"
And I check multiple option
And I specify its min entries value as 8
And I specify its max entries value as 6
And I try to add it
Then I should be notified that max entries value must be greater or equal to the min entries value
And the attribute with code "mug_material" should not appear in the store

@ui @javascript
Scenario: Trying to add a new select product attribute with wrong min entries value
When I want to create a new select product attribute
And I name it "Mug material" in "English (United States)"
And I specify its code as "mug_material"
And I add value "Banana Skin"
And I also add value "Orange Skin"
And I check multiple option
And I specify its min entries value as 4
And I specify its max entries value as 6
And I try to add it
Then I should be notified that min entries value must be lower or equal to the number of added choices
And the attribute with code "mug_material" should not appear in the store

@ui @javascript
Scenario: Trying to add a new select product attribute with specified entries values but without multiple option
When I want to create a new select product attribute
And I name it "Mug material" in "English (United States)"
And I specify its code as "mug_material"
And I add value "Banana Skin"
And I also add value "Orange Skin"
And I do not check multiple option
And I specify its min entries value as 4
And I specify its max entries value as 6
And I try to add it
Then I should be notified that multiple must be true if min or max entries values are specified
And the attribute with code "mug_material" should not appear in the store
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ Feature: Text product attribute validation

@ui
Scenario: Trying to add a new text product attribute without name
Given I want to create a new text product attribute
When I specify its code as "t_shirt_with_cotton"
When I want to create a new text product attribute
And I specify its code as "t_shirt_with_cotton"
But I do not name it
And I try to add it
Then I should be notified that name is required
And the attribute with code "t_shirt_with_cotton" should not appear in the store

@ui
Scenario: Trying to add a new text product attribute without code
Given I want to create a new text product attribute
When I name it "T-shirt brand" in "English (United States)"
When I want to create a new text product attribute
And I name it "T-shirt brand" in "English (United States)"
But I do not specify its code
And I try to add it
Then I should be notified that code is required
Expand All @@ -29,9 +29,20 @@ Feature: Text product attribute validation
@ui
Scenario: Trying to remove name for existing text product attribute
Given the store has a text product attribute "T-shirt cotton brand"
And I want to edit this product attribute
When I specify its code as "t_shirt_with_cotton"
When I want to edit this product attribute
And I specify its code as "t_shirt_with_cotton"
And I remove its name from "English (United States)" translation
And I try to save my changes
Then I should be notified that name is required
And the attribute with code "t_shirt_with_cotton" should not appear in the store

@ui
Scenario: Trying to add a new text product attribute with wrong configuration
When I want to create a new text product attribute
And I name it "T-shirt brand" in "English (United States)"
And I specify its code as "t_shirt_brand"
And I specify its min length as 8
And I specify its max length as 6
And I try to add it
Then I should be notified that max length must be greater or equal to the min length
And the attribute with code "t_shirt_brand" should not appear in the store
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,40 @@ function () use ($productAttribute, $oldValue, $newValue) {
);
}

/**
* @When I specify its min length as :min
* @When I specify its min entries value as :min
*/
public function iSpecifyItsMinValueAs(int $min): void
{
$this->createPage->specifyMinValue($min);
}

/**
* @When I specify its max length as :max
* @When I specify its max entries value as :max
*/
public function iSpecifyItsMaxLengthAs(int $max): void
{
$this->createPage->specifyMaxValue($max);
}

/**
* @When I check multiple option
*/
public function iCheckMultipleOption(): void
{
$this->createPage->checkMultiple();
}

/**
* @When I do not check multiple option
*/
public function iDoNotCheckMultipleOption(): void
{
// Intentionally left blank to fulfill context expectation
}

/**
* @Then /^I should see (\d+) product attributes in the list$/
*/
Expand Down Expand Up @@ -327,15 +361,70 @@ public function theSelectAttributeShouldHaveValue(ProductAttributeInterface $pro
Assert::true($this->updatePage->hasAttributeValue($value));
}

/**
* @Then I should be notified that max length must be greater or equal to the min length
*/
public function iShouldBeNotifiedThatMaxLengthMustBeGreaterOrEqualToTheMinLength(): void
{
$this->assertValidationMessage(
'Configuration max length must be greater or equal to the min length.'
);
}

/**
* @Then I should be notified that max entries value must be greater or equal to the min entries value
*/
public function iShouldBeNotifiedThatMaxEntriesValueMustBeGreaterOrEqualToTheMinEntriesValue(): void
{
$this->assertValidationMessage(
'Configuration max entries value must be greater or equal to the min entries value.'
);
}

/**
* @Then I should be notified that min entries value must be lower or equal to the number of added choices
*/
public function iShouldBeNotifiedThatMinEntriesValueMustBeLowerOrEqualToTheNumberOfAddedChoices(): void
{
$this->assertValidationMessage(
'Configuration min entries value must be lower or equal to the number of added choices.'
);
}

/**
* @Then I should be notified that multiple must be true if min or max entries values are specified
*/
public function iShouldBeNotifiedThatMultipleMustBeTrueIfMinOrMaxEntriesValuesAreSpecified(): void
{
$this->assertValidationMessage(
'Configuration multiple must be true if min or max entries values are specified.'
);
}

/**
* @param string $element
* @param string $expectedMessage
*
* @throws \InvalidArgumentException
*/
private function assertFieldValidationMessage($element, $expectedMessage)
private function assertFieldValidationMessage(string $element, string $expectedMessage): void
{
/** @var CreatePageInterface|UpdatePageInterface $currentPage */
$currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);

Assert::same($currentPage->getValidationMessage($element), $expectedMessage);
}

/**
* @param string $expectedMessage
*
* @throws \InvalidArgumentException
*/
private function assertValidationMessage(string $expectedMessage): void
{
/** @var CreatePageInterface|UpdatePageInterface $currentPage */
$currentPage = $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);

Assert::same($currentPage->getValidationErrors(), $expectedMessage);
}
}
38 changes: 38 additions & 0 deletions src/Sylius/Behat/Page/Admin/ProductAttribute/CreatePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\Behat\Page\Admin\ProductAttribute;

use Behat\Mink\Exception\ElementNotFoundException;
use Sylius\Behat\Behaviour\SpecifiesItsCode;
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;

Expand Down Expand Up @@ -54,6 +55,40 @@ public function addAttributeValue($value)
++$this->choiceListIndex;
}

/**
* {@inheritdoc}
*/
public function specifyMinValue(int $min): void
{
$this->getElement('min')->setValue($min);
}

/**
* {@inheritdoc}
*/
public function specifyMaxValue(int $max): void
{
$this->getElement('max')->setValue($max);
}

public function checkMultiple(): void
{
$this->getElement('multiple')->click();
}

/**
* {@inheritdoc}
*/
public function getValidationErrors(): string
{
$validationMessage = $this->getDocument()->find('css', '.sylius-validation-error');
if (null === $validationMessage) {
throw new ElementNotFoundException($this->getSession(), 'Validation message', 'css', '.sylius-validation-error');
}

return $validationMessage->getText();
}

/**
* {@inheritdoc}
*/
Expand All @@ -63,6 +98,9 @@ protected function getDefinedElements()
'attribute_choice_list' => 'div[data-form-collection="list"]',
'attribute_choice_list_element' => '#sylius_product_attribute_configuration_choices_%index%',
'code' => '#sylius_product_attribute_code',
'max' => '#sylius_product_attribute_configuration_max',
'min' => '#sylius_product_attribute_configuration_min',
'multiple' => 'label[for=sylius_product_attribute_configuration_multiple]',
'name' => '#sylius_product_attribute_translations_en_US_name',
'type' => '#sylius_product_attribute_type',
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sylius\Behat\Page\Admin\ProductAttribute;

use Behat\Mink\Exception\ElementNotFoundException;
use Sylius\Behat\Page\Admin\Crud\CreatePageInterface as BaseCreatePageInterface;

/**
Expand Down Expand Up @@ -40,4 +41,23 @@ public function isTypeDisabled();
* @param string $value
*/
public function addAttributeValue($value);

/**
* @param int $min
*/
public function specifyMinValue(int $min): void;

/**
* @param int $max
*/
public function specifyMaxValue(int $max): void;

public function checkMultiple(): void;

/**
* @return string
*
* @throws ElementNotFoundException
*/
public function getValidationErrors(): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder
->add('format', TextType::class, [
'label' => 'sylius.form.attribute_type_configuration.date.format',
'required' => false,
])
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder
->add('format', TextType::class, [
'label' => 'sylius.form.attribute_type_configuration.datetime.format',
'required' => false,
])
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'sylius.form.attribute_type_configuration.select.values',
'allow_add' => true,
'allow_delete' => true,
'required' => false,
])
->add('multiple', CheckboxType::class, [
'label' => 'sylius.form.attribute_type_configuration.select.multiple',
])
->add('min', NumberType::class, [
'label' => 'sylius.form.attribute_type_configuration.select.min',
'required' => false,
])
->add('max', NumberType::class, [
'label' => 'sylius.form.attribute_type_configuration.select.max',
'required' => false,
])
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder
->add('min', NumberType::class, [
'label' => 'sylius.form.attribute_type_configuration.text.min',
'required' => false,
])
->add('max', NumberType::class, [
'label' => 'sylius.form.attribute_type_configuration.text.max',
'required' => false,
])
;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,13 @@
<argument type="service" id="sylius.registry.attribute_type" />
<tag name="validator.constraint_validator" alias="sylius_valid_attribute_value_validator" />
</service>

<service id="sylius.validator.valid_text_attribute" class="Sylius\Bundle\AttributeBundle\Validator\Constraints\ValidTextAttributeConfigurationValidator">
<tag name="validator.constraint_validator" alias="sylius_valid_text_attribute_validator" />
</service>

<service id="sylius.validator.valid_select_attribute" class="Sylius\Bundle\AttributeBundle\Validator\Constraints\ValidSelectAttributeConfigurationValidator">
<tag name="validator.constraint_validator" alias="sylius_valid_select_attribute_validator" />
</service>
</services>
</container>
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
<option name="message">sylius.attribute.code.unique</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Sylius\Bundle\AttributeBundle\Validator\Constraints\ValidTextAttributeConfiguration">
<option name="groups">sylius</option>
</constraint>
<constraint name="Sylius\Bundle\AttributeBundle\Validator\Constraints\ValidSelectAttributeConfiguration">
<option name="groups">sylius</option>
</constraint>
<property name="code">
<constraint name="NotBlank">
<option name="message">sylius.attribute.code.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ sylius:
not_blank: Please enter attribute code.
regex: Attribute code can only be comprised of letters, numbers, dashes and underscores.
unique: This code is already in use.
configuration:
max_entries: Configuration max entries value must be greater or equal to the min entries value.
max_length: Configuration max length must be greater or equal to the min length.
min_entries: Configuration min entries value must be lower or equal to the number of added choices.
multiple: Configuration multiple must be true if min or max entries values are specified.
presentation:
not_blank: Please enter attribute presentation.
min_length: Attribute presentation must be at least 1 character long.|Attribute presentation must be at least {{ limit }} characters long.
Expand Down
Loading