Skip to content

Commit

Permalink
feature #26075 [Validator] Deprecate use of Locale validation const…
Browse files Browse the repository at this point in the history
…raint without setting "canonicalize" option to `true` (phansys)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[Validator] Deprecate use of `Locale` validation constraint without setting "canonicalize" option to `true`

|Q            |A        |
|---          |---      |
|Branch       |master   |
|Bug fix?     |no       |
|New feature? |no       |
|BC breaks?   |no       |
|Deprecations?|yes      |
|Tests pass?  |yes      |
|Fixed tickets|#22353   |
|License      |MIT      |
|Doc PR       |symfony/symfony-docs#9248|

See symfony/symfony#22353 (comment).

Commits
-------

1572540a3a Deprecate use of `Locale` validation constraint without setting "canonicalize" option to `true`
  • Loading branch information
fabpot committed Feb 20, 2018
2 parents cf2e986 + b82d1b3 commit 4c6d4dd
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Deprecated the `checkDNS` and `dnsMessage` options of the `Url` constraint.
* added a `values` option to the `Expression` constraint
* Deprecated use of `Locale` constraint without setting `true` at "canonicalize" option, which will be the default value in 5.0

4.0.0
-----
Expand Down
9 changes: 9 additions & 0 deletions Constraints/Locale.php
Expand Up @@ -29,4 +29,13 @@ class Locale extends Constraint

public $message = 'This value is not a valid locale.';
public $canonicalize = false;

public function __construct($options = null)
{
if (!($options['canonicalize'] ?? false)) {
@trigger_error('The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead.', E_USER_DEPRECATED);
}

parent::__construct($options);
}
}
5 changes: 3 additions & 2 deletions Constraints/LocaleValidator.php
Expand Up @@ -40,7 +40,8 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($value, 'string');
}

$value = (string) $value;
$inputValue = (string) $value;
$value = $inputValue;
if ($constraint->canonicalize) {
$value = \Locale::canonicalize($value);
}
Expand All @@ -49,7 +50,7 @@ public function validate($value, Constraint $constraint)

if (!isset($locales[$value]) && !in_array($value, $localeBundle->getAliases(), true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ value }}', $this->formatValue($inputValue))
->setCode(Locale::NO_SUCH_LOCALE_ERROR)
->addViolation();
}
Expand Down
101 changes: 90 additions & 11 deletions Tests/Constraints/LocaleValidatorTest.php
Expand Up @@ -22,47 +22,123 @@ protected function createValidator()
return new LocaleValidator();
}

public function testNullIsValid()
/**
* @group legacy
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead.
*
* @dataProvider getValidLocales
*/
public function testLegacyNullIsValid()
{
$this->validator->validate(null, new Locale());

$this->assertNoViolation();
}

public function testEmptyStringIsValid()
public function testNullIsValid()
{
$this->validator->validate(null, new Locale(array('canonicalize' => true)));

$this->assertNoViolation();
}

/**
* @group legacy
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead.
*
* @dataProvider getValidLocales
*/
public function testLegacyEmptyStringIsValid()
{
$this->validator->validate('', new Locale());

$this->assertNoViolation();
}

public function testEmptyStringIsValid()
{
$this->validator->validate('', new Locale(array('canonicalize' => true)));

$this->assertNoViolation();
}

/**
* @group legacy
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead.
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
public function testExpectsStringCompatibleType()
public function testLegacyExpectsStringCompatibleType()
{
$this->validator->validate(new \stdClass(), new Locale());
}

/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
public function testExpectsStringCompatibleType()
{
$this->validator->validate(new \stdClass(), new Locale(array('canonicalize' => true)));
}

/**
* @group legacy
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead.
*
* @dataProvider getValidLocales
*/
public function testValidLocales($locale)
public function testLegacyValidLocales(string $locale)
{
$this->validator->validate($locale, new Locale());

$this->assertNoViolation();
}

/**
* @dataProvider getValidLocales
*/
public function testValidLocales($locale, array $options)
{
$this->validator->validate($locale, new Locale($options));

$this->assertNoViolation();
}

public function getValidLocales()
{
return array(
array('en'),
array('en_US'),
array('pt'),
array('pt_PT'),
array('zh_Hans'),
array('fil_PH'),
array('en', array('canonicalize' => true)),
array('en_US', array('canonicalize' => true)),
array('pt', array('canonicalize' => true)),
array('pt_PT', array('canonicalize' => true)),
array('zh_Hans', array('canonicalize' => true)),
array('fil_PH', array('canonicalize' => true)),
);
}

/**
* @group legacy
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead.
* @dataProvider getLegacyInvalidLocales
*/
public function testLegacyInvalidLocales(string $locale)
{
$constraint = new Locale(array(
'message' => 'myMessage',
));

$this->validator->validate($locale, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$locale.'"')
->setCode(Locale::NO_SUCH_LOCALE_ERROR)
->assertRaised();
}

public function getLegacyInvalidLocales()
{
return array(
array('EN'),
array('foobar'),
);
}

Expand All @@ -73,6 +149,7 @@ public function testInvalidLocales($locale)
{
$constraint = new Locale(array(
'message' => 'myMessage',
'canonicalize' => true,
));

$this->validator->validate($locale, $constraint);
Expand All @@ -86,12 +163,14 @@ public function testInvalidLocales($locale)
public function getInvalidLocales()
{
return array(
array('EN'),
array('baz'),
array('foobar'),
);
}

/**
* @group legacy
* @expectedDeprecation The "canonicalize" option with value "false" is deprecated since Symfony 4.1, set it to "true" instead.
* @dataProvider getUncanonicalizedLocales
*/
public function testInvalidLocalesWithoutCanonicalization(string $locale)
Expand Down

0 comments on commit 4c6d4dd

Please sign in to comment.