You are here

public function DynamicEntityReferenceBaseFieldRevisionTest::testEntityReferenceNonRevisionableFieldValidation in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/DynamicEntityReferenceBaseFieldRevisionTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceBaseFieldRevisionTest::testEntityReferenceNonRevisionableFieldValidation()

Tests non-revisionable reference field validation.

File

tests/src/Kernel/DynamicEntityReferenceBaseFieldRevisionTest.php, line 56

Class

DynamicEntityReferenceBaseFieldRevisionTest
Tests for the dynamic entity reference base field for revisionable entities.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testEntityReferenceNonRevisionableFieldValidation() {
  \Drupal::state()
    ->set('dynamic_entity_reference_entity_test_entities', [
    $this->entityType,
    $this->referencedEntityType,
  ]);
  \Drupal::state()
    ->set('dynamic_entity_reference_entity_test_cardinality', 1);
  \Drupal::state()
    ->set('dynamic_entity_reference_entity_test_exclude', [
    $this->entityType,
  ]);
  $this
    ->enableModules([
    'dynamic_entity_reference_entity_test',
  ]);
  $this
    ->installEntitySchema($this->entityType);
  $this
    ->installEntitySchema($this->referencedEntityType);
  $entity_type_manager = \Drupal::entityTypeManager();

  // Test a valid reference.
  $referenced_entity = $entity_type_manager
    ->getStorage($this->referencedEntityType)
    ->create([
    'type' => $this->bundle,
  ]);
  $referenced_entity
    ->save();
  $entity = $entity_type_manager
    ->getStorage($this->entityType)
    ->create([
    'type' => $this->bundle,
  ]);
  $entity->{$this->fieldName}->target_type = $referenced_entity
    ->getEntityTypeId();
  $entity->{$this->fieldName}->target_id = $referenced_entity
    ->id();
  $violations = $entity->{$this->fieldName}
    ->validate();
  $this
    ->assertEquals($violations
    ->count(), 0, 'Validation passes.');
  $entity = $entity_type_manager
    ->getStorage($this->entityType)
    ->create([
    'type' => $this->bundle,
  ]);
  $entity->{$this->fieldName}->entity = $referenced_entity;
  $violations = $entity->{$this->fieldName}
    ->validate();
  $this
    ->assertEquals($violations
    ->count(), 0, 'Validation passes.');

  // Test an invalid reference.
  $entity = $entity_type_manager
    ->getStorage($this->entityType)
    ->create([
    'type' => $this->bundle,
  ]);
  $entity->{$this->fieldName}->target_type = $referenced_entity
    ->getEntityTypeId();
  $entity->{$this->fieldName}->target_id = 9999;
  $violations = $entity->{$this->fieldName}
    ->validate();
  $this
    ->assertEquals($violations
    ->count(), 1, 'Validation throws a violation.');
  $this
    ->assertEquals($violations[0]
    ->getMessage(), t('The referenced entity (%type: %id) does not exist.', [
    '%type' => $this->referencedEntityType,
    '%id' => 9999,
  ]));

  // Test an invalid target_type.
  $entity = $entity_type_manager
    ->getStorage($this->entityType)
    ->create([
    'type' => $this->bundle,
  ]);
  $entity->{$this->fieldName}->target_type = $entity
    ->getEntityTypeId();
  $entity->{$this->fieldName}->target_id = $referenced_entity
    ->id();
  $violations = $entity->{$this->fieldName}
    ->validate();
  $this
    ->assertEquals($violations
    ->count(), 1, 'Validation throws a violation.');
  $this
    ->assertEquals($violations[0]
    ->getMessage(), t('The referenced entity type (%type) is not allowed for this field.', [
    '%type' => $entity
      ->getEntityTypeId(),
  ]));

  // Test an invalid entity.
  $entity = $entity_type_manager
    ->getStorage($this->entityType)
    ->create([
    'type' => $this->bundle,
  ]);
  $entity->{$this->fieldName}->entity = $entity;
  $violations = $entity->{$this->fieldName}
    ->validate();
  $this
    ->assertEquals($violations
    ->count(), 1, 'Validation throws a violation.');
  $this
    ->assertEquals($violations[0]
    ->getMessage(), t('The referenced entity type (%type) is not allowed for this field.', [
    '%type' => $entity
      ->getEntityTypeId(),
  ]));

  // @todo Implement a test case for invalid bundle references after
  // https://drupal.org/node/2064191 is fixed
}