function TermFieldTest::testTaxonomyTermFieldWidgets

Test widgets.

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TermFieldTest.php, line 112
Definition of Drupal\taxonomy\Tests\TermFieldTest.

Class

TermFieldTest
Tests for taxonomy term field and formatter.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyTermFieldWidgets() {

  // Create a term in the vocabulary.
  $term = $this
    ->createTerm($this->vocabulary);

  // Display creation form.
  $langcode = Language::LANGCODE_NOT_SPECIFIED;
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName("{$this->field_name}[{$langcode}]", '', 'Widget is displayed.');

  // Submit with some value.
  $edit = array(
    'user_id' => 1,
    'name' => $this
      ->randomName(),
    "{$this->field_name}[{$langcode}]" => array(
      $term
        ->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,
  )));

  // Display the object.
  $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($term
    ->label(), 'Term label is displayed.');

  // Delete the vocabulary and verify that the widget is gone.
  $this->vocabulary
    ->delete();
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertNoFieldByName("{$this->field_name}[{$langcode}]", '', 'Widget is not displayed');
}