You are here

public function TermMergerNodeCrudTest::ifNodeReferencesBothTermsItWillOnlyReferenceTargetTermOnce in Term Merge 8

Tests a node with both term references has a single value after a merge.

@test

File

tests/src/Kernel/TermMergerNodeCrudTest.php, line 76

Class

TermMergerNodeCrudTest
Tests term merging for nodes.

Namespace

Drupal\Tests\term_merge\Kernel

Code

public function ifNodeReferencesBothTermsItWillOnlyReferenceTargetTermOnce() {
  $firstTerm = reset($this->terms);
  $lastTerm = end($this->terms);
  $values = [
    'field_terms' => [
      'target_id' => $firstTerm
        ->id(),
    ],
    [
      'target_id' => $lastTerm
        ->id(),
    ],
  ];
  $node = $this
    ->createNode($values);
  $migrator = \Drupal::service('term_reference_change.migrator');
  $dispatcher = \Drupal::service('event_dispatcher');
  $sut = new TermMerger($this->entityTypeManager, $migrator, $dispatcher);
  $newTerm = $sut
    ->mergeIntoNewTerm($this->terms, 'NewTerm');

  /** @var \Drupal\node\Entity\Node $loadedNode */
  $loadedNode = $this->entityTypeManager
    ->getStorage('node')
    ->load($node
    ->id());
  $referencedTerms = $loadedNode->field_terms
    ->getValue();
  self::assertCount(1, $referencedTerms);
  $firstReference = reset($referencedTerms);
  self::assertEquals($newTerm
    ->id(), $firstReference['target_id']);
}