Cover the max code length validation in the Product bundle

This commit is contained in:
Jacob Tobiasz 2024-03-21 14:35:20 +01:00
parent 92cc51efc3
commit 05876b1f19
No known key found for this signature in database
GPG key ID: 3F84290201B006E0
16 changed files with 174 additions and 3 deletions

View file

@ -18,6 +18,14 @@ Feature: Product option validation
Then I should be notified that code is required
And the product option with name "T-Shirt size" should not be added
@ui @api
Scenario: Trying to add a new product option with a too long code
Given I want to create a new product option
And I name it "T-Shirt size" in "English (United States)"
When I specify a too long code
And I try to add it
Then I should be notified that the code is too long
@ui @api
Scenario: Trying to add a new product option without specifying its name
When I want to create a new product option

View file

@ -85,6 +85,14 @@ Feature: Product variant validation
Then I should be notified that code is required
And the "Wyborowa Vodka" product should have no variants
@api @ui
Scenario: Adding a new product variant with a too long code
Given I want to create a new variant of this product
And I set its price to "$80.00" for "United States" channel
When I specify a too long code
And I try to add it
Then I should be notified that the code is too long
@api @ui
Scenario: Adding a new product variant with duplicated code
Given this product has "Wyborowa Exquisite" variant priced at "$90.00" identified by "VODKA_WYBOROWA_PREMIUM"

View file

@ -86,6 +86,14 @@ Feature: Products validation
Then I should be notified that code is required
And product with name "Dice Brewing" should not be added
@ui @api
Scenario: Adding a new configurable product with too long code
Given I want to create a new configurable product
And I name it "Dice Brewing" in "English (United States)"
When I specify a too long code
And I try to add it
Then I should be notified that the code is too long
@ui @api
Scenario: Adding a new configurable product with duplicated code
Given the store has a product "7 Wonders" with code "AWESOME_GAME"

View file

@ -0,0 +1,43 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Behat\Context\Api\Admin\Helper;
use Webmozart\Assert\Assert;
trait CodeValidationTrait
{
/**
* @When I specify a too long code
*/
public function iSpecifyATooLongCode(): void
{
$this->client->addRequestData('code', str_repeat('a', $this->getMaxCodeLength() + 1));
}
/**
* @Then I should be notified that the code is too long
*/
public function iShouldBeNotifiedThatTheCodeIsTooLong(): void
{
Assert::contains(
$this->responseChecker->getError($this->client->getLastResponse()),
' The code must not be longer than',
);
}
private function getMaxCodeLength(): int
{
return 255;
}
}

View file

