src/Tests/BigPipeTest.php | 177 +++++++++++---------- .../big_pipe_test/src/Form/BigPipeTestForm.php | 2 +- .../Render/Placeholder/BigPipeStrategyTest.php | 2 +- 3 files changed, 91 insertions(+), 90 deletions(-) diff --git a/src/Tests/BigPipeTest.php b/src/Tests/BigPipeTest.php index c192b91..f44844b 100644 --- a/src/Tests/BigPipeTest.php +++ b/src/Tests/BigPipeTest.php @@ -122,45 +122,106 @@ class BigPipeTest extends WebTestBase { $this->assertSessionCookieExists(TRUE); $this->assertBigPipeNoJsCookieExists(FALSE); - // By not calling checkForMetaRefresh() manually here, we simulate - // JavaScript being enabled, because as far as the BigPipe module is - // concerned, JavaScript is enabled in the browser as long as the BigPipe - // no-JS cookie is *not* set. + // By not calling performMetaRefresh() here, we simulate JavaScript being + // enabled, because as far as the BigPipe module is concerned, JavaScript is + // enabled in the browser as long as the BigPipe no-JS cookie is *not* set. // @see setUp() + // @see performMetaRefresh() $this->drupalGet(Url::fromRoute('big_pipe_test')); + $this->assertBigPipeResponseHeadersPresent(); - // Ensure we can generate CSRF tokens for the current user's session. - $session_data = $this->container->get('session_handler.write_safe')->read($this->cookies[$this->getSessionName()]['value']); - $csrf_token_seed = unserialize(explode('_sf2_meta|', $session_data)[1])['s']; - $this->container->get('session_manager.metadata_bag')->setCsrfTokenSeed($csrf_token_seed); - - $this->pass('Verifying BigPipe response headers…', 'Debug'); - $this->assertTrue(FALSE !== strpos($this->drupalGetHeader('Cache-Control'), 'private'), 'Cache-Control header set to "private".'); - $this->assertEqual('no-store, content="BigPipe/1.0"', $this->drupalGetHeader('Surrogate-Control')); - $this->assertEqual('no', $this->drupalGetHeader('X-Accel-Buffering')); - - $cases = \Drupal\big_pipe\Tests\BigPipePlaceholderTestCases::cases($this->container, $this->rootUser); - - // Keys: BigPipe no-JS placeholder markup. Values: expected replacement markup. - $expected_big_pipe_nojs_placeholders = [ + $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, - ]; - // Keys: BigPipe placeholder IDs. Values: expected AJAX response. - $expected_big_pipe_placeholders = [ + ]); + $this->assertBigPipePlaceholders([ $cases['html']->bigPipePlaceholderId => Json::encode($cases['html']->embeddedAjaxResponseCommands), $cases['edge_case__html_non_lazy_builder']->bigPipePlaceholderId => Json::encode($cases['edge_case__html_non_lazy_builder']->embeddedAjaxResponseCommands), - ]; + ]); + + $this->pass('Verifying BigPipe assets are present…', 'Debug'); + $this->assertFalse(empty($this->getDrupalSettings()), 'drupalSettings present.'); + $this->assertTrue(in_array('big_pipe/big_pipe', explode(',', $this->getDrupalSettings()['ajaxPageState']['libraries'])), 'BigPipe asset library is present.'); + } + + /** + * Tests BigPipe-delivered HTML responses when JavaScript is disabled. + * + * Covers: + * - \Drupal\big_pipe\EventSubscriber\HtmlResponseBigPipeSubscriber + * - \Drupal\big_pipe\Render\BigPipe + * - \Drupal\big_pipe\Render\BigPipe::sendNoJsPlaceholders() + * + * @see \Drupal\big_pipe\Tests\BigPipePlaceholderTestCases + */ + public function testBigPipeNoJs() { + $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(); + + $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, + ]); + + $this->pass('Verifying there are no BigPipe placeholders & replacements…', 'Debug'); + $this->assertEqual('', $this->drupalGetHeader('BigPipe-Test-Placeholders')); + $this->pass('Verifying BigPipe start/stop signals are absent…', 'Debug'); + $this->assertNoRaw(static::START_SIGNAL, 'BigPipe start signal absent.'); + $this->assertNoRaw(static::STOP_SIGNAL, 'BigPipe stop signal absent.'); + + $this->pass('Verifying BigPipe assets are absent…', 'Debug'); + $this->assertFalse(empty($this->getDrupalSettings()), 'drupalSettings and BigPipe asset library absent.'); + } + + protected function assertBigPipeResponseHeadersPresent() { + $this->pass('Verifying BigPipe response headers…', 'Debug'); + $this->assertTrue(FALSE !== strpos($this->drupalGetHeader('Cache-Control'), 'private'), 'Cache-Control header set to "private".'); + $this->assertEqual('no-store, content="BigPipe/1.0"', $this->drupalGetHeader('Surrogate-Control')); + $this->assertEqual('no', $this->drupalGetHeader('X-Accel-Buffering')); + } + + /** + * Asserts expected BigPipe no-JS placeholders are present and replaced. + * + * @param array $expected_big_pipe_nojs_placeholders + * Keys: BigPipe no-JS placeholder markup. Values: expected replacement + * markup. + */ + protected function assertBigPipeNoJsPlaceholders(array $expected_big_pipe_nojs_placeholders) { $this->pass('Verifying BigPipe no-JS placeholders & replacements…', 'Debug'); $this->assertSetsEqual(array_keys($expected_big_pipe_nojs_placeholders), array_map('rawurldecode', explode(' ', $this->drupalGetHeader('BigPipe-Test-No-Js-Placeholders')))); foreach ($expected_big_pipe_nojs_placeholders as $big_pipe_nojs_placeholder => $expected_replacement) { $this->pass('Checking whether the replacement for the BigPipe no-JS placeholder "' . $big_pipe_nojs_placeholder . '" is present:'); $this->assertRaw($expected_replacement); } + } + /** + * Asserts expected BigPipe placeholders are present and replaced. + * + * @param array $expected_big_pipe_placeholders + * Keys: BigPipe placeholder IDs. Values: expected AJAX response. + */ + protected function assertBigPipePlaceholders(array $expected_big_pipe_placeholders) { $this->pass('Verifying BigPipe placeholders & replacements…', 'Debug'); $this->assertSetsEqual(array_keys($expected_big_pipe_placeholders), explode(' ', $this->drupalGetHeader('BigPipe-Test-Placeholders'))); $placeholder_positions = []; @@ -186,10 +247,6 @@ class BigPipeTest extends WebTestBase { $this->assertEqual(array_keys($expected_big_pipe_placeholders), array_values($placeholder_replacement_positions)); $this->assertEqual(count($expected_big_pipe_placeholders), preg_match_all('/' . preg_quote('