bug #37279 [Form] Fixed prototype block prefixes hierarchy of the CollectionType (yceruto)

This PR was merged into the 5.1 branch.

Discussion
----------

[Form] Fixed prototype block prefixes hierarchy of the CollectionType

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #37024
| License       | MIT
| Doc PR        |

Following https://github.com/symfony/symfony/pull/37276

Commits
-------

65efc36e94 fixed prototype block prefixes hierarchy of the CollectionType
This commit is contained in:
Fabien Potencier 2020-06-15 07:12:47 +02:00
commit 8bb0897a6d
2 changed files with 36 additions and 7 deletions

View File

@ -93,7 +93,7 @@ class CollectionType extends AbstractType
$view->vars['multipart'] = true; $view->vars['multipart'] = true;
} }
if ($prefixOffset > -2 && $prototype->getConfig()->getOption('block_prefix')) { if ($prefixOffset > -3 && $prototype->getConfig()->getOption('block_prefix')) {
--$prefixOffset; --$prefixOffset;
} }

View File

@ -428,37 +428,66 @@ class CollectionTypeTest extends BaseTypeTest
public function testEntriesBlockPrefixesWithCustomBlockPrefix() public function testEntriesBlockPrefixesWithCustomBlockPrefix()
{ {
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [ $collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
'allow_add' => true,
'entry_options' => ['block_prefix' => 'field'], 'entry_options' => ['block_prefix' => 'field'],
]) ])
->createView() ->createView()
; ;
$this->assertCount(1, $collectionView); $expectedBlockPrefixes = [
$this->assertSame([
'form', 'form',
'collection_entry', 'collection_entry',
'text', 'text',
'field', 'field',
'_fields_entry', '_fields_entry',
], $collectionView[0]->vars['block_prefixes']); ];
$this->assertCount(1, $collectionView);
$this->assertSame($expectedBlockPrefixes, $collectionView[0]->vars['block_prefixes']);
$this->assertSame($expectedBlockPrefixes, $collectionView->vars['prototype']->vars['block_prefixes']);
} }
public function testEntriesBlockPrefixesWithCustomBlockPrefixedType() public function testEntriesBlockPrefixesWithCustomBlockPrefixedType()
{ {
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [ $collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [''], [
'allow_add' => true,
'entry_type' => BlockPrefixedFooTextType::class, 'entry_type' => BlockPrefixedFooTextType::class,
]) ])
->createView() ->createView()
; ;
$this->assertCount(1, $collectionView); $expectedBlockPrefixes = [
$this->assertSame([
'form', 'form',
'collection_entry', 'collection_entry',
'block_prefixed_foo_text', 'block_prefixed_foo_text',
'foo', 'foo',
'_fields_entry', '_fields_entry',
], $collectionView[0]->vars['block_prefixes']); ];
$this->assertCount(1, $collectionView);
$this->assertSame($expectedBlockPrefixes, $collectionView[0]->vars['block_prefixes']);
$this->assertSame($expectedBlockPrefixes, $collectionView->vars['prototype']->vars['block_prefixes']);
}
public function testPrototypeBlockPrefixesWithCustomBlockPrefix()
{
$collectionView = $this->factory->createNamed('fields', static::TESTED_TYPE, [], [
'allow_add' => true,
'entry_options' => ['block_prefix' => 'field'],
])
->createView()
;
$expectedBlockPrefixes = [
'form',
'collection_entry',
'text',
'field',
'_fields_entry',
];
$this->assertCount(0, $collectionView);
$this->assertSame($expectedBlockPrefixes, $collectionView->vars['prototype']->vars['block_prefixes']);
} }
public function testSubmitNull($expected = null, $norm = null, $view = null) public function testSubmitNull($expected = null, $norm = null, $view = null)