function MenuLinksTest::testMenuLinkContentGetEntity

Same name in other branches
  1. 11.x core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php \Drupal\Tests\menu_link_content\Kernel\MenuLinksTest::testMenuLinkContentGetEntity()

Tests that getEntity() method returns correct value.

File

core/modules/menu_link_content/tests/src/Kernel/MenuLinksTest.php, line 442

Class

MenuLinksTest
Tests handling of menu links hierarchies.

Namespace

Drupal\Tests\menu_link_content\Kernel

Code

public function testMenuLinkContentGetEntity() : void {
    // Set up a custom menu link pointing to a specific path.
    $user = User::create([
        'name' => 'username',
    ]);
    $user->save();
    $title = $this->randomMachineName();
    $menu_link = MenuLinkContent::create([
        'title' => $title,
        'link' => [
            [
                'uri' => 'internal:/' . $user->toUrl('collection')
                    ->getInternalPath(),
            ],
        ],
        'menu_name' => 'menu-test',
    ]);
    $menu_link->save();
    $menu_tree = \Drupal::menuTree()->load('menu-test', new MenuTreeParameters());
    $this->assertCount(1, $menu_tree);
    
    /** @var \Drupal\Core\Menu\MenuLinkTreeElement $tree_element */
    $tree_element = reset($menu_tree);
    $this->assertInstanceOf(MenuLinkContentPlugin::class, $tree_element->link);
    $this->assertInstanceOf(MenuLinkContent::class, $tree_element->link
        ->getEntity());
    $this->assertEquals($title, $tree_element->link
        ->getEntity()
        ->getTitle());
    $this->assertEquals($menu_link->id(), $tree_element->link
        ->getEntity()
        ->id());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.