You are here

public function EntityMetadataIntegrationTestCase::testCRUDfunctions in Entity API 7

Runs some generic tests on each entity.

File

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

Class

EntityMetadataIntegrationTestCase
Tests provided entity property info of the core modules.

Code

public function testCRUDfunctions() {
  $info = entity_get_info();
  foreach ($info as $entity_type => $entity_info) {

    // Test using access callback.
    entity_access('view', $entity_type);
    entity_access('update', $entity_type);
    entity_access('create', $entity_type);
    entity_access('delete', $entity_type);

    // Test creating the entity.
    if (!isset($entity_info['creation callback'])) {
      continue;
    }

    // Populate $values with all values that are setable. They will be set
    // with an metadata wrapper, so we also test setting that way.
    $values = array();
    foreach (entity_metadata_wrapper($entity_type) as $name => $wrapper) {
      $info = $wrapper
        ->info();
      if (!empty($info['setter callback'])) {
        $values[$name] = $this
          ->createValue($wrapper);
      }
    }
    $entity = entity_property_values_create_entity($entity_type, $values)
      ->value();
    $this
      ->assertTrue($entity, "Created {$entity_type} and set all setable values.");

    // Save the new entity.
    $return = entity_save($entity_type, $entity);
    if ($return === FALSE) {
      continue;

      // No support for saving.
    }
    $id = entity_metadata_wrapper($entity_type, $entity)
      ->getIdentifier();
    $this
      ->assertTrue($id, "{$entity_type} has been successfully saved.");

    // And delete it.
    $return = entity_delete($entity_type, $id);
    if ($return === FALSE) {
      continue;

      // No support for deleting.
    }
    $return = entity_load_single($entity_type, $id);
    $this
      ->assertFalse($return, "{$entity_type} has been successfully deleted.");
  }
}