You are here

public function EntityAPITestCase::testCRUDAPIfunctions in Entity API 7

Tests CRUD API functions: entity_(create|delete|save)

File

./entity.test, line 209
Entity CRUD API tests.

Class

EntityAPITestCase
Test basic API.

Code

public function testCRUDAPIfunctions() {
  module_enable(array(
    'entity_feature',
  ));
  $user1 = $this
    ->drupalCreateUser();

  // Create test entities for the user1 and unrelated to a user.
  $entity = entity_create('entity_test', array(
    'name' => 'test',
    'uid' => $user1->uid,
  ));
  entity_save('entity_test', $entity);
  $entity = entity_create('entity_test', array(
    'name' => 'test2',
    'uid' => $user1->uid,
  ));
  entity_save('entity_test', $entity);
  $entity = entity_create('entity_test', array(
    'name' => 'test',
    'uid' => NULL,
  ));
  entity_save('entity_test', $entity);
  $entities = array_values(entity_test_load_multiple(FALSE, array(
    'name' => 'test',
  )));
  $this
    ->assertEqual($entities[0]->name, 'test', 'Created and loaded entity.');
  $this
    ->assertEqual($entities[1]->name, 'test', 'Created and loaded entity.');

  // Test getting the entity label, which is the used test-type's label.
  $label = entity_label('entity_test', $entities[0]);
  $this
    ->assertEqual($label, 'label', 'Default label returned.');
  $results = entity_test_load_multiple(array(
    $entity->pid,
  ));
  $loaded = array_pop($results);
  $this
    ->assertEqual($loaded->pid, $entity->pid, 'Loaded the entity unrelated to a user.');
  $entities = array_values(entity_test_load_multiple(FALSE, array(
    'name' => 'test2',
  )));
  entity_delete('entity_test', $entities[0]->pid);
  $entities = array_values(entity_test_load_multiple(FALSE, array(
    'name' => 'test2',
  )));
  $this
    ->assertEqual($entities, array(), 'Entity successfully deleted.');
  entity_save('entity_test', $entity);
  $this
    ->assertEqual($entity->pid, $loaded->pid, 'Entity successfully updated.');

  // Try deleting multiple test entities by deleting all.
  $pids = array_keys(entity_test_load_multiple(FALSE));
  entity_delete_multiple('entity_test', $pids);
}