在用户注册的功能测试中:csrf_protection: true时,即使在dev中注册成功,注册也会失败。 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-不是最好的解决方案!

注册功能测试

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',添加到用户表单类! h!

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

基本问题是使用数组方法(提交表单的$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);
}

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

10-10 06:00