- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在使用 Liip functional test bundle 在 Symfony 中创建一个功能测试.
我目前无法提交表单。
我正在尝试使用功能测试添加一个新的“日志”。
如果我尝试通过 UI 添加新日志,我会收到以下请求参数:
'WorkLog' => array(
'submit' => '',
'hours' => '8',
'minutes' => '0',
'note' => 'some text',
'_token' => '4l5oPcdCRzxDKKlJt_RG-B1342X52o0C187ZLLVWre4'
);
但是当测试提交表单时,我得到相同的参数但没有 token
'WorkLog' => array(
'submit' => '',
'hours' => '8',
'minutes' => '0',
'note' => 'some text'
);
我以为我可以通过将“_token”字段添加到表单请求来解决问题,但是当我运行然后再次测试时它给了我一个错误:
InvalidArgumentException: Unreachable field "_token"
功能测试代码:
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);
}
}
我的问题:如何使用 token 提交表单?
最佳答案
我不使用 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,
),
));
希望这对您有所帮助。
关于php - Symfony2功能测试Unreachable字段 "_token",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33830853/
我正在使用 Liip functional test bundle 在 Symfony 中创建一个功能测试. 我目前无法提交表单。 我正在尝试使用功能测试添加一个新的“日志”。 如果我尝试通过 UI
当我尝试登录时,请求被 Security 组件屏蔽。我怎样才能让它正常工作? 我有一个简单的登录表单 Form->create('User', array('acti
编辑: 提出这个问题后获得的一系列新见解告诉我问题是什么,它绝对与描述的服务器迁移没有任何关系。 给出的两个答案显示了如何为 CakePHP 2 和 3“修复”这个问题,但请记住这可能会带来安全风险。
我在 laravel 4.1 中做简单的 cms,我创建了许多表单并且它们工作正常,但我创建的最后一个表单在提交时抛出错误。 Illuminate \ Database \ Eloquent \ Ma
我有一个应用程序,客户端用 HTML 和 Angularjs 编写,服务器端用 Laravel 5 编写。每次提交表单时,我都会使用 $http 将模型发送到我的 laravel 5 应用程序,但我一
我不明白为什么 AJAX 请求 (XSRF-TOKEN) 的 token 与 _token 不同正常形式使用。此外,它更长。为什么?为什么有 2 个 token 呢?为什么不使用一个对 ajax 和普
我尝试更新我的表类别的记录,但它显示错误 Column not found: 1054 Unknown column '_token' 路线 Route::post('/categorias/edit
我是一名优秀的程序员,十分优秀!