diff --git a/features/currency/managing_currencies/currency_validation.feature b/features/currency/managing_currencies/currency_validation.feature new file mode 100644 index 0000000000..b1092fb8e1 --- /dev/null +++ b/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 diff --git a/src/Sylius/Behat/Context/Api/Admin/ManagingCurrenciesContext.php b/src/Sylius/Behat/Context/Api/Admin/ManagingCurrenciesContext.php index f69fc42efc..c8d4d721b1 100644 --- a/src/Sylius/Behat/Context/Api/Admin/ManagingCurrenciesContext.php +++ b/src/Sylius/Behat/Context/Api/Admin/ManagingCurrenciesContext.php @@ -45,8 +45,10 @@ final class ManagingCurrenciesContext implements Context /** * @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); } @@ -107,6 +109,32 @@ final class ManagingCurrenciesContext implements Context 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 */ diff --git a/tests/Api/Admin/CurrenciesTest.php b/tests/Api/Admin/CurrenciesTest.php index 7507107fc5..42e60f3778 100644 --- a/tests/Api/Admin/CurrenciesTest.php +++ b/tests/Api/Admin/CurrenciesTest.php @@ -59,6 +59,30 @@ final class CurrenciesTest extends JsonApiTestCase ); } + /** @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 { diff --git a/tests/Api/Responses/admin/currency/post_currency_with_invalid_code_response.json b/tests/Api/Responses/admin/currency/post_currency_with_invalid_code_response.json new file mode 100644 index 0000000000..1e206c9de7 --- /dev/null +++ b/tests/Api/Responses/admin/currency/post_currency_with_invalid_code_response.json @@ -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@ + } + ] +}