RESTTestBase.php 5.45 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

/**
 * @file
 * Definition of Drupal\rest\test\RESTTestBase.
 */

namespace Drupal\rest\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Test helper class that provides a REST client method to send HTTP requests.
 */
abstract class RESTTestBase extends WebTestBase {

17 18 19 20 21 22 23
  /**
   * Stores HTTP response headers from the last HTTP request.
   *
   * @var array
   */
  protected $responseHeaders;

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
  /**
   * Helper function to issue a HTTP request with simpletest's cURL.
   *
   * @param string $url
   *   The relative URL, e.g. "entity/node/1"
   * @param string $method
   *   HTTP method, one of GET, POST, PUT or DELETE.
   * @param array $body
   *   Either the body for POST and PUT or additional URL parameters for GET.
   * @param string $format
   *   The MIME type of the transmitted content.
   */
  protected function httpRequest($url, $method, $body = NULL, $format = 'application/ld+json') {
    switch ($method) {
      case 'GET':
        // Set query if there are additional GET parameters.
        $options = isset($body) ? array('absolute' => TRUE, 'query' => $body) : array('absolute' => TRUE);
41
        $curl_options = array(
42 43
          CURLOPT_HTTPGET => TRUE,
          CURLOPT_URL => url($url, $options),
44
          CURLOPT_NOBODY => FALSE
45
        );
46 47
        break;

48
      case 'POST':
49
        $curl_options = array(
50 51 52 53 54 55
          CURLOPT_HTTPGET => FALSE,
          CURLOPT_POST => TRUE,
          CURLOPT_POSTFIELDS => $body,
          CURLOPT_URL => url($url, array('absolute' => TRUE)),
          CURLOPT_NOBODY => FALSE,
          CURLOPT_HTTPHEADER => array('Content-Type: ' . $format),
56 57 58
        );
        break;

59
      case 'PUT':
60
        $curl_options = array(
61 62 63 64 65 66
          CURLOPT_HTTPGET => FALSE,
          CURLOPT_CUSTOMREQUEST => 'PUT',
          CURLOPT_POSTFIELDS => $body,
          CURLOPT_URL => url($url, array('absolute' => TRUE)),
          CURLOPT_NOBODY => FALSE,
          CURLOPT_HTTPHEADER => array('Content-Type: ' . $format),
67 68 69
        );
        break;

70
      case 'DELETE':
71
        $curl_options = array(
72 73 74 75
          CURLOPT_HTTPGET => FALSE,
          CURLOPT_CUSTOMREQUEST => 'DELETE',
          CURLOPT_URL => url($url, array('absolute' => TRUE)),
          CURLOPT_NOBODY => FALSE,
76 77
        );
        break;
78
    }
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    // Include all HTTP headers in the response.
    $curl_options[CURLOPT_HEADER] = TRUE;

    $response = $this->curlExec($curl_options);

    list($header, $body) = explode("\r\n\r\n", $response, 2);
    $header_lines = explode("\r\n", $header);
    foreach ($header_lines as $line) {
      $parts = explode(':', $line, 2);
      $this->responseHeaders[$parts[0]] = isset($parts[1]) ? trim($parts[1]) : '';
    }

    $this->verbose($method . ' request to: ' . $url .
      '<hr />Code: ' . curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE) .
      '<hr />Response headers: ' . $header .
      '<hr />Response body: ' . $body);

    return $body;
  }

  /**
   * Creates entity objects based on their types.
   *
   * Required properties differ from entity type to entity type, so we keep a
   * minimum mapping here.
   *
   * @param string $entity_type
   *   The type of the entity that should be created..
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The new entity object.
   */
  protected function entityCreate($entity_type) {
    switch ($entity_type) {
      case 'entity_test':
        return entity_create('entity_test', array('name' => $this->randomName(), 'user_id' => 1));
      case 'node':
        return entity_create('node', array('title' => $this->randomString()));
      case 'user':
        return entity_create('user', array('name' => $this->randomName()));
      default:
        return entity_create($entity_type, array());
    }
  }

  /**
   * Enables the web service interface for a specific entity type.
   *
   * @param string|FALSE $resource_type
   *   The resource type that should get web API enabled or FALSE to disable all
   *   resource types.
   */
  protected function enableService($resource_type) {
    // Enable web API for this entity type.
    $config = config('rest');
    if ($resource_type) {
      $config->set('resources', array(
        $resource_type => $resource_type,
      ));
    }
    else {
      $config->set('resources', array());
    }
    $config->save();

    // Rebuild routing cache, so that the web API paths are available.
    drupal_container()->get('router.builder')->rebuild();
    // Reset the Simpletest permission cache, so that the new resource
    // permissions get picked up.
    drupal_static_reset('checkPermissions');
  }

  /**
   * Check if a HTTP response header exists and has the expected value.
   *
   * @param string $header
   *   The header key, example: Content-Type
   * @param string $value
   *   The header value.
   * @param string $message
   *   (optional) A message to display with the assertion.
   * @param string $group
   *   (optional) The group this message is in, which is displayed in a column
   *   in test output. Use 'Debug' to indicate this is debugging output. Do not
   *   translate this string. Defaults to 'Other'; most tests do not override
   *   this default.
   *
   * @return bool
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertHeader($header, $value, $message = '', $group = 'Browser') {
    $match = isset($this->responseHeaders[$header]) && $this->responseHeaders[$header] == $value;
    return $this->assertTrue($match, $message ? $message : 'HTTP response header ' . $header . ' with value ' . $value . ' found.', $group);
172 173
  }
}