You are here

public function PathAliasMenuLinkContentTest::testPathAliasChange in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/menu_link_content/src/Tests/PathAliasMenuLinkContentTest.php \Drupal\menu_link_content\Tests\PathAliasMenuLinkContentTest::testPathAliasChange()

Tests the path aliasing changing.

File

core/modules/menu_link_content/src/Tests/PathAliasMenuLinkContentTest.php, line 57
Contains \Drupal\menu_link_content\Tests\PathAliasMenuLinkContentTest.

Class

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

Namespace

Drupal\menu_link_content\Tests

Code

public function testPathAliasChange() {
  \Drupal::service('router.builder')
    ->rebuild();

  /** @var \Drupal\Core\Path\AliasStorageInterface $path_alias_storage */
  $path_alias_storage = \Drupal::service('path.alias_storage');
  $alias = $path_alias_storage
    ->save('/test-page', '/my-blog');
  $pid = $alias['pid'];
  $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
    ->assertEqual('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_storage
    ->save('/test-render-title', '/my-blog', LanguageInterface::LANGCODE_NOT_SPECIFIED, $pid);
  $tree = \Drupal::menuTree()
    ->load('tools', new MenuTreeParameters());
  $this
    ->assertEqual('test_page_test.render_title', $tree[$menu_link_content
    ->getPluginId()]->link
    ->getPluginDefinition()['route_name']);

  // Delete the alias.
  $path_alias_storage
    ->delete([
    'pid' => $pid,
  ]);
  $tree = \Drupal::menuTree()
    ->load('tools', new MenuTreeParameters());
  $this
    ->assertTrue(isset($tree[$menu_link_content
    ->getPluginId()]));
  $this
    ->assertEqual('', $tree[$menu_link_content
    ->getPluginId()]->link
    ->getRouteName());

  // Verify the plugin now references a path that does not match any route.
  $this
    ->assertEqual('base:my-blog', $tree[$menu_link_content
    ->getPluginId()]->link
    ->getUrlObject()
    ->getUri());
}