Skip to content

Commit

Permalink
bug #16028 [Currency][Locale][Country] Update intl-based validation a…
Browse files Browse the repository at this point in the history
…nd cleanup obsolete options (NoResponseMate)

This PR was merged into the 1.13 branch.

Discussion
----------

| Q               | A                                                            |
|-----------------|--------------------------------------------------------------|
| Branch?         | 1.13 |
| Bug fix?        | yes                                                       |
| New feature?    | no                                                       |
| BC breaks?      | no?                                                       |
| Deprecations?   | no |
| Related tickets | #14880, #15472                      |
| License         | MIT                                                          |

Commits
-------

ae04f10 [Currency] Add `Currency` constraint to code field
c0a77ac [API][Behat] Test currency code validation
d524f7a [Addressing] Remove obsolete country code validation
75b2d9e [Locale] Remove obsolete locale code validation
bcba0c9 [Currency] Remove obsolete currency code validation
  • Loading branch information
GSadee committed Mar 21, 2024
2 parents b38617e + bcba0c9 commit 414fb02
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 29 deletions.
14 changes: 11 additions & 3 deletions UPGRADE-1.13.md
Expand Up @@ -895,9 +895,17 @@ and use one of the new attributes accordingly to the type of your class, e.g.:
has been deprecated.
Use the `parentCode` query parameter instead.

1. The `Regex` constraint has been removed from `Sylius\Component\Addressing\Model\Country` in favour of the `Country`
constraint.
Due to that, it's translation message `sylius.country.code.regex` was also removed.
1. The `Regex` and `Length` constraints have been removed from `Sylius\Component\Addressing\Model\Country`
in favour of the `Country` constraint.
Due to that, their translation messages `sylius.country.code.regex` and `sylius.country.code.exact_length` were also removed.

1. The `Regex` and `Length` constraints have been removed from `Sylius\Component\Currency\Model\Currency`
in favour of the `Currency` constraint.
Due to that, their translation messages `sylius.currency.regex` and `sylius.currency.exact_length` were also removed.

1. The `Regex` constraint has been removed from `Sylius\Component\Locale\Model\Locale`
in favour of the `Locale` constraint.
Due to that, the translation message `sylius.locale.code.regex` was also removed.

1. The `sylius_admin_ajax_taxon_move` route has been deprecated. If you're relaying on it, consider migrating to new
`sylius_admin_ajax_taxon_move_up` and `sylius_admin_ajax_taxon_move_down` routes.
Expand Down
29 changes: 29 additions & 0 deletions features/currency/managing_currencies/currency_validation.feature
@@ -0,0 +1,29 @@
@managing_currencies
Feature: Currency validation
In order to avoid making mistakes when managing currencies
As an Administrator
I want to be prevented from adding an invalid currency

Background:
Given I am logged in as an administrator

@api @no-ui
Scenario: Trying to add currency without code
When I want to add a new currency
And I do not choose a code
And I try to add it
Then I should be notified that a code is required

@api @no-ui
Scenario: Trying to add a currency with an invalid code
When I want to add a new currency
And I set code to "invalid"
And I try to add it
Then I should be notified that the code is invalid

@api @no-ui
Scenario: Trying to add a currency with an non-existent code
When I want to add a new currency
And I set code to "B0B"
And I try to add it
Then I should be notified that the code is invalid
30 changes: 29 additions & 1 deletion src/Sylius/Behat/Context/Api/Admin/ManagingCurrenciesContext.php
Expand Up @@ -45,8 +45,10 @@ public function iWantToAddNewCurrency(): void

/**
* @When I choose :currencyCode
* @When I set code to :code
* @When I do not choose a code
*/
public function iChoose(string $currencyCode): void
public function iChoose(string $currencyCode = ''): void
{
$this->client->addRequestData('code', $currencyCode);
}
Expand Down Expand Up @@ -107,6 +109,32 @@ public function iShouldBeNotifiedThatCurrencyCodeMustBeUnique(): void
Assert::same($this->responseChecker->getError($response), 'code: Currency code must be unique.');
}

/**
* @Then I should be notified that a code is required
*/
public function iShouldBeNotifiedThatACodeIsRequired(): void
{
$response = $this->client->getLastResponse();
Assert::false(
$this->responseChecker->isCreationSuccessful($response),
'Currency has been created successfully, but it should not',
);
Assert::same($this->responseChecker->getError($response), 'code: Please choose currency code.');
}

