You are here

public function ServicesEntityGenericEntityResource::testEntityIndex in Services Entity API 7.2

Test 'Index' service.

File

tests/services_entity.test, line 552
Services Entity Tests

Class

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

Code

public function testEntityIndex() {

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

  // Create some entities to index.
  $entity_data = array(
    'type' => 'alpha',
    'name' => 'one',
    'uid' => $this->privilegedUser->uid,
  );
  $entity = entity_create('services_entity_test', $entity_data);
  $wrapper = entity_metadata_wrapper('services_entity_test', $entity);
  $wrapper->field_test_text_alpha
    ->set('field-value-1');
  $entity
    ->save();
  $entity_data = array(
    'type' => 'alpha',
    'name' => 'two',
    'uid' => $this->privilegedUser->uid,
  );
  $entity = entity_create('services_entity_test', $entity_data);
  $wrapper = entity_metadata_wrapper('services_entity_test', $entity);
  $wrapper->field_test_text_alpha
    ->set('field-value-1');
  $entity
    ->save();
  $entity_data = array(
    'type' => 'alpha',
    'name' => 'three',
    'uid' => 0,
  );
  $entity = entity_create('services_entity_test', $entity_data);
  $wrapper = entity_metadata_wrapper('services_entity_test', $entity);
  $wrapper->field_test_text_alpha
    ->set('field-value-2');
  $entity
    ->save();
  $entity_data = array(
    'type' => 'beta',
    'name' => 'one',
    'uid' => 0,
  );
  $entity = entity_create('services_entity_test', $entity_data);
  $entity
    ->save();

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

  // Attempt to get the index of all entities without access.
  $response = $this
    ->servicesGet($this->resource_path);
  $this
    ->assertTrue($response['code'] == '404', "Retrieving an index of entities without 'view' access for any returns a 404.");

  // Log in as the privileged user.
  $this
    ->drupalLogin($this->privilegedUser);

  // Get the index of all entities.
  $response = $this
    ->servicesGet($this->resource_path);
  $retrieved_data = $response['body'];
  $this
    ->assertEqual(count($retrieved_data), 4, 'All entities were listed by the index service.');

  // Get the index of only the 'alpha' bundle entities.
  $response = $this
    ->servicesGet($this->resource_path, array(
    'parameters[type]' => 'alpha',
  ));
  $retrieved_data = $response['body'];
  $this
    ->assertEqual(count($retrieved_data), 3, 'The correct number of entities was returned by the index when filtered by entity type.');
  $all_correct = TRUE;
  foreach ($retrieved_data as $retrieved_entity) {
    $all_correct &= $retrieved_entity->type == 'alpha';
  }
  $this
    ->assertTrue($all_correct, 'All the retrieved entities were of the requested entity type.');

  // Get the index of entities by name.
  $response = $this
    ->servicesGet($this->resource_path, array(
    'parameters[name]' => 'one',
  ));
  $retrieved_data = $response['body'];
  $this
    ->assertEqual(count($retrieved_data), 2, "The correct number of entities was returned by the index when filtered by entity 'name' property.");
  $all_correct = TRUE;
  foreach ($retrieved_data as $retrieved_entity) {
    $all_correct &= $retrieved_entity->name == 'one';
  }
  $this
    ->assertTrue($all_correct, 'All the retrieved entities had the requested entity property.');

  // Get the index of entities by uid.
  $response = $this
    ->servicesGet($this->resource_path, array(
    'parameters[uid]' => '0',
  ));
  $retrieved_data = $response['body'];
  debug($retrieved_data);
  $this
    ->assertEqual(count($retrieved_data), 2, "The correct number of entities was returned by the index when filtered by entity 'uid' property.");
  $all_correct = TRUE;
  foreach ($retrieved_data as $retrieved_entity) {
    $all_correct &= $retrieved_entity->uid == 0;
  }
  $this
    ->assertTrue($all_correct, 'All the retrieved entities had the requested entity property.');

  // Get the index of entities by multiple properties.
  $response = $this
    ->servicesGet($this->resource_path, array(
    'parameters[type]' => 'alpha',
    'parameters[uid]' => '0',
  ));
  $retrieved_data = $response['body'];
  $this
    ->assertEqual(count($retrieved_data), 1, "The correct number of entities was returned by the index when filtered by entity type and 'uid' property.");

  // Get the index of entities by a field value.
  $response = $this
    ->servicesGet($this->resource_path, array(
    'parameters[field_test_text_alpha]' => 'field-value-1',
  ));
  $retrieved_data = $response['body'];
  $this
    ->assertEqual(count($retrieved_data), 2, "The correct number of entities was returned by the index when filtered by a field value.");

  // todo: check that if a single entity denies access, it is excluded from
  // the returned list of entities.
}