Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
PrestaShop
PrestaShop
Commits
2610085d
Unverified
Commit
2610085d
authored
Dec 02, 2020
by
Jonathan Lelievre
Committed by
GitHub
Dec 02, 2020
Browse files
Merge pull request #22191 from jolelievre/validate-currency-name
Validate currency localized names
parents
850031e7
cc5a9e43
Changes
5
Hide whitespace changes
Inline
Side-by-side
src/Adapter/Currency/CommandHandler/AbstractCurrencyHandler.php
View file @
2610085d
...
...
@@ -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
*
...
...
src/Core/Domain/Currency/Exception/CurrencyConstraintException.php
View file @
2610085d
...
...
@@ -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
;
}
src/PrestaShopBundle/Controller/Admin/Improve/International/CurrencyController.php
View file @
2610085d
...
...
@@ -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'
...
...
tests/Integration/Behaviour/Features/Context/Domain/CurrencyFeatureContext.php
View file @
2610085d
...
...
@@ -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
*/
...
...
tests/Integration/Behaviour/Features/Scenario/Currency/currency_management.feature
View file @
2610085d
...
...
@@ -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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment