Skip to content

Commit

Permalink
[Intl] Improved FormTypeCsrfExtension to use the type class as defaul…
Browse files Browse the repository at this point in the history
…t intention if the form name is empty
  • Loading branch information
webmozart committed Oct 18, 2013
1 parent 1b97ad4 commit 219e44d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Expand Up @@ -52,7 +52,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
->addEventSubscriber(new CsrfValidationListener(
$options['csrf_field_name'],
$options['csrf_provider'],
$options['intention'] ?: $builder->getName()
$options['intention'] ?: ($builder->getName() ?: get_class($builder->getType()->getInnerType()))
))
;
}
Expand All @@ -68,7 +68,8 @@ public function finishView(FormView $view, FormInterface $form, array $options)
{
if ($options['csrf_protection'] && !$view->parent && $options['compound']) {
$factory = $form->getConfig()->getAttribute('csrf_factory');
$data = $options['csrf_provider']->generateCsrfToken($options['intention'] ?: $form->getName());
$intention = $options['intention'] ?: ($form->getName() ?: get_class($form->getConfig()->getType()->getInnerType()));
$data = $options['csrf_provider']->generateCsrfToken($intention);

$csrfForm = $factory->createNamed($options['csrf_field_name'], 'hidden', $data, array(
'mapped' => false,
Expand Down
Expand Up @@ -147,6 +147,24 @@ public function testGenerateCsrfTokenUsesFormNameAsIntentionByDefault()
$this->assertEquals('token', $view['csrf']->vars['value']);
}

public function testGenerateCsrfTokenUsesTypeClassAsIntentionIfEmptyFormName()
{
$this->csrfProvider->expects($this->once())
->method('generateCsrfToken')
->with('Symfony\Component\Form\Extension\Core\Type\FormType')
->will($this->returnValue('token'));

$view = $this->factory
->createNamed('', 'form', null, array(
'csrf_field_name' => 'csrf',
'csrf_provider' => $this->csrfProvider,
'compound' => true,
))
->createView();

$this->assertEquals('token', $view['csrf']->vars['value']);
}

public function provideBoolean()
{
return array(
Expand Down Expand Up @@ -218,6 +236,37 @@ public function testValidateTokenOnBindIfRootAndCompoundUsesFormNameAsIntentionB
$this->assertSame($valid, $form->isValid());
}

/**
* @dataProvider provideBoolean
*/
public function testValidateTokenOnBindIfRootAndCompoundUsesTypeClassAsIntentionIfEmptyFormName($valid)
{
$this->csrfProvider->expects($this->once())
->method('isCsrfTokenValid')
->with('Symfony\Component\Form\Extension\Core\Type\FormType', 'token')
->will($this->returnValue($valid));

$form = $this->factory
->createNamedBuilder('', 'form', null, array(
'csrf_field_name' => 'csrf',
'csrf_provider' => $this->csrfProvider,
'compound' => true,
))
->add('child', 'text')
->getForm();

$form->bind(array(
'child' => 'foobar',
'csrf' => 'token',
));

// Remove token from data
$this->assertSame(array('child' => 'foobar'), $form->getData());

// Validate accordingly
$this->assertSame($valid, $form->isValid());
}

public function testFailIfRootAndCompoundAndTokenMissing()
{
$this->csrfProvider->expects($this->never())
Expand Down

0 comments on commit 219e44d

Please sign in to comment.