function TermFieldMultipleVocabularyTest::testTaxonomyTermFieldMultipleVocabularies

Tests term reference field and widget with multiple vocabularies.

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldMultipleVocabularyTest.php, line 85
Definition of Drupal\taxonomy\Tests\TermFieldMultipleVocabularyTest.

Class

TermFieldMultipleVocabularyTest
Tests a taxonomy term reference field that allows multiple vocabularies.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyTermFieldMultipleVocabularies() {

  // Create a term in each vocabulary.
  $term1 = $this
    ->createTerm($this->vocabulary1);
  $term2 = $this
    ->createTerm($this->vocabulary2);

  // Submit an entity with both terms.
  $langcode = Language::LANGCODE_NOT_SPECIFIED;
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$this->field_name}[{$langcode}][]", '', 'Widget is displayed');
  $edit = array(
    'user_id' => mt_rand(0, 10),
    'name' => $this
      ->randomName(),
    "{$this->field_name}[{$langcode}][]" => array(
      $term1
        ->id(),
      $term2
        ->id(),
    ),
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)/edit|', $this->url, $match);
  $id = $match[1];
  $this
    ->assertText(t('entity_test @id has been created.', array(
    '@id' => $id,
  )), 'Entity was created.');

  // Render the entity.
  $entity = entity_load('entity_test', $id);
  $entities = array(
    $id => $entity,
  );
  $display = entity_get_display($entity
    ->entityType(), $entity
    ->bundle(), 'full');
  field_attach_prepare_view('entity_test', $entities, array(
    $entity
      ->bundle() => $display,
  ));
  $entity->content = field_attach_view($entity, $display);
  $this->content = drupal_render($entity->content);
  $this
    ->assertText($term1
    ->label(), 'Term 1 name is displayed.');
  $this
    ->assertText($term2
    ->label(), 'Term 2 name is displayed.');

  // Delete vocabulary 2.
  $this->vocabulary2
    ->delete();

  // Re-render the content.
  $entity = entity_load('entity_test', $id);
  $entities = array(
    $id => $entity,
  );
  $display = entity_get_display($entity
    ->entityType(), $entity
    ->bundle(), 'full');
  field_attach_prepare_view('entity_test', $entities, array(
    $entity
      ->bundle() => $display,
  ));
  $entity->content = field_attach_view($entity, $display);
  $this->plainTextContent = FALSE;
  $this->content = drupal_render($entity->content);

  // Term 1 should still be displayed; term 2 should not be.
  $this
    ->assertText($term1
    ->label(), 'Term 1 name is displayed.');
  $this
    ->assertNoText($term2
    ->label(), 'Term 2 name is not displayed.');

  // Verify that field and instance settings are correct.
  $field_info = field_info_field($this->field_name);
  $this
    ->assertEqual(count($field_info['settings']['allowed_values']), 1, 'Only one vocabulary is allowed for the field.');

  // The widget should still be displayed.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$this->field_name}[{$langcode}][]", '', 'Widget is still displayed');

  // Term 1 should still pass validation.
  $edit = array(
    'user_id' => mt_rand(0, 10),
    'name' => $this
      ->randomName(),
    "{$this->field_name}[{$langcode}][]" => array(
      $term1
        ->id(),
    ),
  );
  $this
    ->drupalPost(NULL, $edit, t('Save'));
}