View source
<?php
namespace Drupal\Tests\depcalc\Kernel\EventSubscriber\DependencyCollector;
use Drupal\depcalc\DependencyStack;
use Drupal\depcalc\DependentEntityWrapper;
use Drupal\depcalc\Event\CalculateEntityDependenciesEvent;
use Drupal\depcalc\EventSubscriber\DependencyCollector\MenuItemContentDependencyCollector;
use Drupal\KernelTests\KernelTestBase;
class MenuDependencyTest extends KernelTestBase {
protected static $modules = [
'depcalc_test',
'depcalc',
'system',
'node',
'menu_link_content',
'user',
'link',
'path_alias',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('menu_link_content');
$this
->installEntitySchema('path_alias');
$this
->installSchema('system', [
'sequences',
]);
$user = \Drupal::entityTypeManager()
->getStorage('user')
->create([
'uuid' => '3e346612-34c0-4d83-a6ef-e181fd562281',
'name' => 'Test user',
]);
$user
->save();
$node = \Drupal::entityTypeManager()
->getStorage('node')
->create([
'type' => 'article',
'title' => 'A test article',
'field_body' => 'body content',
'uid' => 1,
]);
$node
->save();
$menu = \Drupal::entityTypeManager()
->getStorage('menu')
->create([
'id' => 'test',
'label' => 'Test Menu',
'uuid' => '38584be3-605a-47b1-8881-838e1424d0f9',
]);
$menu
->save();
}
public function testMenuEntityDependencies(string $test_entity_type, string $test_entity_uuid, array $entities, array $expected_entities, array $expected_modules) {
$repository = \Drupal::service('entity.repository');
$entity_type_manager = \Drupal::entityTypeManager();
foreach ($entities as $entity_type => $entities_values) {
foreach ($entities_values as $entity_values) {
$entity = $entity_type_manager
->getStorage($entity_type)
->create($entity_values);
$entity
->save();
}
}
$test_entity = $repository
->loadEntityByUuid($test_entity_type, $test_entity_uuid);
$wrapper = new DependentEntityWrapper($test_entity);
$stack = new DependencyStack();
$event = new CalculateEntityDependenciesEvent($wrapper, $stack);
$subscriber = new MenuItemContentDependencyCollector();
$subscriber
->onCalculateDependencies($event);
$this
->assertEqual(array_keys($wrapper
->getDependencies()), $expected_entities);
$this
->assertEqual($wrapper
->getModuleDependencies(), $expected_modules);
}
public function menuDependenciesDataProvider() {
return [
[
'menu_link_content',
'6e452034-9a51-42c4-8c51-eda1be63d048',
[
'menu_link_content' => [
[
'uuid' => '6e452034-9a51-42c4-8c51-eda1be63d048',
'title' => 'Test 1',
'menu_name' => 'test',
'link' => 'http://www.google.com',
'external' => TRUE,
],
],
],
[
'38584be3-605a-47b1-8881-838e1424d0f9',
],
[
'menu_link_content',
'system',
],
],
[
'menu_link_content',
'6e452034-9a51-42c4-8c51-eda1be63d047',
[
'menu_link_content' => [
[
'uuid' => '6e452034-9a51-42c4-8c51-eda1be63d048',
'title' => 'Test 1',
'menu_name' => 'test',
'link' => 'http://www.google.com',
'external' => TRUE,
],
[
'uuid' => '6e452034-9a51-42c4-8c51-eda1be63d047',
'title' => 'Test 2',
'menu_name' => 'test',
'link' => 'http://www.yahoo.com',
'external' => TRUE,
'parent' => 'menu_link_content:6e452034-9a51-42c4-8c51-eda1be63d048',
],
],
],
[
'38584be3-605a-47b1-8881-838e1424d0f9',
'6e452034-9a51-42c4-8c51-eda1be63d048',
],
[
'menu_link_content',
'system',
],
],
];
}
}