function BigPipeTest::assertBigPipePlaceholders

Same name and namespace in other branches
  1. 9 core/modules/big_pipe/tests/src/Functional/BigPipeTest.php \Drupal\Tests\big_pipe\Functional\BigPipeTest::assertBigPipePlaceholders()
  2. 8.9.x core/modules/big_pipe/tests/src/Functional/BigPipeTest.php \Drupal\Tests\big_pipe\Functional\BigPipeTest::assertBigPipePlaceholders()
  3. 11.x core/modules/big_pipe/tests/src/Functional/BigPipeTest.php \Drupal\Tests\big_pipe\Functional\BigPipeTest::assertBigPipePlaceholders()

Asserts expected BigPipe placeholders are present and replaced.

@internal

Parameters

array $expected_big_pipe_placeholders: Keys: BigPipe placeholder IDs. Values: expected AJAX response.

array $expected_big_pipe_placeholder_stream_order: Keys: BigPipe placeholder IDs. Values: expected AJAX response. Keys are defined in the order that they are expected to be rendered & streamed.

1 call to BigPipeTest::assertBigPipePlaceholders()
BigPipeTest::testBigPipe in core/modules/big_pipe/tests/src/Functional/BigPipeTest.php
Tests BigPipe-delivered HTML responses when JavaScript is enabled.

File

core/modules/big_pipe/tests/src/Functional/BigPipeTest.php, line 371

Class

BigPipeTest
Tests BigPipe's no-JS detection & response delivery (with and without JS).

Namespace

Drupal\Tests\big_pipe\Functional

Code

protected function assertBigPipePlaceholders(array $expected_big_pipe_placeholders, array $expected_big_pipe_placeholder_stream_order) : void {
  $this->assertSetsEqual(array_keys($expected_big_pipe_placeholders), explode(' ', $this->getSession()
    ->getResponseHeader('BigPipe-Test-Placeholders')));
  $placeholder_positions = [];
  $placeholder_replacement_positions = [];
  foreach ($expected_big_pipe_placeholders as $big_pipe_placeholder_id => $expected_ajax_response) {
    // Verify expected placeholder.
    $expected_placeholder_html = '<span data-big-pipe-placeholder-id="' . $big_pipe_placeholder_id . '">';
    $this->assertSession()
      ->responseContains($expected_placeholder_html);
    $pos = strpos($this->getSession()
      ->getPage()
      ->getContent(), $expected_placeholder_html);
    $placeholder_positions[$pos] = $big_pipe_placeholder_id;
    // Verify expected placeholder replacement.
    $expected_placeholder_replacement = '<script type="application/vnd.drupal-ajax" data-big-pipe-replacement-for-placeholder-with-id="' . $big_pipe_placeholder_id . '">';
    $xpath = '//script[@data-big-pipe-replacement-for-placeholder-with-id="' . Html::decodeEntities($big_pipe_placeholder_id) . '"]';
    if ($expected_ajax_response === NULL) {
      $this->assertSession()
        ->elementNotExists('xpath', $xpath);
      $this->assertSession()
        ->responseNotContains($expected_placeholder_replacement);
      continue;
    }
    $this->assertSession()
      ->elementTextContains('xpath', $xpath, $expected_ajax_response);
    $this->assertSession()
      ->responseContains($expected_placeholder_replacement);
    $pos = strpos($this->getSession()
      ->getPage()
      ->getContent(), $expected_placeholder_replacement);
    $placeholder_replacement_positions[$pos] = $big_pipe_placeholder_id;
  }
  ksort($placeholder_positions, SORT_NUMERIC);
  $this->assertEquals(array_keys($expected_big_pipe_placeholders), array_values($placeholder_positions));
  $placeholders = array_map(function (NodeElement $element) {
    return $element->getAttribute('data-big-pipe-placeholder-id');
  }, $this->cssSelect('[data-big-pipe-placeholder-id]'));
  $this->assertSameSize($expected_big_pipe_placeholders, array_unique($placeholders));
  $expected_big_pipe_placeholders_with_replacements = [];
  foreach ($expected_big_pipe_placeholder_stream_order as $big_pipe_placeholder_id) {
    $expected_big_pipe_placeholders_with_replacements[$big_pipe_placeholder_id] = $expected_big_pipe_placeholders[$big_pipe_placeholder_id];
  }
  $this->assertEquals($expected_big_pipe_placeholders_with_replacements, array_filter($expected_big_pipe_placeholders));
  $this->assertSetsEqual(array_keys($expected_big_pipe_placeholders_with_replacements), array_values($placeholder_replacement_positions));
  $this->assertSame(count($expected_big_pipe_placeholders_with_replacements), preg_match_all('/' . preg_quote('<script type="application/vnd.drupal-ajax" data-big-pipe-replacement-for-placeholder-with-id="', '/') . '/', $this->getSession()
    ->getPage()
    ->getContent()));
  // Verifying BigPipe start/stop signals.
  $this->assertSession()
    ->responseContains(BigPipe::START_SIGNAL);
  $this->assertSession()
    ->responseContains(BigPipe::STOP_SIGNAL);
  $start_signal_position = strpos($this->getSession()
    ->getPage()
    ->getContent(), BigPipe::START_SIGNAL);
  $stop_signal_position = strpos($this->getSession()
    ->getPage()
    ->getContent(), BigPipe::STOP_SIGNAL);
  $this->assertTrue($start_signal_position < $stop_signal_position, 'BigPipe start signal appears before stop signal.');
  // Verifying BigPipe placeholder replacements and start/stop signals were
  // streamed in the correct order.
  $expected_stream_order = array_keys($expected_big_pipe_placeholders_with_replacements);
  array_unshift($expected_stream_order, BigPipe::START_SIGNAL);
  array_push($expected_stream_order, BigPipe::STOP_SIGNAL);
  $actual_stream_order = $placeholder_replacement_positions + [
    $start_signal_position => BigPipe::START_SIGNAL,
    $stop_signal_position => BigPipe::STOP_SIGNAL,
  ];
  ksort($actual_stream_order, SORT_NUMERIC);
  $this->assertEquals($expected_stream_order, array_values($actual_stream_order));
}

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