Skip to content

Commit d68fbd5

Browse files
authored
Improve how locale filter type migrates data from old format (#438)
* Improve how locale filter type migrates data from old format * Improve how locale filter type migrates data from old format
1 parent 1890bac commit d68fbd5

2 files changed

Lines changed: 127 additions & 22 deletions

File tree

src/Form/Admin/LocaleFilterType.php

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
4040
$availableLocales = $options['available_locales'];
4141

4242
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (PreSetDataEvent $event) use ($availableLocales) {
43-
$data = $event->getData();
44-
45-
$initialValue = array_combine($availableLocales, array_fill(0, count($availableLocales), true));
46-
47-
if (null === $data) {
48-
$data = $initialValue;
49-
}
50-
51-
foreach ($data as $key => $value) {
52-
if (is_int($key) && is_string($value)) {
53-
unset($data[$key]);
54-
$data[$value] = true;
55-
}
56-
}
57-
58-
foreach ($availableLocales as $locale) {
59-
if (!array_key_exists($locale, $data)) {
60-
$data[$locale] = true;
61-
}
62-
}
63-
64-
$event->setData($data);
43+
self::onPreSetData($event, $availableLocales);
6544
});
6645

6746
$builder->addModelTransformer(new CallbackTransformer(function ($data) {
@@ -88,4 +67,33 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
8867
return $data;
8968
}));
9069
}
70+
71+
public static function onPreSetData(PreSetDataEvent $event, array $availableLocales): void
72+
{
73+
$data = $event->getData();
74+
75+
$initialValue = array_combine($availableLocales, array_fill(0, count($availableLocales), true));
76+
77+
if (null === $data) {
78+
$data = $initialValue;
79+
}
80+
81+
// migrate legacy format to new one
82+
$isLegacy = 0 === sizeof($data);
83+
foreach ($data as $key => $value) {
84+
if (is_int($key) && is_string($value)) {
85+
unset($data[$key]);
86+
$data[$value] = true;
87+
$isLegacy = true;
88+
}
89+
}
90+
91+
foreach ($availableLocales as $locale) {
92+
if (!array_key_exists($locale, $data)) {
93+
$data[$locale] = !$isLegacy;
94+
}
95+
}
96+
97+
$event->setData($data);
98+
}
9199
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
3+
namespace Softspring\CmsBundle\Test\Unit\Form\Admin;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Softspring\CmsBundle\Form\Admin\LocaleFilterType;
7+
use Symfony\Component\Form\Event\PreSetDataEvent;
8+
9+
class LocaleFilterTypeTest extends TestCase
10+
{
11+
/**
12+
* @dataProvider migratingFormatProvider
13+
*/
14+
public function testMigratingFormat(string $description, array $provided, array $expected, array $available_locales): void
15+
{
16+
$form = $this->createMock('Symfony\Component\Form\FormInterface');
17+
$event = new PreSetDataEvent($form, $provided);
18+
19+
LocaleFilterType::onPreSetData($event, $available_locales);
20+
21+
$this->assertEquals($expected, $event->getData(), $description);
22+
}
23+
24+
public static function migratingFormatProvider(): array
25+
{
26+
return [
27+
[
28+
'legacy format empty',
29+
[],
30+
[
31+
'en' => false,
32+
'fr' => false,
33+
'de' => false
34+
],
35+
['en', 'fr', 'de']
36+
],
37+
[
38+
'legacy format some selected',
39+
[ 0 => 'en', 1 => 'fr' ],
40+
[
41+
'en' => true,
42+
'fr' => true,
43+
'de' => false
44+
],
45+
['en', 'fr', 'de']
46+
],
47+
[
48+
'legacy format all selected',
49+
[ 0 => 'en', 1 => 'de', 2 => 'fr'],
50+
[
51+
'en' => true,
52+
'fr' => true,
53+
'de' => true
54+
],
55+
['en', 'fr', 'de']
56+
],
57+
[
58+
'new format some with values, so others are new locales',
59+
['de' => false],
60+
[
61+
'en' => true,
62+
'fr' => true,
63+
'de' => false
64+
],
65+
['en', 'fr', 'de']
66+
],
67+
[
68+
'new format all with values',
69+
[
70+
'en' => true,
71+
'fr' => false,
72+
'de' => true
73+
],
74+
[
75+
'en' => true,
76+
'fr' => false,
77+
'de' => true
78+
],
79+
['en', 'fr', 'de']
80+
],
81+
[
82+
'new format all with values',
83+
[
84+
'en' => false,
85+
'fr' => false,
86+
'de' => false
87+
],
88+
[
89+
'en' => false,
90+
'fr' => false,
91+
'de' => false
92+
],
93+
['en', 'fr', 'de']
94+
],
95+
];
96+
}
97+
}

0 commit comments

Comments
 (0)