/**
* @Then I should be notified that the code is invalid
*/
public function iShouldBeNotifiedThatTheCodeIsInvalid(): void
{
$response = $this->client->getLastResponse();
Assert::false(
$this->responseChecker->isCreationSuccessful($response),
'Currency has been created successfully, but it should not',
);
Assert::same($this->responseChecker->getError($response), 'code: This value is not a valid currency code.');
}

/**
* @Then I should be notified that it has been successfully created
*/
Expand Down
Expand Up @@ -20,19 +20,13 @@
</constraint>
<property name="code">
<constraint name="Country">
<option name="message">sylius.country.code.invalid</option>
<option name="message">sylius.country.code.country</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="NotBlank">
<option name="message">sylius.country.code.not_blank</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Length">
<option name="min">2</option>
<option name="max">2</option>
<option name="exactMessage">sylius.country.code.exact_length</option>
<option name="groups">sylius</option>
</constraint>
</property>
<property name="provinces">
<constraint name="Sylius\Bundle\AddressingBundle\Validator\Constraints\UniqueProvinceCollection">
Expand Down
Expand Up @@ -30,7 +30,7 @@ sylius:
code:
not_blank: Please enter country ISO code.
unique: Country ISO code must be unique.
invalid: Country ISO code is invalid.
country: Country ISO code is invalid.
unique_provinces: All provinces within this country need to have unique codes and names.
province:
code:
Expand Down
Expand Up @@ -23,15 +23,8 @@
<option name="message">sylius.currency.code.not_blank</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Length">
<option name="min">3</option>
<option name="max">3</option>
<option name="exactMessage">sylius.currency.code.exact_length</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Regex">
<option name="message">sylius.currency.code.regex</option>
<option name="pattern">/^[\w-]*$/</option>
<constraint name="Currency">
<option name="message">sylius.currency.code.currency</option>
<option name="groups">sylius</option>
</constraint>
</property>
Expand Down
@@ -1,10 +1,9 @@
sylius:
currency:
code:
exact_length: Currency code must be exactly 3 characters long.
not_blank: Please choose currency code.
regex: Currency code can only be comprised of letters, numbers, dashes and underscores.
unique: Currency code must be unique.
currency: This value is not a valid currency code.
exchange_rate:
not_blank: Please enter exchange rate.
exchange_rate:
Expand Down
Expand Up @@ -27,11 +27,6 @@
<option name="message">sylius.locale.code.locale</option>
<option name="groups">sylius</option>
</constraint>
<constraint name="Regex">
<option name="message">sylius.locale.code.regex</option>
<option name="pattern">/^[\w-]*$/</option>
<option name="groups">sylius</option>
</constraint>
</property>
</class>
</constraint-mapping>
Expand Up @@ -2,6 +2,5 @@ sylius:
locale:
code:
not_blank: Please choose locale code.
regex: Locale code can only be comprised of letters, numbers, dashes and underscores.
locale: This value is not a valid locale code.
unique: Locale code must be unique.
24 changes: 24 additions & 0 deletions tests/Api/Admin/CurrenciesTest.php
Expand Up @@ -59,6 +59,30 @@ public function it_gets_currencies(): void
);
}

/** @test */
public function it_does_not_allow_creating_a_currency_with_invalid_code(): void
{
$this->loadFixturesFromFiles(['channel.yaml', 'authentication/api_administrator.yaml']);
$header = array_merge($this->logInAdminUser('api@example.com'), self::CONTENT_TYPE_HEADER);

$this->client->request(
'POST',
'/api/v2/admin/currencies',
[],
[],
$header,
json_encode([
'code' => 'lol',
], \JSON_THROW_ON_ERROR),
);

$this->assertResponse(
$this->client->getResponse(),
'admin/currency/post_currency_with_invalid_code_response',
Response::HTTP_UNPROCESSABLE_ENTITY,
);
}

/** @test */
public function it_creates_a_currency(): void
{
Expand Down
@@ -0,0 +1,13 @@
{
"@context": "\/api\/v2\/contexts\/ConstraintViolationList",
"@type": "ConstraintViolationList",
"hydra:title": "An error occurred",
"hydra:description": "code: This value is not a valid currency code.",
"violations": [
{
"propertyPath": "code",
"message": "This value is not a valid currency code.",
"code": @string@
}
]
}

0 comments on commit 414fb02

Please sign in to comment.