You are here

public function WorkspaceTest::testActiveWorkspace in Multiversion 8

File

tests/src/Functional/WorkspaceTest.php, line 52

Class

WorkspaceTest
Test the workspace entity.

Namespace

Drupal\Tests\multiversion\Functional

Code

public function testActiveWorkspace() {
  $live = $this->workspaceManager
    ->getActiveWorkspace();
  $this
    ->assertEqual('live', $live
    ->getMachineName());

  // Create a test workspaces.
  $test1 = Workspace::create([
    'machine_name' => 'test1',
    'label' => 'test1',
    'type' => 'basic',
  ]);
  $test1
    ->save();
  $test2 = Workspace::create([
    'machine_name' => 'test2',
    'label' => 'test2',
    'type' => 'basic',
  ]);
  $test2
    ->save();

  // Create test users.
  $user1 = $this
    ->drupalCreateUser([
    'administer workspaces',
  ]);
  $user2 = $this
    ->drupalCreateUser([
    'administer workspaces',
  ]);

  // Assert the workspace doesn't change when logging in.
  $this
    ->drupalLogin($user1);
  $this
    ->assertEqual('live', $this->workspaceManager
    ->getActiveWorkspace()
    ->getMachineName());

  // Switch and check that the manager return the correct workspace.
  $this->workspaceManager
    ->setActiveWorkspace($test1);
  $this
    ->assertEqual('test1', $this->workspaceManager
    ->getActiveWorkspace()
    ->getMachineName());

  // Log out and check that we go back to the default workspace and log back
  // in and check that the previous workspace has persisted for that user.
  $this
    ->drupalLogout();
  $this
    ->assertEqual('live', $this->workspaceManager
    ->getActiveWorkspace()
    ->getMachineName());
  $this
    ->drupalLogin($user1);
  $this
    ->assertEqual('test1', $this->workspaceManager
    ->getActiveWorkspace()
    ->getMachineName());
  $this
    ->drupalLogout();

  // Login as a different user and set another workspace. Then check that the
  // last user still has the previous workspace persisted.
  $this
    ->drupalLogin($user2);
  $this->workspaceManager
    ->setActiveWorkspace($test2);
  $this
    ->assertEqual('test2', $this->workspaceManager
    ->getActiveWorkspace()
    ->getMachineName());
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($user1);
  $this
    ->assertEqual('test1', $this->workspaceManager
    ->getActiveWorkspace()
    ->getMachineName());
}