You are here

public function ServicesEntityGenericEntityResource::testEntityRetrieve in Services Entity API 7.2

Test 'Retrieve' service.

File

tests/services_entity.test, line 363
Services Entity Tests

Class

ServicesEntityGenericEntityResource
Test resources on the Generic controller, using a test entity type.

Code

public function testEntityRetrieve() {

  // Create our privileged user.
  $this->privilegedUser = $this
    ->drupalCreateUser(array(
    'view services_entity_test entities',
  ));

  // Create an entity to retrieve.
  $entity = entity_create('services_entity_test', array(
    'type' => 'alpha',
    'name' => $this
      ->randomString(),
    'uid' => $this->privilegedUser->uid,
  ));
  $wrapper = entity_metadata_wrapper('services_entity_test', $entity);

  // Set a field value.
  $test_text_value = $this
    ->randomString();
  $wrapper->field_test_text_alpha
    ->set($test_text_value);
  $entity
    ->save();
  $this
    ->drupalGet($this->resource_path . '/' . $entity->eid);

  // Log in as the unprivileged user.
  $this
    ->drupalLogin($this->unPrivilegedUser);

  // Try getting an entity without access.
  $response = $this
    ->servicesGet($this->endpoint->path . '/entity_services_entity_test/' . $entity->eid);
  $this
    ->assertTrue($response['code'] == '401', 'Retrieval of an entity without view access returns a 401.');

  // Log in as the privileged user.
  $this
    ->drupalLogin($this->privilegedUser);
  $response = $this
    ->servicesGet($this->endpoint->path . '/entity_services_entity_test/' . $entity->eid);
  $retrieved_entity_data = $response['body'];

  // Check values on the retrieved entity data.
  $this
    ->assertEqual($entity->name, $retrieved_entity_data->name, 'Retrieved entity has the name property correctly set.');
  $this
    ->assertEqual($test_text_value, $retrieved_entity_data->field_test_text_alpha['und'][0]['value'], 'Retrieved entity has the field value correctly set.');

  // Try getting an entity that doesn't exist.
  $responseArray = $this
    ->servicesGet($this->resource_path . '/42');
  $this
    ->assertTrue($responseArray['code'] == '404', 'Retrieval of a non-existent entity returns a 404.');
}