View source
<?php
namespace Drupal\Tests\depcalc\Kernel\EventSubscriber\DependencyCollector;
use Drupal;
use Drupal\depcalc\DependencyStack;
use Drupal\depcalc\DependentEntityWrapper;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\link\LinkItemInterface;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
class LinkFieldCollectorTest extends KernelTestBase {
use NodeCreationTrait {
createNode as drupalCreateNode;
}
use ContentTypeCreationTrait {
createContentType as drupalCreateContentType;
}
use UserCreationTrait;
protected static $modules = [
'depcalc',
'field',
'filter',
'link',
'node',
'path_alias',
'system',
'user',
'book',
'text',
];
protected $calculator;
protected function setUp() {
parent::setUp();
$this
->installSchema('node', 'node_access');
$this
->installSchema('system', 'sequences');
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installEntitySchema('path_alias');
$this
->installConfig('filter');
$this
->installConfig('node');
$this
->installConfig([
'field',
'system',
]);
$this
->installSchema('book', 'book');
$this
->installConfig('book');
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$field_storage = FieldStorageConfig::create([
'field_name' => 'link',
'entity_type' => 'node',
'type' => 'link',
'cardinality' => -1,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'page',
'label' => 'link',
'settings' => [
'link_type' => LinkItemInterface::LINK_GENERIC,
],
]);
$field
->save();
$this->calculator = Drupal::service('entity.dependency.calculator');
}
public function testLinkFieldCollector() {
$node = $this
->drupalCreateNode([]);
$linked_nodes = [];
for ($i = 0; $i < 2; $i++) {
$linked_nodes['entity'][] = $this
->drupalCreateNode([]);
$linked_nodes['internal'][] = $this
->drupalCreateNode([]);
}
foreach ($linked_nodes as $key => $linked_node_array) {
foreach ($linked_node_array as $linked_node) {
$uri_key = $key === 'entity' ? "{$key}:" : "{$key}:/";
$node
->get('link')
->appendItem([
'uri' => "{$uri_key}node/{$linked_node->id()}",
'title' => $linked_node
->label(),
]);
}
}
$node
->get('link')
->appendItem([
'uri' => 'internal:/book',
'title' => 'Books',
]);
$node
->get('link')
->appendItem([
'uri' => 'internal:/',
'title' => 'No route',
]);
$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);
}
}
$this
->assertContains('book', $dependencies['module']);
}
public function testLinkFieldCollectorDeletedReference() {
$node = $this
->drupalCreateNode([]);
$linked_node = $this
->drupalCreateNode([]);
$id = $linked_node
->id();
$node
->get('link')
->appendItem([
'uri' => "entity:node/{$id}",
'title' => $linked_node
->label(),
]);
$node
->save();
try {
$wrapper = new DependentEntityWrapper($node);
} catch (\Exception $exception) {
$this
->markTestIncomplete($exception
->getMessage());
}
$dependencies = $this->calculator
->calculateDependencies($wrapper, new DependencyStack());
$this
->assertArrayHasKey($linked_node
->uuid(), $dependencies);
$linked_node
->delete();
try {
$wrapper = new DependentEntityWrapper($node);
} catch (\Exception $exception) {
$this
->markTestIncomplete($exception
->getMessage());
}
$dependencies = $this->calculator
->calculateDependencies($wrapper, new DependencyStack());
$this
->assertArrayNotHasKey($linked_node
->uuid(), $dependencies);
}
}