function FieldAttachStorageTest::testFieldAttachCreateRenameBundle

Test field_attach_create_bundle() and field_attach_rename_bundle().

File

drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php, line 425
Definition of Drupal\field\Tests\FieldAttachStorageTest.

Class

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

Namespace

Drupal\field\Tests

Code

function testFieldAttachCreateRenameBundle() {

  // Create a new bundle.
  $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_entity(0, 0, $this->instance['bundle']);
  $langcode = LANGUAGE_NOT_SPECIFIED;
  $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_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.
  $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_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");
}