Same name and namespace in other branches
  1. 8.9.x core/modules/workspaces/tests/src/Functional/WorkspaceSwitcherTest.php \Drupal\Tests\workspaces\Functional\WorkspaceSwitcherTest
  2. 9 core/modules/workspaces/tests/src/Functional/WorkspaceSwitcherTest.php \Drupal\Tests\workspaces\Functional\WorkspaceSwitcherTest

Tests workspace switching functionality.

@group workspaces

Hierarchy

Expanded class hierarchy of WorkspaceSwitcherTest

File

core/modules/workspaces/tests/src/Functional/WorkspaceSwitcherTest.php, line 17

Namespace

Drupal\Tests\workspaces\Functional
View source
class WorkspaceSwitcherTest extends BrowserTestBase {
  use AssertPageCacheContextsAndTagsTrait;
  use WorkspaceTestUtilities;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'block',
    'dynamic_page_cache',
    'toolbar',
    'workspaces',
  ];

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $permissions = [
      'create workspace',
      'edit own workspace',
      'view own workspace',
      'bypass entity access own workspace',
    ];
    $this
      ->setupWorkspaceSwitcherBlock();
    $mayer = $this
      ->drupalCreateUser($permissions);
    $this
      ->drupalLogin($mayer);
  }

  /**
   * Tests switching workspace via the switcher block and admin page.
   */
  public function testSwitchingWorkspaces() {
    $this
      ->createAndActivateWorkspaceThroughUi('Vultures', 'vultures');
    $gravity = $this
      ->createWorkspaceThroughUi('Gravity', 'gravity');

    // Confirm the block shows on the front page.
    $this
      ->drupalGet('<front>');
    $page = $this
      ->getSession()
      ->getPage();
    $this
      ->assertTrue($page
      ->hasContent('Workspace switcher'));
    $this
      ->drupalGet('/admin/config/workflow/workspaces/manage/' . $gravity
      ->id() . '/activate');
    $this
      ->assertSession()
      ->statusCodeEquals(200);
    $page = $this
      ->getSession()
      ->getPage();
    $page
      ->findButton('Confirm')
      ->click();

    // Check that WorkspaceCacheContext provides the cache context used to
    // support its functionality.
    $this
      ->assertCacheContext('session');
    $page
      ->findLink($gravity
      ->label());
  }

  /**
   * Tests switching workspace via a query parameter.
   */
  public function testQueryParameterNegotiator() {
    $web_assert = $this
      ->assertSession();

    // Initially the default workspace should be active.
    $web_assert
      ->elementContains('css', '#block-workspace-switcher', 'None');

    // When adding a query parameter the workspace will be switched.
    $current_user_url = \Drupal::currentUser()
      ->getAccount()
      ->toUrl();
    $this
      ->drupalGet($current_user_url, [
      'query' => [
        'workspace' => 'stage',
      ],
    ]);
    $web_assert
      ->elementContains('css', '#block-workspace-switcher', 'Stage');

    // The workspace switching via query parameter should persist.
    $this
      ->drupalGet($current_user_url);
    $web_assert
      ->elementContains('css', '#block-workspace-switcher', 'Stage');

    // Check that WorkspaceCacheContext provides the cache context used to
    // support its functionality.
    $this
      ->assertCacheContext('session');
  }

  /**
   * Tests that the toolbar workspace switcher doesn't disable the page cache.
   */
  public function testToolbarSwitcherDynamicPageCache() {
    $this
      ->drupalLogin($this
      ->drupalCreateUser([
      'access toolbar',
      'view any workspace',
    ]));

    // Front-page is visited right after login.
    $this
      ->assertSession()
      ->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'MISS');

    // Reload the page, it should be cached now.
    $this
      ->drupalGet(Url::fromRoute('<front>'));
    $this
      ->assertSession()
      ->elementExists('css', '.workspaces-toolbar-tab');
    $this
      ->assertSession()
      ->responseHeaderEquals(DynamicPageCacheSubscriber::HEADER, 'HIT');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AssertPageCacheContextsAndTagsTrait::assertCacheContext protected function Asserts whether an expected cache context was present in the last response.
AssertPageCacheContextsAndTagsTrait::assertCacheContexts protected function Ensures that some cache contexts are present in the current response.
AssertPageCacheContextsAndTagsTrait::assertCacheMaxAge protected function Asserts the max age header.
AssertPageCacheContextsAndTagsTrait::assertCacheTags protected function Ensures that some cache tags are present in the current response.
AssertPageCacheContextsAndTagsTrait::assertNoCacheContext protected function Asserts that a cache context was not present in the last response.
AssertPageCacheContextsAndTagsTrait::assertPageCacheContextsAndTags protected function Asserts page cache miss, then hit for the given URL; checks cache headers.
AssertPageCacheContextsAndTagsTrait::enablePageCaching protected function Enables page caching.
AssertPageCacheContextsAndTagsTrait::getCacheHeaderValues protected function Gets a specific header value as array.
BlockCreationTrait::placeBlock protected function Creates a block instance based on default settings.
WorkspaceSwitcherTest::$defaultTheme protected property
WorkspaceSwitcherTest::$modules protected static property
WorkspaceSwitcherTest::setUp protected function
WorkspaceSwitcherTest::testQueryParameterNegotiator public function Tests switching workspace via a query parameter.
WorkspaceSwitcherTest::testSwitchingWorkspaces public function Tests switching workspace via the switcher block and admin page.
WorkspaceSwitcherTest::testToolbarSwitcherDynamicPageCache public function Tests that the toolbar workspace switcher doesn't disable the page cache.
WorkspaceTestUtilities::$switcherBlockConfigured protected property
WorkspaceTestUtilities::createAndActivateWorkspaceThroughUi protected function Creates and activates a new Workspace through the UI.
WorkspaceTestUtilities::createNodeThroughUi protected function Creates a node by "clicking" buttons.
WorkspaceTestUtilities::createWorkspaceThroughUi protected function Creates a new Workspace through the UI.
WorkspaceTestUtilities::getOneEntityByLabel protected function Loads a single entity by its label.
WorkspaceTestUtilities::isLabelInContentOverview protected function Determine if the content list has an entity's label.
WorkspaceTestUtilities::setupWorkspaceSwitcherBlock protected function Adds the workspace switcher block to the site.
WorkspaceTestUtilities::switchToLive protected function Switches to the live version of the site for subsequent requests.
WorkspaceTestUtilities::switchToWorkspace protected function Sets a given workspace as "active" for subsequent requests.