public function ReadTest::testRead

Tests several valid and invalid read requests on all entity types.

File

drupal/core/modules/rest/lib/Drupal/rest/Tests/ReadTest.php, line 35
Definition of Drupal\rest\test\ReadTest.

Class

ReadTest
Tests resource read operations on test entities, nodes and users.

Namespace

Drupal\rest\Tests

Code

public function testRead() {

  // @todo once EntityNG is implemented for other entity types expand this at
  // least to nodes and users.
  // Define the entity types we want to test.
  $entity_types = array(
    'entity_test',
  );
  foreach ($entity_types as $entity_type) {
    $this
      ->enableService('entity:' . $entity_type);

    // Create a user account that has the required permissions to delete
    // resources via the web API.
    $account = $this
      ->drupalCreateUser(array(
      'restful get entity:' . $entity_type,
    ));

    // Reset cURL here because it is confused from our previously used cURL
    // options.
    unset($this->curlHandle);
    $this
      ->drupalLogin($account);

    // Create an entity programmatically.
    $entity = $this
      ->entityCreate($entity_type);
    $entity
      ->save();

    // Read it over the web API.
    $response = $this
      ->httpRequest('entity/' . $entity_type . '/' . $entity
      ->id(), 'GET', NULL, 'application/vnd.drupal.ld+json');
    $this
      ->assertResponse('200', 'HTTP response code is correct.');
    $this
      ->assertHeader('Content-Type', 'application/vnd.drupal.ld+json');
    $data = drupal_json_decode($response);

    // Only assert one example property here, other properties should be
    // checked in serialization tests.
    $this
      ->assertEqual($data['uuid'][LANGUAGE_DEFAULT][0]['value'], $entity
      ->uuid(), 'Entity UUID is correct');

    // Try to read an entity that does not exist.
    $response = $this
      ->httpRequest('entity/' . $entity_type . '/9999', 'GET', NULL, 'application/ld+json');
    $this
      ->assertResponse(404);
    $this
      ->assertEqual($response, 'Entity with ID 9999 not found', 'Response message is correct.');

    // Try to read an entity without proper permissions.
    $this
      ->drupalLogout();
    $response = $this
      ->httpRequest('entity/' . $entity_type . '/' . $entity
      ->id(), 'GET', NULL, 'application/vnd.drupal.ld+json');
    $this
      ->assertResponse(403);
    $this
      ->assertNull(drupal_json_decode($response), 'No valid JSON found.');
  }

  // Try to read a resource which is not web API enabled.
  $account = $this
    ->drupalCreateUser();

  // Reset cURL here because it is confused from our previously used cURL
  // options.
  unset($this->curlHandle);
  $this
    ->drupalLogin($account);
  $response = $this
    ->httpRequest('entity/user/' . $account
    ->id(), 'GET', NULL, 'application/vnd.drupal.ld+json');
  $this
    ->assertResponse(404);
  $this
    ->assertNull(drupal_json_decode($response), 'No valid JSON found.');
}