Version 1
[yaffs-website] / web / core / modules / menu_link_content / tests / src / Kernel / PathAliasMenuLinkContentTest.php
1 <?php
2
3 namespace Drupal\Tests\menu_link_content\Kernel;
4
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Core\Menu\MenuTreeParameters;
8 use Drupal\menu_link_content\Entity\MenuLinkContent;
9 use Drupal\KernelTests\KernelTestBase;
10
11 /**
12  * Ensures that the menu tree adapts to path alias changes.
13  *
14  * @group menu_link_content
15  */
16 class PathAliasMenuLinkContentTest extends KernelTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $modules = ['menu_link_content', 'system', 'link', 'test_page_test'];
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28
29     $this->installEntitySchema('menu_link_content');
30
31     // Ensure that the weight of module_link_content is higher than system.
32     // @see menu_link_content_install()
33     module_set_weight('menu_link_content', 1);
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public function register(ContainerBuilder $container) {
40     parent::register($container);
41
42     $definition = $container->getDefinition('path_processor_alias');
43     $definition
44       ->addTag('path_processor_inbound', ['priority' => 100]);
45   }
46
47
48   /**
49    * Tests the path aliasing changing.
50    */
51   public function testPathAliasChange() {
52     \Drupal::service('router.builder')->rebuild();
53
54     /** @var \Drupal\Core\Path\AliasStorageInterface $path_alias_storage */
55     $path_alias_storage = \Drupal::service('path.alias_storage');
56     $alias = $path_alias_storage->save('/test-page', '/my-blog');
57     $pid = $alias['pid'];
58
59     $menu_link_content = MenuLinkContent::create([
60       'title' => 'Menu title',
61       'link' => ['uri' => 'internal:/my-blog'],
62       'menu_name' => 'tools',
63     ]);
64     $menu_link_content->save();
65
66     $tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
67     $this->assertEqual('test_page_test.test_page', $tree[$menu_link_content->getPluginId()]->link->getPluginDefinition()['route_name']);
68
69     // Saving an alias should clear the alias manager cache.
70     $path_alias_storage->save('/test-render-title', '/my-blog', LanguageInterface::LANGCODE_NOT_SPECIFIED, $pid);
71
72     $tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
73     $this->assertEqual('test_page_test.render_title', $tree[$menu_link_content->getPluginId()]->link->getPluginDefinition()['route_name']);
74
75     // Delete the alias.
76     $path_alias_storage->delete(['pid' => $pid]);
77     $tree = \Drupal::menuTree()->load('tools', new MenuTreeParameters());
78     $this->assertTrue(isset($tree[$menu_link_content->getPluginId()]));
79     $this->assertEqual('', $tree[$menu_link_content->getPluginId()]->link->getRouteName());
80     // Verify the plugin now references a path that does not match any route.
81     $this->assertEqual('base:my-blog', $tree[$menu_link_content->getPluginId()]->link->getUrlObject()->getUri());
82   }
83
84 }