Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Fixed cannot unset string offsets in CsrfValidationListener #5838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public function preBind(FormEvent $event)
$form->addError(new FormError('The CSRF token is invalid. Please try to resubmit the form.'));
}

unset($data[$this->fieldName]);
if (is_array($data)) {
unset($data[$this->fieldName]);
}
}

$event->setData($data);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Extension\Csrf\EventListener;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\Extension\Csrf\EventListener\CsrfValidationListener;

class CsrfValidationListenerTest extends \PHPUnit_Framework_TestCase
{
protected $dispatcher;
protected $factory;
protected $csrfProvider;

protected function setUp()
{
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
$this->markTestSkipped('The "EventDispatcher" component is not available');
}

$this->dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->factory = $this->getMock('Symfony\Component\Form\FormFactoryInterface');
$this->csrfProvider = $this->getMock('Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface');
$this->form = $this->getBuilder('post')
->setDataMapper($this->getDataMapper())
->getForm();
}

protected function tearDown()
{
$this->dispatcher = null;
$this->factory = null;
$this->csrfProvider = null;
$this->form = null;
}

protected function getBuilder($name = 'name')
{
return new FormBuilder($name, null, $this->dispatcher, $this->factory, array('compound' => true));
}

protected function getForm($name = 'name')
{
return $this->getBuilder($name)->getForm();
}

protected function getDataMapper()
{
return $this->getMock('Symfony\Component\Form\DataMapperInterface');
}

protected function getMockForm()
{
return $this->getMock('Symfony\Component\Form\Tests\FormInterface');
}

// https://github.com/symfony/symfony/pull/5838
public function testStringFormData()
{
$data = "XP4HUzmHPi";
$event = new FormEvent($this->form, $data);

$validation = new CsrfValidationListener('csrf', $this->csrfProvider, 'unknown');
$validation->preBind($event);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test should include an assertion

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tobion Since the error will be a fatal error, I can't link a assert to this test (since the test itself will trigger the fatal error).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jfcixmedia If you run PHPUnit in strict mode, a test without any assertion in it will be marked as a failure

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof It was unknown to me. Thanks


// Validate accordingly
$this->assertSame($data, $event->getData());
}
}