Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / workspaces / tests / src / Kernel / WorkspaceIntegrationTest.php
index 9d77cdf001f1a2c524c73c6a522d10c2d4efc846..39c365b9468f42aa1ce0a73af51336a68352bee5 100644 (file)
@@ -14,7 +14,6 @@ use Drupal\Tests\node\Traits\NodeCreationTrait;
 use Drupal\Tests\user\Traits\UserCreationTrait;
 use Drupal\views\Tests\ViewResultAssertionTrait;
 use Drupal\views\Views;
-use Drupal\workspaces\Entity\Workspace;
 
 /**
  * Tests a complete deployment scenario across different workspaces.
@@ -28,6 +27,7 @@ class WorkspaceIntegrationTest extends KernelTestBase {
   use NodeCreationTrait;
   use UserCreationTrait;
   use ViewResultAssertionTrait;
+  use WorkspaceTestTrait;
 
   /**
    * The entity type manager.
@@ -36,13 +36,6 @@ class WorkspaceIntegrationTest extends KernelTestBase {
    */
   protected $entityTypeManager;
 
-  /**
-   * An array of test workspaces, keyed by workspace ID.
-   *
-   * @var \Drupal\workspaces\WorkspaceInterface[]
-   */
-  protected $workspaces = [];
-
   /**
    * Creation timestamp that should be incremented for each new entity.
    *
@@ -93,34 +86,6 @@ class WorkspaceIntegrationTest extends KernelTestBase {
     $this->createNode(['title' => 'live - 2 - r2 - unpublished', 'created' => $this->createdTimestamp++, 'status' => FALSE]);
   }
 
-  /**
-   * Enables the Workspaces module and creates two workspaces.
-   */
-  protected function initializeWorkspacesModule() {
-    // Enable the Workspaces module here instead of the static::$modules array
-    // so we can test it with default content.
-    $this->enableModules(['workspaces']);
-    $this->container = \Drupal::getContainer();
-    $this->entityTypeManager = \Drupal::entityTypeManager();
-
-    $this->installEntitySchema('workspace');
-    $this->installEntitySchema('workspace_association');
-
-    // Create two workspaces by default, 'live' and 'stage'.
-    $this->workspaces['live'] = Workspace::create(['id' => 'live']);
-    $this->workspaces['live']->save();
-    $this->workspaces['stage'] = Workspace::create(['id' => 'stage']);
-    $this->workspaces['stage']->save();
-
-    $permissions = [
-      'administer nodes',
-      'create workspace',
-      'edit any workspace',
-      'view any workspace',
-    ];
-    $this->setCurrentUser($this->createUser($permissions));
-  }
-
   /**
    * Tests various scenarios for creating and deploying content in workspaces.
    */
@@ -492,6 +457,57 @@ class WorkspaceIntegrationTest extends KernelTestBase {
     $entity_test->delete();
   }
 
+  /**
+   * @covers \Drupal\workspaces\WorkspaceManager::executeInWorkspace
+   */
+  public function testExecuteInWorkspaceContext() {
+    $this->initializeWorkspacesModule();
+
+    // Create an entity in the default workspace.
+    $this->switchToWorkspace('live');
+    $node = $this->createNode([
+      'title' => 'live node 1',
+    ]);
+    $node->save();
+
+    // Switch to the 'stage' workspace and change some values for the referenced
+    // entities.
+    $this->switchToWorkspace('stage');
+    $node->title->value = 'stage node 1';
+    $node->save();
+
+    // Switch back to the default workspace and run the baseline assertions.
+    $this->switchToWorkspace('live');
+    $storage = $this->entityTypeManager->getStorage('node');
+
+    $this->assertEquals('live', $this->workspaceManager->getActiveWorkspace()->id());
+
+    $live_node = $storage->loadUnchanged($node->id());
+    $this->assertEquals('live node 1', $live_node->title->value);
+
+    $result = $storage->getQuery()
+      ->condition('title', 'live node 1')
+      ->execute();
+    $this->assertEquals([$live_node->getRevisionId() => $node->id()], $result);
+
+    // Try the same assertions in the context of the 'stage' workspace.
+    $this->workspaceManager->executeInWorkspace('stage', function () use ($node, $storage) {
+      $this->assertEquals('stage', $this->workspaceManager->getActiveWorkspace()->id());
+
+      $stage_node = $storage->loadUnchanged($node->id());
+      $this->assertEquals('stage node 1', $stage_node->title->value);
+
+      $result = $storage->getQuery()
+        ->condition('title', 'stage node 1')
+        ->execute();
+      $this->assertEquals([$stage_node->getRevisionId() => $stage_node->id()], $result);
+    });
+
+    // Check that the 'stage' workspace was not persisted by the workspace
+    // manager.
+    $this->assertEquals('live', $this->workspaceManager->getActiveWorkspace()->id());
+  }
+
   /**
    * Checks entity load, entity queries and views results for a test scenario.
    *
@@ -681,18 +697,6 @@ class WorkspaceIntegrationTest extends KernelTestBase {
     }
   }
 
-  /**
-   * Sets a given workspace as active.
-   *
-   * @param string $workspace_id
-   *   The ID of the workspace to switch to.
-   */
-  protected function switchToWorkspace($workspace_id) {
-    // Switch the test runner's context to the specified workspace.
-    $workspace = $this->entityTypeManager->getStorage('workspace')->load($workspace_id);
-    \Drupal::service('workspaces.manager')->setActiveWorkspace($workspace);
-  }
-
   /**
    * Flattens the expectations array defined by testWorkspaces().
    *