function EntityComputedFieldTest::testFormatterComputedFieldCacheableMetadata

Same name in other branches
  1. 10 core/modules/system/tests/src/Functional/Entity/EntityComputedFieldTest.php \Drupal\Tests\system\Functional\Entity\EntityComputedFieldTest::testFormatterComputedFieldCacheableMetadata()

Tests that formatters bubble the cacheable metadata of computed fields.

File

core/modules/system/tests/src/Functional/Entity/EntityComputedFieldTest.php, line 45

Class

EntityComputedFieldTest
Tests that entities with computed fields work correctly.

Namespace

Drupal\Tests\system\Functional\Entity

Code

public function testFormatterComputedFieldCacheableMetadata() : void {
    $this->drupalLogin($this->drupalCreateUser([
        'administer entity_test content',
    ]));
    $entity = EntityTestComputedField::create([
        'name' => 'Test entity with a cacheable, computed field',
    ]);
    $entity->save();
    $this->state
        ->set('entity_test_computed_integer_value', 2024);
    $this->drupalGet($entity->toUrl('canonical')
        ->toString());
    $field_item_selector = '.field--name-computed-test-cacheable-integer-field .field__item';
    $this->assertSession()
        ->elementTextEquals('css', $field_item_selector, '2024');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Contexts', 'url.query_args:computed_test_cacheable_integer_field');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'field:computed_test_cacheable_integer_field');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache-Max-Age', "31536000");
    $this->state
        ->set('entity_test_computed_integer_value', 2025);
    $this->drupalGet($entity->toUrl('canonical')
        ->toString());
    $this->assertSession()
        ->elementTextEquals('css', $field_item_selector, '2024');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Contexts', 'url.query_args:computed_test_cacheable_integer_field');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'field:computed_test_cacheable_integer_field');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache-Max-Age', "31536000");
    Cache::invalidateTags([
        'field:computed_test_cacheable_integer_field',
    ]);
    $this->drupalGet($entity->toUrl('canonical')
        ->toString());
    $this->assertSession()
        ->elementTextEquals('css', $field_item_selector, '2025');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Contexts', 'url.query_args:computed_test_cacheable_integer_field');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'field:computed_test_cacheable_integer_field');
    $this->assertSession()
        ->responseHeaderEquals('X-Drupal-Cache-Max-Age', "31536000");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.