Skip to content

Instantly share code, notes, and snippets.

@joachim-n
Created April 7, 2023 12:33
Show Gist options
  • Save joachim-n/7b1cee453372131bb7b65846739f5b22 to your computer and use it in GitHub Desktop.
Save joachim-n/7b1cee453372131bb7b65846739f5b22 to your computer and use it in GitHub Desktop.
Make a POST request to a form
/**
* Make a POST request to a form.
*
* @param string $url
* @param array $data
*/
protected function doPostForm(string $url, array $data = []) {
$request = Request::create($url);
$response = $this->doRequest($request);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
$data['form_build_id'] = (string) $this->cssSelect('input[name="form_build_id"]')[0]->attributes()->value[0];
$data['form_id'] = (string) $this->cssSelect('input[name="form_id"]')[0]->attributes()->value[0];
if (\Drupal::currentUser()->isAuthenticated()) {
$data['form_token'] = (string) $this->cssSelect('input[name="form_token"]')[0]->attributes()->value[0];
}
$request = Request::create($url, 'POST', $data);
$response = $this->doRequest($request);
$content = $response->getContent();
self::assertEquals(303, $response->getStatusCode(), $this->content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment