function WorkspaceIntegrationTest::testExecuteInWorkspaceContext

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

Tests execute in workspace context.

@legacy-covers \Drupal\workspaces\WorkspaceManager::executeInWorkspace

File

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

Class

WorkspaceIntegrationTest
Tests a complete publishing scenario across different workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testExecuteInWorkspaceContext() : void {
  $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->assertNull($this->workspaceManager
    ->getActiveWorkspace());
  // Register an event listener to capture isTemporary values from the switch
  // events.
  $switch_events = [];
  $this->container
    ->get('event_dispatcher')
    ->addListener(WorkspaceSwitchEvent::class, function (WorkspaceSwitchEvent $event) use (&$switch_events) {
    $switch_events[] = $event->isTemporary();
  });
  // Persistent switches should dispatch non-temporary events.
  $this->switchToWorkspace('stage');
  $this->assertSame([
    FALSE,
  ], $switch_events);
  $this->workspaceManager
    ->switchToLive();
  $this->assertSame([
    FALSE,
    FALSE,
  ], $switch_events);
  // executeInWorkspace() should dispatch temporary events (switch in + switch
  // back).
  $switch_events = [];
  $this->workspaceManager
    ->executeInWorkspace('stage', function () {
  });
  $this->assertSame([
    TRUE,
    TRUE,
  ], $switch_events);
  // executeOutsideWorkspace() should also dispatch temporary events.
  $this->switchToWorkspace('stage');
  $switch_events = [];
  $this->workspaceManager
    ->executeOutsideWorkspace(function () {
  });
  $this->assertSame([
    TRUE,
    TRUE,
  ], $switch_events);
}

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