You are here

public function ServicesEntityGenericEntityResource::testEntityUpdate in Services Entity API 7.2

Test 'Update' service.

File

tests/services_entity.test, line 454
Services Entity Tests

Class

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

Code

public function testEntityUpdate() {

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

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

  // Set a field value.
  $original_entity_wrapper->field_test_text_alpha
    ->set($this
    ->randomString());
  $original_entity
    ->save();

  // Build an array of data to update to the entity.
  $update_entity_data = $original_entity_data;

  // We have to add the entity id.
  $update_entity_data['eid'] = $original_entity->eid;

  // Change the name.
  $update_entity_data['name'] = $this
    ->randomString();

  // Change the field value.
  $test_text_value = $this
    ->randomString();
  $update_entity_data['field_test_text_alpha']['und'][0]['value'] = $test_text_value;

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

  // Attempt to update the entity using the service, without access.
  $response = $this
    ->servicesPut($this->resource_path . '/' . $original_entity->eid, $update_entity_data);
  $this
    ->assertTrue($response['code'] == '401', "Update of an entity without 'update' access returns a 401.");

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

  // Update the entity using the service.
  $response = $this
    ->servicesPut($this->resource_path . '/' . $original_entity->eid, $update_entity_data);

  // We get the updated entity returned to us.
  $returned_entity = $response['body'];

  // Load the entity from the DB, using the entity ID from the response we
  // got back from the service.
  // Clear the cache first.
  entity_get_controller('services_entity_test')
    ->resetCache();
  $updated_entity = entity_load_single('services_entity_test', $original_entity->eid);
  $updated_entity_wrapper = entity_metadata_wrapper('services_entity_test', $updated_entity);
  $this
    ->assertEqual($update_entity_data['name'], $updated_entity->name, 'Name property was changed on the updated entity.');
  $this
    ->assertEqual($test_text_value, $updated_entity_wrapper->field_test_text_alpha
    ->raw(), 'Field value was changed on the updated entity.');
}