3 namespace Drupal\Tests\menu_link_content\Kernel\Migrate\d7;
5 use Drupal\Core\Menu\MenuTreeParameters;
6 use Drupal\menu_link_content\Entity\MenuLinkContent;
7 use Drupal\menu_link_content\MenuLinkContentInterface;
8 use Drupal\node\Entity\Node;
9 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
12 * Menu link migration.
14 * @group menu_link_content
16 class MigrateMenuLinkTest extends MigrateDrupal7TestBase {
17 const MENU_NAME = 'menu-test-menu';
22 public static $modules = ['link', 'menu_ui', 'menu_link_content', 'node'];
27 protected function setUp() {
29 $this->installEntitySchema('menu_link_content');
30 $this->installEntitySchema('node');
31 $node = Node::create([
33 'title' => 'node link test',
37 $this->executeMigrations(['d7_menu', 'd7_menu_links']);
38 \Drupal::service('router.builder')->rebuild();
42 * Asserts various aspects of a menu link entity.
46 * @param string $title
47 * The expected title of the link.
49 * The expected ID of the menu to which the link will belong.
50 * @param string $description
51 * The link's expected description.
52 * @param bool $enabled
53 * Whether the link is enabled.
54 * @param bool $expanded
55 * Whether the link is expanded
56 * @param array $attributes
57 * Additional attributes the link is expected to have.
59 * The expected URI of the link.
61 * The expected weight of the link.
63 * @return \Drupal\menu_link_content\MenuLinkContentInterface
64 * The menu link content.
66 protected function assertEntity($id, $title, $menu, $description, $enabled, $expanded, array $attributes, $uri, $weight) {
67 /** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link */
68 $menu_link = MenuLinkContent::load($id);
69 $this->assertTrue($menu_link instanceof MenuLinkContentInterface);
70 $this->assertSame($title, $menu_link->getTitle());
71 $this->assertSame($menu, $menu_link->getMenuName());
72 // The migration sets the description of the link to the value of the
73 // 'title' attribute. Bit strange, but there you go.
74 $this->assertSame($description, $menu_link->getDescription());
75 $this->assertSame($enabled, $menu_link->isEnabled());
76 $this->assertSame($expanded, $menu_link->isExpanded());
77 $this->assertSame($attributes, $menu_link->link->options);
78 $this->assertSame($uri, $menu_link->link->uri);
79 $this->assertSame($weight, $menu_link->getWeight());
84 * Tests migration of menu links.
86 public function testMenuLinks() {
87 $this->assertEntity(469, 'Bing', static::MENU_NAME, 'Bing', TRUE, FALSE, ['attributes' => ['title' => 'Bing']], 'http://bing.com', 0);
88 $this->assertEntity(467, 'Google', static::MENU_NAME, 'Google', TRUE, FALSE, ['attributes' => ['title' => 'Google']], 'http://google.com', 0);
89 $this->assertEntity(468, 'Yahoo', static::MENU_NAME, 'Yahoo', TRUE, FALSE, ['attributes' => ['title' => 'Yahoo']], 'http://yahoo.com', 0);
90 // Tests migrating an external link with an undefined title attribute.
91 $this->assertEntity(470, 'Ask', static::MENU_NAME, NULL, TRUE, FALSE, [], 'http://ask.com', 0);
92 $this->assertEntity(245, 'Home', 'main', NULL, TRUE, FALSE, [], 'internal:/', 0);
93 $this->assertEntity(478, 'custom link test', 'admin', NULL, TRUE, FALSE, ['attributes' => ['title' => '']], 'internal:/admin/content/book', 0);
94 $this->assertEntity(479, 'node link test', 'tools', 'node 3', TRUE, FALSE, ['attributes' => ['title' => 'node 3']], 'entity:node/3', 3);
96 $menu_link_tree_service = \Drupal::service('menu.link_tree');
97 $parameters = new MenuTreeParameters();
98 $tree = $menu_link_tree_service->load(static::MENU_NAME, $parameters);
99 $this->assertCount(2, $tree);
101 $google_found = FALSE;
102 foreach ($tree as $menu_link_tree_element) {
103 $children += $menu_link_tree_element->hasChildren;
104 if ($menu_link_tree_element->link->getUrlObject()->toString() == 'http://bing.com') {
105 $this->assertEquals(reset($menu_link_tree_element->subtree)->link->getUrlObject()->toString(), 'http://google.com');
106 $google_found = TRUE;
109 $this->assertEquals(1, $children);
110 $this->assertTrue($google_found);
111 // Now find the custom link under a system link.
112 $parameters->root = 'system.admin_structure';
113 $tree = $menu_link_tree_service->load(static::MENU_NAME, $parameters);
115 foreach ($tree as $menu_link_tree_element) {
116 $this->assertTrue($menu_link_tree_element->link->getUrlObject()->toString());
117 if ($menu_link_tree_element->link->getTitle() == 'custom link test') {
122 $this->assertTrue($found);