You are here

protected function BigPipeTest::assertBigPipePlaceholders in Drupal 8

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. 10 core/modules/big_pipe/tests/src/Functional/BigPipeTest.php \Drupal\Tests\big_pipe\Functional\BigPipeTest::assertBigPipePlaceholders()

Asserts expected BigPipe placeholders are present and replaced.

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 362

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) {
  $this
    ->assertSetsEqual(array_keys($expected_big_pipe_placeholders), explode(' ', $this
    ->drupalGetHeader('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 . '"></span>';
    $this
      ->assertRaw($expected_placeholder_html, 'BigPipe placeholder for placeholder ID "' . $big_pipe_placeholder_id . '" found.');
    $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 . '">';
    $result = $this
      ->xpath('//script[@data-big-pipe-replacement-for-placeholder-with-id=:id]', [
      ':id' => Html::decodeEntities($big_pipe_placeholder_id),
    ]);
    if ($expected_ajax_response === NULL) {
      $this
        ->assertCount(0, $result);
      $this
        ->assertNoRaw($expected_placeholder_replacement);
      continue;
    }
    $this
      ->assertEqual($expected_ajax_response, trim($result[0]
      ->getText()));
    $this
      ->assertRaw($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
    ->assertEqual(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
    ->assertEqual(count($expected_big_pipe_placeholders), count(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
    ->assertEqual($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
    ->assertEqual(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
    ->assertRaw(BigPipe::START_SIGNAL, 'BigPipe start signal present.');
  $this
    ->assertRaw(BigPipe::STOP_SIGNAL, 'BigPipe stop signal present.');
  $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
    ->assertEqual($expected_stream_order, array_values($actual_stream_order));
}