You are here

public function EntityReferenceFieldDependencyCollectorTest::testDependenciesCollection in Dependency Calculation 8

Tests dependencies calculation for an entity reference field.

Throws

\Drupal\Core\Entity\EntityStorageException

\Exception

File

tests/src/Kernel/EventSubscriber/DependencyCollector/EntityReferenceFieldDependencyCollectorTest.php, line 80

Class

EntityReferenceFieldDependencyCollectorTest
Class EntityReferenceFieldDependencyCollectorTest.

Namespace

Drupal\Tests\depcalc\Kernel\EventSubscriber\DependencyCollector

Code

public function testDependenciesCollection() {
  $bundle = 'depcalc_dummy_content_type';
  $tagsFieldName = 'field_dummy_tags';
  $contentType = $this
    ->createContentType([
    'type' => $bundle,
    'name' => 'Depcalc. Dummy content type',
  ]);
  $contentType
    ->save();
  $this
    ->createEntityReferenceField('node', $bundle, $tagsFieldName, 'Tags', 'taxonomy_term');
  $user1 = $this
    ->createUser();
  $user2 = $this
    ->createUser();
  $node = $this
    ->createNode([
    'type' => $bundle,
  ]);
  $node
    ->setOwner($user1);
  $node
    ->save();
  $dependencies = $this
    ->calculateDependencies($node);
  $this
    ->assertArrayHasKey($user1
    ->uuid(), $dependencies);
  $this
    ->assertArrayNotHasKey($user2
    ->uuid(), $dependencies);
  $node
    ->setOwner($user2);
  $node
    ->save();
  $dependencies = $this
    ->calculateDependencies($node);
  $this
    ->assertArrayNotHasKey($user1
    ->uuid(), $dependencies);
  $this
    ->assertArrayHasKey($user2
    ->uuid(), $dependencies);
  $vocab = $this
    ->createVocabulary();
  $term1 = $this
    ->createTerm($vocab);
  $term2 = $this
    ->createTerm($vocab, [
    'parent' => [
      'target_id' => $term1
        ->id(),
    ],
  ]);
  $node
    ->set($tagsFieldName, [
    'target_id' => $term1
      ->id(),
  ]);
  $node
    ->save();
  $dependencies = $this
    ->calculateDependencies($node);
  $this
    ->assertArrayHasKey($vocab
    ->uuid(), $dependencies);
  $this
    ->assertArrayHasKey($term1
    ->uuid(), $dependencies);
  $node
    ->set($tagsFieldName, [
    'target_id' => $term2
      ->id(),
  ]);
  $node
    ->save();
  $dependencies = $this
    ->calculateDependencies($node);
  $this
    ->assertArrayHasKey($vocab
    ->uuid(), $dependencies);
  $this
    ->assertArrayHasKey($term1
    ->uuid(), $dependencies);
  $this
    ->assertArrayHasKey($term2
    ->uuid(), $dependencies);
}