Skip to content

Commit

Permalink
Added handling for multiple sessions in bigpipe. (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgaunt committed Dec 18, 2023
1 parent cc4008b commit 4dc5ee7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/BigPipeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace DrevOps\BehatSteps;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Behat\Hook\Scope\BeforeStepScope;
use Behat\Mink\Exception\DriverException;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Drupal\big_pipe\Render\Placeholder\BigPipeStrategy;

Expand All @@ -13,12 +15,32 @@
*/
trait BigPipeTrait {

/**
* Flag for JS not supported by driver.
*
* @var bool
*/
protected $bigPipeNoJS;

/**
* Skip Big Pipe BeforeStep.
*
* @var bool
*/
protected bool $bigPipeBeforeStepSkip = FALSE;

/**
* Prepares Big Pipe NOJS cookie if needed.
*
* @BeforeScenario
*/
public function bigPipeBeforeScenarioInit(BeforeScenarioScope $scope) {
// Allow to skip resetting cookies on step.
// BeforeStep scope does not have access to scenario where tagging is
// made.
if ($scope->getScenario()->hasTag('behat-steps-skip:bigPipeBeforeStep')) {
$this->bigPipeBeforeStepSkip = TRUE;
}
// Allow to skip this by adding a tag.
if ($scope->getScenario()->hasTag('behat-steps-skip:' . __FUNCTION__)) {
return;
Expand All @@ -36,8 +58,10 @@ public function bigPipeBeforeScenarioInit(BeforeScenarioScope $scope) {
$driver->start();
}
$driver->executeScript('true');
$this->bigPipeNoJS = FALSE;
}
catch (UnsupportedDriverActionException $e) {
$this->bigPipeNoJS = TRUE;
$this
->getSession()
->setCookie(BigPipeStrategy::NOJS_COOKIE, 'true');
Expand All @@ -47,4 +71,25 @@ public function bigPipeBeforeScenarioInit(BeforeScenarioScope $scope) {
}
}

/**
* Prepares Big Pipe NO JS cookie if needed.
*
* @BeforeStep
*/
public function bigPipeBeforeStep(BeforeStepScope $scope) {
if ($this->bigPipeBeforeStepSkip) {
return;
}
try {
if ($this->bigPipeNoJS && !$this->getSession()->getCookie(BigPipeStrategy::NOJS_COOKIE)) {
$this->getSession()
->setCookie(BigPipeStrategy::NOJS_COOKIE, 'true');
}
}
catch (DriverException $e) {
// Mute not visited page exception.
return;
}
}

}
25 changes: 25 additions & 0 deletions tests/behat/features/big_pipe.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,28 @@ Feature: Check that BigPipeTrait works for or D9
Given I install a "big_pipe" module
When I visit "/"
Then cookie "big_pipe_nojs" does not exist

@api
Scenario: Assert that Big Pipe cookie is preserved across multiple users in a scenario
Given users:
| name | mail | roles | status |
| administrator_user | administrator_user@myexample.com | administrator | 1 |
And I install a "big_pipe" module
When I visit "/"
Then cookie "big_pipe_nojs" exists
And I am logged in as "administrator_user"
And I visit "/"
Then cookie "big_pipe_nojs" exists

@api @behat-steps-skip:bigPipeBeforeStep
Scenario: Assert that Big Pipe cookie is not preserved across multiple users when skip tag is used
Given users:
| name | mail | roles | status |
| administrator_user | administrator_user@myexample.com | administrator | 1 |
And I install a "big_pipe" module
When I visit "/"
Then cookie "big_pipe_nojs" exists
# Logging in as a new user removes cookies.
And I am logged in as "administrator_user"
When I visit "/"
Then cookie "big_pipe_nojs" does not exist

0 comments on commit 4dc5ee7

Please sign in to comment.