function VocabularyUnitTest::testTaxonomyVocabularyChangeMachineName

Tests that machine name changes are properly reflected.

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/VocabularyUnitTest.php, line 155
Definition of Drupal\taxonomy\Tests\VocabularyUnitTest.

Class

VocabularyUnitTest
Tests for taxonomy vocabulary functions.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyVocabularyChangeMachineName() {

  // Add a field instance to the vocabulary.
  $field = array(
    'field_name' => 'field_test',
    'type' => 'test_field',
  );
  field_create_field($field);
  $instance = array(
    'field_name' => 'field_test',
    'entity_type' => 'taxonomy_term',
    'bundle' => $this->vocabulary
      ->id(),
  );
  field_create_instance($instance);

  // Change the machine name.
  $old_name = $this->vocabulary
    ->id();
  $new_name = drupal_strtolower($this
    ->randomName());
  $this->vocabulary->vid = $new_name;
  $this->vocabulary
    ->save();

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

  // Check that the field instance is still attached to the vocabulary.
  $this
    ->assertTrue(field_info_instance('taxonomy_term', 'field_test', $new_name), 'The bundle name was updated correctly.');
}