From c2d897e7055809ea44ef6a7572748006faafc616 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ga=CC=81bor=20Hojtsy?= <gabor@hojtsy.hu>
Date: Tue, 6 Aug 2019 14:36:38 +0200
Subject: [PATCH] Issue #2984938 by vijaycs85, amateescu, jeqq, timmillwood,
 larowlan, Wim Leers, alexpott: Remember the page you were on and take you
 back there when switching Workspaces

---
 .../src/Form/WorkspaceActivateForm.php        |  1 -
 .../workspaces/src/WorkspaceListBuilder.php   |  2 +-
 .../WorkspaceToolbarIntegrationTest.php       | 85 +++++++++++++++++++
 core/modules/workspaces/workspaces.module     |  4 +-
 4 files changed, 87 insertions(+), 5 deletions(-)
 create mode 100644 core/modules/workspaces/tests/src/FunctionalJavascript/WorkspaceToolbarIntegrationTest.php

diff --git a/core/modules/workspaces/src/Form/WorkspaceActivateForm.php b/core/modules/workspaces/src/Form/WorkspaceActivateForm.php
index e55c47e4f3dc..e16ec4d56f6f 100644
--- a/core/modules/workspaces/src/Form/WorkspaceActivateForm.php
+++ b/core/modules/workspaces/src/Form/WorkspaceActivateForm.php
@@ -107,7 +107,6 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
     try {
       $this->workspaceManager->setActiveWorkspace($this->entity);
       $this->messenger->addMessage($this->t('%workspace_label is now the active workspace.', ['%workspace_label' => $this->entity->label()]));
-      $form_state->setRedirect('<front>');
     }
     catch (WorkspaceAccessException $e) {
       $this->messenger->addError($this->t('You do not have access to activate the %workspace_label workspace.', ['%workspace_label' => $this->entity->label()]));
diff --git a/core/modules/workspaces/src/WorkspaceListBuilder.php b/core/modules/workspaces/src/WorkspaceListBuilder.php
index 360c1e89e143..9fbe60d998f0 100644
--- a/core/modules/workspaces/src/WorkspaceListBuilder.php
+++ b/core/modules/workspaces/src/WorkspaceListBuilder.php
@@ -208,7 +208,7 @@ protected function offCanvasRender(array &$build) {
     $rows = array_slice($build['table']['#rows'], 0, 5, TRUE);
     foreach ($rows as $id => $row) {
       if ($active_workspace->id() !== $id) {
-        $url = Url::fromRoute('entity.workspace.activate_form', ['workspace' => $id]);
+        $url = Url::fromRoute('entity.workspace.activate_form', ['workspace' => $id], ['query' => $this->getDestinationArray()]);
         $default_class = $id === WorkspaceInterface::DEFAULT_WORKSPACE ? 'workspaces__item--default' : 'workspaces__item--not-default';
         $items[] = [
           '#type' => 'link',
diff --git a/core/modules/workspaces/tests/src/FunctionalJavascript/WorkspaceToolbarIntegrationTest.php b/core/modules/workspaces/tests/src/FunctionalJavascript/WorkspaceToolbarIntegrationTest.php
new file mode 100644
index 000000000000..c0eb05728092
--- /dev/null
+++ b/core/modules/workspaces/tests/src/FunctionalJavascript/WorkspaceToolbarIntegrationTest.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Drupal\Tests\workspaces\FunctionalJavascript;
+
+use Drupal\Tests\system\FunctionalJavascript\OffCanvasTestBase;
+
+/**
+ * Tests workspace settings stray integration.
+ *
+ * @group workspaces
+ */
+class WorkspaceToolbarIntegrationTest extends OffCanvasTestBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public static $modules = ['toolbar', 'workspaces'];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+    $admin_user = $this->drupalCreateUser([
+      'administer workspaces',
+      'access toolbar',
+      'access administration pages',
+    ]);
+    $this->drupalLogin($admin_user);
+  }
+
+  /**
+   * Test workspace canvas can be toggled with JavaScript.
+   */
+  public function testWorkspaceCanvasToggling() {
+    $page = $this->getSession()->getPage();
+    $assert_session = $this->assertSession();
+
+    // Set size for horizontal toolbar.
+    $this->getSession()->resizeWindow(1200, 600);
+    $this->drupalGet('<front>');
+    // Wait for toolbar to appear.
+    $this->assertNotEmpty($assert_session->waitForElement('css', 'body.toolbar-horizontal'));
+
+    // Open workspace canvas.
+    $page->clickLink('Switch workspace');
+    $this->waitForOffCanvasToOpen('top');
+    $assert_session->elementExists('css', '.workspaces-dialog');
+
+    // Close Canvas.
+    $page->pressButton('Close');
+    $this->waitForOffCanvasToClose();
+    $assert_session->assertNoElementAfterWait('css', '.workspaces-dialog');
+  }
+
+  /**
+   * Test workspace switch and landing page behaviour.
+   */
+  public function testWorkspaceSwitch() {
+    $page = $this->getSession()->getPage();
+    $assert_session = $this->assertSession();
+
+    // Wait for toolbar to appear.
+    $this->getSession()->resizeWindow(1200, 600);
+    $this->drupalGet('admin');
+
+    // Wait for toolbar to appear.
+    $this->assertNotEmpty($assert_session->waitForElement('css', 'body.toolbar-horizontal'));
+
+    // Open workspace canvas.
+    $page->clickLink('Switch workspace');
+    $this->waitForOffCanvasToOpen('top');
+
+    // Click 'stage' workspace and confirm switch.
+    $page->clickLink('Stage');
+    $this->assertElementVisibleAfterWait('css', '.workspace-activate-form.workspace-confirm-form');
+    $page->find('css', '.ui-dialog-buttonset .button--primary')->click();
+    $assert_session->waitForElementVisible('css', '.messages--status');
+
+    // Make sure we stay on same page after switch.
+    $assert_session->responseContains('<em class="placeholder">Stage</em> is now the active workspace.');
+    $assert_session->addressEquals('admin');
+  }
+
+}
diff --git a/core/modules/workspaces/workspaces.module b/core/modules/workspaces/workspaces.module
index c080ec431868..817edc47da3f 100644
--- a/core/modules/workspaces/workspaces.module
+++ b/core/modules/workspaces/workspaces.module
@@ -152,7 +152,6 @@ function workspaces_cron() {
  * Implements hook_toolbar().
  */
 function workspaces_toolbar() {
-  $items = [];
   $items['workspace'] = [
     '#cache' => [
       'contexts' => [
@@ -160,7 +159,6 @@ function workspaces_toolbar() {
       ],
     ],
   ];
-
   $current_user = \Drupal::currentUser();
   if (!$current_user->hasPermission('administer workspaces')
     && !$current_user->hasPermission('view own workspace')
@@ -176,7 +174,7 @@ function workspaces_toolbar() {
     'tab' => [
       '#type' => 'link',
       '#title' => $active_workspace->label(),
-      '#url' => $active_workspace->toUrl('collection'),
+      '#url' => $active_workspace->toUrl('collection', ['query' => \Drupal::destination()->getAsArray()]),
       '#attributes' => [
         'title' => t('Switch workspace'),
         'class' => ['use-ajax', 'toolbar-icon', 'toolbar-icon-workspace'],
-- 
GitLab