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

/**
 * @file
 * Contains \Drupal\tour\Tests\TourCacheTagsTest.
 */

namespace Drupal\tour\Tests;

10
use Drupal\Core\Url;
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
use Drupal\system\Tests\Cache\PageCacheTagsTestBase;
use Drupal\tour\Entity\Tour;
use Drupal\user\Entity\Role;

/**
 * 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.
    Role::load(DRUPAL_ANONYMOUS_RID)->grantPermission('access tour')
     ->save();
  }

  /**
   * Tests cache tags presence and invalidation of the Tour entity.
   *
   * Tests the following cache tags:
43
   * - 'tour:<tour ID>'
44 45
   */
  public function testRenderedTour() {
46
    $url = Url::fromRoute('tour_test.1');
47 48

    // Prime the page cache.
49
    $this->verifyPageCache($url, 'MISS');
50 51

    // Verify a cache hit, but also the presence of the correct cache tags.
52 53
    $expected_tags = [
      'config:tour.tour.tour-test',
54
      'rendered',
55
    ];
56
    $this->verifyPageCache($url, 'HIT', $expected_tags);
57 58 59 60

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

    // Verify a cache hit.
64
    $this->verifyPageCache($url, 'HIT', $expected_tags);
65 66 67 68

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

    // Verify a cache hit.
72 73 74
    $expected_tags = [
      'rendered',
    ];
75
    $this->verifyPageCache($url, 'HIT', $expected_tags);
76 77 78
  }

}