You are here

public function EntityAPITestCase::testCRUD in Entity API 7

Tests CRUD.

File

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

Class

EntityAPITestCase
Test basic API.

Code

public function testCRUD() {
  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 = entity_create('entity_test', array(
    'name' => 'test2',
    'uid' => $user1->uid,
  ));
  $entity
    ->save();
  $entity = entity_create('entity_test', array(
    'name' => 'test',
    'uid' => NULL,
  ));
  $entity
    ->save();
  $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.');
  $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',
  )));
  $entities[0]
    ->delete();
  $entities = array_values(entity_test_load_multiple(FALSE, array(
    'name' => 'test2',
  )));
  $this
    ->assertEqual($entities, array(), 'Entity successfully deleted.');
  $entity
    ->save();
  $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_test_delete_multiple($pids);
}