Execute a POST request on a Drupal page. It will be done as usual POST request with SimpleBrowser.
$path: Location of the post form. Either a Drupal path or an absolute path or NULL to post to the current page. For multi-stage forms you can set the path to NULL and have it post to the last received page. Example:
// First step in form.
$edit = array(...);
$this->drupalPost('some_url', $edit, t('Save'));
// Second step in form.
$edit = array(...);
$this->drupalPost(NULL, $edit, t('Save'));
$edit: Field data in an associative array. Changes the current input fields (where possible) to the values indicated.
When working with form tests, the keys for an $edit element should match the 'name' parameter of the HTML of the form. For example, the 'body' field for a node has the following HTML:
<textarea id="edit-body-und-0-value" class="text-full form-textarea
resize-vertical" placeholder="" cols="60" rows="9"
name="body[und][0][value]"></textarea>
When testing this field using an $edit parameter, the code becomes:
$edit["body[und][0][value]"] = 'My test value';
A checkbox can be set to TRUE to be checked and should be set to FALSE to be unchecked. Multiple select fields can be tested using 'name[]' and setting each of the desired values in an array:
$edit = array();
$edit['name[]'] = array(
'value1',
'value2',
);
Note that when a form contains file upload fields, other fields cannot start with the '@' character.
$submit: Value of the submit button whose click is to be emulated. For example, t('Save'). The processing of the request depends on this value. For example, a form may have one button with the value t('Save') and another button with the value t('Delete'), and execute different code depending on which one is clicked.
This function can also be called to emulate an Ajax submission. In this case, this value needs to be an array with the following keys:
This can also be set to NULL in order to emulate an Internet Explorer submission of a form with a single text field, and pressing ENTER in that textfield: under these conditions, no button information is added to the POST data.
$options: Options to be forwarded to the url generator.
$headers: An array containing additional HTTP request headers, each formatted as "name: value".
$form_html_id: (optional) HTML ID of the form to be submitted. On some pages there are many identical forms, so just using the value of the submit button is not enough. For example: 'trigger-node-presave-assign-form'. Note that this is not the Drupal $form_id, but rather the HTML ID of the form, which is typically the same thing but with hyphens replacing the underscores.
$extra_post: (optional) A string of additional data to append to the POST submission. This can be used to add POST data for which there are no HTML fields, as is done by drupalPostAJAX(). This string is literally appended to the POST data, so it must already be urlencoded and contain a leading "&" (e.g., "&extra_var1=hello+world&extra_var2=you%26me").
protected function drupalPost($path, $edit, $submit, array $options = array(), array $headers = array(), $form_html_id = NULL, $extra_post = NULL) {
$submit_matches = FALSE;
$ajax = is_array($submit);
if (isset($path)) {
$this
->drupalGet($path, $options);
}
if ($this
->parse()) {
$edit_save = $edit;
// Let's iterate over all the forms.
$xpath = "//form";
if (!empty($form_html_id)) {
$xpath .= "[@id='" . $form_html_id . "']";
}
$forms = $this
->xpath($xpath);
foreach ($forms as $form) {
// We try to set the fields of this form as specified in $edit.
$edit = $edit_save;
$post = array();
$upload = array();
$submit_matches = $this
->handleForm($post, $edit, $upload, $ajax ? NULL : $submit, $form);
$action = isset($form['action']) ? $this
->getAbsoluteUrl((string) $form['action']) : $this
->getUrl();
if ($ajax) {
$action = $this
->getAbsoluteUrl(!empty($submit['path']) ? $submit['path'] : 'system/ajax');
// Ajax callbacks verify the triggering element if necessary, so while
// we may eventually want extra code that verifies it in the
// handleForm() function, it's not currently a requirement.
$submit_matches = TRUE;
}
// We post only if we managed to handle every field in edit and the
// submit button matches.
if (!$edit && ($submit_matches || !isset($submit))) {
$post_array = $post;
if ($upload) {
// TODO: cURL handles file uploads for us, but the implementation
// is broken. This is a less than elegant workaround. Alternatives
// are being explored at #253506.
foreach ($upload as $key => $file) {
$file = drupal_realpath($file);
if ($file && is_file($file)) {
$post[$key] = '@' . $file;
}
}
}
else {
foreach ($post as $key => $value) {
// Encode according to application/x-www-form-urlencoded
// Both names and values needs to be urlencoded, according to
// http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
$post[$key] = urlencode($key) . '=' . urlencode($value);
}
$post = implode('&', $post) . $extra_post;
}
$out = $this
->curlExec(array(
CURLOPT_URL => $action,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $post,
CURLOPT_HTTPHEADER => $headers,
));
// Ensure that any changes to variables in the other thread are picked up.
$this
->refreshVariables();
// Replace original page output with new output from redirected page(s).
if ($new = $this
->checkForMetaRefresh()) {
$out = $new;
}
$verbose = 'POST request to: ' . $path;
$verbose .= '<hr />Ending URL: ' . $this
->getUrl();
if ($this->dumpHeaders) {
$verbose .= '<hr />Headers: <pre>' . check_plain(var_export(array_map('trim', $this->headers), TRUE)) . '</pre>';
}
$verbose .= '<hr />Fields: ' . highlight_string('<?php ' . var_export($post_array, TRUE), TRUE);
$verbose .= '<hr />' . $out;
$this
->verbose($verbose);
return $out;
}
}
// We have not found a form which contained all fields of $edit.
foreach ($edit as $name => $value) {
$this
->fail(t('Failed to set field @name to @value', array(
'@name' => $name,
'@value' => $value,
)));
}
if (!$ajax && isset($submit)) {
$this
->assertTrue($submit_matches, format_string('Found the @submit button', array(
'@submit' => $submit,
)));
}
$this
->fail(format_string('Found the requested form fields at @path', array(
'@path' => $path,
)));
}
}