功能测试失败,csrf_protection = true

时间:2014-11-20 20:11:03

标签: php symfony testing

在用户注册的功能测试中:当csrf_protection: true时,即使注册成功开发,注册也会失败。 csrf_protection: false时测试成功。 (应用程序使用PUGXMultiUserBundle)。我已经尝试清除测试缓存等。将$this->client->getResponse()->getContent()转储到文件会显示包含所有字段的注册表单,但密码已完成。逐步调试测试会显示提交的_token字段,但在fos_user_registration_form[]中到达public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true)行之前,似乎已从Client.php中删除。

目前我已将csrf_protection: false设置为config_test.yml - 不是最佳解决方案!

RegistrationFunctionalTest

namespace Truckee\UserBundle\Tests\Controller;

use Liip\FunctionalTestBundle\Test\WebTestCase;
class RegistrationFunctionalTest extends WebTestCase
{
    private $volunteerValues;
    private $client;

    public function setup()
    {
        $classes = array(
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadFocusSkillData',
            'Truckee\MatchingBundle\DataFixtures\SampleData\LoadTemplateData',
        );
        $this->loadFixtures($classes);
        $this->client = static::createClient();

        $this->volunteerValues = array(
            'fos_user_registration_form' => array(
                'email' => 'hvolunteer@bogus.info',
                'username' => 'hvolunteer',
                'firstName' => 'Harry',
                'lastName' => 'Volunteer',
                'plainPassword' => array(
                    'first' => '123Abcd',
                    'second' => '123Abcd',
                ),
                'focuses' => array('1'),
                'skills' => array('14'),
            )
        );
    }


    public function submitVolunteerForm()
    {
        $crawler = $this->client->request('GET', '/register/volunteer');
        $form = $crawler->selectButton('Save')->form();
        $this->client->request($form->getMethod(), $form->getUri(), $this->volunteerValues);
    }

    public function testRegisterVolunteer()
    {
        $this->submitVolunteerForm();

        $this->client->enableProfiler();
        if ($profile = $this->client->getProfile()) {
            $mailCollector = $this->client->getProfile()->getCollector('swiftmailer');
             $this->assertEquals(1, $mailCollector->getMessageCount());
        }
        $crawler = $this->client->followRedirect();

        $this->assertTrue($crawler->filter('html:contains("An email has been sent")')->count() > 0);
    }
...
}

显示_token

的注册表单(摘要)
<div class="row">
    <div class="col-md-5">
        {%if form._token is defined %}{{ form_widget(form._token) }}{% endif %}
        {{ form_row(form.firstName) }}
        {{ form_row(form.lastName) }}
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

经过多次实验,我发现了自己的解决方案: 将'intention' => 'registration',添加到用户表单类!卫生署!

编辑:以上不是解决方案!!!

基本问题是使用提交表单的数组方法($this->client->request($form->getMethod(), $form->getUri(), $this->volunteerValues);。这样做排除了csrf令牌!相反,我这样做是为了允许使用表单的令牌字段:

public function submitVolunteerForm()
{
    $crawler = $this->client->request('GET', '/register/volunteer');
    $form = $crawler->selectButton('Save')->form();
    $form['fos_user_registration_form[email]'] = 'hvolunteer@bogus.info';
    $form['fos_user_registration_form[username]'] = 'hvolunteer';
    $form['fos_user_registration_form[firstName]'] = 'Harry';
    $form['fos_user_registration_form[lastName]'] = 'Volunteer';
    $form['fos_user_registration_form[plainPassword][first]'] = '123Abcd';
    $form['fos_user_registration_form[plainPassword][second]'] = '123Abcd';
    $form['fos_user_registration_form[focuses]'] = [1];
    $form['fos_user_registration_form[skills]'] = [14];

    $crawler = $this->client->submit($form);
}