bug #10568 [Form] Fixed hashing of choice lists containing non-UTF-8 characters (webmozart)

This PR was merged into the 2.3 branch.

Discussion
----------

[Form] Fixed hashing of choice lists containing non-UTF-8 characters

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #10409
| License       | MIT
| Doc PR        | -

Commits
-------

18dc9a7 Added test case for 4c6a2d15095c13b2a35751b2b2712b183be489c4
73d56f7 Fixed bug in ChoiceType triggering a warning when not using utf-8
This commit is contained in:
Fabien Potencier 2014-03-28 11:31:34 +01:00
commit e2b82d36cb
2 changed files with 42 additions and 1 deletions

View File

@ -166,7 +166,7 @@ class ChoiceType extends AbstractType
$choices = null !== $options['choices'] ? $options['choices'] : array();
// Reuse existing choice lists in order to increase performance
$hash = md5(json_encode(array($choices, $options['preferred_choices'])));
$hash = hash('sha256', serialize(array($choices, $options['preferred_choices'])));
if (!isset($choiceListCache[$hash])) {
$choiceListCache[$hash] = new SimpleChoiceList($choices, $options['preferred_choices']);

View File

@ -1216,6 +1216,47 @@ class ChoiceTypeTest extends \Symfony\Component\Form\Test\TypeTestCase
));
}
// https://github.com/symfony/symfony/issues/10409
public function testReuseNonUtf8ChoiceLists()
{
$form1 = $this->factory->createNamed('name', 'choice', null, array(
'choices' => array(
'meter' => 'm',
'millimeter' => 'mm',
'micrometer' => chr(181).'meter',
),
));
$form2 = $this->factory->createNamed('name', 'choice', null, array(
'choices' => array(
'meter' => 'm',
'millimeter' => 'mm',
'micrometer' => chr(181).'meter',
),
));
$form3 = $this->factory->createNamed('name', 'choice', null, array(
'choices' => array(
'meter' => 'm',
'millimeter' => 'mm',
'micrometer' => null,
),
));
// $form1 and $form2 use the same ChoiceList
$this->assertSame(
$form1->getConfig()->getOption('choice_list'),
$form2->getConfig()->getOption('choice_list')
);
// $form3 doesn't, but used to use the same when using json_encode()
// instead of serialize for the hashing algorithm
$this->assertNotSame(
$form1->getConfig()->getOption('choice_list'),
$form3->getConfig()->getOption('choice_list')
);
}
public function testInitializeWithDefaultObjectChoice()
{
$obj1 = (object) array('value' => 'a', 'label' => 'A');