ShortcutCacheTagsTest.php 1.87 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\shortcut\Tests;

5
use Drupal\Core\Cache\CacheBackendInterface;
6
use Drupal\shortcut\Entity\Shortcut;
7
use Drupal\system\Tests\Entity\EntityCacheTagsTestBase;
8
use Drupal\user\Entity\Role;
9
use Drupal\user\RoleInterface;
10 11 12

/**
 * Tests the Shortcut entity's cache tags.
13 14
 *
 * @group shortcut
15 16 17 18 19 20 21 22 23 24 25
 */
class ShortcutCacheTagsTest extends EntityCacheTagsTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = array('shortcut');

  /**
   * {@inheritdoc}
   */
26
  protected function setUp() {
27 28 29 30
    parent::setUp();

    // Give anonymous users permission to customize shortcut links, so that we
    // can verify the cache tags of cached versions of shortcuts.
31
    $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
32
    $user_role->grantPermission('customize shortcut links');
33
    $user_role->grantPermission('access shortcuts');
34 35 36 37 38 39 40 41
    $user_role->save();
  }

  /**
   * {@inheritdoc}
   */
  protected function createEntity() {
    // Create a "Llama" shortcut.
42
    $shortcut = Shortcut::create(array(
43
      'shortcut_set' => 'default',
44 45
      'title' => t('Llama'),
      'weight' => 0,
46
      'link' => [['uri' => 'internal:/admin']],
47 48 49 50 51 52 53 54 55 56 57
    ));
    $shortcut->save();

    return $shortcut;
  }

  /**
   * Tests that when creating a shortcut, the shortcut set tag is invalidated.
   */
  public function testEntityCreation() {
    // Create a cache entry that is tagged with a shortcut set cache tag.
58
    $cache_tags = ['config:shortcut.set.default'];
59
    \Drupal::cache('render')->set('foo', 'bar', CacheBackendInterface::CACHE_PERMANENT, $cache_tags);
60 61

    // Verify a cache hit.
62
    $this->verifyRenderCache('foo', $cache_tags);
63 64 65 66 67 68 69 70 71

    // Now create a shortcut entity in that shortcut set.
    $this->createEntity();

    // Verify a cache miss.
    $this->assertFalse(\Drupal::cache('render')->get('foo'), 'Creating a new shortcut invalidates the cache tag of the shortcut set.');
  }

}