测试symfony2表单时无效的csrf标记

时间:2015-07-13 12:24:08

标签: php forms symfony testing csrf

我正在为symfony2 [2.7.1]网站编写功能测试。 测试填写一个表单,提交它(从而创建一个实体),然后它跟随到第二个表单的链接,它尝试创建另一个实体(与第一个实体相关)。

尝试提交第二个表单时,我收到无效的csrf令牌错误。

有谁知道发生了什么?

我忘记了代码! [facepalm]

请注意,我更改了按钮的字符串和断言,因为它们原来是希伯来语。

此外,当我自动渲染表单时,也会出现错误(twig:form(form))

public function testCompleteScenario()
{
    // create a customer
    $client = $this->getAdminClient();
    $crawler = $client->request('GET', '/customer/new');
    $this->assertGreaterThan(
        0,
        $crawler->filter('html:contains("create new customer")')->count(),
        "Failed to get to the create customer form"
    );
    $customerForm = $crawler->selectButton('save')->form();
    $customerForm['crm_customer[name]'] = 'test_customer' . rand(1000, 99999);
    $client->submit($customerForm);
    $crawler = $client->followRedirect();
    $this->assertGreaterThan(
        0,
        $crawler->filter('html:contains("Customer created successfully")')->count(),
        "Failed to create a customer"
    );


    $this->assertGreaterThan(
        0,
        $crawler->filter('html:contains("create new contact")')->count()
    );

    // click the "add new contact" button
    $createContactBtn = $crawler
        ->filter('a:contains("create new contact")')
        ->eq(0)
        ->link()
    ;
    $crawler = $client->click($createContactBtn);

    $this->assertGreaterThan(
        0,
        $crawler->filter('button:contains("save")')->count()
    );

    // create the contact
    $contactForm = $crawler->selectButton('save')->form();
    $contactForm['crm_corebundle_contact[firstName]'] = 'test_contact' . rand(1000, 99999);
    $contactForm['crm_corebundle_contact[lastName]'] = 'lastName';
    $client->submit($contactForm);
    $crawler = $client->followRedirect();
    $this->assertGreaterThan(
        0,
        $crawler->filter('html:contains("contact created successfully")')->count(),
        "Failed to create the contact"
    );
}

0 个答案:

没有答案