Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Deprecated the `checkDNS` and `dnsMessage` options of the `Url` constraint. They will be removed in 5.0.
* 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 9 src/Symfony/Component/Validator/Constraints/Locale.php
Original file line number Diff line number Diff line change
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);
}
}
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.