Symfony2 function test Unreachable field "_token"

I am creating a functional test in Symfony using the Liip functional test suite .

I am currently late in submitting the form.
I am trying to add a new “log” using a functional test.

If I try to add a new log via the user interface, I get the following query parameters:

'WorkLog' => array(
    'submit' => '',
    'hours' => '8',
    'minutes' => '0',
    'note' => 'some text',
    '_token' => '4l5oPcdCRzxDKKlJt_RG-B1342X52o0C187ZLLVWre4' 
);

But when the test submits the form, I get the same parameters, but without a token

 'WorkLog' => array(
    'submit' => '',
    'hours' => '8',
    'minutes' => '0',
    'note' => 'some text'
);

I thought I could fix the problem by adding the "_token" field to the form request, but when I ran, repeat the test, it gave me an error:

InvalidArgumentException: unreachable field "_token"

Functional Test Code:

namespace App\AdminBundle\Tests\Controller;

use Liip\FunctionalTestBundle\Test\WebTestCase;

use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\BrowserKit\Cookie;

class LogControllerTest extends WebTestCase
{
    private $client;
    private $em;
    private $fixtures;

    public function setUp()
    {
        $this->client = static::makeClient();
        $this->em = $this->client->getContainer()->get('doctrine')->getManager();

        $this->fixtures = $this->loadFixtures(array(
            'App\AdminBundle\DataFixtures\ORM\LoadUserData',
            'App\AdminBundle\DataFixtures\ORM\LoadSubscriptionTypesData',
            'App\AdminBundle\DataFixtures\ORM\LoadSubscriptionData',
            'App\AdminBundle\DataFixtures\ORM\LoadWorkLogData',
        ))->getReferenceRepository();
    }

    public function testAddNewLog()
    {
        $accountId = $this->fixtures->getReference('userAccount')->getId();

        // log in with admin account
        $this->logIn('adminAccount');

        $crawler = $this->client->request('GET', '/admin/worklog/account/'.$accountId.'/add');
        $csrfToken = $this->client->getContainer()->get('form.csrf_provider')->generateCsrfToken('post_type');

        $form = $crawler->selectButton('WorkLog_submit')->form(array(
            'WorkLog' => array(
                'hours' => '8',
                'minutes' => '0',
                'note' => 'some text',
                '_token' => $csrfToken
            ),
        ), 'POST');

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

: ?

+4
2

Liip, _token :

    $crawler = $this->client->request('GET', $url);

    // retrieves the form token
    $token = $crawler->filter('[name="select_customer[_token]"]')->attr("value");

    // makes the POST request
    $crawler = $this->client->request('POST', $url, array(
        'select_customer' => array(
            '_token' => $token,
            'customerId' => $customerId,
        ),
    ));

, .

+3

... . , Stackoverflow , . .

:

    $form = $crawler->selectButton('WorkLog_submit')->form(array(
        'WorkLog' => array(
            'hours' => '8',
            'minutes' => '0',
            'note' => 'some text',
            '_token' => $csrfToken
        ),
    ), 'POST');

. , Liip - , . , :

( , Liip):

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class GameControllerTest extends WebTestCase
{
    public function testLoadGame(){


        $client = static::createClient();
        $crawler = $client->request('GET', '/loadGame');
        $form = $crawler->selectButton('Load')->form();
        $field = $form->get("load[uuid]");
        $field->setValue($uuid1[0]);
        $form->set($field);
        $client->submit($form);
        $response = $client->getResponse();
        self::assertTrue($response->isRedirect('/game'));

    }
}

, , :

    $form = $crawler->selectButton('WorkLog_submit')->form();        
    //dump($form) //uncomment this line to have a look on the array of array
    $fieldToken = $form->get("WorkLog[_token]");
    $fieldToken->setValue($csrfToken);
    $form->set($fieldToken);
    $client->submit($form);
0

Source: https://habr.com/ru/post/1616762/


All Articles