View source
<?php
namespace Drupal\Tests\layout_builder\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class LayoutBuilderFieldBlockEntityReferenceCacheTagsTest extends BrowserTestBase {
use ContentTypeCreationTrait;
use EntityReferenceTestTrait;
use NodeCreationTrait;
protected static $modules = [
'node',
'layout_builder',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$config = $this
->config('system.performance');
$config
->set('cache.page.max_age', 3600);
$config
->save();
$this
->createContentType([
'type' => 'bundle_with_reference_field',
'name' => 'bundle_with_reference_field',
]);
$this
->createContentType([
'type' => 'bundle_referenced',
'name' => 'bundle_referenced',
]);
$this
->createEntityReferenceField('node', 'bundle_with_reference_field', 'field_reference', 'Reference field', 'node', 'default', [
'target_bundles' => [
'bundle_referenced',
],
]);
$this->container
->get('entity_display.repository')
->getViewDisplay('node', 'bundle_with_reference_field', 'full')
->enableLayoutBuilder()
->setComponent('field_reference', [
'type' => 'entity_reference_label',
])
->save();
}
public function testEntityReferenceFieldBlockCaching() {
$assert_session = $this
->assertSession();
$referenced_node = $this
->createNode([
'type' => 'bundle_referenced',
'title' => 'The referenced node title',
'status' => 0,
]);
$referencing_node = $this
->createNode([
'type' => 'bundle_with_reference_field',
'title' => 'The referencing node title',
'field_reference' => [
'entity' => $referenced_node,
],
]);
$referencing_node_url = $referencing_node
->toUrl();
$this
->verifyPageCacheContainsTags($referencing_node_url, 'MISS');
$this
->verifyPageCacheContainsTags($referencing_node_url, 'HIT', $referenced_node
->getCacheTags());
$this
->drupalGet($referencing_node_url);
$assert_session
->linkNotExists('The referenced node title');
$referenced_node
->setPublished()
->save();
$this
->verifyPageCacheContainsTags($referencing_node_url, 'MISS');
$this
->verifyPageCacheContainsTags($referencing_node_url, 'HIT', $referenced_node
->getCacheTags());
$this
->drupalGet($referencing_node_url);
$assert_session
->linkExists('The referenced node title');
}
protected function verifyPageCacheContainsTags(Url $url, $hit_or_miss, $tags = FALSE) {
$this
->drupalGet($url);
$message = new FormattableMarkup('Page cache @hit_or_miss for %path.', [
'@hit_or_miss' => $hit_or_miss,
'%path' => $url
->toString(),
]);
$this
->assertEqual($this
->drupalGetHeader('X-Drupal-Cache'), $hit_or_miss, $message);
if ($hit_or_miss === 'HIT' && is_array($tags)) {
$cache_tags = explode(' ', $this
->drupalGetHeader('X-Drupal-Cache-Tags'));
$tags = array_unique($tags);
$this
->assertEmpty(array_diff($tags, $cache_tags), 'Page cache tags contains all expected tags.');
}
}
}