You are here

protected function DrupalWebTestCase::drupalPost in SimpleTest 7

Same name and namespace in other branches
  1. 6.2 drupal_web_test_case.php \DrupalWebTestCase::drupalPost()
  2. 7.2 drupal_web_test_case.php \DrupalWebTestCase::drupalPost()

Execute a POST request on a Drupal page. It will be done as usual POST request with SimpleBrowser.

Parameters

$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. A checkbox can be set to TRUE to be checked and FALSE to be unchecked. Note that when a form contains file upload fields, other fields cannot start with the '@' character.

Multiple select fields can be set using name[] and setting each of the possible values. Example: $edit = array(); $edit['name[]'] = array('value1', 'value2');

$submit: Value of the submit button.

$options: Options to be forwarded to url().

$headers: An array containing additional HTTP request headers, each formatted as "name: value".

22 calls to DrupalWebTestCase::drupalPost()
ActionLoopTestCase::testActionLoop in tests/actions.test
Set up a loop with 10-50 recursions, and see if it aborts properly.
ActionsConfigurationTestCase::testActionConfiguration in tests/actions.test
Test the configuration of advanced actions through the administration interface.
DrupalWebTestCase::drupalLogin in ./drupal_web_test_case.php
Log in a user with the internal browser.
FileSaveUploadTest::setUp in tests/file.test
Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix.…
FileSaveUploadTest::testExistingError in tests/file.test
Test for failure when uploading over a file that already exists.

... See full list

File

./drupal_web_test_case.php, line 1436

Class

DrupalWebTestCase
Test case for typical Drupal tests.

Code

protected function drupalPost($path, $edit, $submit, array $options = array(), array $headers = array()) {
  $submit_matches = FALSE;
  if (isset($path)) {
    $html = $this
      ->drupalGet($path, $options);
  }
  if ($this
    ->parse()) {
    $edit_save = $edit;

    // Let's iterate over all the forms.
    $forms = $this
      ->xpath('//form');
    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, $submit, $form);
      $action = isset($form['action']) ? $this
        ->getAbsoluteUrl($form['action']) : $this
        ->getUrl();

      // We post only if we managed to handle every field in edit and the
      // submit button matches.
      if (!$edit && $submit_matches) {
        $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);
        }
        $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;
        }
        $this
          ->verbose('POST request to: ' . $path . '<hr />Ending URL: ' . $this
          ->getUrl() . '<hr />Fields: ' . highlight_string('<?php ' . var_export($post_array, TRUE), TRUE) . '<hr />' . $out);
        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,
      )));
    }
    $this
      ->assertTrue($submit_matches, t('Found the @submit button', array(
      '@submit' => $submit,
    )));
    $this
      ->fail(t('Found the requested form fields at @path', array(
      '@path' => $path,
    )));
  }
}