MenuCacheTagsTest.php 3.3 KB
Newer Older
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
<?php

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

namespace Drupal\menu\Tests;

use Drupal\system\Tests\Cache\PageCacheTagsTestBase;

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

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

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => "Menu & Menu link entities cache tags",
      'description' => "Test the Menu & Menu link entities' cache tags.",
      'group' => 'Menu',
    );
  }

  /**
   * Tests cache tags presence and invalidation of the Menu entity.
   *
   * Tests the following cache tags:
   * - "menu:<menu ID>"
   */
  public function testMenuBlock() {
    $path = 'test-page';

    // Create a Llama menu, add a link to it and place the corresponding block.
    $menu = entity_create('menu', array(
      'id' => 'llama',
      'label' => 'Llama',
      'description' => 'Description text',
    ));
    $menu->save();
    $menu_link = entity_create('menu_link', array(
      'link_path' => '<front>',
      'link_title' => 'Vicuña',
      'menu_name' => 'llama',
    ));
    $menu_link->save();
    $block = $this->drupalPlaceBlock('system_menu_block:llama', array('label' => 'Llama', 'module' => 'system', 'region' => 'footer'));

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

    // Verify a cache hit, but also the presence of the correct cache tags.
61 62 63 64 65 66 67 68
    $expected_tags = array(
      'content:1',
      'block_view:1',
      'block:' . $block->id(),
      'block_plugin:system_menu_block__llama',
      'menu:llama',
    );
    $this->verifyPageCache($path, 'HIT', $expected_tags);
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


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

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


    // Verify that after modifying the menu link, there is a cache miss.
    $this->pass('Test modification of menu link.', 'Debug');
    $menu_link->link_title = 'Guanaco';
    $menu_link->save();
    $this->verifyPageCache($path, 'MISS');

    // Verify a cache hit.
    $this->verifyPageCache($path, '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', array(
      'link_path' => '<front>',
      'link_title' => 'Alpaca',
      'menu_name' => 'llama',
    ));
    $menu_link_2->save();
    $this->verifyPageCache($path, 'MISS');

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


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

    // Verify a cache hit.
111
    $this->verifyPageCache($path, 'HIT', $expected_tags);
112 113 114 115 116 117 118 119 120 121 122 123


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

    // Verify a cache hit.
    $this->verifyPageCache($path, 'HIT', array('content:1'));
  }

}