ShortcutCacheTagsTest.php 1.87 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\shortcut\Tests\ShortcutCacheTagsTest.
 */

namespace Drupal\shortcut\Tests;

10
use Drupal\shortcut\Entity\Shortcut;
11
use Drupal\system\Tests\Entity\EntityCacheTagsTestBase;
12
use Drupal\user\Entity\Role;
13 14 15

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

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

  /**
   * {@inheritdoc}
   */
29
  protected function setUp() {
30 31 32 33
    parent::setUp();

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

  /**
   * {@inheritdoc}
   */
  protected function createEntity() {
    // Create a "Llama" shortcut.
45
    $shortcut = Shortcut::create(array(
46
      'shortcut_set' => 'default',
47 48
      'title' => t('Llama'),
      'weight' => 0,
49
      'link' => ['uri' => 'admin'],
50 51 52 53 54 55 56 57 58 59 60
    ));
    $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.
61
    $cache_tags = ['config:shortcut.set.default'];
62 63 64
    \Drupal::cache('render')->set('foo', 'bar', \Drupal\Core\Cache\CacheBackendInterface::CACHE_PERMANENT, $cache_tags);

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

    // 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.');
  }

}