3 namespace Drupal\Tests\menu_ui\Functional;
6 use Drupal\menu_link_content\Entity\MenuLinkContent;
7 use Drupal\system\Tests\Cache\PageCacheTagsTestBase;
8 use Drupal\system\Entity\Menu;
11 * Tests the Menu and Menu Link entities' cache tags.
15 class MenuCacheTagsTest extends PageCacheTagsTestBase {
20 public static $modules = ['menu_ui', 'block', 'test_page_test'];
23 * Tests cache tags presence and invalidation of the Menu entity.
25 * Tests the following cache tags:
28 public function testMenuBlock() {
29 $url = Url::fromRoute('test_page_test.test_page');
31 // Create a Llama menu, add a link to it and place the corresponding block.
32 $menu = Menu::create([
35 'description' => 'Description text',
38 /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
39 $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
40 // Move a link into the new menu.
41 $menu_link = $menu_link_manager->updateDefinition('test_page_test.test_page', ['menu_name' => 'llama', 'parent' => '']);
42 $block = $this->drupalPlaceBlock('system_menu_block:llama', ['label' => 'Llama', 'provider' => 'system', 'region' => 'footer']);
44 // Prime the page cache.
45 $this->verifyPageCache($url, 'MISS');
47 // Verify a cache hit, but also the presence of the correct cache tags.
53 'config:block.block.' . $block->id(),
54 'config:system.menu.llama',
55 // The cache contexts associated with the (in)accessible menu links are
57 'config:user.role.anonymous',
59 $this->verifyPageCache($url, 'HIT', $expected_tags);
61 // Verify that after modifying the menu, there is a cache miss.
62 $this->pass('Test modification of menu.', 'Debug');
63 $menu->set('label', 'Awesome llama');
65 $this->verifyPageCache($url, 'MISS');
67 // Verify a cache hit.
68 $this->verifyPageCache($url, 'HIT');
70 // Verify that after modifying the menu link weight, there is a cache miss.
71 $menu_link_manager->updateDefinition('test_page_test.test_page', ['weight' => -10]);
72 $this->pass('Test modification of menu link.', 'Debug');
73 $this->verifyPageCache($url, 'MISS');
75 // Verify a cache hit.
76 $this->verifyPageCache($url, 'HIT');
78 // Verify that after adding a menu link, there is a cache miss.
79 $this->pass('Test addition of menu link.', 'Debug');
80 $menu_link_2 = MenuLinkContent::create([
84 'menu_name' => 'llama',
86 'uri' => 'internal:/',
88 'bundle' => 'menu_name',
91 $this->verifyPageCache($url, 'MISS');
93 // Verify a cache hit.
94 $this->verifyPageCache($url, 'HIT');
96 // Verify that after resetting the first menu link, there is a cache miss.
97 $this->pass('Test reset of menu link.', 'Debug');
98 $this->assertTrue($menu_link->isResettable(), 'First link can be reset');
99 $menu_link = $menu_link_manager->resetLink($menu_link->getPluginId());
100 $this->verifyPageCache($url, 'MISS');
102 // Verify a cache hit.
103 $this->verifyPageCache($url, 'HIT', $expected_tags);
105 // Verify that after deleting the menu, there is a cache miss.
106 $this->pass('Test deletion of menu.', 'Debug');
108 $this->verifyPageCache($url, 'MISS');
110 // Verify a cache hit.
111 $this->verifyPageCache($url, 'HIT', ['config:block_list', 'config:user.role.anonymous', 'http_response', 'rendered']);