php - 功能测试失败,csrf_protection = true

标签 php symfony testing

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

现在我在 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 token !相反,我这样做是为了允许使用表单的 token 字段:

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);
}

关于php - 功能测试失败,csrf_protection = true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27048618/

相关文章:

php - 爆炸结果然后内爆 php

php - mysql查询是否存在更新否则插入没有唯一ID的查询

unit-testing - Symfony2,夹具和单元测试。在多对多关系中的测试期间添加测试数据

php - Symfony2 中具有相同模式的多个安全防火墙

java - 如何测试服务层中的方法调用java中的存储过程?

php - 如何使 CodeIgniter 文件上传类接受所有扩展?

php - MySQL 服务器安装时间过长

php - 在 Symfony2 中使用 PdfBundle 强制一个新页面

java - Easymock 调用 Autowiring 的对象方法

django - 测试链接是否指向 Django 中的正确页面