Unverified Commit 2610085d authored by Jonathan Lelievre's avatar Jonathan Lelievre Committed by GitHub
Browse files

Merge pull request #22191 from jolelievre/validate-currency-name

Validate currency localized names
parents 850031e7 cc5a9e43
......@@ -32,6 +32,7 @@ use PrestaShop\PrestaShop\Core\Domain\Currency\Command\AddCurrencyCommand;
use PrestaShop\PrestaShop\Core\Domain\Currency\Command\EditCurrencyCommand;
use PrestaShop\PrestaShop\Core\Domain\Currency\Exception\CannotCreateCurrencyException;
use PrestaShop\PrestaShop\Core\Domain\Currency\Exception\CannotUpdateCurrencyException;
use PrestaShop\PrestaShop\Core\Domain\Currency\Exception\CurrencyConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Language\Exception\LanguageNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Language\ValueObject\LanguageId;
use PrestaShop\PrestaShop\Core\Language\LanguageInterface;
......@@ -160,6 +161,7 @@ abstract class AbstractCurrencyHandler extends AbstractObjectModelHandler
$this->applyPatternTransformations($entity, $command->getLocalizedTransformations());
$this->refreshLocalizedData($entity);
$this->validateCurrency($entity);
//IMPORTANT: specify that we want to save null values
if (false === $entity->save(true, true)) {
......@@ -201,6 +203,7 @@ abstract class AbstractCurrencyHandler extends AbstractObjectModelHandler
}
$this->refreshLocalizedData($entity);
$this->validateCurrency($entity);
//IMPORTANT: specify that we want to save null values
if (false === $entity->update(true)) {
......@@ -218,6 +221,45 @@ abstract class AbstractCurrencyHandler extends AbstractObjectModelHandler
}
}
/**
* @param Currency $currency
*
* @throws CurrencyConstraintException
* @throws PrestaShopException
*/
private function validateCurrency(Currency $currency): void
{
$this->validateLocalizedField($currency, 'name', CurrencyConstraintException::INVALID_NAME);
$this->validateLocalizedField($currency, 'symbol', CurrencyConstraintException::INVALID_SYMBOL);
$this->validateLocalizedField($currency, 'pattern', CurrencyConstraintException::INVALID_SYMBOL);
}
/**
* @param Currency $currency
* @param string $propertyName
* @param int $errorCode
*
* @throws CurrencyConstraintException
* @throws PrestaShopException
*/
private function validateLocalizedField(Currency $currency, string $propertyName, int $errorCode): void
{
$localizedValues = $currency->{$propertyName};
foreach ($localizedValues as $langId => $value) {
if (true !== $currency->validateField($propertyName, $value, $langId)) {
throw new CurrencyConstraintException(
sprintf(
'Invalid Currency localized property "%s" for language with id "%d"',
$propertyName,
$langId
),
$errorCode
);
}
}
}
/**
* @param int $langId
*
......
......@@ -78,4 +78,19 @@ class CurrencyConstraintException extends CurrencyException
* @see Precision
*/
const INVALID_PRECISION = 8;
/**
* Code used when an invalid name is used
*/
const INVALID_NAME = 9;
/**
* Code used when an invalid symbol is used
*/
const INVALID_SYMBOL = 10;
/**
* Code used when an invalid pattern is used
*/
const INVALID_PATTERN = 11;
}
......@@ -496,6 +496,13 @@ class CurrencyController extends FrameworkBundleAdminController
sprintf('"%s"', $this->trans('Exchange rate', 'Admin.International.Feature')),
]
),
CurrencyConstraintException::INVALID_NAME => $this->trans(
'The %s field is not valid',
'Admin.Notifications.Error',
[
sprintf('"%s"', $this->trans('Currency name', 'Admin.International.Feature')),
]
),
CurrencyConstraintException::CURRENCY_ALREADY_EXISTS => $this->trans(
'This currency already exists.',
'Admin.International.Notification'
......
......@@ -267,6 +267,14 @@ class CurrencyFeatureContext extends AbstractDomainFeatureContext
$this->assertLastErrorIs(InvalidUnofficialCurrencyException::class);
}
/**
* @Then /^I should get error that currency name is invalid$/
*/
public function assertLastErrorIsInvalidCurrencyName()
{
$this->assertLastErrorIs(CurrencyConstraintException::class, CurrencyConstraintException::INVALID_NAME);
}
/**
* @Then I should get error that currency already exists
*/
......
......@@ -396,3 +396,41 @@ Feature: Currency Management
And currency "currency18" should be available in shop "shop1"
And currency "currency18" should have pattern empty for language "fr-FR"
And currency "currency18" should have pattern empty for language "en-US"
Scenario: Adding invalid named currency
When I add new currency "currency19" with following properties:
| iso_code | IVL |
| exchange_rate | 0.88 |
| name | <> |
| symbol | |
| is_enabled | 1 |
| is_unofficial | 1 |
| shop_association | shop1 |
Then I should get error that currency name is invalid
When I add new currency "currency19" with following properties:
| iso_code | AOA |
| exchange_rate | 0.88 |
| name | <> |
| symbol | |
| is_enabled | 1 |
| is_unofficial | 0 |
| shop_association | shop1 |
Then I should get error that currency name is invalid
When I edit currency "currency17" with following properties:
| iso_code | JPP |
| exchange_rate | 0.8 |
| name | <> |
| is_enabled | 1 |
| is_unofficial | 1 |
| shop_association | shop1 |
| transformations | en-US:rightWithoutSpace |
Then I should get error that currency name is invalid
When I edit currency "currency16" with following properties:
| iso_code | JPY |
| exchange_rate | 0.08 |
| name | <> |
| is_enabled | 1 |
| is_unofficial | 0 |
| shop_association | shop1 |
| transformations | en-US:rightWithSpace |
Then I should get error that currency name is invalid
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment