You are here

public function LinkFieldCollectorTest::testLinkFieldCollector in Dependency Calculation 8

Test dependency calculation.

Checks the node's dependencies contains entities referenced in link field.

File

tests/src/Kernel/EventSubscriber/DependencyCollector/LinkFieldCollectorTest.php, line 101

Class

LinkFieldCollectorTest
Class LinkFieldCollectorTest to check Link field dependencies.

Namespace

Drupal\Tests\depcalc\Kernel\EventSubscriber\DependencyCollector

Code

public function testLinkFieldCollector() {
  $node = $this
    ->drupalCreateNode([]);
  $linked_nodes = [];
  for ($i = 0; $i < 2; $i++) {

    // Nodes to be linked as "entity:".
    $linked_nodes['entity'][] = $this
      ->drupalCreateNode([]);

    // Nodes to be linked as "internal:".
    $linked_nodes['internal'][] = $this
      ->drupalCreateNode([]);
  }
  foreach ($linked_nodes as $key => $linked_node_array) {
    foreach ($linked_node_array as $linked_node) {

      /** @var \Drupal\node\NodeInterface $linked_node */
      $uri_key = $key === 'entity' ? "{$key}:" : "{$key}:/";
      $node
        ->get('link')
        ->appendItem([
        'uri' => "{$uri_key}node/{$linked_node->id()}",
        'title' => $linked_node
          ->label(),
      ]);
    }
  }

  // Add internal route to check module dependency for the given route.
  $node
    ->get('link')
    ->appendItem([
    'uri' => 'internal:/book',
    'title' => 'Books',
  ]);

  // Add internal route with route name outside of the standard
  // $MODULE_NAME.ROUTE_DESCRIPTION to assert it won't fail.
  $node
    ->get('link')
    ->appendItem([
    'uri' => 'internal:/',
    'title' => 'No route',
  ]);

  // Adding an empty link to make sure empty links
  // won't break dependency calculation.
  // See issue 3199592.
  $node
    ->get('link')
    ->appendItem([
    'uri' => NULL,
  ]);
  $node
    ->save();
  try {
    $wrapper = new DependentEntityWrapper($node);
  } catch (\Exception $exception) {
    $this
      ->markTestIncomplete($exception
      ->getMessage());
  }
  $dependencies = $this->calculator
    ->calculateDependencies($wrapper, new DependencyStack());
  foreach ($linked_nodes as $linked_node_array) {
    foreach ($linked_node_array as $linked_node) {
      $this
        ->assertArrayHasKey($linked_node
        ->uuid(), $dependencies);
    }
  }

  // Check whether the module has been added to the module dependencies.
  $this
    ->assertContains('book', $dependencies['module']);
}