f7bf7f88d40c386a200c9942a1247a6b4a5ee19b
[yaffs-website] / web / core / modules / menu_ui / tests / src / Functional / MenuCacheTagsTest.php
1 <?php
2
3 namespace Drupal\Tests\menu_ui\Functional;
4
5 use Drupal\Core\Url;
6 use Drupal\menu_link_content\Entity\MenuLinkContent;
7 use Drupal\system\Tests\Cache\PageCacheTagsTestBase;
8 use Drupal\system\Entity\Menu;
9
10 /**
11  * Tests the Menu and Menu Link entities' cache tags.
12  *
13  * @group menu_ui
14  */
15 class MenuCacheTagsTest extends PageCacheTagsTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['menu_ui', 'block', 'test_page_test'];
21
22   /**
23    * Tests cache tags presence and invalidation of the Menu entity.
24    *
25    * Tests the following cache tags:
26    * - "menu:<menu ID>"
27    */
28   public function testMenuBlock() {
29     $url = Url::fromRoute('test_page_test.test_page');
30
31     // Create a Llama menu, add a link to it and place the corresponding block.
32     $menu = Menu::create([
33       'id' => 'llama',
34       'label' => 'Llama',
35       'description' => 'Description text',
36     ]);
37     $menu->save();
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']);
43
44     // Prime the page cache.
45     $this->verifyPageCache($url, 'MISS');
46
47     // Verify a cache hit, but also the presence of the correct cache tags.
48     $expected_tags = [
49       'http_response',
50       'rendered',
51       'block_view',
52       'config:block_list',
53       'config:block.block.' . $block->id(),
54       'config:system.menu.llama',
55       // The cache contexts associated with the (in)accessible menu links are
56       // bubbled.
57       'config:user.role.anonymous',
58     ];
59     $this->verifyPageCache($url, 'HIT', $expected_tags);
60
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');
64     $menu->save();
65     $this->verifyPageCache($url, 'MISS');
66
67     // Verify a cache hit.
68     $this->verifyPageCache($url, 'HIT');
69
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');
74
75     // Verify a cache hit.
76     $this->verifyPageCache($url, 'HIT');
77
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([
81       'id' => '',
82       'parent' => '',
83       'title' => 'Alpaca',
84       'menu_name' => 'llama',
85       'link' => [[
86         'uri' => 'internal:/',
87       ]],
88       'bundle' => 'menu_name',
89     ]);
90     $menu_link_2->save();
91     $this->verifyPageCache($url, 'MISS');
92
93     // Verify a cache hit.
94     $this->verifyPageCache($url, 'HIT');
95
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');
101
102     // Verify a cache hit.
103     $this->verifyPageCache($url, 'HIT', $expected_tags);
104
105     // Verify that after deleting the menu, there is a cache miss.
106     $this->pass('Test deletion of menu.', 'Debug');
107     $menu->delete();
108     $this->verifyPageCache($url, 'MISS');
109
110     // Verify a cache hit.
111     $this->verifyPageCache($url, 'HIT', ['config:block_list', 'config:user.role.anonymous', 'http_response', 'rendered']);
112   }
113
114 }