MenuCacheTagsTest.php 3.71 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\menu_ui\Functional;
4

5
use Drupal\Core\Url;
6
use Drupal\menu_link_content\Entity\MenuLinkContent;
7
use Drupal\Tests\system\Functional\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 16 17 18 19
 */
class MenuCacheTagsTest extends PageCacheTagsTestBase {

  /**
   * {@inheritdoc}
   */
20
  public static $modules = ['menu_ui', 'block', 'test_page_test'];
21 22 23 24 25 26 27 28

  /**
   * Tests cache tags presence and invalidation of the Menu entity.
   *
   * Tests the following cache tags:
   * - "menu:<menu ID>"
   */
  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 34 35
      'id' => 'llama',
      'label' => 'Llama',
      'description' => 'Description text',
36
    ]);
37
    $menu->save();
38 39 40
    /** @var \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager */
    $menu_link_manager = \Drupal::service('plugin.manager.menu.link');
    // Move a link into the new menu.
41 42
    $menu_link = $menu_link_manager->updateDefinition('test_page_test.test_page', ['menu_name' => 'llama', 'parent' => '']);
    $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 51
      'rendered',
      'block_view',
52 53 54
      'config:block_list',
      'config:block.block.' . $block->id(),
      'config:system.menu.llama',
55 56 57
      // The cache contexts associated with the (in)accessible menu links are
      // bubbled.
      'config:user.role.anonymous',
58
    ];
59
    $this->verifyPageCache($url, 'HIT', $expected_tags);
60 61 62

    // Verify that after modifying the menu, there is a cache miss.
    $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 79

    // Verify that after adding a menu link, there is a cache miss.
    $this->pass('Test addition of menu link.', 'Debug');
80
    $menu_link_2 = MenuLinkContent::create([
81 82 83
      'id' => '',
      'parent' => '',
      'title' => 'Alpaca',
84
      'menu_name' => 'llama',
85 86 87
      'link' => [
        ['uri' => 'internal:/'],
      ],
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 97 98 99
    // 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());
100
    $this->verifyPageCache($url, 'MISS');
101 102

    // Verify a cache hit.
103
    $this->verifyPageCache($url, 'HIT', $expected_tags);
104 105 106 107

    // Verify that after deleting the menu, there is a cache miss.
    $this->pass('Test deletion of menu.', 'Debug');
    $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
  }

}