summaryrefslogtreecommitdiffstats
path: root/core/modules/menu_ui/src/Tests/MenuCacheTagsTest.php
blob: 5d9ce5ffde7017ff912d8bb0b7c5f4c140e357f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php

/**
 * @file
 * Contains \Drupal\menu_ui\Tests\MenuCacheTagsTest.
 */

namespace Drupal\menu_ui\Tests;

use Drupal\Core\Url;
use Drupal\system\Tests\Cache\PageCacheTagsTestBase;
use Drupal\system\Entity\Menu;

/**
 * Tests the Menu and Menu Link entities' cache tags.
 *
 * @group menu_ui
 */
class MenuCacheTagsTest extends PageCacheTagsTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = array('menu_ui', 'block', 'test_page_test');

  /**
   * Tests cache tags presence and invalidation of the Menu entity.
   *
   * Tests the following cache tags:
   * - "menu:<menu ID>"
   */
  public function testMenuBlock() {
    $url = Url::fromRoute('test_page_test.test_page');

    // Create a Llama menu, add a link to it and place the corresponding block.
    $menu = Menu::create(array(
      'id' => 'llama',
      'label' => 'Llama',
      'description' => 'Description text',
    ));
    $menu->save();
    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    // Move a link into the new menu.
    $menu_link = $menu_link_manager->updateDefinition('test_page_test.test_page', array('menu_name' => 'llama', 'parent' => ''));
    $block = $this->drupalPlaceBlock('system_menu_block:llama', array('label' => 'Llama', 'provider' => 'system', 'region' => 'footer'));

    // Prime the page cache.
    $this->verifyPageCache($url, 'MISS');

    // Verify a cache hit, but also the presence of the correct cache tags.
    $expected_tags = array(
      'rendered',
      'block_view',
      'config:block_list',
      'config:block.block.' . $block->id(),
      'config:system.menu.llama',
      // The cache contexts associated with the (in)accessible menu links are
      // bubbled.
      'config:user.role.anonymous',
    );
    $this->verifyPageCache($url, 'HIT', $expected_tags);

    // Verify that after modifying the menu, there is a cache miss.
    $this->pass('Test modification of menu.', 'Debug');
    $menu->set('label', 'Awesome llama');
    $menu->save();
    $this->verifyPageCache($url, 'MISS');

    // Verify a cache hit.
    $this->verifyPageCache($url, 'HIT');

    // Verify that after modifying the menu link weight, there is a cache miss.
    $menu_link_manager->updateDefinition('test_page_test.test_page', array('weight' => -10));
    $this->pass('Test modification of menu link.', 'Debug');
    $this->verifyPageCache($url, 'MISS');

    // Verify a cache hit.
    $this->verifyPageCache($url, 'HIT');

    // Verify that after adding a menu link, there is a cache miss.
    $this->pass('Test addition of menu link.', 'Debug');
    $menu_link_2 = entity_create('menu_link_content', array(
      'id' => '',
      'parent' => '',
      'title' => 'Alpaca',
      'menu_name' => 'llama',
      'link' => [[
        'uri' => 'internal:/',
      ]],
      'bundle' => 'menu_name',
    ));
    $menu_link_2->save();
    $this->verifyPageCache($url, 'MISS');

    // Verify a cache hit.
    $this->verifyPageCache($url, 'HIT');

    // Verify that after resetting the first menu link, there is a cache miss.
    $this->pass('Test reset of menu link.', 'Debug');
    $this->assertTrue($menu_link->isResettable(), 'First link can be reset');
    $menu_link = $menu_link_manager->resetLink($menu_link->getPluginId());
    $this->verifyPageCache($url, 'MISS');

    // Verify a cache hit.
    $this->verifyPageCache($url, 'HIT', $expected_tags);

    // Verify that after deleting the menu, there is a cache miss.
    $this->pass('Test deletion of menu.', 'Debug');
    $menu->delete();
    $this->verifyPageCache($url, 'MISS');

    // Verify a cache hit.
    $this->verifyPageCache($url, 'HIT', ['config:block_list', 'config:user.role.anonymous', 'rendered']);
  }

}