TourCacheTagsTest.php 1.9 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\tour\Tests;

5
use Drupal\Core\Url;
6 7 8
use Drupal\system\Tests\Cache\PageCacheTagsTestBase;
use Drupal\tour\Entity\Tour;
use Drupal\user\Entity\Role;
9
use Drupal\user\RoleInterface;
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

/**
 * Tests the Tour entity's cache tags.
 *
 * @group tour
 */
class TourCacheTagsTest extends PageCacheTagsTestBase {

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    // Give anonymous users permission to view nodes, so that we can verify the
    // cache tags of cached versions of node pages.
31
    Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('access tour')
32
      ->save();
33 34 35 36 37 38
  }

  /**
   * Tests cache tags presence and invalidation of the Tour entity.
   *
   * Tests the following cache tags:
39
   * - 'tour:<tour ID>'
40 41
   */
  public function testRenderedTour() {
42
    $url = Url::fromRoute('tour_test.1');
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 49
    $expected_tags = [
      'config:tour.tour.tour-test',
50
      'config:user.role.anonymous',
51
      'rendered',
52
    ];
53
    $this->verifyPageCache($url, 'HIT', $expected_tags);
54 55 56 57

    // Verify that after modifying the tour, there is a cache miss.
    $this->pass('Test modification of tour.', 'Debug');
    Tour::load('tour-test')->save();
58
    $this->verifyPageCache($url, 'MISS');
59 60

    // Verify a cache hit.
61
    $this->verifyPageCache($url, 'HIT', $expected_tags);
62 63 64 65

    // Verify that after deleting the tour, there is a cache miss.
    $this->pass('Test deletion of tour.', 'Debug');
    Tour::load('tour-test')->delete();
66
    $this->verifyPageCache($url, 'MISS');
67 68

    // Verify a cache hit.
69
    $expected_tags = [
70
      'config:user.role.anonymous',
71 72
      'rendered',
    ];
73
    $this->verifyPageCache($url, 'HIT', $expected_tags);
74 75 76
  }

}