功能测试失败,csrf_protection = true


Functional test fails with csrf_protection = true

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

现在我已经config_test.yml设置了csrf_protection: false - 不是最好的解决方案!

注册功能测试

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>

经过多次实验,我发现了自己的解决方案:将'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);
}