You are here

function TaxonomyTermFieldTestCase::testTaxonomyTermFieldChangeMachineName in Drupal 7

Tests that vocabulary machine name changes are mirrored in field definitions.

File

modules/taxonomy/taxonomy.test, line 1576
Tests for taxonomy.module.

Class

TaxonomyTermFieldTestCase
Tests for taxonomy term field and formatter.

Code

function testTaxonomyTermFieldChangeMachineName() {

  // Add several entries in the 'allowed_values' setting, to make sure that
  // they all get updated.
  $this->field['settings']['allowed_values'] = array(
    array(
      'vocabulary' => $this->vocabulary->machine_name,
      'parent' => '0',
    ),
    array(
      'vocabulary' => $this->vocabulary->machine_name,
      'parent' => '0',
    ),
    array(
      'vocabulary' => 'foo',
      'parent' => '0',
    ),
  );
  field_update_field($this->field);

  // Change the machine name.
  $old_name = $this->vocabulary->machine_name;
  $new_name = drupal_strtolower($this
    ->randomName());
  $this->vocabulary->machine_name = $new_name;
  taxonomy_vocabulary_save($this->vocabulary);

  // Check that entity bundles are properly updated.
  $info = entity_get_info('taxonomy_term');
  $this
    ->assertFalse(isset($info['bundles'][$old_name]), 'The old bundle name does not appear in entity_get_info().');
  $this
    ->assertTrue(isset($info['bundles'][$new_name]), 'The new bundle name appears in entity_get_info().');

  // Check that the field instance is still attached to the vocabulary.
  $field = field_info_field($this->field_name);
  $allowed_values = $field['settings']['allowed_values'];
  $this
    ->assertEqual($allowed_values[0]['vocabulary'], $new_name, 'Index 0: Machine name was updated correctly.');
  $this
    ->assertEqual($allowed_values[1]['vocabulary'], $new_name, 'Index 1: Machine name was updated correctly.');
  $this
    ->assertEqual($allowed_values[2]['vocabulary'], 'foo', 'Index 2: Machine name was left untouched.');
}