Skip to content

Commit

Permalink
Set choices_as_values to true in SecurityRolesType (sonata-project#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelVella authored and OskarStark committed Dec 9, 2017
1 parent 1e4243e commit b39281f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Form/Type/SecurityRolesType.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ public function configureOptions(OptionsResolver $resolver): void

'data_class' => null,
]);

// Symfony 2.8 BC
if ($resolver->isDefined('choices_as_values')) {
$resolver->setDefault('choices_as_values', true);
}
}

/**
Expand Down
19 changes: 19 additions & 0 deletions tests/Form/Type/SecurityRolesTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ public function testSubmitWithHiddenRoleData(): void
$this->assertContains('ROLE_SUPER_ADMIN', $form->getData());
}

public function testChoicesAsValues(): void
{
$resolver = new OptionsResolver();
$type = new SecurityRolesType($this->roleBuilder);

// If 'choices_as_values' option is not defined (Symfony >= 3.0), default value should not be set.
$type->configureOptions($resolver);

$this->assertFalse($resolver->hasDefault('choices_as_values'));

// If 'choices_as_values' option is defined (Symfony 2.8), default value should be set to true.
$resolver->setDefined(['choices_as_values']);
$type->configureOptions($resolver);
$options = $resolver->resolve();

$this->assertTrue($resolver->hasDefault('choices_as_values'));
$this->assertTrue($options['choices_as_values']);
}

protected function getExtensions()
{
$this->roleBuilder = $roleBuilder = $this->createMock(EditableRolesBuilder::class);
Expand Down

0 comments on commit b39281f

Please sign in to comment.