src/Tests/BigPipeNoJsDetectionTest.php | 85 +++++++++++++++++----- .../src/EventSubscriber/BigPipeTestSubscriber.php | 2 +- 2 files changed, 68 insertions(+), 19 deletions(-) diff --git a/src/Tests/BigPipeNoJsDetectionTest.php b/src/Tests/BigPipeNoJsDetectionTest.php index 75366d0..8a0cfb2 100644 --- a/src/Tests/BigPipeNoJsDetectionTest.php +++ b/src/Tests/BigPipeNoJsDetectionTest.php @@ -32,7 +32,7 @@ class BigPipeNoJsDetectionTest extends WebTestBase { * * @var array */ - public static $modules = ['big_pipe']; + public static $modules = ['big_pipe', 'big_pipe_test']; /** * {@inheritdoc} @@ -93,10 +93,6 @@ class BigPipeNoJsDetectionTest extends WebTestBase { * Tests BigPipe-delivered HTML responses when JavaScript is enabled. */ public function testBigPipe() { - $this->container->get('module_installer')->install(['big_pipe_test']); - $this->rebuildContainer(); - $this->rebuildAll(); - $this->drupalLogin($this->rootUser); $this->assertSessionCookieExists(TRUE); $this->assertBigPipeNoJsCookieExists(FALSE); @@ -167,7 +163,7 @@ class BigPipeNoJsDetectionTest extends WebTestBase { ]; $this->pass('Verifying BigPipe no-JS placeholders & replacements…', 'Debug'); - $this->assertSetsEqual(array_keys($expected_big_pipe_nojs_placeholders), explode(' ', $this->drupalGetHeader('BigPipe-Test-No-Js-Placeholders'))); + $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); @@ -221,6 +217,63 @@ class BigPipeNoJsDetectionTest extends WebTestBase { $this->assertEqual($expected_stream_order, array_values($actual_stream_order)); } + /** + * Tests BigPipe-delivered HTML responses when JavaScript is disabled. + */ + public function testBigPipeNoJs() { + $this->drupalLogin($this->rootUser); + $this->assertSessionCookieExists(TRUE); + $this->assertBigPipeNoJsCookieExists(FALSE); + + // By calling checkForMetaRefresh() manually 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() + $this->doMetaRefresh(); + $this->assertBigPipeNoJsCookieExists(TRUE); + + $this->drupalGet(Url::fromRoute('big_pipe_test')); + + // 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')); + + // Keys: BigPipe no-JS placeholder markup. Values: expected replacement markup. + $expected_big_pipe_nojs_placeholders = [ + 'big_pipe_nojs_placeholder_attribute_safe:<hello' => 'Yarhar llamas forever!', + 'big_pipe_nojs_placeholder_attribute_safe:form_action_cc611e1d' => '
$this->container->get('csrf_token')->get('admin/appearance/default'), + '
' => 'Yarhar llamas forever!', + '
' => '', + ]; + + $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); + } + + $this->pass('Verifying BigPipe placeholders & replacements…', 'Debug'); + $this->assertEqual('', $this->drupalGetHeader('BigPipe-Test-Placeholders')); + + $this->pass('Verifying BigPipe assets are absent…', 'Debug'); + $this->assertFalse(empty($this->getDrupalSettings()), 'drupalSettings and BigPipe asset library absent.'); + + $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.'); + } + + /** + * Asserts whether arrays A and B are equal, when treated as sets. + */ protected function assertSetsEqual(array $a, array $b) { $a_set = $a; sort($a_set); @@ -230,16 +283,6 @@ class BigPipeNoJsDetectionTest extends WebTestBase { } /** - * Tests BigPipe-delivered HTML responses when JavaScript is disabled. - */ - public function atestBigPipeNoJs() { - // BigPipe asset library: absent. - $this->assertFalse(empty($this->getDrupalSettings()), 'drupalSettings present.'); - $this->assertFalse(in_array('big_pipe/big_pipe', explode(',', $this->getDrupalSettings()['ajaxPageState']['libraries'])), 'BigPipe asset library is absent.'); - - } - - /** * Asserts whether a BigPipe no-JS cookie exists or not. */ protected function assertBigPipeNoJsCookieExists($expected) { @@ -267,8 +310,7 @@ class BigPipeNoJsDetectionTest extends WebTestBase { protected function assertBigPipeNoJsMetaRefreshRedirect() { // Let the browser check for a meta refresh. $original_url = $this->url; - $this->maximumMetaRefreshCount++; - $this->checkForMetaRefresh(); + $this->doMetaRefresh(); $this->assertEqual($original_url, $this->url, 'Redirected back to the original location.'); @@ -290,4 +332,11 @@ class BigPipeNoJsDetectionTest extends WebTestBase { $this->assertNoRaw('