You are here

public function WorkspaceIntegrationTest::testExecuteInWorkspaceContext in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::testExecuteInWorkspaceContext()

@covers \Drupal\workspaces\WorkspaceManager::executeInWorkspace

File

core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php, line 699

Class

WorkspaceIntegrationTest
Tests a complete publishing scenario across different workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testExecuteInWorkspaceContext() {
  $this
    ->initializeWorkspacesModule();

  // Create an entity in the default workspace.
  $this->workspaceManager
    ->switchToLive();
  $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->workspaceManager
    ->switchToLive();
  $storage = $this->entityTypeManager
    ->getStorage('node');
  $this
    ->assertFalse($this->workspaceManager
    ->hasActiveWorkspace());
  $live_node = $storage
    ->load($node
    ->id());
  $this
    ->assertEquals('live node 1', $live_node->title->value);
  $result = $storage
    ->getQuery()
    ->accessCheck(FALSE)
    ->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
      ->load($node
      ->id());
    $this
      ->assertEquals('stage node 1', $stage_node->title->value);
    $result = $storage
      ->getQuery()
      ->accessCheck(FALSE)
      ->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
    ->assertFalse($this->workspaceManager
    ->getActiveWorkspace());
}