function WorkspaceIntegrationTest::testExecuteInWorkspaceContext

Same name in other branches
  1. 8.9.x core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::testExecuteInWorkspaceContext()
  2. 10 core/modules/workspaces/tests/src/Kernel/WorkspaceIntegrationTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceIntegrationTest::testExecuteInWorkspaceContext()
  3. 11.x 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 706

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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.