@ -16,6 +16,7 @@ namespace Sylius\Behat\Context\Api\Admin;
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Api\Admin\Helper\CodeValidationTrait;
use Sylius\Behat\Context\Api\Resources;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\Component\Product\Model\ProductOptionInterface;
@ -23,6 +24,8 @@ use Webmozart\Assert\Assert;
final class ManagingProductOptionsContext implements Context
{
use CodeValidationTrait;
public function __construct(
private ApiClientInterface $client,
private ResponseCheckerInterface $responseChecker,

View file

@ -17,6 +17,7 @@ use ApiPlatform\Api\IriConverterInterface;
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Api\Admin\Helper\CodeValidationTrait;
use Sylius\Behat\Context\Api\Resources;
use Sylius\Behat\Service\Converter\SectionAwareIriConverterInterface;
use Sylius\Component\Core\Formatter\StringInflector;
@ -31,6 +32,8 @@ use Webmozart\Assert\Assert;
final class ManagingProductVariantsContext implements Context
{
use CodeValidationTrait;
private const FIRST_COLLECTION_ITEM = 0;
private const HUGE_NUMBER = 2147483647;

View file

@ -17,6 +17,7 @@ use ApiPlatform\Api\IriConverterInterface;
use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ResponseCheckerInterface;
use Sylius\Behat\Context\Api\Admin\Helper\CodeValidationTrait;
use Sylius\Behat\Context\Api\Resources;
use Sylius\Behat\Service\Converter\SectionAwareIriConverterInterface;
use Sylius\Behat\Service\SharedStorageInterface;
@ -37,6 +38,8 @@ use Webmozart\Assert\Assert;
final class ManagingProductsContext implements Context
{
use CodeValidationTrait;
public const SORT_TYPES = ['ascending' => 'asc', 'descending' => 'desc'];
public function __construct(

View file

@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Sylius Sp. z o.o.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Sylius\Behat\Context\Ui\Admin\Helper;
use FriendsOfBehat\PageObjectExtension\Page\SymfonyPageInterface;
use Webmozart\Assert\Assert;
trait CodeValidationTrait
{
/**
* @When I specify a too long code
*/
public function iSpecifyATooLongCode(): void
{
$this->iSpecifyItsCodeAs(str_repeat('a', $this->getMaxCodeLength() + 1));
}
/**
* @Then I should be notified that the code is too long
*/
public function iShouldBeNotifiedThatTheCodeIsTooLong(): void
{
$currentPage = $this->resolveCurrentPage();
Assert::contains(
$currentPage->getValidationMessage('code'),
'The code must not be longer than',
);
}
abstract protected function iSpecifyItsCodeAs(?string $code = null): void;
abstract protected function resolveCurrentPage(): SymfonyPageInterface;
private function getMaxCodeLength(): int
{
return 255;
}
}

View file

@ -14,6 +14,8 @@ declare(strict_types=1);
namespace Sylius\Behat\Context\Ui\Admin;
use Behat\Behat\Context\Context;
use FriendsOfBehat\PageObjectExtension\Page\SymfonyPageInterface;
use Sylius\Behat\Context\Ui\Admin\Helper\CodeValidationTrait;
use Sylius\Behat\Page\Admin\Crud\IndexPageInterface;
use Sylius\Behat\Page\Admin\ProductOption\CreatePageInterface;
use Sylius\Behat\Page\Admin\ProductOption\UpdatePageInterface;
@ -23,6 +25,8 @@ use Webmozart\Assert\Assert;
final class ManagingProductOptionsContext implements Context
{
use CodeValidationTrait;
public function __construct(
private IndexPageInterface $indexPage,
private CreatePageInterface $createPage,
@ -102,7 +106,7 @@ final class ManagingProductOptionsContext implements Context
* @When I specify its code as :code
* @When I do not specify its code
*/
public function iSpecifyItsCodeAs($code = null)
public function iSpecifyItsCodeAs(?string $code = null): void
{
$this->createPage->specifyCode($code ?? '');
}
@ -257,4 +261,9 @@ final class ManagingProductOptionsContext implements Context
Assert::same(end($values), $value);
}
protected function resolveCurrentPage(): SymfonyPageInterface
{
return $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
}
}

View file

@ -14,6 +14,8 @@ declare(strict_types=1);
namespace Sylius\Behat\Context\Ui\Admin;
use Behat\Behat\Context\Context;
use FriendsOfBehat\PageObjectExtension\Page\SymfonyPageInterface;
use Sylius\Behat\Context\Ui\Admin\Helper\CodeValidationTrait;
use Sylius\Behat\NotificationType;
use Sylius\Behat\Page\Admin\ProductVariant\CreatePageInterface;
use Sylius\Behat\Page\Admin\ProductVariant\GeneratePageInterface;
@ -31,6 +33,8 @@ final class ManagingProductVariantsContext implements Context
{
private const HUGE_NUMBER = '2147483647';
use CodeValidationTrait;
public function __construct(
private SharedStorageInterface $sharedStorage,
private CreatePageInterface $createPage,
@ -54,7 +58,7 @@ final class ManagingProductVariantsContext implements Context
* @When I specify its code as :code
* @When I do not specify its code
*/
public function iSpecifyItsCodeAs($code = null)
public function iSpecifyItsCodeAs(?string $code = null): void
{
$this->createPage->specifyCode($code ?? '');
}
@ -758,4 +762,9 @@ final class ManagingProductVariantsContext implements Context
Assert::same($currentPage->getValidationMessage($element), $message);
}
protected function resolveCurrentPage(): SymfonyPageInterface
{
return $this->currentPageResolver->getCurrentPageWithForm([$this->createPage, $this->updatePage]);
}
}

View file

@ -14,6 +14,7 @@ declare(strict_types=1);
namespace Sylius\Behat\Context\Ui\Admin;
use Behat\Behat\Context\Context;
use Sylius\Behat\Context\Ui\Admin\Helper\CodeValidationTrait;
use Sylius\Behat\NotificationType;
use Sylius\Behat\Page\Admin\Crud\CreatePageInterface;
use Sylius\Behat\Page\Admin\Crud\UpdatePageInterface;
@ -40,6 +41,8 @@ use Webmozart\Assert\Assert;
final class ManagingProductsContext implements Context
{
use CodeValidationTrait;
public function __construct(
private SharedStorageInterface $sharedStorage,
private CreateSimpleProductPageInterface $createSimpleProductPage,
@ -78,7 +81,7 @@ final class ManagingProductsContext implements Context
* @When I specify its code as :code
* @When I do not specify its code
*/
public function iSpecifyItsCodeAs($code = null)
public function iSpecifyItsCodeAs(?string $code = null): void
{
$currentPage = $this->resolveCurrentPage();

View file

@ -31,6 +31,11 @@
<option name="pattern">/^[\w-]*$/</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Length">
<option name="max">255</option>
<option name="maxMessage">sylius.product.code.max_length</option>
<option name="groups">sylius</option>
</constraint>
</property>
<getter property="variants">
<constraint name="Sylius\Bundle\ResourceBundle\Validator\Constraints\UniqueWithinCollectionConstraint">

View file

@ -28,6 +28,11 @@
<option name="pattern">/^[\w-]*$/</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Length">
<option name="max">255</option>
<option name="maxMessage">sylius.option.code.max_length</option>
<option name="groups">sylius</option>
</constraint>
</property>
<property name="values">
<constraint name="Valid" />

View file

@ -28,6 +28,11 @@
<option name="pattern">/^[\w-]*$/</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Length">
<option name="max">255</option>
<option name="maxMessage">sylius.option_value.code.max_length</option>
<option name="groups">sylius</option>
</constraint>
</property>
<property name="translations">
<constraint name="Valid" />

View file

@ -35,6 +35,11 @@
<option name="pattern">/^[\w-]*$/</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Length">
<option name="max">255</option>
<option name="maxMessage">sylius.product_variant.code.max_length</option>
<option name="groups">sylius</option>
</constraint>
</property>
<property name="product">
<constraint name="NotBlank">

View file

@ -8,6 +8,7 @@ sylius:
unique: Product slug must be unique.
max_length: Product slug must not be longer than 1 character.|Product slug must not be longer than {{ limit }} characters.
code:
max_length: The code must not be longer than {{ limit }} characters.
not_blank: Please enter product code.
regex: Product code can only be comprised of letters, numbers, dashes and underscores.
unique: Product code must be unique.
@ -22,6 +23,7 @@ sylius:
product_variant:
combination: Variant with this option set already exists.
code:
max_length: The code must not be longer than {{ limit }} characters.
not_blank: Please enter the code.
regex: Product variant code can only be comprised of letters, numbers, dashes and underscores.
unique: Product variant code must be unique.
@ -33,6 +35,7 @@ sylius:
unique: Simple product code must be unique among all products and product variants.
option:
code:
max_length: The code must not be longer than {{ limit }} characters.
not_blank: Please enter option code.
regex: Option code can only be comprised of letters, numbers, dashes and underscores.
unique: The option with given code already exists.
@ -44,6 +47,7 @@ sylius:
min_count: Please add at least {{ limit }} option value.|Please add at least {{ limit }} option values.
option_value:
code:
max_length: The code must not be longer than {{ limit }} characters.
not_blank: Please enter option value code.
regex: Option value code can only be comprised of letters, numbers, dashes and underscores.
unique: The option value with given code already exists.