You are here

public function ServicesEntityGenericEntityResource::testEntityCreate in Services Entity API 7.2

Test 'Create' service.

File

tests/services_entity.test, line 410
Services Entity Tests

Class

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

Code

public function testEntityCreate() {

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

  // An array of entity data to pass to the service to create an entity.
  $entity_data = array(
    'type' => 'alpha',
    'name' => $this
      ->randomString(),
    'uid' => $this->privilegedUser->uid,
  );
  $test_text_value = $this
    ->randomString();
  $entity_data['field_test_text_alpha']['und'][0]['value'] = $test_text_value;

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

  // Try to create the entity using the service, without access.
  $response = $this
    ->servicesPost($this->resource_path, $entity_data);
  $this
    ->assertTrue($response['code'] == '401', "Creation of an entity without 'create' access returns a 401.");

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

  // Create the entity using the service.
  $response = $this
    ->servicesPost($this->resource_path, $entity_data);

  // We get the new entity returned to us.
  $returned_entity = $response['body'];
  $new_entity_id = $returned_entity->eid;

  // Load the entity from the DB, using the entity ID from the response we
  // got back from the service.
  $entity = entity_load_single('services_entity_test', $new_entity_id);
  $wrapper = entity_metadata_wrapper('services_entity_test', $entity);
  $this
    ->assertEqual($entity->name, $entity_data['name'], 'Created entity has the name property correctly set.');
  $this
    ->assertEqual($wrapper->field_test_text_alpha
    ->raw(), $test_text_value, 'Created entity has the text field value correctly set.');
}