You are here

public function ReferenceFinderTest::findsReferencingEntities in Term reference change 8

Tests referencing entities are found.

@test

File

tests/src/Kernel/ReferenceFinderTest.php, line 91

Class

ReferenceFinderTest
Tests entities referencing a term are found.

Namespace

Drupal\Tests\term_reference_change\Kernel

Code

public function findsReferencingEntities() {
  $term = $this
    ->createTerm($this->targetVocabulary);
  $referencingNode = $this
    ->createNode([
    'field_terms' => [
      'target_id' => $term
        ->id(),
    ],
  ]);
  $nonReferencingNode = $this
    ->createNode();
  $referencingTerm = $this
    ->createTerm($this->referencingVocabulary, [
    'field_terms' => [
      'target_id' => $term
        ->id(),
    ],
  ]);
  $nonReferencingTerm = $this
    ->createTerm($this->referencingVocabulary);
  $sut = new ReferenceFinder($this->entityTypeManager, \Drupal::service('entity_type.bundle.info'), \Drupal::service('entity_field.manager'));
  $references = $sut
    ->findReferencesFor($term);
  $referencingNode = $this->entityTypeManager
    ->getStorage('node')
    ->load($referencingNode
    ->id());
  $referencingTerm = $this->termStorage
    ->load($referencingTerm
    ->id());
  $expected = [
    'node' => [
      $referencingNode,
    ],
    'taxonomy_term' => [
      $referencingTerm,
    ],
  ];
  $this
    ->assertEquals($expected, $references);
}