function FieldAttachStorageTestCase::testFieldAttachCreateRenameBundle

Test field_attach_create_bundle() and field_attach_rename_bundle().

File

drupal/modules/field/tests/field.test, line 605
Tests for field.module.

Class

FieldAttachStorageTestCase
Unit test class for storage-related field_attach_* functions.

Code

function testFieldAttachCreateRenameBundle() {

  // Create a new bundle. This has to be initiated by the module so that its
  // hook_entity_info() is consistent.
  $new_bundle = 'test_bundle_' . drupal_strtolower($this
    ->randomName());
  field_test_create_bundle($new_bundle);

  // Add an instance to that bundle.
  $this->instance['bundle'] = $new_bundle;
  field_create_instance($this->instance);

  // Save an entity with data in the field.
  $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
  $langcode = LANGUAGE_NONE;
  $values = $this
    ->_generateTestFieldValues($this->field['cardinality']);
  $entity->{$this->field_name}[$langcode] = $values;
  $entity_type = 'test_entity';
  field_attach_insert($entity_type, $entity);

  // Verify the field data is present on load.
  $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
  field_attach_load($entity_type, array(
    0 => $entity,
  ));
  $this
    ->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field['cardinality'], "Data is retrieved for the new bundle");

  // Rename the bundle. This has to be initiated by the module so that its
  // hook_entity_info() is consistent.
  $new_bundle = 'test_bundle_' . drupal_strtolower($this
    ->randomName());
  field_test_rename_bundle($this->instance['bundle'], $new_bundle);

  // Check that the instance definition has been updated.
  $this->instance = field_info_instance($entity_type, $this->field_name, $new_bundle);
  $this
    ->assertIdentical($this->instance['bundle'], $new_bundle, "Bundle name has been updated in the instance.");

  // Verify the field data is present on load.
  $entity = field_test_create_stub_entity(0, 0, $new_bundle);
  field_attach_load($entity_type, array(
    0 => $entity,
  ));
  $this
    ->assertEqual(count($entity->{$this->field_name}[$langcode]), $this->field['cardinality'], "Bundle name has been updated in the field storage");
}