You are here

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

Tests BigPipe-delivered HTML responses when JavaScript is disabled.

Covers:

See also

\Drupal\big_pipe_test\BigPipePlaceholderTestCases

File

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

Class

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

Namespace

Drupal\Tests\big_pipe\Functional

Code

public function testBigPipeNoJs() {

  // Simulate production.
  $this
    ->config('system.logging')
    ->set('error_level', ERROR_REPORTING_HIDE)
    ->save();
  $this
    ->drupalLogin($this->rootUser);
  $this
    ->assertSessionCookieExists(TRUE);
  $this
    ->assertBigPipeNoJsCookieExists(FALSE);

  // By calling performMetaRefresh() here, we simulate JavaScript being
  // disabled, because as far as the BigPipe module is concerned, it is
  // enabled in the browser when the BigPipe no-JS cookie is set.
  // @see setUp()
  // @see performMetaRefresh()
  $this
    ->performMetaRefresh();
  $this
    ->assertBigPipeNoJsCookieExists(TRUE);
  $this
    ->drupalGet(Url::fromRoute('big_pipe_test'));
  $this
    ->assertBigPipeResponseHeadersPresent();
  $this
    ->assertSession()
    ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'cache_tag_set_in_lazy_builder');
  $this
    ->setCsrfTokenSeedInTestEnvironment();
  $cases = $this
    ->getTestCases();
  $this
    ->assertBigPipeNoJsPlaceholders([
    $cases['edge_case__invalid_html']->bigPipeNoJsPlaceholder => $cases['edge_case__invalid_html']->embeddedHtmlResponse,
    $cases['html_attribute_value']->bigPipeNoJsPlaceholder => $cases['html_attribute_value']->embeddedHtmlResponse,
    $cases['html_attribute_value_subset']->bigPipeNoJsPlaceholder => $cases['html_attribute_value_subset']->embeddedHtmlResponse,
    $cases['html']->bigPipeNoJsPlaceholder => $cases['html']->embeddedHtmlResponse,
    $cases['edge_case__html_non_lazy_builder']->bigPipeNoJsPlaceholder => $cases['edge_case__html_non_lazy_builder']->embeddedHtmlResponse,
    $cases['exception__lazy_builder']->bigPipePlaceholderId => NULL,
    $cases['exception__embedded_response']->bigPipePlaceholderId => NULL,
  ]);

  // Verifying there are no BigPipe placeholders & replacements.
  $this
    ->assertEqual('<none>', $this
    ->drupalGetHeader('BigPipe-Test-Placeholders'));

  // Verifying BigPipe start/stop signals are absent.
  $this
    ->assertNoRaw(BigPipe::START_SIGNAL, 'BigPipe start signal absent.');
  $this
    ->assertNoRaw(BigPipe::STOP_SIGNAL, 'BigPipe stop signal absent.');

  // Verifying BigPipe assets are absent.
  $this
    ->assertTrue(!isset($this
    ->getDrupalSettings()['bigPipePlaceholderIds']) && empty($this
    ->getDrupalSettings()['ajaxPageState']), 'BigPipe drupalSettings and BigPipe asset library absent.');
  $this
    ->assertRaw('</body>', 'Closing body tag present.');

  // Verify that 4xx responses work fine. (4xx responses are handled by
  // subrequests to a route pointing to a controller with the desired output.)
  $this
    ->drupalGet(Url::fromUri('base:non-existing-path'));

  // Simulate development.
  // Verifying BigPipe provides useful error output when an error occurs
  // while rendering a placeholder if verbose error logging is enabled.
  $this
    ->config('system.logging')
    ->set('error_level', ERROR_REPORTING_DISPLAY_VERBOSE)
    ->save();
  $this
    ->drupalGet(Url::fromRoute('big_pipe_test'));

  // The 'edge_case__html_exception' case throws an exception.
  $this
    ->assertRaw('The website encountered an unexpected error. Please try again later');
  $this
    ->assertRaw('You are not allowed to say llamas are not cool!');
  $this
    ->assertNoRaw('</body>', 'Closing body tag absent: error occurred before then.');

  // The exception is expected. Do not interpret it as a test failure.
  unlink($this->root . '/' . $this->siteDirectory . '/error.log');
}