function TermFieldTest::testTaxonomyTermFieldValidation

Test term field validation.

File

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

Class

TermFieldTest
Tests for taxonomy term field and formatter.

Namespace

Drupal\taxonomy\Tests

Code

function testTaxonomyTermFieldValidation() {

  // Test valid and invalid values with field_attach_validate().
  $langcode = LANGUAGE_NOT_SPECIFIED;
  $entity = field_test_create_entity();
  $term = $this
    ->createTerm($this->vocabulary);
  $entity->{$this->field_name}[$langcode][0]['tid'] = $term->tid;
  try {
    field_attach_validate('test_entity', $entity);
    $this
      ->pass('Correct term does not cause validation error.');
  } catch (FieldValidationException $e) {
    $this
      ->fail('Correct term does not cause validation error.');
  }
  $entity = field_test_create_entity();
  $bad_term = $this
    ->createTerm($this
    ->createVocabulary());
  $entity->{$this->field_name}[$langcode][0]['tid'] = $bad_term->tid;
  try {
    field_attach_validate('test_entity', $entity);
    $this
      ->fail('Wrong term causes validation error.');
  } catch (FieldValidationException $e) {
    $this
      ->pass('Wrong term causes validation error.');
  }
}