class EntityConcurrentRenderTest

Same name in this branch
  1. main core/tests/Drupal/FunctionalTests/Entity/EntityConcurrentRenderTest.php \Drupal\FunctionalTests\Entity\EntityConcurrentRenderTest
Same name and namespace in other branches
  1. 11.x core/tests/Drupal/FunctionalTests/Entity/EntityConcurrentRenderTest.php \Drupal\FunctionalTests\Entity\EntityConcurrentRenderTest

Tests that the same entity can be rendered multiple times on a page.

Attributes

#[Group('Entity')] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of EntityConcurrentRenderTest

File

core/tests/Drupal/KernelTests/Core/Entity/EntityConcurrentRenderTest.php, line 20

Namespace

Drupal\KernelTests\Core\Entity
View source
class EntityConcurrentRenderTest extends KernelTestBase {
  use BlockCreationTrait {
    placeBlock as drupalPlaceBlock;
  }
  use UserCreationTrait {
    createUser as drupalCreateUser;
  }
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'block',
    'entity_test',
    'field',
    'filter',
    'text',
    'system',
    'user',
  ];
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->installEntitySchema('entity_test');
    $this->installEntitySchema('user');
    // Add a formatted text field. The text format processing creates filter
    // placeholders during rendering, which causes the block's Fiber to
    // suspend and allows other block Fibers to interleave.
    FieldStorageConfig::create([
      'entity_type' => 'entity_test',
      'field_name' => 'body',
      'type' => 'text_long',
    ])->save();
    FieldConfig::create([
      'entity_type' => 'entity_test',
      'bundle' => 'entity_test',
      'field_name' => 'body',
      'label' => 'Body',
    ])->save();
    \Drupal::service('entity_display.repository')->getViewDisplay('entity_test', 'entity_test')
      ->setComponent('body')
      ->save();
    \Drupal::service(ThemeInstallerInterface::class)->install([
      'stark',
    ]);
    \Drupal::configFactory()->getEditable('system.theme')
      ->set('default', 'stark')
      ->save();
    $this->setCurrentUser($this->drupalCreateUser([
      'view test entity',
    ]));
  }
  
  /**
   * Tests that two blocks rendering the same entity both produce output.
   */
  public function testSameEntityInMultipleBlocks() : void {
    $entity = EntityTest::create([
      'name' => 'Unique entity content',
      'body' => [
        'value' => 'Body text',
        'format' => 'plain_text',
      ],
    ]);
    $entity->save();
    $this->drupalPlaceBlock('entity_test_block', [
      'id' => 'first',
      'label' => 'First',
      'entity_id' => $entity->id(),
    ]);
    $this->drupalPlaceBlock('entity_test_block', [
      'id' => 'second',
      'label' => 'Second',
      'entity_id' => $entity->id(),
    ]);
    $this->drupalGet('<front>');
    // Both blocks should render the entity content.
    $first = $this->assertSession()
      ->elementExists('css', '#block-first');
    $second = $this->assertSession()
      ->elementExists('css', '#block-second');
    $this->assertStringContainsString('Unique entity content', $first->getText());
    $this->assertStringContainsString('Unique entity content', $second->getText());
  }

}

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