Created
April 7, 2023 12:33
-
-
Save joachim-n/7b1cee453372131bb7b65846739f5b22 to your computer and use it in GitHub Desktop.
Make a POST request to a form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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