a5e8dd0e705e929ee45e6c4088047c110ac8fdd9
[yaffs-website] / web / core / modules / menu_link_content / tests / src / Kernel / Migrate / d7 / MigrateMenuLinkTest.php
1 <?php
2
3 namespace Drupal\Tests\menu_link_content\Kernel\Migrate\d7;
4
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;
10
11 /**
12  * Menu link migration.
13  *
14  * @group menu_link_content
15  */
16 class MigrateMenuLinkTest extends MigrateDrupal7TestBase {
17   const MENU_NAME = 'menu-test-menu';
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['link', 'menu_ui', 'menu_link_content', 'node'];
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29     $this->installEntitySchema('menu_link_content');
30     $this->installEntitySchema('node');
31     $node = Node::create([
32       'nid' => 2,
33       'title' => 'node link test',
34       'type' => 'article',
35     ]);
36     $node->save();
37     $this->executeMigrations(['d7_menu', 'd7_menu_links']);
38     \Drupal::service('router.builder')->rebuild();
39   }
40
41   /**
42    * Asserts various aspects of a menu link entity.
43    *
44    * @param string $id
45    *   The link ID.
46    * @param string $title
47    *   The expected title of the link.
48    * @param string $menu
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.
58    * @param string $uri
59    *   The expected URI of the link.
60    * @param int $weight
61    *   The expected weight of the link.
62    *
63    * @return \Drupal\menu_link_content\MenuLinkContentInterface
64    *   The menu link content.
65    */
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());
80     return $menu_link;
81   }
82
83   /**
84    * Tests migration of menu links.
85    */
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', 0);
94     $this->assertEntity(479, 'node link test', 'tools', 'node 2', TRUE, FALSE, ['attributes' => ['title' => 'node 2']], 'entity:node/2', 3);
95
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);
100     $children = 0;
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;
107       }
108     }
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);
114     $found = FALSE;
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') {
118         $found = TRUE;
119         break;
120       }
121     }
122     $this->assertTrue($found);
123   }
124
125 }