Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / workspaces / tests / src / Kernel / WorkspaceTestTrait.php
1 <?php
2
3 namespace Drupal\Tests\workspaces\Kernel;
4
5 use Drupal\workspaces\Entity\Workspace;
6
7 /**
8  * A trait with common workspaces testing functionality.
9  */
10 trait WorkspaceTestTrait {
11
12   /**
13    * The workspaces manager.
14    *
15    * @var \Drupal\workspaces\WorkspaceManagerInterface
16    */
17   protected $workspaceManager;
18
19   /**
20    * An array of test workspaces, keyed by workspace ID.
21    *
22    * @var \Drupal\workspaces\WorkspaceInterface[]
23    */
24   protected $workspaces = [];
25
26   /**
27    * Enables the Workspaces module and creates two workspaces.
28    */
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');
36
37     $this->installEntitySchema('workspace');
38     $this->installEntitySchema('workspace_association');
39
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();
45
46     $permissions = array_intersect([
47       'administer nodes',
48       'create workspace',
49       'edit any workspace',
50       'view any workspace',
51     ], array_keys($this->container->get('user.permissions')->getPermissions()));
52     $this->setCurrentUser($this->createUser($permissions));
53   }
54
55   /**
56    * Sets a given workspace as active.
57    *
58    * @param string $workspace_id
59    *   The ID of the workspace to switch to.
60    */
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);
65   }
66
67 }