View source
<?php
namespace Drupal\FunctionalTests\Entity;
use Drupal\Core\Url;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\entity_test\Entity\EntityTestWithBundle;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
class EntityBundleListCacheTest extends BrowserTestBase {
use AssertPageCacheContextsAndTagsTrait;
public static $modules = [
'cache_test',
'entity_test',
];
protected $defaultTheme = 'classy';
protected function setUp() {
parent::setUp();
EntityTestBundle::create([
'id' => 'bundle_a',
'label' => 'Bundle A',
])
->save();
EntityTestBundle::create([
'id' => 'bundle_b',
'label' => 'Bundle B',
])
->save();
}
public function testBundleListingCache() {
$bundle_a_url = Url::fromRoute('cache_test_list.bundle_tags', [
'entity_type_id' => 'entity_test_with_bundle',
'bundle' => 'bundle_a',
]);
$bundle_b_url = Url::fromRoute('cache_test_list.bundle_tags', [
'entity_type_id' => 'entity_test_with_bundle',
'bundle' => 'bundle_b',
]);
$this
->drupalGet($bundle_a_url);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->assertCacheTags([
'rendered',
'entity_test_with_bundle_list:bundle_a',
]);
$this
->drupalGet($bundle_a_url);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$this
->assertCacheTags([
'rendered',
'entity_test_with_bundle_list:bundle_a',
]);
$this
->drupalGet($bundle_b_url);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->assertCacheTags([
'rendered',
'entity_test_with_bundle_list:bundle_b',
]);
$this
->drupalGet($bundle_b_url);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$entity1 = EntityTestWithBundle::create([
'type' => 'bundle_a',
'name' => 'entity1',
]);
$entity1
->save();
$this
->drupalGet($bundle_a_url);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'MISS');
$this
->drupalGet($bundle_a_url);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
$this
->drupalGet($bundle_b_url);
$this
->assertSession()
->responseHeaderEquals('X-Drupal-Cache', 'HIT');
}
}