Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
The form csrf extension should use the translator if available
Browse files Browse the repository at this point in the history
Fixes #896
  • Loading branch information
davedevelopment committed Jan 29, 2014
1 parent 68f9b2a commit c074545
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Silex/Provider/FormServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ public function register(Application $app)
return array();
});

$app['form.extension.csrf'] = $app->share(function ($app) {
if (isset($app['translator'])) {
return new CsrfExtension($app['form.csrf_provider'], $app['translator']);
}

return new CsrfExtension($app['form.csrf_provider']);
});

$app['form.extensions'] = $app->share(function ($app) {
$extensions = array(
new CsrfExtension($app['form.csrf_provider']),
$app['form.extension.csrf'],
new HttpFoundationExtension(),
);

Expand Down
46 changes: 46 additions & 0 deletions tests/Silex/Tests/Provider/FormServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

use Silex\Application;
use Silex\Provider\FormServiceProvider;
use Silex\Provider\TranslationServiceProvider;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
use Symfony\Component\Form\FormTypeGuesserChain;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -59,6 +62,36 @@ public function testFormServiceProviderWillLoadTypeGuessers()

$this->assertInstanceOf('Symfony\Component\Form\FormFactory', $app['form.factory']);
}

public function testFormServiceProviderWillUseTranslatorIfAvailable()
{
$app = new Application();

$app->register(new FormServiceProvider());
$app->register(new TranslationServiceProvider());
$app['translator.domains'] = array(
'messages' => array(
'de' => array (
'The CSRF token is invalid. Please try to resubmit the form.' => 'German translation',
)
)
);
$app['locale'] = 'de';

$app['form.csrf_provider'] = $app->share(function () {
return new FakeCsrfProvider();
});

$form = $app['form.factory']->createBuilder('form', array())
->getForm();

$form->handleRequest($req = Request::create('/', 'POST', array('form' => array(
'_token' => 'the wrong token'
))));

$this->assertFalse($form->isValid());
$this->assertContains('ERROR: German translation', $form->getErrorsAsString());
}
}

class DummyFormTypeExtension extends AbstractTypeExtension
Expand All @@ -73,3 +106,16 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$resolver->setOptional(array('image_path'));
}
}

class FakeCsrfProvider implements CsrfProviderInterface
{
public function generateCsrfToken($intention)
{
return $intention.'123';
}

public function isCsrfTokenValid($intention, $token)
{
return $token === $this->generateCsrfToken($intention);
}
}

0 comments on commit c074545

Please sign in to comment.