EntityBundleListCacheTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Drupal\FunctionalTests\Entity;
  3. use Drupal\Core\Url;
  4. use Drupal\entity_test\Entity\EntityTestBundle;
  5. use Drupal\entity_test\Entity\EntityTestWithBundle;
  6. use Drupal\Tests\BrowserTestBase;
  7. use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
  8. /**
  9. * Tests that bundle tags are invalidated when entities change.
  10. *
  11. * @group Entity
  12. */
  13. class EntityBundleListCacheTest extends BrowserTestBase {
  14. use AssertPageCacheContextsAndTagsTrait;
  15. /**
  16. * Modules to enable.
  17. *
  18. * @var array
  19. */
  20. public static $modules = ['cache_test', 'entity_test'];
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected $defaultTheme = 'classy';
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function setUp() {
  29. parent::setUp();
  30. EntityTestBundle::create([
  31. 'id' => 'bundle_a',
  32. 'label' => 'Bundle A',
  33. ])->save();
  34. EntityTestBundle::create([
  35. 'id' => 'bundle_b',
  36. 'label' => 'Bundle B',
  37. ])->save();
  38. }
  39. /**
  40. * Tests that tags are invalidated when an entity with that bundle changes.
  41. */
  42. public function testBundleListingCache() {
  43. // Access to lists of test entities with each bundle.
  44. $bundle_a_url = Url::fromRoute('cache_test_list.bundle_tags', ['entity_type_id' => 'entity_test_with_bundle', 'bundle' => 'bundle_a']);
  45. $bundle_b_url = Url::fromRoute('cache_test_list.bundle_tags', ['entity_type_id' => 'entity_test_with_bundle', 'bundle' => 'bundle_b']);
  46. $this->drupalGet($bundle_a_url);
  47. $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
  48. $this->assertCacheTags(['rendered', 'entity_test_with_bundle_list:bundle_a']);
  49. $this->drupalGet($bundle_a_url);
  50. $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
  51. $this->assertCacheTags(['rendered', 'entity_test_with_bundle_list:bundle_a']);
  52. $this->drupalGet($bundle_b_url);
  53. $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
  54. $this->assertCacheTags(['rendered', 'entity_test_with_bundle_list:bundle_b']);
  55. $this->drupalGet($bundle_b_url);
  56. $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
  57. $entity1 = EntityTestWithBundle::create(['type' => 'bundle_a', 'name' => 'entity1']);
  58. $entity1->save();
  59. // Check that tags are invalidated after creating an entity of the current
  60. // bundle.
  61. $this->drupalGet($bundle_a_url);
  62. $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'MISS');
  63. $this->drupalGet($bundle_a_url);
  64. $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
  65. // Check that tags are not invalidated after creating an entity of a
  66. // different bundle than the current in the request.
  67. $this->drupalGet($bundle_b_url);
  68. $this->assertSession()->responseHeaderEquals('X-Drupal-Cache', 'HIT');
  69. }
  70. }