function PathAliasMenuLinkContentTest::testPathAliasChange

Same name in other branches
  1. 8.9.x core/modules/menu_link_content/tests/src/Kernel/PathAliasMenuLinkContentTest.php \Drupal\Tests\menu_link_content\Kernel\PathAliasMenuLinkContentTest::testPathAliasChange()
  2. 10 core/modules/menu_link_content/tests/src/Kernel/PathAliasMenuLinkContentTest.php \Drupal\Tests\menu_link_content\Kernel\PathAliasMenuLinkContentTest::testPathAliasChange()
  3. 11.x core/modules/menu_link_content/tests/src/Kernel/PathAliasMenuLinkContentTest.php \Drupal\Tests\menu_link_content\Kernel\PathAliasMenuLinkContentTest::testPathAliasChange()

Tests the path aliasing changing.

File

core/modules/menu_link_content/tests/src/Kernel/PathAliasMenuLinkContentTest.php, line 62

Class

PathAliasMenuLinkContentTest
Ensures that the menu tree adapts to path alias changes.

Namespace

Drupal\Tests\menu_link_content\Kernel

Code

public function testPathAliasChange() {
    $path_alias = $this->createPathAlias('/test-page', '/my-blog');
    $menu_link_content = MenuLinkContent::create([
        'title' => 'Menu title',
        'link' => [
            'uri' => 'internal:/my-blog',
        ],
        'menu_name' => 'tools',
    ]);
    $menu_link_content->save();
    $tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
    $this->assertEquals('test_page_test.test_page', $tree[$menu_link_content->getPluginId()]->link
        ->getPluginDefinition()['route_name']);
    // Saving an alias should clear the alias manager cache.
    $path_alias->setPath('/test-render-title');
    $path_alias->setAlias('/my-blog');
    $path_alias->save();
    $tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
    $this->assertEquals('test_page_test.render_title', $tree[$menu_link_content->getPluginId()]->link
        ->getPluginDefinition()['route_name']);
    // Delete the alias.
    $path_alias->delete();
    $tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
    $this->assertTrue(isset($tree[$menu_link_content->getPluginId()]));
    $this->assertEquals('', $tree[$menu_link_content->getPluginId()]->link
        ->getRouteName());
    // Verify the plugin now references a path that does not match any route.
    $this->assertEquals('base:my-blog', $tree[$menu_link_content->getPluginId()]->link
        ->getUrlObject()
        ->getUri());
}

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