3 namespace Drupal\Tests\workspaces\Kernel;
5 use Drupal\workspaces\Entity\Workspace;
8 * A trait with common workspaces testing functionality.
10 trait WorkspaceTestTrait {
13 * The workspaces manager.
15 * @var \Drupal\workspaces\WorkspaceManagerInterface
17 protected $workspaceManager;
20 * An array of test workspaces, keyed by workspace ID.
22 * @var \Drupal\workspaces\WorkspaceInterface[]
24 protected $workspaces = [];
27 * Enables the Workspaces module and creates two workspaces.
29 protected function initializeWorkspacesModule() {
30 // Enable the Workspaces module here instead of the static::$modules array
31 // so we can test it with default content.
32 $this->enableModules(['workspaces']);
33 $this->container = \Drupal::getContainer();
34 $this->entityTypeManager = \Drupal::entityTypeManager();
35 $this->workspaceManager = \Drupal::service('workspaces.manager');
37 $this->installEntitySchema('workspace');
38 $this->installEntitySchema('workspace_association');
40 // Create two workspaces by default, 'live' and 'stage'.
41 $this->workspaces['live'] = Workspace::create(['id' => 'live']);
42 $this->workspaces['live']->save();
43 $this->workspaces['stage'] = Workspace::create(['id' => 'stage']);
44 $this->workspaces['stage']->save();
46 $permissions = array_intersect([
51 ], array_keys($this->container->get('user.permissions')->getPermissions()));
52 $this->setCurrentUser($this->createUser($permissions));
56 * Sets a given workspace as active.
58 * @param string $workspace_id
59 * The ID of the workspace to switch to.
61 protected function switchToWorkspace($workspace_id) {
62 // Switch the test runner's context to the specified workspace.
63 $workspace = $this->entityTypeManager->getStorage('workspace')->load($workspace_id);
64 \Drupal::service('workspaces.manager')->setActiveWorkspace($workspace);