You are here

protected function WebTestBase::curlHeaderCallback in Drupal 8

Reads headers and registers errors received from the tested site.

Parameters

$curlHandler: The cURL handler.

$header: An header.

See also

_drupal_log_error()

File

core/modules/simpletest/src/WebTestBase.php, line 689

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function curlHeaderCallback($curlHandler, $header) {

  // Header fields can be extended over multiple lines by preceding each
  // extra line with at least one SP or HT. They should be joined on receive.
  // Details are in RFC2616 section 4.
  if ($header[0] == ' ' || $header[0] == "\t") {

    // Normalize whitespace between chucks.
    $this->headers[] = array_pop($this->headers) . ' ' . trim($header);
  }
  else {
    $this->headers[] = $header;
  }

  // Errors are being sent via X-Drupal-Assertion-* headers,
  // generated by _drupal_log_error() in the exact form required
  // by \Drupal\simpletest\WebTestBase::error().
  if (preg_match('/^X-Drupal-Assertion-[0-9]+: (.*)$/', $header, $matches)) {
    $parameters = unserialize(urldecode($matches[1]));

    // Handle deprecation notices triggered by system under test.
    if ($parameters[1] === 'User deprecated function') {
      if (getenv('SYMFONY_DEPRECATIONS_HELPER') !== 'disabled') {
        $message = (string) $parameters[0];
        $test_info = TestDiscovery::getTestInfo(get_called_class());
        if (!in_array('legacy', $test_info['groups']) && !in_array($message, DeprecationListenerTrait::getSkippedDeprecations())) {
          call_user_func_array([
            &$this,
            'error',
          ], $parameters);
        }
      }
    }
    else {

      // Call \Drupal\simpletest\WebTestBase::error() with the parameters from
      // the header.
      call_user_func_array([
        &$this,
        'error',
      ], $parameters);
    }
  }

  // Save cookies.
  if (preg_match('/^Set-Cookie: ([^=]+)=(.+)/', $header, $matches)) {
    $name = $matches[1];
    $parts = array_map('trim', explode(';', $matches[2]));
    $value = array_shift($parts);
    $this->cookies[$name] = [
      'value' => $value,
      'secure' => in_array('secure', $parts),
    ];
    if ($name === $this
      ->getSessionName()) {
      if ($value != 'deleted') {
        $this->sessionId = $value;
      }
      else {
        $this->sessionId = NULL;
      }
    }
  }

  // This is required by cURL.
  return strlen($header);
}